PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.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.7.3.2, at inc/admin/class-lp-admin-notice.php

629 lines 12.9 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, 'admin_notices' ) );
62 }
63
64 /**
65 * Show notices in admin
66 *
67 * @return void
68 * @deprecated 4.1.7.3.2
69 */
70 public function admin_notices() {
71 try {
72 $rules = [
73 'check_right_plugin_base' => [
74 'class' => 'notice-error',
75 'template' => 'admin-notices/wrong-name-plugin.php',
76 'display' => call_user_func( [ $this, 'check_right_plugin_base' ] ),
77 ],
78 'notices' => [
79 'class' => 'notice-error',
80 'template' => 'admin-notices/wrong-name-plugin.php',
81 'display' => call_user_func( [ $this, 'check_right_plugin_base' ] ),
82 ],
83 ];
84
85 foreach ( $rules as $rule => $template_data ) {
86 if ( $template_data['display'] ) {
87 learn_press_admin_view( $template_data['template'] ?? '', [ 'data' => $template_data ], true );
88 }
89 }
90 } catch ( Throwable $e ) {
91 error_log( $e->getMessage() );
92 }
93 }
94
95 /**
96 * Check is right plugin base.
97 *
98 * @return bool
99 */
100 public static function check_plugin_base(): bool {
101 return 0 !== strcmp( LP_PLUGIN_BASENAME, 'learnpress/learnpress.php' );
102 }
103
104 /**
105 * Check LP has beta version.
106 *
107 * @return bool|[]
108 */
109 public static function check_lp_beta_version() {
110 $url = 'https://learnpress.github.io/learnpress/lp-beta-version.json';
111 $config = [];
112
113 try {
114 $res = wp_remote_get( $url );
115 if ( is_wp_error( $res ) ) {
116 throw new Exception( $res->get_error_message() );
117 }
118
119 $config = json_decode( wp_remote_retrieve_body( $res ), true );
120 if ( json_last_error() ) {
121 throw new Exception( json_last_error_msg() );
122 }
123
124 $version = $config['version'] ?? 0;
125 if ( ! $version ) {
126 throw new Exception( 'Version LP beta is invalid!' );
127 }
128
129 if ( ! version_compare( $version, LEARNPRESS_VERSION, '>' ) ) {
130 return false;
131 }
132 } catch ( Throwable $e ) {
133 error_log( $e->getMessage() );
134 }
135
136 return $config;
137 }
138
139 /**
140 * Get description of beta version.
141 *
142 * @param array $config
143 *
144 * @return array
145 */
146 public static function get_data_lp_beta( array $config = [] ): array {
147 try {
148 $keys = array_keys( $config );
149 $title = $config['title'] ?? '';
150 $description = $config['description'] ?? '';
151 foreach ( $keys as $key ) {
152 $description = str_replace( '[[' . $key . ']]', $config[ $key ], $description );
153 $title = str_replace( '[[' . $key . ']]', $config[ $key ], $title );
154 }
155 } catch ( Throwable $e ) {
156 $title = '';
157 $description = '';
158 error_log( $e->getMessage() );
159 }
160
161 return [
162 'title' => $title,
163 'description' => $description,
164 ];
165 }
166
167 /**
168 * @deprecated 4.1.7.3.2
169 */
170 /*public function load() {
171 $notices = get_option( $this->option_id );
172
173 if ( ! $notices ) {
174 return false;
175 }
176
177 $this->notices = array_merge( $notices, $this->notices );
178
179 delete_option( $this->option_id );
180
181 return true;
182 }*/
183
184 /**
185 * Add new notice to show in admin page.
186 *
187 * @since 3.2.6
188 *
189 * @param string|WP_Error $message
190 * @param string $type
191 * @param bool $dismissible
192 * @param string $id
193 * @param bool $redirect
194 * @param bool $override
195 *
196 * @return boolean
197 */
198 public function add( $message, $type = 'success', $dismissible = true, $id = '', $redirect = false, $override = false ) {
199 if ( ! $id ) {
200 $id = uniqid();
201 }
202
203 if ( empty( $this->notices[ $id ] ) || $override ) {
204 $this->notices[ $id ] = array(
205 'message' => $message,
206 'type' => $type ? $type : 'success',
207 'redirect' => $redirect,
208 'dismissible' => $dismissible,
209 );
210
211 return true;
212 }
213
214 return false;
215 }
216
217 /**
218 * @param string|WP_Error $message
219 * @param string $type
220 * @param bool $dismissible
221 * @param string $id
222 * @param bool $override
223 *
224 * @return bool
225 */
226 public function add_redirect( $message, $type = 'success', $dismissible = false, $id = '', $override = false ) {
227 return $this->add( $message, $type, $dismissible, $id, true, $override );
228 }
229
230 /**
231 * Show all notices.
232 *
233 * @return bool
234 */
235 public function show_notices() {
236 if ( empty( $this->notices ) ) {
237 return false;
238 }
239
240 $redirect_notices = array();
241
242 foreach ( $this->notices as $id => $notice ) {
243 $notice['message'] = $notice['message'] instanceof WP_Error ? $notice['message']->get_error_message() : $notice['message'];
244
245 if ( $notice['redirect'] ) {
246 $notice['redirect'] = false;
247 $redirect_notices[ $id ] = $notice;
248 } else {
249 if ( ! $this->has_dismissed_notice( $id ) ) {
250 if ( preg_match( '/.php$/', $notice['message'] ) ) {
251 $view = $notice['message'];
252 } else {
253 $view = 'admin-notice.php';
254 }
255
256 learn_press_admin_view( $view, array_merge( $notice, array( 'id' => $id ) ) );
257 }
258 }
259 }
260
261 if ( $redirect_notices ) {
262 update_option( $this->option_id, $redirect_notices );
263 }
264
265 return true;
266 }
267
268 /**
269 * Check if a notice is already added by id.
270 *
271 * @since 3.2.6
272 *
273 * @param string[] $notice
274 *
275 * @return bool
276 */
277 public function has_notice( $notice ) {
278 settype( $notice, 'array' );
279
280 if ( ! $this->notices ) {
281 return false;
282 }
283
284 foreach ( $notice as $id ) {
285 if ( ! empty( $this->notices[ $id ] ) ) {
286 return true;
287 }
288 }
289
290 return false;
291 }
292
293 /**
294 * Remove one or more notices from queue by ids.
295 *
296 * @since 3.2.6
297 *
298 * @param string|array $notice
299 *
300 * @return array|bool
301 */
302 public function remove( $notice ) {
303 settype( $notice, 'array' );
304
305 if ( ! $this->notices ) {
306 return false;
307 }
308
309 foreach ( $notice as $id ) {
310 if ( ! empty( $this->notices[ $id ] ) ) {
311 unset( $this->notices[ $id ] );
312 }
313 }
314
315 // Also remove in db
316 $redirects = get_option( $this->option_id );
317 if ( $redirects ) {
318 foreach ( $notice as $id ) {
319 if ( ! empty( $redirects[ $id ] ) ) {
320 unset( $redirects[ $id ] );
321 }
322 }
323
324 update_option( $this->option_id, $redirects );
325 }
326
327 return $this->notices;
328 }
329
330 /**
331 * Clear all notices.
332 *
333 * @since 3.2.6
334 */
335 public function clear() {
336 $this->notices = array();
337 delete_option( $this->option_id );
338 }
339
340 /**
341 * @since 3.2.6
342 */
343 /*public function dismiss_notice() {
344 $id = LP_Request::get( 'lp-dismiss-notice' );
345
346 if ( ! $id ) {
347 return;
348 }
349
350 $dismissed = get_option( $this->dismissed_option_id );
351
352 if ( ! $dismissed ) {
353 $dismissed = array();
354 }
355
356 do_action( 'learn-press/before-dismiss-notice', $id );
357
358 if ( array_search( $id, $dismissed ) === false ) {
359 $dismissed[] = $id;
360 update_option( $this->dismissed_option_id, $dismissed );
361 }
362
363 do_action( 'learn-press/dismissed-notice', $id );
364
365 learn_press_send_json(
366 apply_filters(
367 'learn-press/dismissed-notice-response',
368 array(
369 'dismissed' => $id,
370 ),
371 $id
372 )
373 );
374 }*/
375
376 /**
377 * Update option to turn-off a notice.
378 *
379 * @since 3.2.6
380 *
381 * @param string $name
382 * @param string $value
383 * @param int $expired
384 * @deprecated 4.1.7.3.2
385 */
386 public function dismiss_notice_2( $name, $value, $expired = 0 ) {
387 if ( $expired ) {
388 set_transient( 'lp_dismiss_notice' . $name, $value, $expired );
389 } else {
390 $values = get_option( 'lp_dismiss_notice' );
391 if ( ! $values ) {
392 $values = array( $name => $value );
393 } else {
394 $values[ $name ] = $value;
395 }
396
397 update_option( 'lp_dismiss_notice', $values );
398 }
399 }
400
401 /**
402 * Check if a notice has dismissed.
403 *
404 * @since 3.2.6
405 *
406 * @param string $name
407 *
408 * @return bool
409 * @deprecated 4.1.7.3.2
410 */
411 public function has_dismissed_notice( $name ) {
412
413 $dismissed = get_option( $this->dismissed_option_id );
414
415 if ( ! $dismissed ) {
416 return false;
417 }
418
419 return array_search( $name, $dismissed ) !== false;
420 }
421
422 /**
423 * Restore dismissed notices by id.
424 *
425 * @since 3.2.6
426 *
427 * @param string[] $notice
428 *
429 * @return bool
430 * @deprecated 4.1.7.3.2
431 */
432 public function restore_dismissed_notice( $notice ) {
433 $dismissed = get_option( $this->dismissed_option_id );
434
435 if ( ! $dismissed ) {
436 return false;
437 }
438
439 settype( $notice, 'array' );
440
441 foreach ( $notice as $id ) {
442 $at = array_search( $id, $dismissed );
443
444 if ( false !== $at ) {
445 unset( $dismissed[ $at ] );
446 }
447 }
448
449 update_option( $this->dismissed_option_id, $dismissed );
450
451 return true;
452 }
453
454 /**
455 * Clear all notices has dismissed.
456 *
457 * @since 3.2.6
458 * @deprecated 4.1.7.3.2
459 */
460 public function clear_dismissed_notice() {
461 delete_option( $this->dismissed_option_id );
462 }
463
464 /**
465 * Remove a notice has been dismissed.
466 *
467 * @since 3.2.6
468 *
469 * @param string|array $name - Optional. NULL will remove all notices.
470 * @param bool $expired - Optional. TRUE if dismiss notice as transient (in case $name passed).
471 *
472 * @return bool
473 * @deprecated 4.1.7.3.2
474 */
475 public function remove_dismissed_notice( $name = '' ) {
476 if ( ! $name ) {
477 global $wpdb;
478
479 $query = $wpdb->prepare( "SELECT SUBSTR(option_name, 12) FROM {$wpdb->options} WHERE option_name LIKE %s", '%' . $wpdb->esc_like( '_transient_lp_dismiss_notice' ) . '%' );
480
481 $all_notices = $wpdb->get_col( $query );
482
483 if ( $all_notices ) {
484 foreach ( $all_notices as $notice ) {
485 delete_transient( $notice );
486 }
487 }
488
489 delete_option( 'lp_dismiss_notice' );
490
491 return true;
492
493 } elseif ( is_array( $name ) ) {
494 foreach ( $name as $notice ) {
495 $this->remove_dismissed_notice( $notice );
496 }
497
498 return true;
499 }
500
501 delete_transient( 'lp_dismiss_notice' . $name );
502
503 $values = get_option( 'lp_dismiss_notice' );
504
505 if ( ! $values ) {
506 return false;
507 } else {
508 if ( array_key_exists( $name, $values ) ) {
509 unset( $values[ $name ] );
510
511 update_option( 'lp_dismiss_notice', $values );
512
513 return $values;
514 }
515 }
516
517 return false;
518 }
519
520 /**
521 * @deprecated 3.2.6
522 */
523 public function dismiss_notice_deprecated() {
524 $notice = learn_press_get_request( 'lp-hide-notice' );
525
526 if ( ! $notice ) {
527 return;
528 }
529
530 $transient = learn_press_get_request( 't' );
531
532 if ( $transient ) {
533 set_transient( 'lp-hide-notice-' . $notice, 'yes', $transient );
534 } else {
535 learn_press_update_user_option( 'hide-notice-' . $notice, 'yes' );
536 }
537
538 $redirect = apply_filters( 'learn_press_hide_notice_redirect', esc_url_raw( remove_query_arg( 'lp-hide-notice' ) ) );
539 if ( $redirect ) {
540 wp_redirect( untrailingslashit( $redirect ) );
541 exit();
542 }
543 }
544
545 /**
546 * Add new notice to queue
547 *
548 * @deprecated 3.2.6
549 *
550 * @param string $message The message want to display
551 * @param string $type The class name of WP message type updated|update-nag|error
552 * @param string $id Custom id for html element's ID
553 * @param bool
554 */
555 public static function add_deprecated( $message, $type = 'success', $id = '', $redirect = false ) {
556 if ( $redirect ) {
557 $notices = get_transient( 'learn_press_redirect_notices' );
558 if ( empty( $notices ) ) {
559 $notices = array();
560 }
561 $notices[] = array(
562 'type' => $type,
563 'message' => $message,
564 'id' => $id,
565 );
566 set_transient( 'learn_press_redirect_notices', $notices );
567 } else {
568 self::$_notices[] = array(
569 'type' => $type,
570 'message' => $message,
571 'id' => $id,
572 );
573 }
574 }
575
576 /**
577 * @deprecated 4.1.7.3.2
578 */
579 public static function add_redirect_reprecated( $message, $type = 'updated', $id = '' ) {
580 self::add( $message, $type, $id, true );
581 }
582
583 /**
584 * Show all notices has registered
585 *
586 * @deprecated 3.2.6
587 */
588 public static function show_notices_deprecated() {
589 /*if ( self::$_notices ) {
590 foreach ( self::$_notices as $notice ) {
591 if ( empty( $notice ) ) {
592 continue;
593 }
594 learn_press_admin_view( 'admin-notice.php', $notice );
595 }
596 }
597
598 $notices = get_transient( 'learn_press_redirect_notices' );
599 if ( $notices ) {
600 foreach ( $notices as $notice ) {
601 if ( empty( $notice ) ) {
602 continue;
603 }
604
605 learn_press_admin_view( 'admin-notice.php', $notice );
606 }
607
608 delete_transient( 'learn_press_redirect_notices' );
609 }*/
610 }
611
612 /**
613 * Get single instance of this class
614 *
615 * @since 3.2.6
616 *
617 * @return LP_Admin_Notice
618 */
619 public static function instance() {
620 if ( ! self::$instance ) {
621 self::$instance = new self();
622 }
623
624 return self::$instance;
625 }
626 }
627
628 LP_Admin_Notice::instance();
629