<?php
/**
 * Blynex Theme Settings
 *
 * This file handles the registration of theme settings with WordPress REST API
 * so they can be accessed and updated from the block editor.
 *
 * @package Blynex
 * @since 1.0.0
 */

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Register theme settings
 */
function blynex_register_theme_settings() {
    // Register the scroll-to-top setting
    register_setting(
        'theme',
        'blynex_scroll_top',
        array(
            'type'         => 'boolean',
            'description'  => __('Enable or disable the scroll to top button', 'blynex'),
            'show_in_rest' => true,
            'default'      => false,
        )
    );
}
add_action('init', 'blynex_register_theme_settings');

/**
 * Update settings when saved in the Site Editor
 */
function blynex_update_settings_from_rest($response, $handler, $request) {
    // Check if we're getting site settings
    if ($request->get_route() !== '/wp/v2/settings') {
        return $response;
    }

    // Get the updated values from the response
    $params = $request->get_params();
    
    // Update our custom settings if they've been changed
    if (isset($params['blynex_scroll_top'])) {
        update_option('blynex_scroll_top', (bool) $params['blynex_scroll_top']);
    }

    return $response;
}
add_filter('rest_request_after_callbacks', 'blynex_update_settings_from_rest', 10, 3); 


