PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.2
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / admin / class-lp-admin-notice.php

class-lp-admin-notice.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9.2, at inc/admin/class-lp-admin-notice.php

515 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manage the admin notices and display them in admin
4 *
5 * @package LearnPress
6 * @author ThimPress
7 * @version 1.0
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class LP_Admin_Notice
16 */
17 class LP_Admin_Notice {
18 /**
19 * Store all notices which added anywhere before show
20 *
21 * @var array
22 */
23 protected static $_notices = array();
24
25 /**
26 * @since 3.2.6
27 *
28 * @var LP_Admin_Notice
29 */
30 protected static $instance = false;
31
32 /**
33 * List of notices.
34 *
35 * @since 3.2.6
36 *
37 * @var array
38 */
39 protected $notices = array();
40
41 /**
42 * Option key for storing notices.
43 *
44 * @since 3.2.6
45 *
46 * @var string
47 */
48 protected $option_id = 'lp_admin_notices';
49
50 /**
51 * @var string
52 */
53 protected $dismissed_option_id = 'lp_admin_dismissed_notices';
54
55 /**
56 * LP_Admin_Notice construct
57 */
58 protected function __construct() {
59 // add_action( 'init', array( $this, 'dismiss_notice' ) );
60 add_action( 'init', array( $this, 'load' ) );
61 add_action( 'admin_notices', array( $this, 'show_notices' ), 90 );
62 }
63
64 public function load() {
65 $notices = get_option( $this->option_id );
66
67 if ( ! $notices ) {
68 return false;
69 }
70
71 $this->notices = array_merge( $notices, $this->notices );
72
73 delete_option( $this->option_id );
74
75 return true;
76 }
77
78 /**
79 * Add new notice to show in admin page.
80 *
81 * @since 3.2.6
82 *
83 * @param string|WP_Error $message
84 * @param string $type
85 * @param bool $dismissible
86 * @param string $id
87 * @param bool $redirect
88 * @param bool $override
89 *
90 * @return boolean
91 */
92 public function add( $message, $type = 'success', $dismissible = true, $id = '', $redirect = false, $override = false ) {
93 if ( ! $id ) {
94 $id = uniqid();
95 }
96
97 if ( empty( $this->notices[ $id ] ) || $override ) {
98 $this->notices[ $id ] = array(
99 'message' => $message,
100 'type' => $type ? $type : 'success',
101 'redirect' => $redirect,
102 'dismissible' => $dismissible,
103 );
104
105 return true;
106 }
107
108 return false;
109 }
110
111 /**
112 * @param string|WP_Error $message
113 * @param string $type
114 * @param bool $dismissible
115 * @param string $id
116 * @param bool $override
117 *
118 * @return bool
119 */
120 public function add_redirect( $message, $type = 'success', $dismissible = false, $id = '', $override = false ) {
121 return $this->add( $message, $type, $dismissible, $id, true, $override );
122 }
123
124 /**
125 * Show all notices.
126 *
127 * @return bool
128 */
129 public function show_notices() {
130 if ( empty( $this->notices ) ) {
131 return false;
132 }
133
134 $redirect_notices = array();
135
136 foreach ( $this->notices as $id => $notice ) {
137 $notice['message'] = $notice['message'] instanceof WP_Error ? $notice['message']->get_error_message() : $notice['message'];
138
139 if ( $notice['redirect'] ) {
140 $notice['redirect'] = false;
141 $redirect_notices[ $id ] = $notice;
142 } else {
143 if ( ! $this->has_dismissed_notice( $id ) ) {
144 if ( preg_match( '/.php$/', $notice['message'] ) ) {
145 $view = $notice['message'];
146 } else {
147 $view = 'admin-notice.php';
148 }
149
150 learn_press_admin_view( $view, array_merge( $notice, array( 'id' => $id ) ) );
151 }
152 }
153 }
154
155 if ( $redirect_notices ) {
156 update_option( $this->option_id, $redirect_notices );
157 }
158
159 return true;
160 }
161
162 /**
163 * Check if a notice is already added by id.
164 *
165 * @since 3.2.6
166 *
167 * @param string[] $notice
168 *
169 * @return bool
170 */
171 public function has_notice( $notice ) {
172 settype( $notice, 'array' );
173
174 if ( ! $this->notices ) {
175 return false;
176 }
177
178 foreach ( $notice as $id ) {
179 if ( ! empty( $this->notices[ $id ] ) ) {
180 return true;
181 }
182 }
183
184 return false;
185 }
186
187 /**
188 * Remove one or more notices from queue by ids.
189 *
190 * @since 3.2.6
191 *
192 * @param string|array $notice
193 *
194 * @return array|bool
195 */
196 public function remove( $notice ) {
197 settype( $notice, 'array' );
198
199 if ( ! $this->notices ) {
200 return false;
201 }
202
203 foreach ( $notice as $id ) {
204 if ( ! empty( $this->notices[ $id ] ) ) {
205 unset( $this->notices[ $id ] );
206 }
207 }
208
209 // Also remove in db
210 $redirects = get_option( $this->option_id );
211 if ( $redirects ) {
212 foreach ( $notice as $id ) {
213 if ( ! empty( $redirects[ $id ] ) ) {
214 unset( $redirects[ $id ] );
215 }
216 }
217
218 update_option( $this->option_id, $redirects );
219 }
220
221 return $this->notices;
222 }
223
224 /**
225 * Clear all notices.
226 *
227 * @since 3.2.6
228 */
229 public function clear() {
230 $this->notices = array();
231 delete_option( $this->option_id );
232 }
233
234 /**
235 * @since 3.2.6
236 */
237 /*public function dismiss_notice() {
238 $id = LP_Request::get( 'lp-dismiss-notice' );
239
240 if ( ! $id ) {
241 return;
242 }
243
244 $dismissed = get_option( $this->dismissed_option_id );
245
246 if ( ! $dismissed ) {
247 $dismissed = array();
248 }
249
250 do_action( 'learn-press/before-dismiss-notice', $id );
251
252 if ( array_search( $id, $dismissed ) === false ) {
253 $dismissed[] = $id;
254 update_option( $this->dismissed_option_id, $dismissed );
255 }
256
257 do_action( 'learn-press/dismissed-notice', $id );
258
259 learn_press_send_json(
260 apply_filters(
261 'learn-press/dismissed-notice-response',
262 array(
263 'dismissed' => $id,
264 ),
265 $id
266 )
267 );
268 }*/
269
270 /**
271 * Update option to turn-off a notice.
272 *
273 * @since 3.2.6
274 *
275 * @param string $name
276 * @param string $value
277 * @param int $expired
278 */
279 public function dismiss_notice_2( $name, $value, $expired = 0 ) {
280 if ( $expired ) {
281 set_transient( 'lp_dismiss_notice' . $name, $value, $expired );
282 } else {
283 $values = get_option( 'lp_dismiss_notice' );
284 if ( ! $values ) {
285 $values = array( $name => $value );
286 } else {
287 $values[ $name ] = $value;
288 }
289
290 update_option( 'lp_dismiss_notice', $values );
291 }
292 }
293
294 /**
295 * Check if a notice has dismissed.
296 *
297 * @since 3.2.6
298 *
299 * @param string $name
300 *
301 * @return bool
302 */
303 public function has_dismissed_notice( $name ) {
304
305 $dismissed = get_option( $this->dismissed_option_id );
306
307 if ( ! $dismissed ) {
308 return false;
309 }
310
311 return array_search( $name, $dismissed ) !== false;
312 }
313
314 /**
315 * Restore dismissed notices by id.
316 *
317 * @since 3.2.6
318 *
319 * @param string[] $notice
320 *
321 * @return bool
322 */
323 public function restore_dismissed_notice( $notice ) {
324 $dismissed = get_option( $this->dismissed_option_id );
325
326 if ( ! $dismissed ) {
327 return false;
328 }
329
330 settype( $notice, 'array' );
331
332 foreach ( $notice as $id ) {
333 $at = array_search( $id, $dismissed );
334
335 if ( false !== $at ) {
336 unset( $dismissed[ $at ] );
337 }
338 }
339
340 update_option( $this->dismissed_option_id, $dismissed );
341
342 return true;
343 }
344
345 /**
346 * Clear all notices has dismissed.
347 *
348 * @since 3.2.6
349 */
350 public function clear_dismissed_notice() {
351 delete_option( $this->dismissed_option_id );
352 }
353
354 /**
355 * Remove a notice has been dismissed.
356 *
357 * @since 3.2.6
358 *
359 * @param string|array $name - Optional. NULL will remove all notices.
360 * @param bool $expired - Optional. TRUE if dismiss notice as transient (in case $name passed).
361 *
362 * @return bool
363 */
364 public function remove_dismissed_notice( $name = '' ) {
365 if ( ! $name ) {
366 global $wpdb;
367
368 $query = $wpdb->prepare( "SELECT SUBSTR(option_name, 12) FROM {$wpdb->options} WHERE option_name LIKE %s", '%' . $wpdb->esc_like( '_transient_lp_dismiss_notice' ) . '%' );
369
370 $all_notices = $wpdb->get_col( $query );
371
372 if ( $all_notices ) {
373 foreach ( $all_notices as $notice ) {
374 delete_transient( $notice );
375 }
376 }
377
378 delete_option( 'lp_dismiss_notice' );
379
380 return true;
381
382 } elseif ( is_array( $name ) ) {
383 foreach ( $name as $notice ) {
384 $this->remove_dismissed_notice( $notice );
385 }
386
387 return true;
388 }
389
390 delete_transient( 'lp_dismiss_notice' . $name );
391
392 $values = get_option( 'lp_dismiss_notice' );
393
394 if ( ! $values ) {
395 return false;
396 } else {
397 if ( array_key_exists( $name, $values ) ) {
398 unset( $values[ $name ] );
399
400 update_option( 'lp_dismiss_notice', $values );
401
402 return $values;
403 }
404 }
405
406 return false;
407 }
408
409 /**
410 * @deprecated
411 */
412 public function dismiss_notice_deprecated() {
413 $notice = learn_press_get_request( 'lp-hide-notice' );
414
415 if ( ! $notice ) {
416 return;
417 }
418
419 $transient = learn_press_get_request( 't' );
420
421 if ( $transient ) {
422 set_transient( 'lp-hide-notice-' . $notice, 'yes', $transient );
423 } else {
424 learn_press_update_user_option( 'hide-notice-' . $notice, 'yes' );
425 }
426
427 $redirect = apply_filters( 'learn_press_hide_notice_redirect', esc_url_raw( remove_query_arg( 'lp-hide-notice' ) ) );
428 if ( $redirect ) {
429 wp_redirect( untrailingslashit( $redirect ) );
430 exit();
431 }
432 }
433
434 /**
435 * Add new notice to queue
436 *
437 * @deprecated
438 *
439 * @param string $message The message want to display
440 * @param string $type The class name of WP message type updated|update-nag|error
441 * @param string $id Custom id for html element's ID
442 * @param bool
443 */
444 public static function add_deprecated( $message, $type = 'success', $id = '', $redirect = false ) {
445 if ( $redirect ) {
446 $notices = get_transient( 'learn_press_redirect_notices' );
447 if ( empty( $notices ) ) {
448 $notices = array();
449 }
450 $notices[] = array(
451 'type' => $type,
452 'message' => $message,
453 'id' => $id,
454 );
455 set_transient( 'learn_press_redirect_notices', $notices );
456 } else {
457 self::$_notices[] = array(
458 'type' => $type,
459 'message' => $message,
460 'id' => $id,
461 );
462 }
463 }
464
465 public static function add_redirect_reprecated( $message, $type = 'updated', $id = '' ) {
466 self::add( $message, $type, $id, true );
467 }
468
469 /**
470 * Show all notices has registered
471 *
472 * @deprecated
473 */
474 public static function show_notices_deprecated() {
475 if ( self::$_notices ) {
476 foreach ( self::$_notices as $notice ) {
477 if ( empty( $notice ) ) {
478 continue;
479 }
480 learn_press_admin_view( 'admin-notice.php', $notice );
481 }
482 }
483
484 $notices = get_transient( 'learn_press_redirect_notices' );
485 if ( $notices ) {
486 foreach ( $notices as $notice ) {
487 if ( empty( $notice ) ) {
488 continue;
489 }
490
491 learn_press_admin_view( 'admin-notice.php', $notice );
492 }
493
494 delete_transient( 'learn_press_redirect_notices' );
495 }
496 }
497
498 /**
499 * Get single instance of this class
500 *
501 * @since 3.2.6
502 *
503 * @return LP_Admin_Notice
504 */
505 public static function instance() {
506 if ( ! self::$instance ) {
507 self::$instance = new self();
508 }
509
510 return self::$instance;
511 }
512 }
513
514 LP_Admin_Notice::instance();
515