-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-mailchimp-admin-notices.php
More file actions
129 lines (113 loc) · 2.41 KB
/
Copy pathclass-mailchimp-admin-notices.php
File metadata and controls
129 lines (113 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Mailchimp admin notices class.
*
* Registers notices and renders them on the admin_notices hook in the same request.
*
* @since 2.1.0
*
* @package Mailchimp
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Mailchimp_Admin_Notices
*
* @since 2.1.0
*/
class Mailchimp_Admin_Notices {
/**
* Singleton instance.
*
* @var Mailchimp_Admin_Notices|null
*/
private static $instance = null;
/**
* Queued notices for the current request.
*
* @var array<int, array{message: string, type: string}>
*/
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() {
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 ) {
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() {
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 ) {
$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(),
);
?>
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
<p>
<?php echo wp_kses( $message, $allowed_html ); ?>
</p>
</div>
<?php
}
}