0) { $mainToggle.prop("checked", true); } else { $mainToggle.prop("checked", false); } showSavedNotification($this); } else { showErrorNotification($this); // Revert the checkbox to its previous state $this.prop("checked", !$this.prop("checked")); } }, error: function() { showErrorNotification($this); // Revert the checkbox to its previous state $this.prop("checked", !$this.prop("checked")); } }); }); function showSavedNotification($element) { // Find the nearest toggle header for notification placement var $toggleHeader = $element.closest(".wp-allstars-toggle").find(".wp-allstars-toggle-header"); var $notification = $("").addClass("wp-setting-notification success").text("Saved"); // Remove any existing notifications $toggleHeader.find(".wp-setting-notification").remove(); // Add the notification $toggleHeader.find("label").append($notification); // Remove notification after delay setTimeout(function() { $notification.fadeOut(300, function() { $(this).remove(); }); }, 2000); } function showErrorNotification($element) { // Find the nearest toggle header for notification placement var $toggleHeader = $element.closest(".wp-allstars-toggle").find(".wp-allstars-toggle-header"); var $notification = $("").addClass("wp-setting-notification error").text("Error Saving"); // Remove any existing notifications $toggleHeader.find(".wp-setting-notification").remove(); // Add the notification $toggleHeader.find("label").append($notification); // Remove notification after delay setTimeout(function() { $notification.fadeOut(300, function() { $(this).remove(); }); }, 2000); } }); '; wp_add_inline_script('wp-allstars-admin', $access_js); } /** * Handle AJAX updates for access settings */ public static function handle_access_setting_update() { // Verify nonce if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'wp-allstars-nonce')) { wp_send_json_error(array('message' => 'Invalid nonce')); return; } // Check user capabilities if (!current_user_can('manage_options')) { wp_send_json_error(array('message' => 'Insufficient permissions')); return; } // Get and validate setting $setting = isset($_POST['setting']) ? sanitize_text_field($_POST['setting']) : ''; $value = isset($_POST['value']) ? $_POST['value'] : ''; if (empty($setting)) { wp_send_json_error(array('message' => 'Invalid setting')); return; } // Handle different setting types $result = false; switch ($setting) { case 'wp_allstars_hide_admin_bar': // When the main toggle is changed, update the roles option if ($value) { $default_roles = array('guest', 'subscriber', 'customer'); $result = update_option('wp_allstars_hide_admin_bar_roles', $default_roles); } else { $result = update_option('wp_allstars_hide_admin_bar_roles', array()); } break; case 'wp_allstars_restrict_dashboard': // When the main toggle is changed, update the roles option if ($value) { $default_roles = array('guest', 'subscriber', 'customer'); $result = update_option('wp_allstars_restrict_dashboard_roles', $default_roles); } else { $result = update_option('wp_allstars_restrict_dashboard_roles', array()); } break; case 'wp_allstars_hide_admin_bar_roles[]': // For role checkboxes, update the complete array if (is_array($value)) { $value = array_map('sanitize_text_field', $value); $result = update_option('wp_allstars_hide_admin_bar_roles', $value); } break; case 'wp_allstars_restrict_dashboard_roles[]': // For role checkboxes, update the complete array if (is_array($value)) { $value = array_map('sanitize_text_field', $value); $result = update_option('wp_allstars_restrict_dashboard_roles', $value); } break; default: wp_send_json_error(array('message' => 'Invalid setting name')); return; } if ($result) { wp_send_json_success(array('message' => 'Setting updated successfully')); } else { wp_send_json_error(array('message' => 'Error Saving')); } } /** * Set up access control hooks */ public static function setup_access_control() { // Only run if the feature is enabled if (!get_option('wp_allstars_hide_admin_bar_roles') && !get_option('wp_allstars_restrict_dashboard_roles')) { return; } // Get current user $user = wp_get_current_user(); if (!$user->exists()) { return; } // Get user roles $user_roles = $user->roles; // Get restricted roles from settings $hide_admin_bar_roles = get_option('wp_allstars_hide_admin_bar_roles', array()); $restrict_dashboard_roles = get_option('wp_allstars_restrict_dashboard_roles', array()); // Check if user's role is in restricted roles $should_hide_admin_bar = array_intersect($user_roles, $hide_admin_bar_roles); $should_restrict_dashboard = array_intersect($user_roles, $restrict_dashboard_roles); // Hide admin bar if needed if (!empty($should_hide_admin_bar)) { add_filter('show_admin_bar', '__return_false'); } // Restrict dashboard access if needed if (!empty($should_restrict_dashboard) && is_admin() && !wp_doing_ajax()) { // Allow access to profile page if (isset($_GET['page']) && $_GET['page'] === 'profile.php') { return; } // Redirect to home page wp_redirect(home_url()); exit; } } /** * Display the access control settings in the advanced tab */ public static function display_access_settings() { // Get current settings $hide_admin_bar_roles = get_option('wp_allstars_hide_admin_bar_roles', array('guest', 'subscriber', 'customer')); $restrict_dashboard_roles = get_option('wp_allstars_restrict_dashboard_roles', array('guest', 'subscriber', 'customer')); // Get all available roles $roles = wp_roles()->get_names(); ?>
$role_name): ?>
$role_name): ?>