option_name, false); // Get the scheme to set $scheme = $modern_colors_enabled ? $this->modern_scheme : $this->default_scheme; // Update user meta to set the color scheme update_user_meta($user_id, 'admin_color', $scheme); } /** * Handle color scheme update via AJAX * Runs early to apply the color scheme change before the general option update handler */ public function handle_color_scheme_update() { // Check for required params if (!isset($_POST['option']) || !isset($_POST['value'])) { return; } // Only process our specific option if ($_POST['option'] !== $this->option_name) { return; } // Get the current user ID $user_id = get_current_user_id(); if (!$user_id) { return; } // Determine which scheme to set based on the value $value = (bool) $_POST['value']; $scheme = $value ? $this->modern_scheme : $this->default_scheme; // Update the user's color scheme update_user_meta($user_id, 'admin_color', $scheme); } /** * AJAX handler to update and get admin color schemes - similar to WordPress core */ public function get_admin_colors_ajax() { // Register handler for our new AJAX action add_action('wp_ajax_wp_allstars_update_admin_colors', array($this, 'update_admin_colors_ajax')); } /** * AJAX handler to update admin color scheme and return class names */ public function update_admin_colors_ajax() { check_ajax_referer('wp-allstars-nonce', '_wpnonce'); $user_id = get_current_user_id(); if (!$user_id) { wp_send_json_error('Not logged in'); } // Get the old scheme $old_scheme = get_user_meta($user_id, 'admin_color', true); if (!$old_scheme) { $old_scheme = 'fresh'; // Default WordPress admin color scheme } // Get the new scheme $scheme = isset($_POST['scheme']) ? sanitize_text_field($_POST['scheme']) : 'fresh'; // Validate scheme global $_wp_admin_css_colors; if (!isset($_wp_admin_css_colors[$scheme])) { wp_send_json_error('Invalid color scheme'); } // Update user meta update_user_meta($user_id, 'admin_color', $scheme); // Also update our plugin option update_option($this->option_name, $scheme === $this->modern_scheme ? 1 : 0); // Return success with old and new scheme names for CSS class updates wp_send_json_success(array( 'previousScheme' => 'admin-color-' . $old_scheme, 'currentScheme' => 'admin-color-' . $scheme )); } }