-
- array(
- 'href' => array(),
- 'title' => array(),
- 'target' => array(),
- ),
- 'strong' => array(),
- 'em' => array(),
- 'br' => array(),
- )
- );
- ?>
-
-
- add( $msg, 'success' );
}
/**
* Display error admin notice.
- *
- * NOTE: WordPress localization i18n functionality should be done
- * on string literals outside of this function in order to work
- * correctly.
- *
- * For more info read here: https://salferrarello.com/why-__-needs-a-hardcoded-string-in-wordpress/
+ * This function is now a wrapper around the Mailchimp_Admin_Notices class, will be deprecated in future versions. Use that class instead.
*
* @since 1.7.0
+ * @since x.x.x - Moved notice rendering to class-mailchimp-admin-notices.php
+ *
* @param string $msg The message to display.
* @return void
*/
-function admin_notice_error( string $msg ) {
- ?>
-
-
- array(
- 'href' => array(),
- 'title' => array(),
- 'target' => array(),
- ),
- 'strong' => array(),
- 'em' => array(),
- )
- );
- ?>
-
-
- add( $msg, 'error' );
}
diff --git a/includes/admin/class-mailchimp-admin-notices.php b/includes/admin/class-mailchimp-admin-notices.php
new file mode 100644
index 00000000..f181c43b
--- /dev/null
+++ b/includes/admin/class-mailchimp-admin-notices.php
@@ -0,0 +1,129 @@
+
+ */
+ private $notices = array();
+
+ /**
+ * Get the singleton instance.
+ *
+ * @return Mailchimp_Admin_Notices
+ */
+ public static function instance(): Mailchimp_Admin_Notices {
+ if ( null === self::$instance ) {
+ self::$instance = new self();
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Register the admin_notices hook.
+ *
+ * @return void
+ */
+ public function init(): void {
+ add_action( 'admin_notices', array( $this, 'render' ) );
+ }
+
+ /**
+ * Queue an admin notice.
+ *
+ * @param string $message Notice message (already escaped/translated by caller).
+ * @param string $type Notice type: success or error.
+ * @return void
+ */
+ public function add( string $message, string $type ): void {
+ if ( ! is_admin() ) {
+ return;
+ }
+
+ if ( did_action( 'admin_notices' ) ) {
+ $this->print_notice( $message, $type );
+ return;
+ }
+
+ $this->notices[] = array(
+ 'message' => $message,
+ 'type' => $type,
+ );
+ }
+
+ /**
+ * Render all queued notices.
+ *
+ * @return void
+ */
+ public function render(): void {
+ foreach ( $this->notices as $notice ) {
+ $this->print_notice( $notice['message'], $notice['type'] );
+ }
+
+ $this->notices = array();
+ }
+
+ /**
+ * Print a single admin notice.
+ *
+ * @param string $message Notice message.
+ * @param string $type Notice type: success or error.
+ * @return void
+ */
+ private function print_notice( string $message, string $type ): void {
+ $classes = array( 'notice', 'notice-' . sanitize_html_class( $type ) );
+
+ if ( 'success' === $type ) {
+ $classes[] = 'is-dismissible';
+ }
+
+ $allowed_html = array(
+ 'a' => array(
+ 'href' => array(),
+ 'title' => array(),
+ 'target' => array(),
+ ),
+ 'strong' => array(),
+ 'em' => array(),
+ 'br' => array(),
+ );
+
+ ?>
+