|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\domain\entities\notifications; |
|
3
|
|
|
|
|
4
|
|
|
use DomainException; |
|
5
|
|
|
use EventEspresso\core\domain\services\capabilities\CapCheck; |
|
6
|
|
|
use EventEspresso\core\domain\services\capabilities\CapCheckInterface; |
|
7
|
|
|
use EventEspresso\core\domain\services\capabilities\RequiresCapCheckInterface; |
|
8
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
9
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
|
10
|
|
|
use EventEspresso\core\services\collections\Collection; |
|
11
|
|
|
|
|
12
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class PersistentAdminNotice |
|
18
|
|
|
* A DTO for recording details about a type of admin notice |
|
19
|
|
|
* that needs to be displayed repeatedly to an admin until dismissed |
|
20
|
|
|
* |
|
21
|
|
|
* @package Event Espresso |
|
22
|
|
|
* @author Brent Christensen |
|
23
|
|
|
* @since 4.9.27 |
|
24
|
|
|
*/ |
|
25
|
|
|
class PersistentAdminNotice implements RequiresCapCheckInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string $name |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $name = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $message |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $message = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var boolean $force_update |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $force_update = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string $capability |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $capability = 'manage_options'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string $cap_context |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $cap_context = 'view persistent admin notice'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var boolean $dismissed |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $dismissed = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var CapCheckInterface $cap_check |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $cap_check; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* if true, then this notice will be deleted from the database |
|
65
|
|
|
* |
|
66
|
|
|
* @var boolean $purge |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $purge = false; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* gets set to true if notice is successfully registered with the PersistentAdminNoticeManager |
|
72
|
|
|
* if false, and WP_DEBUG is on, then an exception will be thrown in the admin footer |
|
73
|
|
|
* |
|
74
|
|
|
* @var boolean $registered |
|
75
|
|
|
*/ |
|
76
|
|
|
private $registered = false; |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* PersistentAdminNotice constructor |
|
82
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name [required] the name, or key of the Persistent Admin Notice to be stored |
|
85
|
|
|
* @param string $message [required] the message to be stored persistently until dismissed |
|
86
|
|
|
* @param bool $force_update enforce the reappearance of a persistent message |
|
87
|
|
|
* @param string $capability user capability required to view this notice |
|
88
|
|
|
* @param string $cap_context description for why the cap check is being performed |
|
89
|
|
|
* @param bool $dismissed whether or not the user has already dismissed/viewed this notice |
|
90
|
|
|
* @throws InvalidDataTypeException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __construct( |
|
93
|
|
|
$name, |
|
94
|
|
|
$message, |
|
95
|
|
|
$force_update = false, |
|
96
|
|
|
$capability = 'manage_options', |
|
97
|
|
|
$cap_context = 'view persistent admin notice', |
|
98
|
|
|
$dismissed = false |
|
99
|
|
|
) { |
|
100
|
|
|
$this->setName($name); |
|
101
|
|
|
$this->setMessage($message); |
|
102
|
|
|
$this->setForceUpdate($force_update); |
|
103
|
|
|
$this->setCapability($capability); |
|
104
|
|
|
$this->setCapContext($cap_context); |
|
105
|
|
|
$this->setDismissed($dismissed); |
|
106
|
|
|
add_action( |
|
107
|
|
|
'AHEE__EventEspresso_core_services_notifications_PersistentAdminNoticeManager__registerNotices', |
|
108
|
|
|
array($this, 'registerPersistentAdminNotice') |
|
109
|
|
|
); |
|
110
|
|
|
add_action('admin_footer', array($this, 'confirmRegistered')); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getName() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->name; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
* @throws InvalidDataTypeException |
|
128
|
|
|
*/ |
|
129
|
|
|
private function setName($name) |
|
130
|
|
|
{ |
|
131
|
|
|
if ( ! is_string($name)) { |
|
132
|
|
|
throw new InvalidDataTypeException('$name', $name, 'string'); |
|
133
|
|
|
} |
|
134
|
|
|
$this->name = sanitize_key($name); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getMessage() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->message; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $message |
|
151
|
|
|
* @throws InvalidDataTypeException |
|
152
|
|
|
*/ |
|
153
|
|
|
private function setMessage($message) |
|
154
|
|
|
{ |
|
155
|
|
|
// \EEH_Debug_Tools::printr($this->name, '$this->name', __FILE__, __LINE__); |
|
156
|
|
|
// \EEH_Debug_Tools::printr($message, '$message', __FILE__, __LINE__); |
|
157
|
|
|
if ( ! is_string($message)) { |
|
158
|
|
|
throw new InvalidDataTypeException('$message', $message, 'string'); |
|
159
|
|
|
} |
|
160
|
|
|
global $allowedtags; |
|
161
|
|
|
$allowedtags['br'] = array(); |
|
162
|
|
|
$this->message = wp_kses($message, $allowedtags); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return bool |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getForceUpdate() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->force_update; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param bool $force_update |
|
179
|
|
|
*/ |
|
180
|
|
|
private function setForceUpdate($force_update) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->force_update = filter_var($force_update, FILTER_VALIDATE_BOOLEAN); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getCapability() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->capability; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $capability |
|
199
|
|
|
* @throws InvalidDataTypeException |
|
200
|
|
|
*/ |
|
201
|
|
|
private function setCapability($capability) |
|
202
|
|
|
{ |
|
203
|
|
|
if ( ! is_string($capability)) { |
|
204
|
|
|
throw new InvalidDataTypeException('$capability', $capability, 'string'); |
|
205
|
|
|
} |
|
206
|
|
|
$this->capability = $capability; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getCapContext() |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->cap_context; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param string $cap_context |
|
223
|
|
|
* @throws InvalidDataTypeException |
|
224
|
|
|
*/ |
|
225
|
|
|
private function setCapContext($cap_context) |
|
226
|
|
|
{ |
|
227
|
|
|
if ( ! is_string($cap_context)) { |
|
228
|
|
|
throw new InvalidDataTypeException('$cap_context', $cap_context, 'string'); |
|
229
|
|
|
} |
|
230
|
|
|
$this->cap_context = $cap_context; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
|
|
public function getDismissed() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->dismissed; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param bool $dismissed |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setDismissed($dismissed) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->dismissed = filter_var($dismissed, FILTER_VALIDATE_BOOLEAN); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @return CapCheckInterface |
|
257
|
|
|
* @throws InvalidDataTypeException |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getCapCheck() |
|
260
|
|
|
{ |
|
261
|
|
|
if ( ! $this->cap_check instanceof CapCheckInterface) { |
|
262
|
|
|
$this->setCapCheck( |
|
263
|
|
|
new CapCheck( |
|
264
|
|
|
$this->capability, |
|
265
|
|
|
$this->cap_context |
|
266
|
|
|
) |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
return $this->cap_check; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @param CapCheckInterface $cap_check |
|
276
|
|
|
*/ |
|
277
|
|
|
private function setCapCheck(CapCheckInterface $cap_check) |
|
278
|
|
|
{ |
|
279
|
|
|
$this->cap_check = $cap_check; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @return bool |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getPurge() |
|
288
|
|
|
{ |
|
289
|
|
|
return $this->purge; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param bool $purge |
|
296
|
|
|
*/ |
|
297
|
|
|
public function setPurge($purge) |
|
298
|
|
|
{ |
|
299
|
|
|
$this->purge = filter_var($purge, FILTER_VALIDATE_BOOLEAN); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* given a valid PersistentAdminNotice Collection, |
|
306
|
|
|
* this notice will be added if it is not already found in the collection (using its name as the identifier) |
|
307
|
|
|
* if an existing notice is found that has already been dismissed, |
|
308
|
|
|
* but we are overriding with a forced update, then we will toggle its dismissed state, |
|
309
|
|
|
* so that the notice is displayed again |
|
310
|
|
|
* |
|
311
|
|
|
* @param Collection $persistent_admin_notice_collection |
|
312
|
|
|
* @throws InvalidEntityException |
|
313
|
|
|
*/ |
|
314
|
|
|
public function registerPersistentAdminNotice(Collection $persistent_admin_notice_collection) |
|
315
|
|
|
{ |
|
316
|
|
|
// first check if this notice has already been added to the collection |
|
317
|
|
|
if ($persistent_admin_notice_collection->has($this->name)) { |
|
318
|
|
|
/** @var PersistentAdminNotice $existing */ |
|
319
|
|
|
$existing = $persistent_admin_notice_collection->get($this->name); |
|
320
|
|
|
// we don't need to add it again (we can't actually) |
|
321
|
|
|
// but if it has already been dismissed, and we are overriding with a forced update |
|
322
|
|
|
if ($existing->getDismissed() && $this->getForceUpdate()) { |
|
323
|
|
|
// then toggle the notice's dismissed state to true |
|
324
|
|
|
// so that it gets displayed again |
|
325
|
|
|
$existing->setDismissed(false); |
|
326
|
|
|
} |
|
327
|
|
|
} else { |
|
328
|
|
|
$persistent_admin_notice_collection->add($this, $this->name); |
|
329
|
|
|
} |
|
330
|
|
|
$this->registered = true; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
|
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @throws DomainException |
|
337
|
|
|
*/ |
|
338
|
|
|
public function confirmRegistered() |
|
339
|
|
|
{ |
|
340
|
|
|
if( ! $this->registered && WP_DEBUG) { |
|
341
|
|
|
throw new DomainException( |
|
342
|
|
|
sprintf( |
|
343
|
|
|
esc_html__( |
|
344
|
|
|
'The "%1$s" PersistentAdminNotice was not successfully registered. Please ensure that it is being created prior to either the "admin_notices" or "network_admin_notices" hooks being triggered.', |
|
345
|
|
|
'event_espresso' |
|
346
|
|
|
), |
|
347
|
|
|
$this->name |
|
348
|
|
|
) |
|
349
|
|
|
); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
|
|
354
|
|
|
} |
|
355
|
|
|
// End of file PersistentAdminNotice.php |
|
356
|
|
|
// Location: core/domain/entities/notifications/PersistentAdminNotice.php |