id, 'wpallstars') === false) { return; // Exit if not a WP Allstars page. } // Enqueue the specific JS file for color handling. wp_enqueue_script( 'wpallstars-admin-colors-script', // More specific handle WPALLSTARS_URL . 'admin/js/wpallstars-admin-colors.js', // Use constant for URL array('jquery'), WPALLSTARS_VERSION, // Use constant for version true // Load in footer ); // Localize data needed by the script. wp_localize_script( 'wpallstars-admin-colors-script', // Must match the script handle 'wpallstarsAdminColorsData', // JavaScript object name array( 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce($this->ajax_nonce_action), 'ajax_action' => 'wpallstars_update_admin_color_scheme', // The action hook name 'l10n' => [ // Localization strings 'saving' => __('Saving...', WPALLSTARS_TEXT_DOMAIN), 'saved' => __('Saved', WPALLSTARS_TEXT_DOMAIN), 'error' => __('Error', WPALLSTARS_TEXT_DOMAIN), ] ) ); } /** * Apply the selected admin color scheme for the current user upon admin initialization. * * Checks if the user has the capability and if the setting is enabled. */ public function apply_user_admin_color_scheme() { // Ensure the current user has the capability to change schemes (usually 'manage_options' or similar). if (!current_user_can('manage_options')) { // Adjust capability if needed return; } $user_id = get_current_user_id(); if (!$user_id) { return; // Should not happen in admin_init, but good practice. } // Check if the modern scheme override is enabled in settings. $scheme_to_apply = $this->is_modern_color_scheme_enabled() ? $this->modern_scheme_slug : $this->default_scheme_slug; // Get the user's currently saved color scheme preference. $current_user_scheme = get_user_meta($user_id, 'admin_color', true); // Only update the user's meta if the desired scheme differs from their current one. // This prevents unnecessary database writes on every admin page load. if ($current_user_scheme !== $scheme_to_apply) { $this->update_user_color_scheme_preference($user_id, $scheme_to_apply); } } /** * Update a user's color scheme preference in their user meta. * * @param int $user_id The ID of the user to update. * @param string $scheme_slug The slug of the color scheme to set (e.g., 'modern', 'fresh'). */ private function update_user_color_scheme_preference($user_id, $scheme_slug) { // WordPress handles validation internally, but ensure the scheme exists if crucial. // global $_wp_admin_css_colors; // if (!isset($_wp_admin_css_colors[$scheme_slug])) { // $scheme_slug = $this->default_scheme_slug; // Fallback to default // } // Update the 'admin_color' user meta field. update_user_meta($user_id, 'admin_color', $scheme_slug); } /** * Handle the AJAX request to enable/disable the modern color scheme override. * * Verifies nonce, checks user capabilities, updates the option, * updates the current user's scheme immediately, and sends a JSON response. */ public function handle_ajax_color_scheme_update() { // 1. Verify Nonce for security. check_ajax_referer($this->ajax_nonce_action, 'nonce'); // Dies on failure // 2. Check User Capabilities. if (!current_user_can('manage_options')) { // Ensure user can change this setting wp_send_json_error(array('message' => __('Insufficient permissions.', WPALLSTARS_TEXT_DOMAIN)), 403); // 403 Forbidden } // 3. Sanitize and Validate Input. // Expecting 'enabled' to be 'true' or 'false' (as strings from JS). $is_enabled_input = isset($_POST['enabled']) ? sanitize_text_field($_POST['enabled']) : 'false'; $is_enabled = ($is_enabled_input === 'true'); // Convert string 'true' to boolean true // 4. Update the Option in the Database. $options = get_option($this->options_key, []); $options[$this->color_scheme_option_key] = $is_enabled ? 1 : 0; // Store as 1 or 0 $update_success = update_option($this->options_key, $options); // 5. Update Current User's Scheme Immediately for instant feedback. $user_id = get_current_user_id(); $scheme_to_set = $is_enabled ? $this->modern_scheme_slug : $this->default_scheme_slug; $this->update_user_color_scheme_preference($user_id, $scheme_to_set); // 6. Send JSON Response. if ($update_success) { wp_send_json_success(array( 'message' => $is_enabled ? __('Modern admin color scheme enabled.', WPALLSTARS_TEXT_DOMAIN) : __('Default admin color scheme restored.', WPALLSTARS_TEXT_DOMAIN), 'new_state' => $is_enabled, // Send back the new state )); } else { // Option update might have failed, or the value was unchanged. // Check if the value was actually unchanged. $current_db_options = get_option($this->options_key, []); $current_db_value = isset($current_db_options[$this->color_scheme_option_key]) ? (bool)$current_db_options[$this->color_scheme_option_key] : false; if ($current_db_value === $is_enabled) { wp_send_json_success(array( 'message' => __('Setting unchanged.', WPALLSTARS_TEXT_DOMAIN), 'new_state' => $is_enabled, 'unchanged' => true )); } else { wp_send_json_error(array('message' => __('Failed to save setting.', WPALLSTARS_TEXT_DOMAIN)), 500); // 500 Internal Server Error } } } /** * Check if the modern color scheme override is enabled in the plugin settings. * * @return bool True if enabled, false otherwise. */ public function is_modern_color_scheme_enabled() { $options = get_option($this->options_key, []); // Check if the specific key exists and is set to 1 (or true). Default to false if not set. return isset($options[$this->color_scheme_option_key]) && $options[$this->color_scheme_option_key] == 1; } }