PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.1
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.1
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 All 126 releases
wp-to-buffer / lib / social / includes / class-notices.php

class-notices.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.1, at lib/social/includes/class-notices.php

496 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Notices class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Persists success, warning and error messages
13 * across Admin Screens.
14 *
15 * @package WPZinc\Social
16 * @author WP Zinc
17 * @version 3.9.6
18 */
19 class Notices {
20
21 /**
22 * Holds the base object.
23 *
24 * @since 3.9.6
25 *
26 * @var object
27 */
28 public $base;
29
30 /**
31 * Holds success and error notices to be displayed
32 *
33 * @since 3.9.6
34 *
35 * @var array
36 */
37 public $notices = array(
38 'success' => array(),
39 'warning' => array(),
40 'error' => array(),
41 );
42
43 /**
44 * Whether to store notices for displaying on the next page load.
45 *
46 * Set using enable_persistence() and disable_persistence().
47 *
48 * @since 3.9.6
49 *
50 * @var bool
51 */
52 private $store = false;
53
54 /**
55 * The key prefix to use for stored notices
56 *
57 * @since 3.9.6
58 *
59 * @var string
60 */
61 private $key_prefix = '';
62
63 /**
64 * Constructor.
65 *
66 * @since 3.9.6
67 *
68 * @param object $base Base Plugin Class.
69 */
70 public function __construct( $base ) {
71
72 // Store base class.
73 $this->base = $base;
74
75 }
76
77 /**
78 * Enable persistence on notices
79 *
80 * @since 3.9.6
81 */
82 public function enable_store() {
83
84 $this->store = true;
85
86 }
87
88 /**
89 * Disable persistence on notices
90 *
91 * @since 3.9.6
92 */
93 public function disable_store() {
94
95 $this->store = false;
96
97 }
98
99 /**
100 * Defines the key prefix to use for setting and getting notices
101 *
102 * @since 3.9.6
103 *
104 * @param string $key_prefix Key Prefix.
105 */
106 public function set_key_prefix( $key_prefix ) {
107
108 $this->key_prefix = $key_prefix;
109
110 }
111
112 /**
113 * Returns all Success Notices that need to be displayed.
114 *
115 * @since 3.9.6
116 *
117 * @return array Notices
118 */
119 public function get_success_notices() {
120
121 // Get notices from store, if required.
122 if ( $this->store ) {
123 $this->notices = $this->get_notices();
124 }
125
126 // Get success notices.
127 $success_notices = ( isset( $this->notices['success'] ) ? $this->notices['success'] : array() );
128
129 /**
130 * Filters the success notices to return.
131 *
132 * @since 3.9.6
133 *
134 * @param array $success_notices Success Notices.
135 * @param object $this->notices Success and Error Notices.
136 */
137 $success_notices = apply_filters( $this->base->plugin->filter_name . '_notices_get_success_notices', $success_notices, $this->notices );
138
139 // Return.
140 return $success_notices;
141
142 }
143
144 /**
145 * Add a single Success Notice
146 *
147 * @since 3.9.6
148 *
149 * @param string $value Message.
150 * @return bool Success
151 */
152 public function add_success_notice( $value ) {
153
154 // Get notices from store, if required.
155 if ( $this->store ) {
156 $this->notices = $this->get_notices();
157 }
158
159 // Add success notice.
160 if ( isset( $this->notices['success'] ) ) {
161 // Bail if the notice already exists.
162 if ( in_array( $value, $this->notices['success'], true ) ) {
163 return true;
164 }
165
166 $this->notices['success'][] = $value;
167 } else {
168 $this->notices['success'] = array( $value );
169 }
170
171 // Remove any duplicates.
172 $this->notices['success'] = array_values( array_unique( $this->notices['success'] ) );
173
174 // Store notices, if required.
175 if ( $this->store ) {
176 $this->save_notices( $this->notices );
177 }
178
179 return true;
180
181 }
182
183 /**
184 * Returns all Warning Notices that need to be displayed.
185 *
186 * @since 4.5.3
187 *
188 * @return array Notices
189 */
190 public function get_warning_notices() {
191
192 // Get notices from store, if required.
193 if ( $this->store ) {
194 $this->notices = $this->get_notices();
195 }
196
197 // Get warning notices.
198 $warning_notices = ( isset( $this->notices['warning'] ) ? $this->notices['warning'] : array() );
199
200 /**
201 * Filters the error notices to return.
202 *
203 * @since 4.5.3
204 *
205 * @param array $warning_notices Warning Notices.
206 * @param object $this->notices Success, Warning and Error Notices.
207 */
208 $warning_notices = apply_filters( $this->base->plugin->filter_name . '_notices_get_warning_notices', $warning_notices, $this->notices );
209
210 // Return.
211 return $warning_notices;
212
213 }
214
215 /**
216 * Add a single Warning Notice
217 *
218 * @since 4.5.3
219 *
220 * @param string $value Message.
221 */
222 public function add_warning_notice( $value ) {
223
224 // Get notices from store, if required.
225 if ( $this->store ) {
226 $this->notices = $this->get_notices();
227 }
228
229 // Add warning notice.
230 if ( isset( $this->notices['warning'] ) ) {
231 $this->notices['warning'][] = $value;
232 } else {
233 $this->notices['warning'] = array( $value );
234 }
235
236 // Remove any duplicates.
237 $this->notices['warning'] = array_values( array_unique( $this->notices['warning'] ) );
238
239 // Store notices, if required.
240 if ( $this->store ) {
241 $this->save_notices( $this->notices );
242 }
243
244 }
245
246 /**
247 * Returns all Error Notices that need to be displayed.
248 *
249 * @since 3.9.6
250 *
251 * @return array Notices
252 */
253 public function get_error_notices() {
254
255 // Get notices from store, if required.
256 if ( $this->store ) {
257 $this->notices = $this->get_notices();
258 }
259
260 // Get error notices.
261 $error_notices = ( isset( $this->notices['error'] ) ? $this->notices['error'] : array() );
262
263 /**
264 * Filters the error notices to return.
265 *
266 * @since 3.9.6
267 *
268 * @param array $error_notices Error Notices.
269 * @param object $this->notices Success and Error Notices.
270 */
271 $error_notices = apply_filters( $this->base->plugin->filter_name . '_notices_get_error_notices', $error_notices, $this->notices );
272
273 // Return.
274 return $error_notices;
275
276 }
277
278 /**
279 * Add a single Error Notice
280 *
281 * @since 3.9.6
282 *
283 * @param string $value Message.
284 */
285 public function add_error_notice( $value ) {
286
287 // Get notices from store, if required.
288 if ( $this->store ) {
289 $this->notices = $this->get_notices();
290 }
291
292 // Add error notice.
293 if ( isset( $this->notices['error'] ) ) {
294 $this->notices['error'][] = $value;
295 } else {
296 $this->notices['error'] = array( $value );
297 }
298
299 // Remove any duplicates.
300 $this->notices['error'] = array_values( array_unique( $this->notices['error'] ) );
301
302 // Store notices, if required.
303 if ( $this->store ) {
304 $this->save_notices( $this->notices );
305 }
306
307 }
308
309 /**
310 * Returns all Success and Error notices
311 *
312 * @since 3.9.6
313 *
314 * @return array Notices
315 */
316 private function get_notices() {
317
318 // Get notices.
319 $notices = get_transient( $this->key_prefix );
320
321 /**
322 * Filters the success and error notices to return.
323 *
324 * @since 3.9.6
325 *
326 * @param array $notices Success and Error Notices.
327 */
328 $notices = apply_filters( $this->base->plugin->filter_name . '_notices_get_notices', $notices );
329
330 // If not an array, setup.
331 if ( ! is_array( $notices ) ) {
332 $notices = array(
333 'success' => array(),
334 'warning' => array(),
335 'error' => array(),
336 );
337 }
338
339 // If some keys aren't set, define them now.
340 if ( ! isset( $notices['success'] ) ) {
341 $notices['success'] = array();
342 }
343 if ( ! isset( $notices['warning'] ) ) {
344 $notices['warning'] = array();
345 }
346 if ( ! isset( $notices['error'] ) ) {
347 $notices['error'] = array();
348 }
349
350 // Return.
351 return $notices;
352
353 }
354
355 /**
356 * Saves the given notices array.
357 *
358 * @since 3.9.6
359 *
360 * @param array $notices Notices.
361 * @return bool Success
362 */
363 private function save_notices( $notices ) {
364
365 /**
366 * Filters the success and error notices to save.
367 *
368 * @since 3.9.6
369 *
370 * @param array $notices Success and Error Notices.
371 */
372 $notices = apply_filters( $this->base->plugin->filter_name . '_notices_save', $notices );
373
374 // Update settings.
375 set_transient( $this->key_prefix, $notices, 60 );
376
377 return true;
378
379 }
380
381 /**
382 * Deletes all notices
383 *
384 * @since 3.9.6
385 */
386 public function delete_notices() {
387
388 // Delete from class.
389 $this->notices['success'] = array();
390 $this->notices['warning'] = array();
391 $this->notices['error'] = array();
392
393 // Delete from transients.
394 delete_transient( $this->key_prefix );
395
396 /**
397 * Run any actions immediately after deleting all notices.
398 *
399 * @since 3.9.6
400 */
401 do_action( $this->base->plugin->filter_name . '_notices_delete_notices' );
402
403 return true;
404
405 }
406
407 /**
408 * Output any success and error notices
409 *
410 * @since 3.9.6
411 */
412 public function output_notices() {
413
414 // If no notices exist in the class, check the storage.
415 if ( count( $this->notices['success'] ) === 0 &&
416 count( $this->notices['warning'] ) === 0 &&
417 count( $this->notices['error'] ) === 0 ) {
418 $this->notices = $this->get_notices();
419 }
420
421 // Success.
422 if ( count( $this->notices['success'] ) > 0 ) {
423 ?>
424 <div class="notice notice-success is-dismissible">
425 <p>
426 <?php
427 echo wp_kses(
428 implode( '<br />', $this->notices['success'] ),
429 array(
430 'a' => array(
431 'href' => array(),
432 'target' => array(),
433 ),
434 'br' => array(),
435 )
436 );
437 ?>
438 </p>
439 </div>
440 <?php
441 }
442
443 // Warning.
444 if ( count( $this->notices['warning'] ) > 0 ) {
445 ?>
446 <div class="notice notice-warning is-dismissible">
447 <p>
448 <?php
449 echo wp_kses(
450 implode( '<br />', $this->notices['warning'] ),
451 array(
452 'a' => array(
453 'href' => array(),
454 'target' => array(),
455 ),
456 'br' => array(),
457 )
458 );
459 ?>
460 </p>
461 </div>
462 <?php
463 }
464
465 // Error.
466 if ( count( $this->notices['error'] ) > 0 ) {
467 ?>
468 <div class="notice notice-error is-dismissible">
469 <p>
470 <?php
471 echo wp_kses(
472 implode( '<br />', $this->notices['error'] ),
473 array(
474 'a' => array(
475 'href' => array(),
476 'target' => array(),
477 ),
478 'br' => array(),
479 )
480 );
481 ?>
482 </p>
483 </div>
484 <?php
485 }
486
487 // Clear storage if it's not enabled.
488 // This prevents notices stored in this page request from being immediately destroyed.
489 if ( ! $this->store ) {
490 $this->delete_notices();
491 }
492
493 }
494
495 }
496