| @@ -1,5 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | + exit; | |
| 4 | +} | |
| 5 | + | |
| 2 | 6 | /** |
| 3 | 7 | * Admin notices class |
| 4 | 8 | * |
| 5 | 9 | * @package Wa_Notifier |
| @@ -10,25 +14,70 @@ | ||
| 10 | 14 | |
| 11 | 15 | /** |
| 12 | 16 | * Notifier Constructor. |
| 13 | 17 | */ |
| 14 | - public function __construct($notices) { | |
| 15 | - $this->notices = $notices; | |
| 16 | - add_action('admin_notices', array( $this, 'show_notices')); | |
| 18 | + public function __construct( $notices, $transient = false, $user_id = 0 ) { | |
| 19 | + if ( $transient ) { | |
| 20 | + if(0 == $user_id){ | |
| 21 | + $user_id = get_current_user_id(); | |
| 22 | + } | |
| 23 | + set_transient( 'notifier_notice_' . $user_id, $notices, 60 ); | |
| 24 | + } else { | |
| 25 | + $this->notices = $notices; | |
| 26 | + add_action('admin_notices', array( $this, 'show_normal_notices')); | |
| 27 | + } | |
| 17 | 28 | } |
| 18 | 29 | |
| 19 | 30 | /** |
| 31 | + * Init | |
| 32 | + */ | |
| 33 | + public static function init() { | |
| 34 | + if(!is_admin()){ | |
| 35 | + return; | |
| 36 | + } | |
| 37 | + $user_id = get_current_user_id(); | |
| 38 | + $transient_notices = get_transient( 'notifier_notice_' . $user_id ); | |
| 39 | + if ( isset($transient_notices) ) { | |
| 40 | + add_action('admin_notices', array( __CLASS__, 'show_transient_notices')); | |
| 41 | + } | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Show normal notices | |
| 46 | + */ | |
| 47 | + public function show_normal_notices() { | |
| 48 | + if ( empty($this->notices) ) { | |
| 49 | + return; | |
| 50 | + } | |
| 51 | + $notices = $this->notices; | |
| 52 | + self::show_notices($notices); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Show transient notices | |
| 57 | + */ | |
| 58 | + public static function show_transient_notices() { | |
| 59 | + $user_id = get_current_user_id(); | |
| 60 | + $transient_notices = get_transient( 'notifier_notice_' . $user_id ); | |
| 61 | + if ( $transient_notices ) { | |
| 62 | + $notices = $transient_notices; | |
| 63 | + delete_transient( 'notifier_notice_' . $user_id ); | |
| 64 | + self::show_notices($notices); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 20 | 69 | * Show notices |
| 21 | 70 | */ |
| 22 | - public function show_notices() { | |
| 23 | - if (empty($this->notices)) { | |
| 71 | + public static function show_notices( $notices ) { | |
| 72 | + if ( empty($notices) ) { | |
| 24 | 73 | return; |
| 25 | 74 | } |
| 26 | 75 | |
| 27 | - foreach ($this->notices as $notice) { | |
| 76 | + foreach ( $notices as $notice ) { | |
| 28 | 77 | ?> |
| 29 | - <div class="notice notice-<?php echo esc_attr($notice['type']); ?> is-dismissible"> | |
| 30 | - <p><?php echo wp_kses_post($notice['message']); ?></p> | |
| 78 | + <div class="notifier-notice notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible"> | |
| 79 | + <p><?php echo wp_kses_post($notice['message']); ?></p> | |
| 31 | 80 | </div> |
| 32 | 81 | <?php |
| 33 | 82 | } |
| 34 | 83 | } |