config($text_domain, $unique_id ?: uniqid()); } /** * Initialize the dismiss AJAX and enqueue scripts. * * @return void */ public static function init() { add_action('wp_ajax_wpmet-notices', [__CLASS__, 'dismiss_ajax_call']); } /** * Configure basic properties of the notice. * * @param string $text_domain * @param string $unique_id * @return $this */ public function config($text_domain, $unique_id) { $this->text_domain = $text_domain; $this->unique_id = $unique_id; $this->notice_id = "$text_domain-$unique_id"; $this->dismissible = false; $this->expired_time = 1; $this->button = [ 'default_class' => 'button', 'class' => 'button-secondary', 'text' => 'Button', 'url' => '#', 'icon' => '', ]; return $this; } /** * Set additional CSS classes. * * @param string $classname * @return $this */ public function set_class($classname = '') { $this->class .= $classname; return $this; } /** * Set the notice type. * * @param string $type * @return $this */ public function set_type($type = '') { $this->class .= " notice-$type"; return $this; } /** * Add a button to the notice. * * @param array $button * @return $this */ public function set_button($button = []) { $this->buttons[] = array_merge($this->button, $button); return $this; } /** * Set a custom ID for the notice. * * @param string $id * @return $this */ public function set_id($id) { $this->notice_id = $id; return $this; } /** * Set the title of the notice. * * @param string $title * @return $this */ public function set_title($title = '') { $this->title .= $title; return $this; } /** * Set the message of the notice. * * @param string $message * @return $this */ public function set_message($message = '') { $this->message .= $message; return $this; } /** * Enable or disable gutter spacing. * * @param bool $gutter * @return $this */ public function set_gutter($gutter = true) { $this->gutter = $gutter; $this->class .= $gutter ? '' : ' no-gutter'; return $this; } /** * Set logo and style. * * @param string $logo * @param string $logo_style * @return $this */ public function set_logo($logo = '', $logo_style = '') { $this->logo = $logo; $this->logo_style = $logo_style; return $this; } /** * Set custom HTML content. * * @param string $html * @return $this */ public function set_html($html = '') { $this->html .= $html; return $this; } /** * Configure dismiss behavior. * * @param string $scope 'user' or 'global' * @param int $time Expiry time in seconds * @return $this */ public function set_dismiss($scope = 'global', $time = 604800) { $this->dismissible = $scope; $this->expired_time = $time; return $this; } /** * Get the compiled data for rendering. * * @return array */ public function get_data() { return [ 'message' => $this->message, 'title' => $this->title, 'buttons' => $this->buttons, 'class' => $this->class, 'html' => $this->html, ]; } /** * Add the notice to the WordPress admin_notices hook. * * @return void */ public function call() { add_action('admin_notices', [$this, 'get_notice']); } /** * Display the notice HTML based on conditions. * * @return void */ public function get_notice() { switch ($this->dismissible) { case 'user': $expired = get_user_meta(get_current_user_id(), $this->notice_id, true); break; case 'global': $expired = get_transient($this->notice_id); break; default: $expired = ''; break; } global $notice_list; $notice_list = isset($notice_list) ? $notice_list : []; if (!isset($notice_list[$this->notice_id]) && (false === $expired || empty($expired))) { $notice_list[$this->notice_id] = __FILE__; $this->generate_html(); } } /** * Render the HTML for the notice. * * @return void */ public function generate_html() { ?>
logo)) : ?>
html)) : ?> title)) : ?>
title); ?>
message); ?>
buttons)) : ?>
buttons as $button) : ?>  
html); ?>
dismissible !== false) : ?>
version = $version; return $this; } /** * Get plugin version. * * @return string */ public function get_version() { return $this->version; } /** * Get file location of this script. * * @return string */ public function get_script_location() { return __FILE__; } }