98 lines
2.5 KiB
PHP
98 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* WP ALLSTARS Admin Colors Feature
|
|
*
|
|
* Handles setting the admin color scheme based on user preferences
|
|
*
|
|
* @package WP_ALLSTARS
|
|
* @since 0.2.3
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly
|
|
}
|
|
|
|
/**
|
|
* Admin Colors Handler Class
|
|
*/
|
|
class WP_Allstars_Admin_Colors {
|
|
|
|
/**
|
|
* Option name for the admin color scheme setting
|
|
*
|
|
* @var string
|
|
*/
|
|
private $option_name = 'wp_allstars_simple_setting';
|
|
|
|
/**
|
|
* Modern color scheme key
|
|
*
|
|
* @var string
|
|
*/
|
|
private $modern_scheme = 'modern';
|
|
|
|
/**
|
|
* Default color scheme key
|
|
*
|
|
* @var string
|
|
*/
|
|
private $default_scheme = 'fresh';
|
|
|
|
/**
|
|
* Initialize the class and set up hooks
|
|
*/
|
|
public function __construct() {
|
|
// Set up hooks
|
|
add_action('admin_init', array($this, 'set_admin_color_scheme'));
|
|
add_action('wp_ajax_wp_allstars_update_option', array($this, 'handle_color_scheme_update'), 5);
|
|
}
|
|
|
|
/**
|
|
* Set the admin color scheme based on the setting value
|
|
*/
|
|
public function set_admin_color_scheme() {
|
|
// Get current user
|
|
$user_id = get_current_user_id();
|
|
if (!$user_id) {
|
|
return;
|
|
}
|
|
|
|
// Check if our setting is enabled
|
|
$modern_colors_enabled = get_option($this->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);
|
|
}
|
|
}
|