1 | <?php |
||
30 | class PersistentAdminNoticeManager |
||
31 | { |
||
32 | |||
33 | const WP_OPTION_KEY = 'ee_pers_admin_notices'; |
||
34 | |||
35 | /** |
||
36 | * @var Collection|PersistentAdminNotice[] $notice_collection |
||
37 | */ |
||
38 | private $notice_collection; |
||
39 | |||
40 | /** |
||
41 | * if AJAX is not enabled, then the return URL will be used for redirecting back to the admin page where the |
||
42 | * persistent admin notice was displayed, and ultimately dismissed from. |
||
43 | * |
||
44 | * @type string $return_url |
||
45 | */ |
||
46 | private $return_url; |
||
47 | |||
48 | /** |
||
49 | * @type CapabilitiesChecker $capabilities_checker |
||
50 | */ |
||
51 | private $capabilities_checker; |
||
52 | |||
53 | /** |
||
54 | * @type EE_Request $request |
||
55 | */ |
||
56 | private $request; |
||
57 | |||
58 | |||
59 | |||
60 | /** |
||
61 | * CapChecker constructor |
||
62 | * |
||
63 | * @param string $return_url where to redirect to after dismissing notices |
||
64 | * @param CapabilitiesChecker $capabilities_checker |
||
65 | * @param EE_Request $request |
||
66 | * @throws InvalidDataTypeException |
||
67 | */ |
||
68 | public function __construct($return_url = '', CapabilitiesChecker $capabilities_checker, EE_Request $request) |
||
80 | |||
81 | |||
82 | |||
83 | /** |
||
84 | * @param string $return_url |
||
85 | * @throws InvalidDataTypeException |
||
86 | */ |
||
87 | private function setReturnUrl($return_url) |
||
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * @return Collection |
||
99 | * @throws InvalidEntityException |
||
100 | * @throws InvalidInterfaceException |
||
101 | * @throws InvalidDataTypeException |
||
102 | * @throws DomainException |
||
103 | */ |
||
104 | protected function getPersistentAdminNoticeCollection() |
||
115 | |||
116 | |||
117 | |||
118 | /** |
||
119 | * generates PersistentAdminNotice objects for all non-dismissed notices saved to the db |
||
120 | * |
||
121 | * @return void |
||
122 | * @throws InvalidEntityException |
||
123 | * @throws DomainException |
||
124 | * @throws InvalidDataTypeException |
||
125 | */ |
||
126 | protected function retrieveStoredNotices() |
||
127 | { |
||
128 | $persistent_admin_notices = get_option(PersistentAdminNoticeManager::WP_OPTION_KEY, array()); |
||
129 | // \EEH_Debug_Tools::printr($persistent_admin_notices, '$persistent_admin_notices', __FILE__, __LINE__); |
||
130 | if (! empty($persistent_admin_notices)) { |
||
131 | foreach ($persistent_admin_notices as $name => $details) { |
||
132 | if (is_array($details)) { |
||
133 | if ( |
||
134 | ! isset( |
||
135 | $details['message'], |
||
136 | $details['capability'], |
||
137 | $details['cap_context'], |
||
138 | $details['dismissed'] |
||
139 | ) |
||
140 | ) { |
||
141 | throw new DomainException( |
||
142 | sprintf( |
||
143 | esc_html__( |
||
144 | 'The "%1$s" PersistentAdminNotice could not be retrieved from the database.', |
||
145 | 'event_espresso' |
||
146 | ), |
||
147 | $name |
||
148 | ) |
||
149 | ); |
||
150 | } |
||
151 | // new format for nag notices |
||
152 | $this->notice_collection->add( |
||
153 | new PersistentAdminNotice( |
||
154 | $name, |
||
155 | $details['message'], |
||
156 | false, |
||
157 | $details['capability'], |
||
158 | $details['cap_context'], |
||
159 | $details['dismissed'] |
||
160 | ), |
||
161 | $name |
||
162 | ); |
||
163 | } else { |
||
164 | try { |
||
165 | // old nag notices, that we want to convert to the new format |
||
166 | $this->notice_collection->add( |
||
167 | new PersistentAdminNotice( |
||
168 | $name, |
||
169 | (string)$details, |
||
170 | false, |
||
171 | '', |
||
172 | '', |
||
173 | empty($details) |
||
174 | ), |
||
175 | $name |
||
176 | ); |
||
177 | } catch (Exception $e) { |
||
178 | EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
||
179 | } |
||
180 | } |
||
181 | // each notice will self register when the action hook in registerNotices is triggered |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | |||
187 | |||
188 | /** |
||
189 | * exposes the Persistent Admin Notice Collection via an action |
||
190 | * so that PersistentAdminNotice objects can be added and/or removed |
||
191 | * without compromising the actual collection like a filter would |
||
192 | */ |
||
193 | protected function registerNotices() |
||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * @throws DomainException |
||
205 | * @throws InvalidClassException |
||
206 | * @throws InvalidDataTypeException |
||
207 | * @throws InvalidInterfaceException |
||
208 | * @throws InvalidEntityException |
||
209 | */ |
||
210 | public function displayNotices() |
||
211 | { |
||
212 | $this->notice_collection = $this->getPersistentAdminNoticeCollection(); |
||
213 | if ($this->notice_collection->hasObjects()) { |
||
214 | $enqueue_assets = false; |
||
215 | // and display notices |
||
216 | foreach ($this->notice_collection as $persistent_admin_notice) { |
||
217 | /** @var PersistentAdminNotice $persistent_admin_notice */ |
||
218 | // don't display notices that have already been dismissed |
||
219 | if ($persistent_admin_notice->getDismissed()) { |
||
220 | continue; |
||
221 | } |
||
222 | try { |
||
223 | $this->capabilities_checker->processCapCheck( |
||
224 | $persistent_admin_notice->getCapCheck() |
||
225 | ); |
||
226 | } catch (InsufficientPermissionsException $e) { |
||
227 | // user does not have required cap, so skip to next notice |
||
228 | // and just eat the exception - nom nom nom nom |
||
229 | continue; |
||
230 | } |
||
231 | if ($persistent_admin_notice->getMessage() === '') { |
||
232 | continue; |
||
233 | } |
||
234 | $this->displayPersistentAdminNotice($persistent_admin_notice); |
||
235 | $enqueue_assets = true; |
||
236 | } |
||
237 | if ($enqueue_assets) { |
||
238 | $this->enqueueAssets(); |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * does what it's named |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | public function enqueueAssets() |
||
280 | |||
281 | |||
282 | |||
283 | /** |
||
284 | * displayPersistentAdminNoticeHtml |
||
285 | * |
||
286 | * @param PersistentAdminNotice $persistent_admin_notice |
||
287 | */ |
||
288 | protected function displayPersistentAdminNotice(PersistentAdminNotice $persistent_admin_notice) |
||
295 | |||
296 | |||
297 | |||
298 | /** |
||
299 | * dismissNotice |
||
300 | * |
||
301 | * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
||
302 | * @param bool $purge if true, then delete it from the db |
||
303 | * @param bool $return forget all of this AJAX or redirect nonsense, and just return |
||
304 | * @return void |
||
305 | * @throws InvalidEntityException |
||
306 | * @throws InvalidInterfaceException |
||
307 | * @throws InvalidDataTypeException |
||
308 | * @throws DomainException |
||
309 | */ |
||
310 | public function dismissNotice($pan_name = '', $purge = false, $return = false) |
||
341 | |||
342 | |||
343 | |||
344 | /** |
||
345 | * saveNotices |
||
346 | * |
||
347 | * @throws DomainException |
||
348 | * @throws InvalidDataTypeException |
||
349 | * @throws InvalidInterfaceException |
||
350 | * @throws InvalidEntityException |
||
351 | */ |
||
352 | public function saveNotices() |
||
378 | |||
379 | |||
380 | |||
381 | /** |
||
382 | * @throws DomainException |
||
383 | * @throws InvalidDataTypeException |
||
384 | * @throws InvalidEntityException |
||
385 | * @throws InvalidInterfaceException |
||
386 | */ |
||
387 | public function registerAndSaveNotices() |
||
393 | |||
394 | |||
395 | } |
||
396 | // End of file PersistentAdminNoticeManager.php |
||
398 |