← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Notification.php
+46
-10
3.1.3
→
3.9.2
View file →
| @@ -83,9 +83,9 @@ | ||
| 83 | 83 | } |
| 84 | 84 | $notification_html = self::get_notification_html( $notification_details ); |
| 85 | 85 | do_action( $notification_details['id'] . '_before_render' ); |
| 86 | 86 | |
| 87 | - echo $notification_html; | |
| 87 | + echo $notification_html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, already escaped internally. | |
| 88 | 88 | |
| 89 | 89 | do_action( $notification_details['id'] . '_after_render' ); |
| 90 | 90 | self::render_snippets(); |
| 91 | 91 | } |
| @@ -225,9 +225,9 @@ | ||
| 225 | 225 | |
| 226 | 226 | /** |
| 227 | 227 | * Get last notification details. |
| 228 | 228 | * |
| 229 | - * @return array Last notification details. | |
| 229 | + * @return int Last notification details. | |
| 230 | 230 | */ |
| 231 | 231 | private static function get_last_active_notification_timestamp() { |
| 232 | 232 | $notification = self::get_notifications_metadata(); |
| 233 | 233 | |
| @@ -268,11 +268,12 @@ | ||
| 268 | 268 | ], |
| 269 | 269 | ], |
| 270 | 270 | ]; |
| 271 | 271 | $notification_details = wp_parse_args( $notification_details, $default ); |
| 272 | + global $pagenow; | |
| 273 | + $notification_details['ctas']['cancel']['link'] = wp_nonce_url( add_query_arg( [ 'nid' => $notification_details['id'] ], admin_url( $pagenow ) ), $notification_details['id'], 'tsdk_dismiss_nonce' ); | |
| 274 | + $notification_html = '<div class="notice notice-success is-dismissible themeisle-sdk-notice" data-notification-id="' . esc_attr( $notification_details['id'] ) . '" id="' . esc_attr( $notification_details['id'] ) . '-notification"> <div class="themeisle-sdk-notification-box">'; | |
| 272 | 275 | |
| 273 | - $notification_html = '<div class="notice notice-success is-dismissible themeisle-sdk-notice" data-notification-id="' . esc_attr( $notification_details['id'] ) . '" id="' . esc_attr( $notification_details['id'] ) . '-notification"> <div class="themeisle-sdk-notification-box">'; | |
| 274 | - | |
| 275 | 276 | if ( ! empty( $notification_details['heading'] ) ) { |
| 276 | 277 | $notification_html .= sprintf( '<h4>%s</h4>', wp_kses_post( $notification_details['heading'] ) ); |
| 277 | 278 | } |
| 278 | 279 | if ( ! empty( $notification_details['message'] ) ) { |
| @@ -339,14 +340,16 @@ | ||
| 339 | 340 | } |
| 340 | 341 | $.post( |
| 341 | 342 | ajaxurl, |
| 342 | 343 | { |
| 343 | - 'nonce': '<?php echo wp_create_nonce( (string) __CLASS__ ); ?>', | |
| 344 | + 'nonce': '<?php echo esc_attr( wp_create_nonce( (string) __CLASS__ ) ); ?>', | |
| 344 | 345 | 'action': 'themeisle_sdk_dismiss_notice', |
| 345 | 346 | 'id': notification_id, |
| 346 | - 'confirm': confirm | |
| 347 | - } | |
| 348 | - ); | |
| 347 | + 'confirm': confirm, | |
| 348 | + }, | |
| 349 | + ).fail(function() { | |
| 350 | + location.href = encodeURI(link.attr('href')); | |
| 351 | + }); | |
| 349 | 352 | if (confirm === 'yes') { |
| 350 | 353 | $(this).trigger('themeisle-sdk:confirmed'); |
| 351 | 354 | } else { |
| 352 | 355 | $(this).trigger('themeisle-sdk:canceled'); |
| @@ -351,9 +354,9 @@ | ||
| 351 | 354 | } else { |
| 352 | 355 | $(this).trigger('themeisle-sdk:canceled'); |
| 353 | 356 | } |
| 354 | 357 | container.hide(); |
| 355 | - if (link.attr('href') === '#') { | |
| 358 | + if (confirm === 'no' || link.attr('href') === '#') { | |
| 356 | 359 | return false; |
| 357 | 360 | } |
| 358 | 361 | }); |
| 359 | 362 | }); |
| @@ -364,9 +367,9 @@ | ||
| 364 | 367 | |
| 365 | 368 | /** |
| 366 | 369 | * Dismiss the notification. |
| 367 | 370 | */ |
| 368 | - static function dismiss() { | |
| 371 | + public static function dismiss() { | |
| 369 | 372 | check_ajax_referer( (string) __CLASS__, 'nonce' ); |
| 370 | 373 | |
| 371 | 374 | $id = isset( $_POST['id'] ) ? sanitize_text_field( $_POST['id'] ) : ''; |
| 372 | 375 | $confirm = isset( $_POST['confirm'] ) ? sanitize_text_field( $_POST['confirm'] ) : 'no'; |
| @@ -373,13 +376,42 @@ | ||
| 373 | 376 | |
| 374 | 377 | if ( empty( $id ) ) { |
| 375 | 378 | wp_send_json( [] ); |
| 376 | 379 | } |
| 380 | + $ids = wp_list_pluck( self::$notifications, 'id' ); | |
| 381 | + if ( ! in_array( $id, $ids, true ) ) { | |
| 382 | + wp_send_json( [] ); | |
| 383 | + } | |
| 377 | 384 | self::set_last_active_notification_timestamp(); |
| 378 | 385 | update_option( $id, $confirm ); |
| 379 | 386 | do_action( $id . '_process_confirm', $confirm ); |
| 380 | 387 | wp_send_json( [] ); |
| 381 | 388 | } |
| 389 | + /** | |
| 390 | + * Dismiss the notification. | |
| 391 | + */ | |
| 392 | + public static function dismiss_get() { | |
| 393 | + $is_nonce_dismiss = sanitize_text_field( isset( $_GET['tsdk_dismiss_nonce'] ) ? $_GET['tsdk_dismiss_nonce'] : '' ); | |
| 394 | + if ( strlen( $is_nonce_dismiss ) < 5 ) { | |
| 395 | + return; | |
| 396 | + } | |
| 397 | + $id = sanitize_text_field( isset( $_GET['nid'] ) ? $_GET['nid'] : '' ); | |
| 398 | + if ( empty( $id ) ) { | |
| 399 | + return; | |
| 400 | + } | |
| 401 | + $nonce = wp_verify_nonce( sanitize_text_field( $_GET['tsdk_dismiss_nonce'] ), $id ); | |
| 402 | + if ( $nonce !== 1 ) { | |
| 403 | + return; | |
| 404 | + } | |
| 405 | + $ids = wp_list_pluck( self::$notifications, 'id' ); | |
| 406 | + if ( ! in_array( $id, $ids, true ) ) { | |
| 407 | + return; | |
| 408 | + } | |
| 409 | + $confirm = 'no'; | |
| 410 | + self::set_last_active_notification_timestamp(); | |
| 411 | + update_option( $id, $confirm ); | |
| 412 | + do_action( $id . '_process_confirm', $confirm ); | |
| 413 | + } | |
| 382 | 414 | |
| 383 | 415 | /** |
| 384 | 416 | * Check if we should load the notification module. |
| 385 | 417 | * |
| @@ -429,8 +461,11 @@ | ||
| 429 | 461 | * |
| 430 | 462 | * @return Notification Module instance. |
| 431 | 463 | */ |
| 432 | 464 | public function load( $product ) { |
| 465 | + if ( apply_filters( 'themeisle_sdk_hide_notifications', false ) ) { | |
| 466 | + return; | |
| 467 | + } | |
| 433 | 468 | $this->product = $product; |
| 434 | 469 | |
| 435 | 470 | $notifications = apply_filters( 'themeisle_sdk_registered_notifications', [] ); |
| 436 | 471 | $notifications = array_filter( |
| @@ -448,8 +483,9 @@ | ||
| 448 | 483 | ); |
| 449 | 484 | self::$notifications = $notifications; |
| 450 | 485 | add_action( 'admin_notices', array( __CLASS__, 'show_notification' ) ); |
| 451 | 486 | add_action( 'wp_ajax_themeisle_sdk_dismiss_notice', array( __CLASS__, 'dismiss' ) ); |
| 487 | + add_action( 'admin_head', array( __CLASS__, 'dismiss_get' ) ); | |
| 452 | 488 | add_action( 'admin_head', array( __CLASS__, 'setup_notifications' ) ); |
| 453 | 489 | |
| 454 | 490 | return $this; |
| 455 | 491 | } |