|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin notices |
|
4
|
|
|
* |
|
5
|
|
|
* @package SimpleCalendar/Admin |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace SimpleCalendar\Admin; |
|
8
|
|
|
|
|
9
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
10
|
|
|
exit; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Admin notices class. |
|
15
|
|
|
* |
|
16
|
|
|
* Handles and displays notices in admin dashboard pages. |
|
17
|
|
|
* |
|
18
|
|
|
* @since 3.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Notices { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get notices. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 3.0.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() { |
|
28
|
|
|
add_action( 'admin_init', array( $this, 'remove_notice' ), 10 ); |
|
29
|
|
|
add_action( 'admin_init', array( $this, 'process_notices' ), 40 ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Process notices. |
|
34
|
|
|
* |
|
35
|
|
|
* @since 3.0.0 |
|
36
|
|
|
*/ |
|
37
|
|
|
public function process_notices() { |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$notices = $this->get_notices(); |
|
40
|
|
|
|
|
41
|
|
|
if ( ! empty( $notices ) && is_array( $notices ) ) { |
|
42
|
|
|
|
|
43
|
|
|
foreach ( $notices as $group ) { |
|
44
|
|
|
foreach ( $group as $notice ) { |
|
45
|
|
|
|
|
46
|
|
|
if ( $notice instanceof Notice ) { |
|
47
|
|
|
|
|
48
|
|
|
if ( $notice->visible === false ) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ( ! empty( $notice->capability ) ) { |
|
53
|
|
|
if ( ! current_user_can( $notice->capability ) ) { |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ( ! empty( $notice->screen ) && is_array( $notice->screen ) && function_exists( 'get_current_screen' ) ) { |
|
59
|
|
|
$screen = get_current_screen(); |
|
60
|
|
|
if ( isset( $screen->id ) ) { |
|
61
|
|
|
if ( ! in_array( $screen->id, $notice->screen ) ) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ( ! empty( $notice->post ) && is_array( $notice->post ) ) { |
|
68
|
|
|
if ( isset( $_GET['post'] ) ) { |
|
69
|
|
|
if ( ! in_array( intval( $_GET['post'] ), $notice->post ) ) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
} else { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->add_notice( $notice ); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add notice. |
|
86
|
|
|
* |
|
87
|
|
|
* @TODO Improve notice dismissal with ajax. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 3.0.0 |
|
90
|
|
|
* |
|
91
|
|
|
* @param Notice $notice |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function add_notice( $notice ) { |
|
96
|
|
|
|
|
97
|
|
|
if ( $notice instanceof Notice ) { |
|
98
|
|
|
|
|
99
|
|
|
add_action( 'admin_notices', $print_notice = function() use ( $notice ) { |
|
100
|
|
|
|
|
101
|
|
|
$name = is_array( $notice->id ) ? key( $notice->id ) : $notice->id; |
|
102
|
|
|
$url = add_query_arg( array( 'dismiss_simcal_notice' => $name ) ); |
|
103
|
|
|
$dismiss_link = $notice->dismissible === true |
|
104
|
|
|
? sprintf( '<a class="dashicons-before dashicons-dismiss simcal-dismiss-notice" href="%1$s"></a>', $url ) |
|
105
|
|
|
: ''; |
|
106
|
|
|
|
|
107
|
|
|
echo '<div class="' . $notice->type . ' ' . $notice->class . ' simcal-admin-notice" data-notice-id="' . $name . '">' . $dismiss_link . $notice->content . '</div>'; |
|
108
|
|
|
} ); |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Dismiss a notice. |
|
115
|
|
|
* |
|
116
|
|
|
* @TODO Improve notice dismissal with ajax. |
|
117
|
|
|
* |
|
118
|
|
|
* @since 3.0.0 |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $notice (optional) The notice id. |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
public function remove_notice( $notice = '' ) { |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$notices = $this->get_notices(); |
|
127
|
|
|
$update = false; |
|
128
|
|
|
|
|
129
|
|
|
if ( ! empty( $notice ) ) { |
|
130
|
|
|
if ( isset( $notices[ $notice ] ) ) { |
|
131
|
|
|
unset( $notices[ $notice ] ); |
|
132
|
|
|
$update = true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ( isset( $_GET['dismiss_simcal_notice'] ) ) { |
|
137
|
|
|
if ( isset( $notices[ $_GET['dismiss_simcal_notice'] ] ) ) { |
|
138
|
|
|
unset( $notices[ esc_attr( $_GET['dismiss_simcal_notice'] ) ] ); |
|
139
|
|
|
$update = true; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ( $update === true ) { |
|
144
|
|
|
update_option( 'simple-calendar_admin_notices', $notices ); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Show a notice. |
|
150
|
|
|
* |
|
151
|
|
|
* @since 3.0.0 |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $notice |
|
154
|
|
|
* |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
public function show_notice( $notice ) { |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
$notices = $this->get_notices(); |
|
160
|
|
|
|
|
161
|
|
|
if ( isset( $notices[ $notice ]->visible ) ) { |
|
162
|
|
|
$notices[ $notice ]->visible = true; |
|
163
|
|
|
update_option( 'simple-calendar_admin_notices', $notices ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Hide a notice. |
|
169
|
|
|
* |
|
170
|
|
|
* @since 3.0.0 |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $notice |
|
173
|
|
|
* |
|
174
|
|
|
* @return void |
|
175
|
|
|
*/ |
|
176
|
|
View Code Duplication |
public function hide_notice( $notice ) { |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
$notices = $this->get_notices(); |
|
179
|
|
|
|
|
180
|
|
|
if ( isset( $notices[ $notice ]->visible ) ) { |
|
181
|
|
|
$notices[ $notice ]->visible = false; |
|
182
|
|
|
update_option( 'simple-calendar_admin_notices', $notices ); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get current notices. |
|
188
|
|
|
* |
|
189
|
|
|
* @since 3.0.0 |
|
190
|
|
|
* |
|
191
|
|
|
* @return array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function get_notices() { |
|
194
|
|
|
return apply_filters( |
|
195
|
|
|
'simcal_admin_notices', |
|
196
|
|
|
get_option( 'simple-calendar_admin_notices', array() ) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: