| 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 wp_kses_post( 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 |
|
| 65 |
Class WPPB_Plugin_Notifications { |
| 66 |
|
| 67 |
public $notifications = array(); |
| 68 |
private static $_instance = null; |
| 69 |
private $prefix = 'wppb'; |
| 70 |
private $menu_slug = 'profile-builder'; |
| 71 |
public $pluginPages = array( 'profile-builder-', 'manage-fields', 'wppb-', 'admin-email-customizer', 'user-email-customizer' ); |
| 72 |
|
| 73 |
protected function __construct() { |
| 74 |
add_action( 'admin_init', array( $this, 'dismiss_admin_notifications' ), 200 ); |
| 75 |
add_action( 'admin_init', array( $this, 'add_admin_menu_notification_counts' ), 1000 ); |
| 76 |
add_action( 'admin_init', array( $this, 'remove_other_plugin_notices' ), 1001 ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
function dismiss_admin_notifications() { |
| 81 |
if( ! empty( $_GET[$this->prefix.'_dismiss_admin_notification'] ) ) { |
| 82 |
$notifications = self::get_instance(); |
| 83 |
$notifications->dismiss_notification( sanitize_text_field( $_GET[$this->prefix.'_dismiss_admin_notification'] ) ); |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
function add_admin_menu_notification_counts() { |
| 89 |
|
| 90 |
global $menu, $submenu; |
| 91 |
|
| 92 |
$notifications = WPPB_Plugin_Notifications::get_instance(); |
| 93 |
|
| 94 |
if( ! empty( $menu ) ) { |
| 95 |
foreach( $menu as $menu_position => $menu_data ) { |
| 96 |
if( ! empty( $menu_data[2] ) && $menu_data[2] == $this->menu_slug ) { |
| 97 |
$menu_count = $notifications->get_count_in_menu(); |
| 98 |
if( ! empty( $menu_count ) ) |
| 99 |
$menu[$menu_position][0] .= '<span class="update-plugins '.$this->prefix.'-update-plugins"><span class="plugin-count">' . $menu_count . '</span></span>'; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if( ! empty( $submenu[$this->menu_slug] ) ) { |
| 105 |
foreach( $submenu[$this->menu_slug] as $menu_position => $menu_data ) { |
| 106 |
$menu_count = $notifications->get_count_in_submenu( $menu_data[2] ); |
| 107 |
|
| 108 |
if( ! empty( $menu_count ) ) |
| 109 |
$submenu[$this->menu_slug][$menu_position][0] .= '<span class="update-plugins '.$this->prefix.'-update-plugins"><span class="plugin-count">' . $menu_count . '</span></span>'; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/* handle other plugin notifications on our plugin pages */ |
| 115 |
function remove_other_plugin_notices(){ |
| 116 |
|
| 117 |
//remove all notifications from start page |
| 118 |
if( isset( $_GET['page'] ) && ( $_GET['page'] == 'profile-builder-basic-info' || ( $_GET['page'] == 'profile-builder-add-ons' && !isset( $_GET['cl_add_ons_listing_success'] ) ) ) ) {//on addons page we use notices to display success mesage so we can't remove it in that case |
| 119 |
remove_all_actions('admin_notices'); |
| 120 |
} |
| 121 |
|
| 122 |
/* remove all other plugin notifications except our own from the rest of the PB pages */ |
| 123 |
if( $this->is_plugin_page() ) { |
| 124 |
global $wp_filter; |
| 125 |
if (!empty($wp_filter['admin_notices'])) { |
| 126 |
if (!empty($wp_filter['admin_notices']->callbacks)) { |
| 127 |
foreach ($wp_filter['admin_notices']->callbacks as $priority => $callbacks_level) { |
| 128 |
if (!empty($callbacks_level)) { |
| 129 |
foreach ($callbacks_level as $key => $callback) { |
| 130 |
if( is_array( $callback['function'] ) ){ |
| 131 |
if( is_object($callback['function'][0])) {//object here |
| 132 |
if (strpos(get_class($callback['function'][0]), 'PMS_') !== 0 && strpos(get_class($callback['function'][0]), 'WPPB_') !== 0 && strpos(get_class($callback['function'][0]), 'TRP_') !== 0 && strpos(get_class($callback['function'][0]), 'WCK_') !== 0) { |
| 133 |
unset($wp_filter['admin_notices']->callbacks[$priority][$key]);//unset everything that doesn't come from our plugins |
| 134 |
} |
| 135 |
} |
| 136 |
} else if( is_string( $callback['function'] ) ){//it should be a function name |
| 137 |
if (strpos($callback['function'], 'pms_') !== 0 && strpos($callback['function'], 'wppb_') !== 0 && strpos($callback['function'], 'trp_') !== 0 && strpos($callback['function'], 'wck_') !== 0) { |
| 138 |
unset($wp_filter['admin_notices']->callbacks[$priority][$key]);//unset everything that doesn't come from our plugins |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* |
| 152 |
* |
| 153 |
*/ |
| 154 |
public static function get_instance() { |
| 155 |
if( is_null( self::$_instance ) ) |
| 156 |
self::$_instance = new WPPB_Plugin_Notifications(); |
| 157 |
|
| 158 |
return self::$_instance; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* |
| 164 |
* |
| 165 |
*/ |
| 166 |
public function add_notification( $notification_id = '', $notification_message = '', $notification_class = 'update-nag', $count_in_menu = true, $count_in_submenu = array() ) { |
| 167 |
|
| 168 |
if( empty( $notification_id ) ) |
| 169 |
return; |
| 170 |
|
| 171 |
if( empty( $notification_message ) ) |
| 172 |
return; |
| 173 |
|
| 174 |
global $current_user; |
| 175 |
|
| 176 |
if( get_user_meta( $current_user->ID, $notification_id . '_dismiss_notification' ) ) |
| 177 |
return; |
| 178 |
|
| 179 |
$this->notifications[$notification_id] = array( |
| 180 |
'id' => $notification_id, |
| 181 |
'message' => $notification_message, |
| 182 |
'class' => $notification_class, |
| 183 |
'count_in_menu' => $count_in_menu, |
| 184 |
'count_in_submenu' => $count_in_submenu |
| 185 |
); |
| 186 |
|
| 187 |
|
| 188 |
if( $this->is_plugin_page() ) { |
| 189 |
new WPPB_Add_General_Notices( $notification_id, $notification_message, $notification_class ); |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* |
| 197 |
* |
| 198 |
*/ |
| 199 |
public function get_notifications() { |
| 200 |
return $this->notifications; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* |
| 206 |
* |
| 207 |
*/ |
| 208 |
public function get_notification( $notification_id = '' ) { |
| 209 |
|
| 210 |
if( empty( $notification_id ) ) |
| 211 |
return null; |
| 212 |
|
| 213 |
$notifications = $this->get_notifications(); |
| 214 |
|
| 215 |
if( ! empty( $notifications[$notification_id] ) ) |
| 216 |
return $notifications[$notification_id]; |
| 217 |
else |
| 218 |
return null; |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/** |
| 224 |
* |
| 225 |
* |
| 226 |
*/ |
| 227 |
public function dismiss_notification( $notification_id = '' ) { |
| 228 |
global $current_user; |
| 229 |
add_user_meta( $current_user->ID, $notification_id . '_dismiss_notification', 'true', true ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/** |
| 234 |
* |
| 235 |
* |
| 236 |
*/ |
| 237 |
public function get_count_in_menu() { |
| 238 |
$count = 0; |
| 239 |
|
| 240 |
foreach( $this->notifications as $notification ) { |
| 241 |
if( ! empty( $notification['count_in_menu'] ) ) |
| 242 |
$count++; |
| 243 |
} |
| 244 |
|
| 245 |
return $count; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* |
| 251 |
* |
| 252 |
*/ |
| 253 |
public function get_count_in_submenu( $submenu = '' ) { |
| 254 |
|
| 255 |
if( empty( $submenu ) ) |
| 256 |
return 0; |
| 257 |
|
| 258 |
$count = 0; |
| 259 |
|
| 260 |
foreach( $this->notifications as $notification ) { |
| 261 |
if( empty( $notification['count_in_submenu'] ) ) |
| 262 |
continue; |
| 263 |
|
| 264 |
if( ! is_array( $notification['count_in_submenu'] ) ) |
| 265 |
continue; |
| 266 |
|
| 267 |
if( ! in_array( $submenu, $notification['count_in_submenu'] ) ) |
| 268 |
continue; |
| 269 |
|
| 270 |
$count++; |
| 271 |
} |
| 272 |
|
| 273 |
return $count; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* |
| 280 |
* |
| 281 |
*/ |
| 282 |
public function is_plugin_page() { |
| 283 |
if( !empty( $this->pluginPages ) ){ |
| 284 |
foreach ( $this->pluginPages as $pluginPage ){ |
| 285 |
if( ! empty( $_GET['page'] ) && false !== strpos( sanitize_text_field( $_GET['page'] ), $pluginPage ) ) |
| 286 |
return true; |
| 287 |
|
| 288 |
if( ! empty( $_GET['post_type'] ) && false !== strpos( sanitize_text_field( $_GET['post_type'] ), $pluginPage ) ) |
| 289 |
return true; |
| 290 |
|
| 291 |
if( ! empty( $_GET['post'] ) && false !== strpos( get_post_type( (int)$_GET['post'] ), $pluginPage ) ) |
| 292 |
return true; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
} |