1 | <?php |
||
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() |
||
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * @param bool $force_update |
||
179 | */ |
||
180 | private function setForceUpdate($force_update) |
||
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getCapability() |
||
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() |
||
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() |
||
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * @param bool $dismissed |
||
247 | */ |
||
248 | public function setDismissed($dismissed) |
||
252 | |||
253 | |||
254 | |||
255 | /** |
||
256 | * @return CapCheckInterface |
||
257 | * @throws InvalidDataTypeException |
||
258 | */ |
||
259 | public function getCapCheck() |
||
271 | |||
272 | |||
273 | |||
274 | /** |
||
275 | * @param CapCheckInterface $cap_check |
||
276 | */ |
||
277 | private function setCapCheck(CapCheckInterface $cap_check) |
||
281 | |||
282 | |||
283 | |||
284 | /** |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function getPurge() |
||
291 | |||
292 | |||
293 | |||
294 | /** |
||
295 | * @param bool $purge |
||
296 | */ |
||
297 | public function setPurge($purge) |
||
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 | * @throws InvalidDataTypeException |
||
314 | */ |
||
315 | public function registerPersistentAdminNotice(Collection $persistent_admin_notice_collection) |
||
338 | |||
339 | |||
340 | |||
341 | /** |
||
342 | * @throws Exception |
||
343 | */ |
||
344 | public function confirmRegistered() |
||
361 | |||
362 | |||
363 | } |
||
364 | // End of file PersistentAdminNotice.php |
||
366 |