|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Mailchimp admin notices class. |
| 4 | + * |
| 5 | + * Registers notices and renders them on the admin_notices hook in the same request. |
| 6 | + * |
| 7 | + * @since x.x.x |
| 8 | + * |
| 9 | + * @package Mailchimp |
| 10 | + */ |
| 11 | + |
| 12 | +// Exit if accessed directly. |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { |
| 14 | + exit; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Class Mailchimp_Admin_Notices |
| 19 | + * |
| 20 | + * @since x.x.x |
| 21 | + */ |
| 22 | +class Mailchimp_Admin_Notices { |
| 23 | + |
| 24 | + /** |
| 25 | + * Singleton instance. |
| 26 | + * |
| 27 | + * @var Mailchimp_Admin_Notices|null |
| 28 | + */ |
| 29 | + private static $instance = null; |
| 30 | + |
| 31 | + /** |
| 32 | + * Queued notices for the current request. |
| 33 | + * |
| 34 | + * @var array<int, array{message: string, type: string}> |
| 35 | + */ |
| 36 | + private $notices = array(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the singleton instance. |
| 40 | + * |
| 41 | + * @return Mailchimp_Admin_Notices |
| 42 | + */ |
| 43 | + public static function instance(): Mailchimp_Admin_Notices { |
| 44 | + if ( null === self::$instance ) { |
| 45 | + self::$instance = new self(); |
| 46 | + } |
| 47 | + |
| 48 | + return self::$instance; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Register the admin_notices hook. |
| 53 | + * |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function init() { |
| 57 | + add_action( 'admin_notices', array( $this, 'render' ) ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Queue an admin notice. |
| 62 | + * |
| 63 | + * @param string $message Notice message (already escaped/translated by caller). |
| 64 | + * @param string $type Notice type: success or error. |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function add( string $message, string $type ) { |
| 68 | + if ( ! is_admin() ) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if ( did_action( 'admin_notices' ) ) { |
| 73 | + $this->print_notice( $message, $type ); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $this->notices[] = array( |
| 78 | + 'message' => $message, |
| 79 | + 'type' => $type, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Render all queued notices. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function render() { |
| 89 | + foreach ( $this->notices as $notice ) { |
| 90 | + $this->print_notice( $notice['message'], $notice['type'] ); |
| 91 | + } |
| 92 | + |
| 93 | + $this->notices = array(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Print a single admin notice. |
| 98 | + * |
| 99 | + * @param string $message Notice message. |
| 100 | + * @param string $type Notice type: success or error. |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + private function print_notice( string $message, string $type ) { |
| 104 | + $classes = array( 'notice', 'notice-' . sanitize_html_class( $type ) ); |
| 105 | + |
| 106 | + if ( 'success' === $type ) { |
| 107 | + $classes[] = 'is-dismissible'; |
| 108 | + } |
| 109 | + |
| 110 | + $allowed_html = array( |
| 111 | + 'a' => array( |
| 112 | + 'href' => array(), |
| 113 | + 'title' => array(), |
| 114 | + 'target' => array(), |
| 115 | + ), |
| 116 | + 'strong' => array(), |
| 117 | + 'em' => array(), |
| 118 | + 'br' => array(), |
| 119 | + ); |
| 120 | + |
| 121 | + ?> |
| 122 | + <div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> |
| 123 | + <p> |
| 124 | + <?php echo wp_kses( $message, $allowed_html ); ?> |
| 125 | + </p> |
| 126 | + </div> |
| 127 | + <?php |
| 128 | + } |
| 129 | +} |
0 commit comments