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