Completed
Branch BUG-10911-php-7.2 (ef442d)
by
unknown
104:08 queued 92:42
created

PersistentAdminNotice   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 334
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 334
rs 9.6
wmc 32
lcom 2
cbo 4

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getForceUpdate() 0 4 1
A setForceUpdate() 0 4 1
A getCapability() 0 4 1
A getCapContext() 0 4 1
A getDismissed() 0 4 1
A setDismissed() 0 4 1
A getCapCheck() 0 12 2
A setCapCheck() 0 4 1
A getPurge() 0 4 1
A setPurge() 0 4 1
A __construct() 0 20 1
A getName() 0 4 1
A setName() 0 7 2
A getMessage() 0 4 1
A setMessage() 0 9 2
A setCapability() 0 7 3
A setCapContext() 0 7 3
B registerPersistentAdminNotice() 0 23 5
A confirmRegistered() 0 16 3
1
<?php
2
3
namespace EventEspresso\core\domain\entities\notifications;
4
5
use DomainException;
6
use EventEspresso\core\domain\services\capabilities\CapCheck;
7
use EventEspresso\core\domain\services\capabilities\CapCheckInterface;
8
use EventEspresso\core\domain\services\capabilities\RequiresCapCheckInterface;
9
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay;
10
use EventEspresso\core\exceptions\InvalidDataTypeException;
11
use EventEspresso\core\exceptions\InvalidEntityException;
12
use EventEspresso\core\services\collections\Collection;
13
use Exception;
14
15
defined('EVENT_ESPRESSO_VERSION') || exit;
16
17
18
19
/**
20
 * Class PersistentAdminNotice
21
 * A DTO for recording details about a type of admin notice
22
 * that needs to be displayed repeatedly to an admin until dismissed
23
 *
24
 * @package Event Espresso
25
 * @author  Brent Christensen
26
 * @since   4.9.27
27
 */
28
class PersistentAdminNotice implements RequiresCapCheckInterface
29
{
30
31
    /**
32
     * @var string $name
33
     */
34
    protected $name = '';
35
36
    /**
37
     * @var string $message
38
     */
39
    protected $message = '';
40
41
    /**
42
     * @var boolean $force_update
43
     */
44
    protected $force_update = false;
45
46
    /**
47
     * @var string $capability
48
     */
49
    protected $capability = 'manage_options';
50
51
    /**
52
     * @var string $cap_context
53
     */
54
    protected $cap_context = 'view persistent admin notice';
55
56
    /**
57
     * @var boolean $dismissed
58
     */
59
    protected $dismissed = false;
60
61
    /**
62
     * @var CapCheckInterface $cap_check
63
     */
64
    protected $cap_check;
65
66
    /**
67
     * if true, then this notice will be deleted from the database
68
     *
69
     * @var boolean $purge
70
     */
71
    protected $purge = false;
72
73
    /**
74
     * gets set to true if notice is successfully registered with the PersistentAdminNoticeManager
75
     * if false, and WP_DEBUG is on, then an exception will be thrown in the admin footer
76
     *
77
     * @var boolean $registered
78
     */
79
    private $registered = false;
80
81
82
83
    /**
84
     * PersistentAdminNotice constructor
85
     *
86
     * @param string $name         [required] the name, or key of the Persistent Admin Notice to be stored
87
     * @param string $message      [required] the message to be stored persistently until dismissed
88
     * @param bool   $force_update enforce the reappearance of a persistent message
89
     * @param string $capability   user capability required to view this notice
90
     * @param string $cap_context  description for why the cap check is being performed
91
     * @param bool   $dismissed    whether or not the user has already dismissed/viewed this notice
92
     * @throws InvalidDataTypeException
93
     */
94
    public function __construct(
95
        $name,
96
        $message,
97
        $force_update = false,
98
        $capability = 'manage_options',
99
        $cap_context = 'view persistent admin notice',
100
        $dismissed = false
101
    ) {
102
        $this->setName($name);
103
        $this->setMessage($message);
104
        $this->setForceUpdate($force_update);
105
        $this->setCapability($capability);
106
        $this->setCapContext($cap_context);
107
        $this->setDismissed($dismissed);
108
        add_action(
109
            'AHEE__EventEspresso_core_services_notifications_PersistentAdminNoticeManager__registerNotices',
110
            array($this, 'registerPersistentAdminNotice')
111
        );
112
        add_action('shutdown', array($this, 'confirmRegistered'), 999);
113
    }
114
115
116
117
    /**
118
     * @return string
119
     */
120
    public function getName()
121
    {
122
        return $this->name;
123
    }
124
125
126
127
    /**
128
     * @param string $name
129
     * @throws InvalidDataTypeException
130
     */
131
    private function setName($name)
132
    {
133
        if (! is_string($name)) {
134
            throw new InvalidDataTypeException('$name', $name, 'string');
135
        }
136
        $this->name = sanitize_key($name);
137
    }
138
139
140
141
    /**
142
     * @return string
143
     */
144
    public function getMessage()
145
    {
146
        return $this->message;
147
    }
148
149
150
151
    /**
152
     * @param string $message
153
     * @throws InvalidDataTypeException
154
     */
155
    private function setMessage($message)
156
    {
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 = ! empty($capability) ? $capability : 'manage_options';
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 = ! empty($cap_context) ? $cap_context : 'view persistent admin notice';
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
        if ($this->registered) {
317
            return;
318
        }
319
        // first check if this notice has already been added to the collection
320
        if ($persistent_admin_notice_collection->has($this->name)) {
321
            /** @var PersistentAdminNotice $existing */
322
            $existing = $persistent_admin_notice_collection->get($this->name);
323
            // we don't need to add it again (we can't actually)
324
            // but if it has already been dismissed, and we are overriding with a forced update
325
            if ($existing->getDismissed() && $this->getForceUpdate()) {
326
                // then toggle the notice's dismissed state to true
327
                // so that it gets displayed again
328
                $existing->setDismissed(false);
329
                // and make sure the message is set
330
                $existing->setMessage($this->message);
331
            }
332
        } else {
333
            $persistent_admin_notice_collection->add($this, $this->name);
334
        }
335
        $this->registered = true;
336
    }
337
338
339
340
    /**
341
     * @throws Exception
342
     */
343
    public function confirmRegistered()
344
    {
345
        if (! $this->registered && WP_DEBUG) {
346
            new ExceptionStackTraceDisplay(
347
                new DomainException(
348
                    sprintf(
349
                        esc_html__(
350
                            '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.',
351
                            'event_espresso'
352
                        ),
353
                        $this->name
354
                    )
355
                )
356
            );
357
        }
358
    }
359
360
361
}
362
// End of file PersistentAdminNotice.php
363
// Location: core/domain/entities/notifications/PersistentAdminNotice.php
364