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