-
Notifications
You must be signed in to change notification settings - Fork 4
/
class.mph-minify-notices.php
150 lines (108 loc) · 3.64 KB
/
class.mph-minify-notices.php
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
if ( ! class_exists( 'MPH_Admin_Notices' ) ) {
/**
* MPH Notices
*
* Abstraction for displaying admin notices across a site.
* Errors on the front end can easily be stored and displayed in the admin.
*
* @param $ID Unique id. Multiple instances of the class wishing to access the same data should use the same ID.
*
*/
class MPH_Admin_Notices {
private $admin_notices = array();
private $ID;
function __construct( $ID = 'mph' ) {
$this->ID = 'mphan_' . $ID;
if ( ! get_option( $this->ID ) )
add_option( $this->ID, array(), null, 'no' );
else
$this->admin_notices = get_option( $this->ID, array() );
if ( isset( $_GET[ $this->ID . '_notice_dismiss' ] ) || isset( $_GET['_wpnonce'] ) )
add_action( 'admin_init', array( $this, 'delete_notice_action' ) );
add_action( 'admin_notices', array( $this, 'display_admin_notices' ) );
add_action( 'shutdown', array( $this, '_update_notices' ) );
}
/**
* Creates an admin notice - saved in options to be shown in the admin, until dismissed.
*
* @param string $new_notice Message content
* @param string $type Message type - added as a class to the message when displayed. Reccommended to use: updated, error.
* @param bool $display_once Display message once, or require manual dismissal.
*/
public function add_notice( $message, $display_once = false, $type = 'updated' ) {
$notice = array(
'message' => $message,
'type' => $type,
'display_once' => $display_once
);
if ( ! in_array( $notice , $this->admin_notices ) )
$this->admin_notices[uniqid()] = $notice;
}
public function _update_notices() {
$this->admin_notices = array_filter( $this->admin_notices );
if ( empty( $this->admin_notices ) ) {
delete_option( $this->ID );
} else {
update_option( $this->ID, $this->admin_notices );
}
}
/**
* Output all notices in the admin.
*
* @return null
*/
public function display_admin_notices() {
// Display admin notices
foreach ( array_keys( $this->admin_notices ) as $notice_id )
$this->display_admin_notice( $notice_id );
}
/**
* Output an individual notice.
*
* @param string $notice_id The notice id (or key)
* @return null
*/
private function display_admin_notice ( $notice_id ) {
if ( ! $notice = $this->admin_notices[$notice_id] )
return;
?>
<div class="<?php echo esc_attr( $notice['type'] ); ?> ' fade">
<p>
<?php echo $notice['message']; ?>
<?php if ( empty( $notice['display_once'] ) ) : ?>
<a class="button" style="margin-left: 10px; color: inherit; text-decoration: none;" href="<?php echo wp_nonce_url( add_query_arg( $this->ID . '_notice_dismiss', $notice_id ), $this->ID . '_notice_dismiss' ); ?>">Dismiss</a>
<?php endif; ?>
</p>
</div>
<?php
if ( $notice['display_once'] )
$this->unset_admin_notice( $notice_id );
}
/**
* Remove an admin notice by key from $this->admin_notices
*
* @param string $notice_id Notice ID (or key)
* @return null
*/
private function unset_admin_notice( $notice_id ) {
if ( array_key_exists( $notice_id, $this->admin_notices ) )
unset( $this->admin_notices[$notice_id] );
}
/**
* Deletes an admin notice.
*
* Requirements:
* $this->ID . '_notice_dismiss' nonce verification
* value of $_GET[$this->ID . '_notice_dismiss'] si the ID of the notice to be deleted.
*
* @param null
* @return null
*/
public function delete_notice_action() {
if ( ! wp_verify_nonce( $_GET['_wpnonce'], $this->ID . '_notice_dismiss' ) )
return;
$this->unset_admin_notice( $_GET[$this->ID . '_notice_dismiss'] );
}
}
}