| @@ -21,13 +21,14 @@ | ||
| 21 | 21 | */ |
| 22 | 22 | class Give_Notices { |
| 23 | 23 | /** |
| 24 | 24 | * List of notices |
| 25 | + * | |
| 25 | 26 | * @var array |
| 26 | 27 | * @since 1.8.9 |
| 27 | 28 | * @access private |
| 28 | 29 | */ |
| 29 | - private static $notices = array(); | |
| 30 | + private static $notices = []; | |
| 30 | 31 | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * Flag to check if any notice auto dismissible among all notices |
| @@ -52,15 +53,15 @@ | ||
| 52 | 53 | * |
| 53 | 54 | * @since 1.8.9 |
| 54 | 55 | */ |
| 55 | 56 | public function __construct() { |
| 56 | - add_action( 'admin_notices', array( $this, 'render_admin_notices' ), 999 ); | |
| 57 | - add_action( 'admin_footer', array( $this, '__reveal_notices' ) ); | |
| 58 | - add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) ); | |
| 57 | + add_action( 'admin_notices', [ $this, 'render_admin_notices' ], 999 ); | |
| 58 | + add_action( 'admin_footer', [$this, 'reveal_notices'] ); | |
| 59 | + add_action( 'give_dismiss_notices', [ $this, 'dismiss_notices' ] ); | |
| 59 | 60 | |
| 60 | - add_action( 'give_frontend_notices', array( $this, 'render_frontend_notices' ), 999 ); | |
| 61 | - add_action( 'give_pre_form_output', array( $this, 'render_frontend_form_notices' ), 10, 1 ); | |
| 62 | - add_action( 'give_ajax_donation_errors', array( $this, 'render_frontend_notices' ) ); | |
| 61 | + add_action( 'give_frontend_notices', [ $this, 'render_frontend_notices' ], 999 ); | |
| 62 | + add_action( 'give_pre_form_output', [ $this, 'render_frontend_form_notices' ], 10, 1 ); | |
| 63 | + add_action( 'give_ajax_donation_errors', [ $this, 'render_frontend_notices' ] ); | |
| 63 | 64 | |
| 64 | 65 | /** |
| 65 | 66 | * Backward compatibility for deprecated params. |
| 66 | 67 | * |
| @@ -65,11 +66,11 @@ | ||
| 65 | 66 | * Backward compatibility for deprecated params. |
| 66 | 67 | * |
| 67 | 68 | * @since 1.8.14 |
| 68 | 69 | */ |
| 69 | - add_filter( 'give_register_notice_args', array( $this, 'bc_deprecated_params' ) ); | |
| 70 | - add_filter( 'give_frontend_errors_args', array( $this, 'bc_deprecated_params' ) ); | |
| 71 | - add_filter( 'give_frontend_notice_args', array( $this, 'bc_deprecated_params' ) ); | |
| 70 | + add_filter( 'give_register_notice_args', [ $this, 'bc_deprecated_params' ] ); | |
| 71 | + add_filter( 'give_frontend_errors_args', [ $this, 'bc_deprecated_params' ] ); | |
| 72 | + add_filter( 'give_frontend_notice_args', [ $this, 'bc_deprecated_params' ] ); | |
| 72 | 73 | } |
| 73 | 74 | |
| 74 | 75 | /** |
| 75 | 76 | * Add backward compatibility to deprecated params. |
| @@ -116,17 +117,17 @@ | ||
| 116 | 117 | } |
| 117 | 118 | |
| 118 | 119 | $notice_args = wp_parse_args( |
| 119 | 120 | $notice_args, |
| 120 | - array( | |
| 121 | + [ | |
| 121 | 122 | 'id' => '', |
| 122 | 123 | 'description' => '', |
| 123 | 124 | |
| 124 | 125 | /* |
| 125 | 126 | * Add custom notice html |
| 126 | - * Note: This param has more priority then description, so if you have both param then this one will be use | |
| 127 | - * for generating notice html. Most of feature of notice attach to core generated html, so if you set | |
| 128 | - * custom html then please add required classes and data attribute which help to apply feature on notice. | |
| 127 | + * Note: This param has more priority than description, so if you use both params then this one will be used | |
| 128 | + * for generating notice html. Most features of the notice attach to core generated html, so if you set | |
| 129 | + * custom html then please add required classes and data attribute which help to apply features on notice. | |
| 129 | 130 | * |
| 130 | 131 | * @since 1.8.16 |
| 131 | 132 | */ |
| 132 | 133 | 'description_html' => '', |
| @@ -150,10 +151,9 @@ | ||
| 150 | 151 | |
| 151 | 152 | // Only set it when custom is defined. |
| 152 | 153 | 'dismiss_interval_time' => null, |
| 153 | 154 | |
| 154 | - | |
| 155 | - ) | |
| 155 | + ] | |
| 156 | 156 | ); |
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | 159 | * Filter to modify Notice args before it get add |
| @@ -164,19 +164,23 @@ | ||
| 164 | 164 | |
| 165 | 165 | // Set extra dismiss links if any. |
| 166 | 166 | if ( false !== strpos( $notice_args['description'], 'data-dismiss-interval' ) ) { |
| 167 | 167 | |
| 168 | - preg_match_all( "/data-([^\"]*)=\"([^\"]*)\"/", $notice_args['description'], $extra_notice_dismiss_link ); | |
| 168 | + preg_match_all( '/data-([^"]*)="([^"]*)"/', $notice_args['description'], $extra_notice_dismiss_link ); | |
| 169 | 169 | |
| 170 | 170 | if ( ! empty( $extra_notice_dismiss_link ) ) { |
| 171 | 171 | $extra_notice_dismiss_links = array_chunk( current( $extra_notice_dismiss_link ), 3 ); |
| 172 | 172 | foreach ( $extra_notice_dismiss_links as $extra_notice_dismiss_link ) { |
| 173 | - // Create array og key ==> value by parsing query string created after renaming data attributes. | |
| 174 | - $data_attribute_query_str = str_replace( array( 'data-', '-', '"' ), array( | |
| 175 | - '', | |
| 176 | - '_', | |
| 177 | - '', | |
| 178 | - ), implode( '&', $extra_notice_dismiss_link ) ); | |
| 173 | + // Create array of key ==> value by parsing query string created after renaming data attributes. | |
| 174 | + $data_attribute_query_str = str_replace( | |
| 175 | + [ 'data-', '-', '"' ], | |
| 176 | + [ | |
| 177 | + '', | |
| 178 | + '_', | |
| 179 | + '', | |
| 180 | + ], | |
| 181 | + implode( '&', $extra_notice_dismiss_link ) | |
| 182 | + ); | |
| 179 | 183 | |
| 180 | 184 | $notice_args['extra_links'][] = wp_parse_args( $data_attribute_query_str ); |
| 181 | 185 | } |
| 182 | 186 | } |
| @@ -181,9 +185,8 @@ | ||
| 181 | 185 | } |
| 182 | 186 | } |
| 183 | 187 | } |
| 184 | 188 | |
| 185 | - | |
| 186 | 189 | self::$notices[ $notice_args['id'] ] = $notice_args; |
| 187 | 190 | |
| 188 | 191 | // Auto set show param if not already set. |
| 189 | 192 | if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) { |
| @@ -201,9 +204,8 @@ | ||
| 201 | 204 | /** |
| 202 | 205 | * Display notice. |
| 203 | 206 | * |
| 204 | 207 | * @since 1.8.9 |
| 205 | - * | |
| 206 | 208 | */ |
| 207 | 209 | public function render_admin_notices() { |
| 208 | 210 | /* @var WP_Screen $wp_screen */ |
| 209 | 211 | $wp_screen = get_current_screen(); |
| @@ -212,9 +214,9 @@ | ||
| 212 | 214 | if ( empty( self::$notices ) ) { |
| 213 | 215 | return; |
| 214 | 216 | } |
| 215 | 217 | |
| 216 | - // Do not render notice on Gutenberg editor page. | |
| 218 | + // Do not render notices on Gutenberg editor page. | |
| 217 | 219 | if ( |
| 218 | 220 | method_exists( $wp_screen, 'is_block_editor' ) |
| 219 | 221 | && $wp_screen->is_block_editor() |
| 220 | 222 | ) { |
| @@ -220,8 +222,18 @@ | ||
| 220 | 222 | ) { |
| 221 | 223 | return; |
| 222 | 224 | } |
| 223 | 225 | |
| 226 | + // Do not render notices on these pages as well. | |
| 227 | + // We don't want to annoy admins with notices on important screens like WP or GiveWP updates, etc. | |
| 228 | + if ( | |
| 229 | + 'update-core' === $wp_screen->id | |
| 230 | + || 'give_forms_page_give-addons' === $wp_screen->id | |
| 231 | + || 'give_forms_page_give-updates' === $wp_screen->id | |
| 232 | + ) { | |
| 233 | + return; | |
| 234 | + } | |
| 235 | + | |
| 224 | 236 | $output = ''; |
| 225 | 237 | |
| 226 | 238 | foreach ( self::$notices as $notice_id => $notice ) { |
| 227 | 239 | // Check flag set to true to show notice. |
| @@ -228,11 +240,10 @@ | ||
| 228 | 240 | if ( ! $notice['show'] ) { |
| 229 | 241 | continue; |
| 230 | 242 | } |
| 231 | 243 | |
| 232 | - | |
| 233 | 244 | // Render custom html. |
| 234 | - if( ! empty( $notice['description_html'] ) ) { | |
| 245 | + if ( ! empty( $notice['description_html'] ) ) { | |
| 235 | 246 | $output .= "{$notice['description_html']} \n"; |
| 236 | 247 | continue; |
| 237 | 248 | } |
| 238 | 249 | |
| @@ -248,9 +259,9 @@ | ||
| 248 | 259 | |
| 249 | 260 | $css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] ); |
| 250 | 261 | |
| 251 | 262 | $css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}"; |
| 252 | - $output .= sprintf( | |
| 263 | + $output .= sprintf( | |
| 253 | 264 | '<div id="%1$s" class="%2$s" data-dismissible="%3$s" data-dismissible-type="%4$s" data-dismiss-interval="%5$s" data-notice-id="%6$s" data-security="%7$s" data-dismiss-interval-time="%8$s" style="display: none">' . " \n", |
| 254 | 265 | $css_id, |
| 255 | 266 | $css_class, |
| 256 | 267 | give_clean( $notice['dismissible'] ), |
| @@ -270,33 +281,35 @@ | ||
| 270 | 281 | $this->print_js(); |
| 271 | 282 | } |
| 272 | 283 | |
| 273 | 284 | |
| 274 | - /** | |
| 275 | - * Render give frontend notices. | |
| 276 | - * | |
| 277 | - * @since 1.8.9 | |
| 278 | - * @access public | |
| 279 | - * | |
| 280 | - * @param int $form_id | |
| 281 | - */ | |
| 282 | - public function render_frontend_notices( $form_id = 0 ) { | |
| 283 | - $errors = give_get_errors(); | |
| 285 | + /** | |
| 286 | + * Render give frontend notices. | |
| 287 | + * | |
| 288 | + * @since 3.1.0 Render errors on Ajax request (Donation form validation - v2 forms) | |
| 289 | + * @since 2.32.0 Display registered error on donation form. | |
| 290 | + * @since 1.8.9 | |
| 291 | + * @access public | |
| 292 | + * | |
| 293 | + * @param int $form_id | |
| 294 | + */ | |
| 295 | + public function render_frontend_notices($form_id = 0) | |
| 296 | + { | |
| 297 | + $errors = give_get_errors(); | |
| 284 | 298 | |
| 285 | - $request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0; | |
| 299 | + $request_form_id = isset($_REQUEST['form-id']) ? absint($_REQUEST['form-id']) : 0; | |
| 286 | 300 | |
| 287 | - // Sanity checks first: Ensure that gateway returned errors display on the appropriate form. | |
| 288 | - if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) { | |
| 289 | - return; | |
| 290 | - } | |
| 301 | + // Sanity checks first: | |
| 302 | + // - Ensure that gateway returned errors display on the appropriate form. | |
| 303 | + // - Error should exist. | |
| 304 | + if (! $errors || ($request_form_id && $request_form_id !== $form_id)) { | |
| 305 | + return; | |
| 306 | + } | |
| 291 | 307 | |
| 292 | - if ( $errors ) { | |
| 293 | - self::print_frontend_errors( $errors ); | |
| 308 | + self::print_frontend_errors($errors); | |
| 309 | + give_clear_errors(); | |
| 310 | + } | |
| 294 | 311 | |
| 295 | - give_clear_errors(); | |
| 296 | - } | |
| 297 | - } | |
| 298 | - | |
| 299 | 312 | /** |
| 300 | 313 | * Renders notices for different actions depending on |
| 301 | 314 | * the type of form display option. |
| 302 | 315 | * |
| @@ -310,11 +323,11 @@ | ||
| 310 | 323 | public function render_frontend_form_notices( $form_id ) { |
| 311 | 324 | $display_option = give_get_meta( $form_id, '_give_payment_display', true ); |
| 312 | 325 | |
| 313 | 326 | if ( 'modal' === $display_option ) { |
| 314 | - add_action( 'give_payment_mode_top', array( $this, 'render_frontend_notices' ) ); | |
| 327 | + add_action( 'give_payment_mode_top', [ $this, 'render_frontend_notices' ] ); | |
| 315 | 328 | } else { |
| 316 | - add_action( 'give_pre_form', array( $this, 'render_frontend_notices' ), 11 ); | |
| 329 | + add_action( 'give_pre_form', [ $this, 'render_frontend_notices' ], 11 ); | |
| 317 | 330 | } |
| 318 | 331 | } |
| 319 | 332 | |
| 320 | 333 | /** |
| @@ -401,25 +414,20 @@ | ||
| 401 | 414 | /** |
| 402 | 415 | * Show notices |
| 403 | 416 | * Note: only for internal use |
| 404 | 417 | * |
| 418 | + * @since 4.9.0 rename function - PHP 8 compatibility | |
| 405 | 419 | * @since 2.3.0 |
| 406 | 420 | */ |
| 407 | - public function __reveal_notices(){ | |
| 421 | + public function reveal_notices() { | |
| 408 | 422 | ?> |
| 409 | 423 | <script> |
| 410 | 424 | jQuery(document).ready(function($){ |
| 411 | 425 | // Fix notice appearance issue. |
| 412 | - window.setTimeout( | |
| 413 | - function(){ | |
| 414 | - var give_notices = $('.give-notice'); | |
| 415 | - | |
| 416 | - if( give_notices.length ) { | |
| 417 | - give_notices.slideDown(); | |
| 418 | - } | |
| 419 | - }, | |
| 420 | - 1000 | |
| 421 | - ); | |
| 426 | + var give_notices = $('.give-notice'); | |
| 427 | + if( give_notices.length ) { | |
| 428 | + give_notices.show(); | |
| 429 | + } | |
| 422 | 430 | }); |
| 423 | 431 | </script> |
| 424 | 432 | <?php |
| 425 | 433 | } |
| @@ -505,14 +513,14 @@ | ||
| 505 | 513 | */ |
| 506 | 514 | public function get_dismiss_link( $notice_args ) { |
| 507 | 515 | $notice_args = wp_parse_args( |
| 508 | 516 | $notice_args, |
| 509 | - array( | |
| 517 | + [ | |
| 510 | 518 | 'title' => __( 'Click here', 'give' ), |
| 511 | 519 | 'dismissible_type' => '', |
| 512 | 520 | 'dismiss_interval' => '', |
| 513 | 521 | 'dismiss_interval_time' => null, |
| 514 | - ) | |
| 522 | + ] | |
| 515 | 523 | ); |
| 516 | 524 | |
| 517 | 525 | return sprintf( |
| 518 | 526 | '<a href="#" class="give_dismiss_notice" data-dismissible-type="%1$s" data-dismiss-interval="%2$s" data-dismiss-interval-time="%3$s">%4$s</a>', |
| @@ -582,15 +590,15 @@ | ||
| 582 | 590 | * Change auto_dismissible to dismissible and set the value to true |
| 583 | 591 | * |
| 584 | 592 | * @since 1.8.14 |
| 585 | 593 | */ |
| 586 | - $default_notice_args = array( | |
| 594 | + $default_notice_args = [ | |
| 587 | 595 | 'dismissible' => true, |
| 588 | 596 | 'dismiss_interval' => 5000, |
| 589 | - ); | |
| 597 | + ]; | |
| 590 | 598 | |
| 591 | 599 | // Note: we will remove give_errors class in future. |
| 592 | - $classes = apply_filters( 'give_error_class', array( 'give_notices', 'give_errors' ) ); | |
| 600 | + $classes = apply_filters( 'give_error_class', [ 'give_notices', 'give_errors' ] ); | |
| 593 | 601 | |
| 594 | 602 | echo sprintf( '<div class="%s">', implode( ' ', $classes ) ); |
| 595 | 603 | |
| 596 | 604 | // Loop error codes and display errors. |
| @@ -596,12 +604,12 @@ | ||
| 596 | 604 | // Loop error codes and display errors. |
| 597 | 605 | foreach ( $errors as $error_id => $error ) { |
| 598 | 606 | // Backward compatibility v<1.8.11 |
| 599 | 607 | if ( is_string( $error ) ) { |
| 600 | - $error = array( | |
| 608 | + $error = [ | |
| 601 | 609 | 'message' => $error, |
| 602 | - 'notice_args' => array(), | |
| 603 | - ); | |
| 610 | + 'notice_args' => [], | |
| 611 | + ]; | |
| 604 | 612 | } |
| 605 | 613 | |
| 606 | 614 | $notice_args = wp_parse_args( $error['notice_args'], $default_notice_args ); |
| 607 | 615 | |
| @@ -630,8 +638,9 @@ | ||
| 630 | 638 | /** |
| 631 | 639 | * Print frontend notice. |
| 632 | 640 | * Notice: notice type can be success/error/warning |
| 633 | 641 | * |
| 642 | + * @since 3.7.0 Escape attributes | |
| 634 | 643 | * @since 1.8.9 |
| 635 | 644 | * @access public |
| 636 | 645 | * |
| 637 | 646 | * @param string $message |
| @@ -640,9 +649,9 @@ | ||
| 640 | 649 | * @param array $notice_args |
| 641 | 650 | * |
| 642 | 651 | * @return string |
| 643 | 652 | */ |
| 644 | - public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = array() ) { | |
| 653 | + public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = [] ) { | |
| 645 | 654 | if ( empty( $message ) ) { |
| 646 | 655 | return ''; |
| 647 | 656 | } |
| 648 | 657 | |
| @@ -650,13 +659,13 @@ | ||
| 650 | 659 | * Change auto_dismissible to dismissible and set the value to true |
| 651 | 660 | * |
| 652 | 661 | * @since 1.8.14 |
| 653 | 662 | */ |
| 654 | - $default_notice_args = array( | |
| 663 | + $default_notice_args = [ | |
| 655 | 664 | 'dismissible' => false, |
| 656 | 665 | 'dismiss_type' => 'auto', |
| 657 | 666 | 'dismiss_interval' => 5000, |
| 658 | - ); | |
| 667 | + ]; | |
| 659 | 668 | |
| 660 | 669 | $notice_args = wp_parse_args( $notice_args, $default_notice_args ); |
| 661 | 670 | |
| 662 | 671 | // Notice dismissible must be true for dismiss type. |
| @@ -671,10 +680,9 @@ | ||
| 671 | 680 | |
| 672 | 681 | $close_icon = 'manual' === $notice_args['dismiss_type'] ? |
| 673 | 682 | sprintf( |
| 674 | 683 | '<img class="notice-dismiss give-notice-close" src="%s" />', |
| 675 | - esc_url( GIVE_PLUGIN_URL . 'assets/dist/images/close.svg' ) | |
| 676 | - | |
| 684 | + esc_url( GIVE_PLUGIN_URL . 'build/assets/dist/images/close.svg' ) | |
| 677 | 685 | ) : |
| 678 | 686 | ''; |
| 679 | 687 | |
| 680 | 688 | // Note: we will remove give_errors class in future. |
| @@ -684,15 +692,14 @@ | ||
| 684 | 692 | %5$s |
| 685 | 693 | </p> |
| 686 | 694 | %6$s |
| 687 | 695 | </div>', |
| 688 | - $notice_type, | |
| 689 | - give_clean( $notice_args['dismissible'] ), | |
| 690 | - absint( $notice_args['dismiss_interval'] ), | |
| 691 | - give_clean( $notice_args['dismiss_type'] ), | |
| 692 | - $message, | |
| 696 | + esc_attr($notice_type), | |
| 697 | + esc_attr( $notice_args['dismissible'] ), | |
| 698 | + esc_attr( $notice_args['dismiss_interval'] ), | |
| 699 | + esc_attr( $notice_args['dismiss_type'] ), | |
| 700 | + esc_html($message), | |
| 693 | 701 | $close_icon |
| 694 | - | |
| 695 | 702 | ); |
| 696 | 703 | |
| 697 | 704 | if ( ! $echo ) { |
| 698 | 705 | return $error; |
| @@ -713,25 +720,25 @@ | ||
| 713 | 720 | * @since 1.8.17 |
| 714 | 721 | * |
| 715 | 722 | * @return string |
| 716 | 723 | */ |
| 717 | - public function print_admin_notices( $notice_args = array() ) { | |
| 724 | + public function print_admin_notices( $notice_args = [] ) { | |
| 718 | 725 | // Bailout. |
| 719 | 726 | if ( empty( $notice_args['description'] ) ) { |
| 720 | 727 | return ''; |
| 721 | 728 | } |
| 722 | 729 | |
| 723 | - $defaults = array( | |
| 730 | + $defaults = [ | |
| 724 | 731 | 'id' => '', |
| 725 | 732 | 'echo' => true, |
| 726 | 733 | 'notice_type' => 'warning', |
| 727 | 734 | 'dismissible' => true, |
| 728 | - ); | |
| 735 | + ]; | |
| 729 | 736 | $notice_args = wp_parse_args( $notice_args, $defaults ); |
| 730 | 737 | |
| 731 | - $output = ''; | |
| 732 | - $css_id = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' ); | |
| 733 | - $css_class = "notice-{$notice_args['notice_type']} give-notice notice inline"; | |
| 738 | + $output = ''; | |
| 739 | + $css_id = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' ); | |
| 740 | + $css_class = "notice-{$notice_args['notice_type']} give-notice notice inline"; | |
| 734 | 741 | $css_class .= ( $notice_args['dismissible'] ) ? ' is-dismissible' : ''; |
| 735 | 742 | $output .= sprintf( |
| 736 | 743 | '<div id="%1$s" class="%2$s"><p>%3$s</p></div>', |
| 737 | 744 | $css_id, |