'', 'url' => '', 'type' => 'button'|'link', 'target' => '_self'|'_blank']. * @return void */ public function add_notice($id, $message, $type = 'info', $persistent = false, $dismissible = true, $action = null) { // Validate action target if provided, default to _self for safety if ($action && isset($action['target']) && !in_array($action['target'], ['_blank', '_self'], true)) { $action['target'] = '_self'; } // Prevent duplicate notices in $this->notices or wuadblockguard_notices $stored_notices = $persistent ? get_option('wuadblockguard_notices', []) : []; if (!is_array($stored_notices)) { $stored_notices = []; } // Check for duplicates in both in-memory and stored notices if (!isset($this->notices[$id]) && !isset($stored_notices[$id])) { $this->notices[$id] = [ 'message' => $message, 'type' => $type, 'persistent' => $persistent, 'dismissible' => $dismissible, 'action' => $action, ]; if ($persistent) { $stored_notices[$id] = $this->notices[$id]; wp_cache_delete('wuadblockguard_notices', 'options'); update_option('wuadblockguard_notices', $stored_notices, false); } } elseif ($persistent && isset($this->notices[$id]) && !isset($stored_notices[$id])) { // Update stored notices if the notice was added in-memory and is persistent $stored_notices[$id] = $this->notices[$id]; wp_cache_delete('wuadblockguard_notices', 'options'); update_option('wuadblockguard_notices', $stored_notices, false); } } /** * Display notices in the admin area. */ public function display_notices() { static $rendered_ids = []; $stored_notices = get_option('wuadblockguard_notices', []); if (!is_array($stored_notices)) { $stored_notices = []; } // Merge notices, prioritizing in-memory notices to avoid duplicates $all_notices = []; foreach ($this->notices as $id => $notice) { $all_notices[$id] = $notice; } foreach ($stored_notices as $id => $notice) { if (!isset($all_notices[$id])) { $all_notices[$id] = $notice; } } // Render each notice only once foreach ($all_notices as $id => $notice) { if (!in_array($id, $rendered_ids, true)) { $this->render_notice($id, $notice); $rendered_ids[] = $id; } } // Update persistent notices $persistent_notices = array_filter( $all_notices, function ($notice) { return !empty($notice['persistent']); } ); wp_cache_delete('wuadblockguard_notices', 'options'); update_option('wuadblockguard_notices', $persistent_notices, false); } /** * Render a single notice. * * @param string $id Unique ID for the notice. * @param array $notice The notice details. */ private function render_notice($id, $notice) { $classes = 'notice notice-' . esc_attr($notice['type']) . ' adblockguard-notice'; // Use raw message and action label to avoid __() calls $message = $notice['message']; $action_label = !empty($notice['action']['label']) ? $notice['action']['label'] : ''; echo '
'; echo '

' . wp_kses_post($message) . '

'; if (!empty($notice['action']) && !empty($action_label)) { $action_url = add_query_arg('adblockguard_notice_action', $id, $notice['action']['url']); $target = isset($notice['action']['target']) && $notice['action']['target'] === '_blank' ? '_blank' : '_self'; if (!empty($notice['action']['type']) && $notice['action']['type'] == 'link') { echo '

' . esc_html($action_label) . '

'; } else { echo '

' . esc_html($action_label) . '

'; } } // Add dismiss link for persistent, dismissible notices if ($notice['persistent'] && $notice['dismissible']) { $dismiss_url = add_query_arg([ 'adblockguard_notice_action' => $id, '_wpnonce' => wp_create_nonce('adblockguard_dismiss_' . $id), ], admin_url('index.php')); echo '

Dismiss

'; } echo '
'; } /** * Enqueue custom CSS for notices. */ public function enqueue_notice_styles() { if (is_admin()) { wp_enqueue_style( 'adblockguard_notices', ADBLOCKGUARD_PLUGIN_URL . 'assets/css/notice-styles.css', [], ADBLOCKGUARD_VERSION ); } } /** * Handle the action link click to remove the notice. */ public function handle_action_click() { if (!isset($_GET['adblockguard_notice_action'])) { return; } $notice_id = sanitize_text_field(wp_unslash($_GET['adblockguard_notice_action'])); /* only treat as “dismiss” if the nonce matches that pattern */ if ( isset($_GET['_wpnonce']) && wp_verify_nonce( sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'adblockguard_dismiss_' . $notice_id ) ) { $this->clear_notice($notice_id); $redirect_url = remove_query_arg( ['adblockguard_notice_action', '_wpnonce'], wp_get_referer() ?: admin_url() ); wp_safe_redirect($redirect_url); exit; } // Non-matching nonce → not a dismiss link; let the request continue. return; } /** * Clear a specific stored notice. * * @param string $id Unique ID of the notice to clear. */ public function clear_notice($id) { $stored_notices = get_option('wuadblockguard_notices', []); if (isset($stored_notices[$id])) { unset($stored_notices[$id]); wp_cache_delete('wuadblockguard_notices', 'options'); update_option('wuadblockguard_notices', $stored_notices, false); } } /** * Clear all stored notices by resetting to an empty array. */ public function clear_all_notices() { update_option('wuadblockguard_notices', [], false); wp_cache_delete('wuadblockguard_notices', 'options'); } }