<?php
/**
 * Cryzel 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 Cryzel
 * @since 1.0.0
 */

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

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

/**
 * Update settings when saved in the Site Editor
 */
function cryzel_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['cryzel_scroll_top'])) {
        update_option('cryzel_scroll_top', (bool) $params['cryzel_scroll_top']);
    }

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


