← All changes
|
inc/freemius/includes/managers/class-fs-admin-notice-manager.php
+89
-23
3.5.4
→
4.3.17
View file →
| @@ -159,9 +159,12 @@ | ||
| 159 | 159 | $msg['id'], |
| 160 | 160 | false, |
| 161 | 161 | isset( $msg['wp_user_id'] ) ? $msg['wp_user_id'] : null, |
| 162 | 162 | ! empty( $msg['plugin'] ) ? $msg['plugin'] : null, |
| 163 | - $is_network_and_blog_admins | |
| 163 | + $is_network_and_blog_admins, | |
| 164 | + isset( $msg['dismissible'] ) ? | |
| 165 | + $msg['dismissible'] : | |
| 166 | + null | |
| 164 | 167 | ); |
| 165 | 168 | } |
| 166 | 169 | } |
| 167 | 170 | } |
| @@ -174,9 +177,14 @@ | ||
| 174 | 177 | * @since 1.0.7 |
| 175 | 178 | * |
| 176 | 179 | */ |
| 177 | 180 | function dismiss_notice_ajax_callback() { |
| 178 | - $this->_sticky_storage->remove( $_POST['message_id'] ); | |
| 181 | + check_admin_referer( 'fs_dismiss_notice_action' ); | |
| 182 | + | |
| 183 | + if ( ! is_numeric( $_POST['message_id'] ) ) { | |
| 184 | + $this->_sticky_storage->remove( $_POST['message_id'] ); | |
| 185 | + } | |
| 186 | + | |
| 179 | 187 | wp_die(); |
| 180 | 188 | } |
| 181 | 189 | |
| 182 | 190 | /** |
| @@ -185,10 +193,16 @@ | ||
| 185 | 193 | * @author Vova Feldman (@svovaf) |
| 186 | 194 | * @since 1.0.7 |
| 187 | 195 | */ |
| 188 | 196 | static function _add_sticky_dismiss_javascript() { |
| 197 | + $sticky_admin_notice_js_template_name = 'sticky-admin-notice-js.php'; | |
| 198 | + | |
| 199 | + if ( ! file_exists( fs_get_template_path( $sticky_admin_notice_js_template_name ) ) ) { | |
| 200 | + return; | |
| 201 | + } | |
| 202 | + | |
| 189 | 203 | $params = array(); |
| 190 | - fs_require_once_template( 'sticky-admin-notice-js.php', $params ); | |
| 204 | + fs_require_once_template( $sticky_admin_notice_js_template_name, $params ); | |
| 191 | 205 | } |
| 192 | 206 | |
| 193 | 207 | private static $_added_sticky_javascript = false; |
| 194 | 208 | |
| @@ -218,11 +232,8 @@ | ||
| 218 | 232 | // Only show messages to admins. |
| 219 | 233 | return; |
| 220 | 234 | } |
| 221 | 235 | |
| 222 | - | |
| 223 | - $show_admin_notices = ( ! $this->is_gutenberg_page() ); | |
| 224 | - | |
| 225 | 236 | foreach ( $this->_notices as $id => $msg ) { |
| 226 | 237 | if ( isset( $msg['wp_user_id'] ) && is_numeric( $msg['wp_user_id'] ) ) { |
| 227 | 238 | if ( get_current_user_id() != $msg['wp_user_id'] ) { |
| 228 | 239 | continue; |
| @@ -263,9 +274,9 @@ | ||
| 263 | 274 | */ |
| 264 | 275 | $show_notice = call_user_func_array( 'fs_apply_filter', array( |
| 265 | 276 | $this->_module_unique_affix, |
| 266 | 277 | 'show_admin_notice', |
| 267 | - $show_admin_notices, | |
| 278 | + $this->show_admin_notices(), | |
| 268 | 279 | $msg |
| 269 | 280 | ) ); |
| 270 | 281 | |
| 271 | 282 | if ( true !== $show_notice ) { |
| @@ -318,8 +329,32 @@ | ||
| 318 | 329 | return false; |
| 319 | 330 | } |
| 320 | 331 | |
| 321 | 332 | /** |
| 333 | + * Check if admin notices should be shown on page. E.g., we don't want to show notices in the Visual Editor. | |
| 334 | + * | |
| 335 | + * @author Xiaheng Chen (@xhchen) | |
| 336 | + * @since 2.4.2 | |
| 337 | + * | |
| 338 | + * @return bool | |
| 339 | + */ | |
| 340 | + function show_admin_notices() { | |
| 341 | + global $pagenow; | |
| 342 | + | |
| 343 | + if ( 'about.php' === $pagenow ) { | |
| 344 | + // Don't show admin notices on the About page. | |
| 345 | + return false; | |
| 346 | + } | |
| 347 | + | |
| 348 | + if ( $this->is_gutenberg_page() ) { | |
| 349 | + // Don't show admin notices in Gutenberg (visual editor). | |
| 350 | + return false; | |
| 351 | + } | |
| 352 | + | |
| 353 | + return true; | |
| 354 | + } | |
| 355 | + | |
| 356 | + /** | |
| 322 | 357 | * Add admin message to admin messages queue, and hook to admin_notices / all_admin_notices if not yet hooked. |
| 323 | 358 | * |
| 324 | 359 | * @author Vova Feldman (@svovaf) |
| 325 | 360 | * @since 1.0.4 |
| @@ -333,8 +368,10 @@ | ||
| 333 | 368 | * @param number|null $wp_user_id |
| 334 | 369 | * @param string|null $plugin_title |
| 335 | 370 | * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network |
| 336 | 371 | * and blog admin pages. |
| 372 | + * @param bool|null $is_dismissible | |
| 373 | + * @param array $data | |
| 337 | 374 | * |
| 338 | 375 | * @uses add_action() |
| 339 | 376 | */ |
| 340 | 377 | function add( |
| @@ -345,9 +382,11 @@ | ||
| 345 | 382 | $id = '', |
| 346 | 383 | $store_if_sticky = true, |
| 347 | 384 | $wp_user_id = null, |
| 348 | 385 | $plugin_title = null, |
| 349 | - $is_network_and_blog_admins = false | |
| 386 | + $is_network_and_blog_admins = false, | |
| 387 | + $is_dismissible = null, | |
| 388 | + $data = array() | |
| 350 | 389 | ) { |
| 351 | 390 | $notices_type = $this->get_notices_type(); |
| 352 | 391 | |
| 353 | 392 | if ( empty( $this->_notices ) ) { |
| @@ -365,16 +404,18 @@ | ||
| 365 | 404 | $id = md5( $title . ' ' . $message . ' ' . $type ); |
| 366 | 405 | } |
| 367 | 406 | |
| 368 | 407 | $message_object = array( |
| 369 | - 'message' => $message, | |
| 370 | - 'title' => $title, | |
| 371 | - 'type' => $type, | |
| 372 | - 'sticky' => $is_sticky, | |
| 373 | - 'id' => $id, | |
| 374 | - 'manager_id' => $this->_id, | |
| 375 | - 'plugin' => ( ! is_null( $plugin_title ) ? $plugin_title : $this->_title ), | |
| 376 | - 'wp_user_id' => $wp_user_id, | |
| 408 | + 'message' => $message, | |
| 409 | + 'title' => $title, | |
| 410 | + 'type' => $type, | |
| 411 | + 'sticky' => $is_sticky, | |
| 412 | + 'id' => $id, | |
| 413 | + 'manager_id' => $this->_id, | |
| 414 | + 'plugin' => ( ! is_null( $plugin_title ) ? $plugin_title : $this->_title ), | |
| 415 | + 'wp_user_id' => $wp_user_id, | |
| 416 | + 'dismissible' => $is_dismissible, | |
| 417 | + 'data' => $data | |
| 377 | 418 | ); |
| 378 | 419 | |
| 379 | 420 | if ( $is_sticky && $store_if_sticky ) { |
| 380 | 421 | $this->_sticky_storage->{$id} = $message_object; |
| @@ -387,10 +428,11 @@ | ||
| 387 | 428 | * @author Vova Feldman (@svovaf) |
| 388 | 429 | * @since 1.0.7 |
| 389 | 430 | * |
| 390 | 431 | * @param string|string[] $ids |
| 432 | + * @param bool $store | |
| 391 | 433 | */ |
| 392 | - function remove_sticky( $ids ) { | |
| 434 | + function remove_sticky( $ids, $store = true ) { | |
| 393 | 435 | if ( ! is_array( $ids ) ) { |
| 394 | 436 | $ids = array( $ids ); |
| 395 | 437 | } |
| 396 | 438 | |
| @@ -395,9 +437,9 @@ | ||
| 395 | 437 | } |
| 396 | 438 | |
| 397 | 439 | foreach ( $ids as $id ) { |
| 398 | 440 | // Remove from sticky storage. |
| 399 | - $this->_sticky_storage->remove( $id ); | |
| 441 | + $this->_sticky_storage->remove( $id, $store ); | |
| 400 | 442 | |
| 401 | 443 | if ( isset( $this->_notices[ $id ] ) ) { |
| 402 | 444 | unset( $this->_notices[ $id ] ); |
| 403 | 445 | } |
| @@ -431,26 +473,50 @@ | ||
| 431 | 473 | * @param number|null $wp_user_id |
| 432 | 474 | * @param string|null $plugin_title |
| 433 | 475 | * @param bool $is_network_and_blog_admins Whether or not the message should be shown both on network |
| 434 | 476 | * and blog admin pages. |
| 477 | + * @param bool $is_dimissible | |
| 478 | + * @param array $data | |
| 435 | 479 | */ |
| 436 | - function add_sticky( $message, $id, $title = '', $type = 'success', $wp_user_id = null, $plugin_title = null, $is_network_and_blog_admins = false ) { | |
| 480 | + function add_sticky( $message, $id, $title = '', $type = 'success', $wp_user_id = null, $plugin_title = null, $is_network_and_blog_admins = false, $is_dimissible = true, $data = array() ) { | |
| 437 | 481 | if ( ! empty( $this->_module_unique_affix ) ) { |
| 438 | 482 | $message = fs_apply_filter( $this->_module_unique_affix, "sticky_message_{$id}", $message ); |
| 439 | 483 | $title = fs_apply_filter( $this->_module_unique_affix, "sticky_title_{$id}", $title ); |
| 440 | 484 | } |
| 441 | 485 | |
| 442 | - $this->add( $message, $title, $type, true, $id, true, $wp_user_id, $plugin_title, $is_network_and_blog_admins ); | |
| 486 | + $this->add( $message, $title, $type, true, $id, true, $wp_user_id, $plugin_title, $is_network_and_blog_admins, $is_dimissible, $data ); | |
| 443 | 487 | } |
| 444 | 488 | |
| 445 | 489 | /** |
| 490 | + * Retrieves the data of an sticky notice. | |
| 491 | + * | |
| 492 | + * @author Leo Fajardo (@leorw) | |
| 493 | + * @since 2.4.3 | |
| 494 | + * | |
| 495 | + * @param string $id Message ID. | |
| 496 | + * | |
| 497 | + * @return array|null | |
| 498 | + */ | |
| 499 | + function get_sticky( $id ) { | |
| 500 | + return isset( $this->_sticky_storage->{$id} ) ? | |
| 501 | + $this->_sticky_storage->{$id} : | |
| 502 | + null; | |
| 503 | + } | |
| 504 | + | |
| 505 | + /** | |
| 446 | 506 | * Clear all sticky messages. |
| 447 | 507 | * |
| 448 | 508 | * @author Vova Feldman (@svovaf) |
| 449 | 509 | * @since 1.0.8 |
| 510 | + * | |
| 511 | + * @param bool $is_temporary @since 2.5.1 | |
| 450 | 512 | */ |
| 451 | - function clear_all_sticky() { | |
| 452 | - $this->_sticky_storage->clear_all(); | |
| 513 | + function clear_all_sticky( $is_temporary = false ) { | |
| 514 | + if ( $is_temporary ) { | |
| 515 | + $this->_notices = array(); | |
| 516 | + } else { | |
| 517 | + $this->_sticky_storage->clear_all(); | |
| 518 | + } | |
| 453 | 519 | } |
| 454 | 520 | |
| 455 | 521 | #-------------------------------------------------------------------------------- |
| 456 | 522 | #region Helper Method |
| @@ -468,5 +534,5 @@ | ||
| 468 | 534 | 'admin_notices'; |
| 469 | 535 | } |
| 470 | 536 | |
| 471 | 537 | #endregion |
| 472 | - } | |
| 538 | + } | |