'', '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 '
' . 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 ''; } } // 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 ''; } echo '