Fix admin color scheme toggle to work reliably for both on and off states

This commit is contained in:
Marcus Quinn
2025-03-25 03:01:55 +00:00
parent 223b0f6922
commit 0626754925

View File

@ -75,49 +75,58 @@ class WP_Allstars_Admin_Colors {
function applyAdminColorScheme(enableModern) { function applyAdminColorScheme(enableModern) {
console.log("Applying admin color scheme:", enableModern ? "modern" : "default"); console.log("Applying admin color scheme:", enableModern ? "modern" : "default");
// AJAX request to get the new color scheme URLs // Define the color schemes
var scheme = enableModern ? "modern" : "fresh";
// AJAX request to update color scheme
$.ajax({ $.ajax({
url: ajaxurl, url: ajaxurl,
type: "POST", type: "POST",
data: { data: {
action: "wp_allstars_get_admin_colors", action: "wp_allstars_update_admin_colors",
enable_modern: enableModern ? 1 : 0, scheme: scheme,
_wpnonce: wpAllstars.nonce _wpnonce: wpAllstars.nonce
}, },
success: function(response) { success: function(response) {
if (response.success && response.data) { if (response.success) {
console.log("Received color scheme data:", response.data); // Get the list of stylesheets to update
var oldScheme = response.data.previousScheme;
var newScheme = response.data.currentScheme;
// Replace the stylesheets console.log("Switching from " + oldScheme + " to " + newScheme);
$.each(response.data, function(index, styleData) {
// Find any style with "colors" in the URL or ID // Update body class - this is the WordPress way of handling color scheme changes
var colorStyleSelector = "link[href*=\'colors\'], link[id*=\'colors\']"; $("body")
var $oldStyles = $(colorStyleSelector); .removeClass(oldScheme)
.addClass(newScheme);
// Replace admin-colors.css
var $adminColors = $("#admin-colors-css, link[href*=\'colors-\']");
if ($adminColors.length) {
// Get the new URL by replacing the scheme in the existing URL
var oldUrl = $adminColors.first().attr("href");
var newUrl = oldUrl.replace(
/\/([^\/]+)\/colors/,
"/" + newScheme + "/colors"
);
console.log("Found color stylesheets:", $oldStyles.length); // Create a new stylesheet
// Create a new link element
var $newStyle = $("<link>", { var $newStyle = $("<link>", {
rel: "stylesheet", rel: "stylesheet",
id: styleData.id, id: "admin-colors-css",
href: styleData.url, href: newUrl,
type: "text/css" type: "text/css"
}); });
// Add new style at the end of head // Add the new stylesheet and remove old ones
$("head").append($newStyle); $("head").append($newStyle);
// Remove old styles after a short delay to ensure new one loads // After a short delay, remove old stylesheets
setTimeout(function() { setTimeout(function() {
$oldStyles.remove(); $adminColors.remove();
}, 100); }, 200);
}); }
} else {
console.error("Error getting color scheme data:", response);
} }
},
error: function(xhr, status, error) {
console.error("AJAX error:", error);
} }
}); });
} }
@ -177,51 +186,49 @@ class WP_Allstars_Admin_Colors {
} }
/** /**
* AJAX handler to get the admin color scheme stylesheets * AJAX handler to update and get admin color schemes - similar to WordPress core
*/ */
public function get_admin_colors_ajax() { public function get_admin_colors_ajax() {
// Verify nonce // 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'); check_ajax_referer('wp-allstars-nonce', '_wpnonce');
// Get current user ID
$user_id = get_current_user_id(); $user_id = get_current_user_id();
if (!$user_id) { if (!$user_id) {
wp_send_json_error('Not logged in'); wp_send_json_error('Not logged in');
return;
} }
// Determine which scheme to set based on the value // Get the old scheme
$enable_modern = isset($_POST['enable_modern']) && $_POST['enable_modern'] == 1; $old_scheme = get_user_meta($user_id, 'admin_color', true);
$scheme = $enable_modern ? $this->modern_scheme : $this->default_scheme; if (!$old_scheme) {
$old_scheme = 'fresh'; // Default WordPress admin color scheme
}
// Get color scheme information // Get the new scheme
$scheme = isset($_POST['scheme']) ? sanitize_text_field($_POST['scheme']) : 'fresh';
// Validate scheme
global $_wp_admin_css_colors; global $_wp_admin_css_colors;
// Make sure the scheme exists
if (!isset($_wp_admin_css_colors[$scheme])) { if (!isset($_wp_admin_css_colors[$scheme])) {
wp_send_json_error('Color scheme not found'); wp_send_json_error('Invalid color scheme');
return;
} }
// Get the stylesheet URLs // Update user meta
$color_scheme = $_wp_admin_css_colors[$scheme]; update_user_meta($user_id, 'admin_color', $scheme);
$stylesheet_urls = array();
// Colors stylesheet // Also update our plugin option
$stylesheet_urls[] = array( update_option($this->option_name, $scheme === $this->modern_scheme ? 1 : 0);
'id' => 'colors',
'url' => $color_scheme->url . 'colors.min.css'
);
// Colors RTL stylesheet if needed // Return success with old and new scheme names for CSS class updates
if (is_rtl() && file_exists($color_scheme->url . 'colors-rtl.min.css')) { wp_send_json_success(array(
$stylesheet_urls[] = array( 'previousScheme' => 'admin-color-' . $old_scheme,
'id' => 'colors-rtl', 'currentScheme' => 'admin-color-' . $scheme
'url' => $color_scheme->url . 'colors-rtl.min.css' ));
);
}
// Return the URLs to the JavaScript
wp_send_json_success($stylesheet_urls);
} }
} }