| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that adds a misc notice |
| 4 |
* |
| 5 |
* @since v.2.0 |
| 6 |
* |
| 7 |
* @return void |
| 8 |
*/ |
| 9 |
class WPPB_Add_General_Notices{ |
| 10 |
public $notificationId = ''; |
| 11 |
public $notificationMessage = ''; |
| 12 |
public $notificationClass = ''; |
| 13 |
public $startDate = ''; |
| 14 |
public $endDate = ''; |
| 15 |
|
| 16 |
function __construct( $notificationId, $notificationMessage, $notificationClass = 'updated' , $startDate = '', $endDate = '' ){ |
| 17 |
$this->notificationId = $notificationId; |
| 18 |
$this->notificationMessage = $notificationMessage; |
| 19 |
$this->notificationClass = $notificationClass; |
| 20 |
|
| 21 |
if( !empty( $startDate ) && time() < strtotime( $startDate ) ) |
| 22 |
return; |
| 23 |
|
| 24 |
if( !empty( $endDate ) && time() > strtotime( $endDate ) ) |
| 25 |
return; |
| 26 |
|
| 27 |
add_action( 'admin_notices', array( $this, 'add_admin_notice' ) ); |
| 28 |
add_action( 'admin_init', array( $this, 'dismiss_notification' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
// Display a notice that can be dismissed in case the serial number is inactive |
| 33 |
function add_admin_notice() { |
| 34 |
global $current_user ; |
| 35 |
global $pagenow; |
| 36 |
|
| 37 |
$user_id = $current_user->ID; |
| 38 |
do_action( $this->notificationId.'_before_notification_displayed', $current_user, $pagenow ); |
| 39 |
|
| 40 |
if ( current_user_can( 'manage_options' ) ){ |
| 41 |
// Check that the user hasn't already clicked to ignore the message |
| 42 |
if ( ! get_user_meta($user_id, $this->notificationId.'_dismiss_notification' ) ) { |
| 43 |
echo $finalMessage = apply_filters($this->notificationId.'_notification_message','<div class="'. $this->notificationClass .'" >'.$this->notificationMessage.'</div>', $this->notificationMessage); |
| 44 |
} |
| 45 |
do_action( $this->notificationId.'_notification_displayed', $current_user, $pagenow ); |
| 46 |
} |
| 47 |
do_action( $this->notificationId.'_after_notification_displayed', $current_user, $pagenow ); |
| 48 |
} |
| 49 |
|
| 50 |
function dismiss_notification() { |
| 51 |
global $current_user; |
| 52 |
|
| 53 |
$user_id = $current_user->ID; |
| 54 |
|
| 55 |
do_action( $this->notificationId.'_before_notification_dismissed', $current_user ); |
| 56 |
|
| 57 |
// If user clicks to ignore the notice, add that to their user meta |
| 58 |
if ( isset( $_GET[$this->notificationId.'_dismiss_notification']) && '0' == $_GET[$this->notificationId.'_dismiss_notification'] ) |
| 59 |
add_user_meta( $user_id, $this->notificationId.'_dismiss_notification', 'true', true ); |
| 60 |
|
| 61 |
do_action( $this->notificationId.'_after_notification_dismissed', $current_user ); |
| 62 |
} |
| 63 |
} |
| 64 |
?> |