PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.5
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.5
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.5, at lib/social/includes/class-notices.php

487 lines 9.6 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 array $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 array $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 array $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 some keys aren't set, define them now.
331 if ( ! isset( $notices['success'] ) ) {
332 $notices['success'] = array();
333 }
334 if ( ! isset( $notices['warning'] ) ) {
335 $notices['warning'] = array();
336 }
337 if ( ! isset( $notices['error'] ) ) {
338 $notices['error'] = array();
339 }
340
341 // Return.
342 return $notices;
343
344 }
345
346 /**
347 * Saves the given notices array.
348 *
349 * @since 3.9.6
350 *
351 * @param array $notices Notices.
352 * @return bool Success
353 */
354 private function save_notices( $notices ) {
355
356 /**
357 * Filters the success and error notices to save.
358 *
359 * @since 3.9.6
360 *
361 * @param array $notices Success and Error Notices.
362 */
363 $notices = apply_filters( $this->base->plugin->filter_name . '_notices_save', $notices );
364
365 // Update settings.
366 set_transient( $this->key_prefix, $notices, 60 );
367
368 return true;
369
370 }
371
372 /**
373 * Deletes all notices
374 *
375 * @since 3.9.6
376 */
377 public function delete_notices() {
378
379 // Delete from class.
380 $this->notices['success'] = array();
381 $this->notices['warning'] = array();
382 $this->notices['error'] = array();
383
384 // Delete from transients.
385 delete_transient( $this->key_prefix );
386
387 /**
388 * Run any actions immediately after deleting all notices.
389 *
390 * @since 3.9.6
391 */
392 do_action( $this->base->plugin->filter_name . '_notices_delete_notices' );
393
394 return true;
395
396 }
397
398 /**
399 * Output any success and error notices
400 *
401 * @since 3.9.6
402 */
403 public function output_notices() {
404
405 // If no notices exist in the class, check the storage.
406 if ( count( $this->notices['success'] ) === 0 &&
407 count( $this->notices['warning'] ) === 0 &&
408 count( $this->notices['error'] ) === 0 ) {
409 $this->notices = $this->get_notices();
410 }
411
412 // Success.
413 if ( count( $this->notices['success'] ) > 0 ) {
414 ?>
415 <div class="notice notice-success is-dismissible">
416 <p>
417 <?php
418 echo wp_kses(
419 implode( '<br />', $this->notices['success'] ),
420 array(
421 'a' => array(
422 'href' => array(),
423 'target' => array(),
424 ),
425 'br' => array(),
426 )
427 );
428 ?>
429 </p>
430 </div>
431 <?php
432 }
433
434 // Warning.
435 if ( count( $this->notices['warning'] ) > 0 ) {
436 ?>
437 <div class="notice notice-warning is-dismissible">
438 <p>
439 <?php
440 echo wp_kses(
441 implode( '<br />', $this->notices['warning'] ),
442 array(
443 'a' => array(
444 'href' => array(),
445 'target' => array(),
446 ),
447 'br' => array(),
448 )
449 );
450 ?>
451 </p>
452 </div>
453 <?php
454 }
455
456 // Error.
457 if ( count( $this->notices['error'] ) > 0 ) {
458 ?>
459 <div class="notice notice-error is-dismissible">
460 <p>
461 <?php
462 echo wp_kses(
463 implode( '<br />', $this->notices['error'] ),
464 array(
465 'a' => array(
466 'href' => array(),
467 'target' => array(),
468 ),
469 'br' => array(),
470 )
471 );
472 ?>
473 </p>
474 </div>
475 <?php
476 }
477
478 // Clear storage if it's not enabled.
479 // This prevents notices stored in this page request from being immediately destroyed.
480 if ( ! $this->store ) {
481 $this->delete_notices();
482 }
483
484 }
485
486 }
487