PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
← All changes | includes/class-notices.php +101 -91 2.3.04.16.9 View file →
@@ -3,9 +3,9 @@
3 3 * Admin Notices Class.
4 4 *
5 5 * @package Give
6 6 * @subpackage Admin/Notices
7 - * @copyright Copyright (c) 2016, WordImpress
7 + * @copyright Copyright (c) 2016, GiveWP
8 8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 9 * @since 1.8.9
10 10 */
11 11
@@ -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,24 +204,36 @@
201 204 /**
202 205 * Display notice.
203 206 *
204 207 * @since 1.8.9
205 - *
206 208 */
207 209 public function render_admin_notices() {
210 + /* @var WP_Screen $wp_screen */
211 + $wp_screen = get_current_screen();
212 +
208 213 // Bailout.
209 214 if ( empty( self::$notices ) ) {
210 215 return;
211 216 }
212 217
213 - // Do not render notice on Gutenberg editor page.
218 + // Do not render notices on Gutenberg editor page.
214 219 if (
215 - function_exists('is_gutenberg_page')
216 - && is_gutenberg_page()
220 + method_exists( $wp_screen, 'is_block_editor' )
221 + && $wp_screen->is_block_editor()
217 222 ) {
218 223 return;
219 224 }
220 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 +
221 236 $output = '';
222 237
223 238 foreach ( self::$notices as $notice_id => $notice ) {
224 239 // Check flag set to true to show notice.
@@ -225,11 +240,10 @@
225 240 if ( ! $notice['show'] ) {
226 241 continue;
227 242 }
228 243
229 -
230 244 // Render custom html.
231 - if( ! empty( $notice['description_html'] ) ) {
245 + if ( ! empty( $notice['description_html'] ) ) {
232 246 $output .= "{$notice['description_html']} \n";
233 247 continue;
234 248 }
235 249
@@ -245,9 +259,9 @@
245 259
246 260 $css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] );
247 261
248 262 $css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}";
249 - $output .= sprintf(
263 + $output .= sprintf(
250 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",
251 265 $css_id,
252 266 $css_class,
253 267 give_clean( $notice['dismissible'] ),
@@ -267,33 +281,35 @@
267 281 $this->print_js();
268 282 }
269 283
270 284
271 - /**
272 - * Render give frontend notices.
273 - *
274 - * @since 1.8.9
275 - * @access public
276 - *
277 - * @param int $form_id
278 - */
279 - public function render_frontend_notices( $form_id = 0 ) {
280 - $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();
281 298
282 - $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;
283 300
284 - // Sanity checks first: Ensure that gateway returned errors display on the appropriate form.
285 - if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) {
286 - return;
287 - }
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 + }
288 307
289 - if ( $errors ) {
290 - self::print_frontend_errors( $errors );
308 + self::print_frontend_errors($errors);
309 + give_clear_errors();
310 + }
291 311
292 - give_clear_errors();
293 - }
294 - }
295 -
296 312 /**
297 313 * Renders notices for different actions depending on
298 314 * the type of form display option.
299 315 *
@@ -307,11 +323,11 @@
307 323 public function render_frontend_form_notices( $form_id ) {
308 324 $display_option = give_get_meta( $form_id, '_give_payment_display', true );
309 325
310 326 if ( 'modal' === $display_option ) {
311 - add_action( 'give_payment_mode_top', array( $this, 'render_frontend_notices' ) );
327 + add_action( 'give_payment_mode_top', [ $this, 'render_frontend_notices' ] );
312 328 } else {
313 - add_action( 'give_pre_form', array( $this, 'render_frontend_notices' ), 11 );
329 + add_action( 'give_pre_form', [ $this, 'render_frontend_notices' ], 11 );
314 330 }
315 331 }
316 332
317 333 /**
@@ -398,25 +414,20 @@
398 414 /**
399 415 * Show notices
400 416 * Note: only for internal use
401 417 *
418 + * @since 4.9.0 rename function - PHP 8 compatibility
402 419 * @since 2.3.0
403 420 */
404 - public function __reveal_notices(){
421 + public function reveal_notices() {
405 422 ?>
406 423 <script>
407 424 jQuery(document).ready(function($){
408 425 // Fix notice appearance issue.
409 - window.setTimeout(
410 - function(){
411 - var give_notices = $('.give-notice');
412 -
413 - if( give_notices.length ) {
414 - give_notices.slideDown();
415 - }
416 - },
417 - 1000
418 - );
426 + var give_notices = $('.give-notice');
427 + if( give_notices.length ) {
428 + give_notices.show();
429 + }
419 430 });
420 431 </script>
421 432 <?php
422 433 }
@@ -502,14 +513,14 @@
502 513 */
503 514 public function get_dismiss_link( $notice_args ) {
504 515 $notice_args = wp_parse_args(
505 516 $notice_args,
506 - array(
517 + [
507 518 'title' => __( 'Click here', 'give' ),
508 519 'dismissible_type' => '',
509 520 'dismiss_interval' => '',
510 521 'dismiss_interval_time' => null,
511 - )
522 + ]
512 523 );
513 524
514 525 return sprintf(
515 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>',
@@ -579,15 +590,15 @@
579 590 * Change auto_dismissible to dismissible and set the value to true
580 591 *
581 592 * @since 1.8.14
582 593 */
583 - $default_notice_args = array(
594 + $default_notice_args = [
584 595 'dismissible' => true,
585 596 'dismiss_interval' => 5000,
586 - );
597 + ];
587 598
588 599 // Note: we will remove give_errors class in future.
589 - $classes = apply_filters( 'give_error_class', array( 'give_notices', 'give_errors' ) );
600 + $classes = apply_filters( 'give_error_class', [ 'give_notices', 'give_errors' ] );
590 601
591 602 echo sprintf( '<div class="%s">', implode( ' ', $classes ) );
592 603
593 604 // Loop error codes and display errors.
@@ -593,12 +604,12 @@
593 604 // Loop error codes and display errors.
594 605 foreach ( $errors as $error_id => $error ) {
595 606 // Backward compatibility v<1.8.11
596 607 if ( is_string( $error ) ) {
597 - $error = array(
608 + $error = [
598 609 'message' => $error,
599 - 'notice_args' => array(),
600 - );
610 + 'notice_args' => [],
611 + ];
601 612 }
602 613
603 614 $notice_args = wp_parse_args( $error['notice_args'], $default_notice_args );
604 615
@@ -627,8 +638,9 @@
627 638 /**
628 639 * Print frontend notice.
629 640 * Notice: notice type can be success/error/warning
630 641 *
642 + * @since 3.7.0 Escape attributes
631 643 * @since 1.8.9
632 644 * @access public
633 645 *
634 646 * @param string $message
@@ -637,9 +649,9 @@
637 649 * @param array $notice_args
638 650 *
639 651 * @return string
640 652 */
641 - 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 = [] ) {
642 654 if ( empty( $message ) ) {
643 655 return '';
644 656 }
645 657
@@ -647,13 +659,13 @@
647 659 * Change auto_dismissible to dismissible and set the value to true
648 660 *
649 661 * @since 1.8.14
650 662 */
651 - $default_notice_args = array(
663 + $default_notice_args = [
652 664 'dismissible' => false,
653 665 'dismiss_type' => 'auto',
654 666 'dismiss_interval' => 5000,
655 - );
667 + ];
656 668
657 669 $notice_args = wp_parse_args( $notice_args, $default_notice_args );
658 670
659 671 // Notice dismissible must be true for dismiss type.
@@ -668,10 +680,9 @@
668 680
669 681 $close_icon = 'manual' === $notice_args['dismiss_type'] ?
670 682 sprintf(
671 683 '<img class="notice-dismiss give-notice-close" src="%s" />',
672 - esc_url( GIVE_PLUGIN_URL . 'assets/dist/images/close.svg' )
673 -
684 + esc_url( GIVE_PLUGIN_URL . 'build/assets/dist/images/close.svg' )
674 685 ) :
675 686 '';
676 687
677 688 // Note: we will remove give_errors class in future.
@@ -681,15 +692,14 @@
681 692 %5$s
682 693 </p>
683 694 %6$s
684 695 </div>',
685 - $notice_type,
686 - give_clean( $notice_args['dismissible'] ),
687 - absint( $notice_args['dismiss_interval'] ),
688 - give_clean( $notice_args['dismiss_type'] ),
689 - $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),
690 701 $close_icon
691 -
692 702 );
693 703
694 704 if ( ! $echo ) {
695 705 return $error;
@@ -710,25 +720,25 @@
710 720 * @since 1.8.17
711 721 *
712 722 * @return string
713 723 */
714 - public function print_admin_notices( $notice_args = array() ) {
724 + public function print_admin_notices( $notice_args = [] ) {
715 725 // Bailout.
716 726 if ( empty( $notice_args['description'] ) ) {
717 727 return '';
718 728 }
719 729
720 - $defaults = array(
730 + $defaults = [
721 731 'id' => '',
722 732 'echo' => true,
723 733 'notice_type' => 'warning',
724 734 'dismissible' => true,
725 - );
735 + ];
726 736 $notice_args = wp_parse_args( $notice_args, $defaults );
727 737
728 - $output = '';
729 - $css_id = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' );
730 - $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";
731 741 $css_class .= ( $notice_args['dismissible'] ) ? ' is-dismissible' : '';
732 742 $output .= sprintf(
733 743 '<div id="%1$s" class="%2$s"><p>%3$s</p></div>',
734 744 $css_id,