PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.19.4
GiveWP – Donation Plugin and Fundraising Platform v3.19.4
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 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / class-notices.php
give / includes Last commit date
admin 1 year ago api 3 years ago database 2 years ago deprecated 3 years ago donors 1 year ago emails 3 years ago forms 1 year ago frontend 6 years ago gateways 1 year ago libraries 2 years ago payments 1 year ago actions.php 5 years ago ajax-functions.php 2 years ago class-give-async-process.php 1 year ago class-give-background-updater.php 2 years ago class-give-cache-setting.php 2 years ago class-give-cache.php 3 years ago class-give-cli-commands.php 3 years ago class-give-comment.php 6 years ago class-give-cron.php 6 years ago class-give-donate-form.php 1 year ago class-give-donor.php 2 years ago class-give-email-access.php 5 years ago class-give-license-handler.php 1 year ago class-give-logging.php 5 years ago class-give-readme-parser.php 4 years ago class-give-roles.php 6 years ago class-give-scripts.php 2 years ago class-give-session.php 5 years ago class-give-stats.php 6 years ago class-give-template-loader.php 6 years ago class-give-tooltips.php 6 years ago class-give-translation.php 4 years ago class-notices.php 2 years ago country-functions.php 1 year ago currencies-list.php 3 years ago currency-functions.php 3 years ago error-tracking.php 6 years ago filters.php 3 years ago formatting.php 1 year ago install.php 2 years ago login-register.php 2 years ago misc-functions.php 1 year ago plugin-compatibility.php 6 years ago post-types.php 1 year ago price-functions.php 6 years ago process-donation.php 1 year ago setting-functions.php 6 years ago shortcodes.php 1 year ago template-functions.php 4 years ago user-functions.php 3 years ago
class-notices.php
755 lines
1 <?php
2 /**
3 * Admin Notices Class.
4 *
5 * @package Give
6 * @subpackage Admin/Notices
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.8.9
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Give_Notices Class
19 *
20 * @since 1.8.9
21 */
22 class Give_Notices {
23 /**
24 * List of notices
25 *
26 * @var array
27 * @since 1.8.9
28 * @access private
29 */
30 private static $notices = [];
31
32
33 /**
34 * Flag to check if any notice auto dismissible among all notices
35 *
36 * @since 1.8.9
37 * @access private
38 * @var bool
39 */
40 private static $has_auto_dismissible_notice = false;
41
42 /**
43 * Flag to check if any notice has dismiss interval among all notices
44 *
45 * @since 1.8.9
46 * @access private
47 * @var bool
48 */
49 private static $has_dismiss_interval_notice = false;
50
51 /**
52 * Get things started.
53 *
54 * @since 1.8.9
55 */
56 public function __construct() {
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' ] );
60
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' ] );
64
65 /**
66 * Backward compatibility for deprecated params.
67 *
68 * @since 1.8.14
69 */
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' ] );
73 }
74
75 /**
76 * Add backward compatibility to deprecated params.
77 *
78 * @since 1.8.14
79 * @access public
80 *
81 * @param array $args Array of notice params
82 *
83 * @return array
84 */
85 public function bc_deprecated_params( $args ) {
86 /**
87 * Param: auto_dismissible
88 * deprecated in 1.8.14
89 *
90 * Check if auto_dismissible is set and it true then unset and change dismissible parameter value to auto
91 */
92 if ( isset( $args['auto_dismissible'] ) ) {
93 if ( ! empty( $args['auto_dismissible'] ) ) {
94 $args['dismissible'] = 'auto';
95 }
96 // unset auto_dismissible as it has been deprecated.
97 unset( $args['auto_dismissible'] );
98 }
99
100 return $args;
101 }
102
103 /**
104 * Register notice.
105 *
106 * @since 1.8.9
107 * @access public
108 *
109 * @param $notice_args
110 *
111 * @return bool
112 */
113 public function register_notice( $notice_args ) {
114 // Bailout.
115 if ( empty( $notice_args['id'] ) || array_key_exists( $notice_args['id'], self::$notices ) ) {
116 return false;
117 }
118
119 $notice_args = wp_parse_args(
120 $notice_args,
121 [
122 'id' => '',
123 'description' => '',
124
125 /*
126 * Add custom notice html
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.
130 *
131 * @since 1.8.16
132 */
133 'description_html' => '',
134
135 /*
136 * Add New Parameter and remove the auto_dismissible parameter.
137 * Value: auto/true/false
138 *
139 * @since 1.8.14
140 */
141 'dismissible' => true,
142
143 // Value: error/warning/success/info/updated
144 'type' => 'error',
145
146 // Value: null/user/all
147 'dismissible_type' => null,
148
149 // Value: shortly/permanent/null/custom
150 'dismiss_interval' => null,
151
152 // Only set it when custom is defined.
153 'dismiss_interval_time' => null,
154
155 ]
156 );
157
158 /**
159 * Filter to modify Notice args before it get add
160 *
161 * @since 1.8.14
162 */
163 $notice_args = apply_filters( 'give_register_notice_args', $notice_args );
164
165 // Set extra dismiss links if any.
166 if ( false !== strpos( $notice_args['description'], 'data-dismiss-interval' ) ) {
167
168 preg_match_all( '/data-([^"]*)="([^"]*)"/', $notice_args['description'], $extra_notice_dismiss_link );
169
170 if ( ! empty( $extra_notice_dismiss_link ) ) {
171 $extra_notice_dismiss_links = array_chunk( current( $extra_notice_dismiss_link ), 3 );
172 foreach ( $extra_notice_dismiss_links as $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 );
183
184 $notice_args['extra_links'][] = wp_parse_args( $data_attribute_query_str );
185 }
186 }
187 }
188
189 self::$notices[ $notice_args['id'] ] = $notice_args;
190
191 // Auto set show param if not already set.
192 if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) {
193 self::$notices[ $notice_args['id'] ]['show'] = $this->is_notice_dismissed( $notice_args ) ? false : true;
194 }
195
196 // Auto set time interval for shortly.
197 if ( 'shortly' === self::$notices[ $notice_args['id'] ]['dismiss_interval'] ) {
198 self::$notices[ $notice_args['id'] ]['dismiss_interval_time'] = DAY_IN_SECONDS;
199 }
200
201 return true;
202 }
203
204 /**
205 * Display notice.
206 *
207 * @since 1.8.9
208 */
209 public function render_admin_notices() {
210 /* @var WP_Screen $wp_screen */
211 $wp_screen = get_current_screen();
212
213 // Bailout.
214 if ( empty( self::$notices ) ) {
215 return;
216 }
217
218 // Do not render notices on Gutenberg editor page.
219 if (
220 method_exists( $wp_screen, 'is_block_editor' )
221 && $wp_screen->is_block_editor()
222 ) {
223 return;
224 }
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
236 $output = '';
237
238 foreach ( self::$notices as $notice_id => $notice ) {
239 // Check flag set to true to show notice.
240 if ( ! $notice['show'] ) {
241 continue;
242 }
243
244 // Render custom html.
245 if ( ! empty( $notice['description_html'] ) ) {
246 $output .= "{$notice['description_html']} \n";
247 continue;
248 }
249
250 // Check if notice dismissible or not.
251 if ( ! self::$has_auto_dismissible_notice ) {
252 self::$has_auto_dismissible_notice = ( 'auto' === $notice['dismissible'] );
253 }
254
255 // Check if notice dismissible or not.
256 if ( ! self::$has_dismiss_interval_notice ) {
257 self::$has_dismiss_interval_notice = $notice['dismiss_interval'];
258 }
259
260 $css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] );
261
262 $css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}";
263 $output .= sprintf(
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",
265 $css_id,
266 $css_class,
267 give_clean( $notice['dismissible'] ),
268 $notice['dismissible_type'],
269 $notice['dismiss_interval'],
270 $notice['id'],
271 empty( $notice['dismissible_type'] ) ? '' : wp_create_nonce( "give_edit_{$notice_id}_notice" ),
272 $notice['dismiss_interval_time']
273 );
274
275 $output .= ( 0 === strpos( $notice['description'], '<div' ) || 0 === strpos( $notice['description'], '<p' ) ? $notice['description'] : "<p>{$notice['description']}</p>" );
276 $output .= "</div> \n";
277 }
278
279 echo $output;
280
281 $this->print_js();
282 }
283
284
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();
298
299 $request_form_id = isset($_REQUEST['form-id']) ? absint($_REQUEST['form-id']) : 0;
300
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 }
307
308 self::print_frontend_errors($errors);
309 give_clear_errors();
310 }
311
312 /**
313 * Renders notices for different actions depending on
314 * the type of form display option.
315 *
316 * @since 2.2
317 * @access public
318 *
319 * @param int $form_id Form ID.
320 *
321 * @return void
322 */
323 public function render_frontend_form_notices( $form_id ) {
324 $display_option = give_get_meta( $form_id, '_give_payment_display', true );
325
326 if ( 'modal' === $display_option ) {
327 add_action( 'give_payment_mode_top', [ $this, 'render_frontend_notices' ] );
328 } else {
329 add_action( 'give_pre_form', [ $this, 'render_frontend_notices' ], 11 );
330 }
331 }
332
333 /**
334 * Print notice js.
335 *
336 * @since 1.8.9
337 * @access private
338 */
339 private function print_js() {
340 if ( self::$has_auto_dismissible_notice ) :
341 ?>
342 <script>
343 jQuery(document).ready(function () {
344 // auto hide setting message in 5 seconds.
345 window.setTimeout(
346 function () {
347 jQuery('.give-notice[data-dismissible="auto"]').slideUp();
348 },
349 5000
350 );
351 })
352 </script>
353 <?php
354 endif;
355
356 if ( self::$has_dismiss_interval_notice ) :
357 ?>
358 <script>
359 jQuery(document).ready(function () {
360 var $body = jQuery('body');
361
362 $body.on('click', '.give_dismiss_notice', function (e) {
363 var $parent = jQuery(this).parents('.give-notice'),
364 custom_notice_data = {
365 'dismissible_type': jQuery(this).data('dismissible-type'),
366 'dismiss_interval': jQuery(this).data('dismiss-interval'),
367 'dismiss_interval_time': jQuery(this).data('dismiss-interval-time')
368 };
369
370 $parent.find('button.notice-dismiss').trigger('click', [custom_notice_data]);
371 return false;
372 });
373
374 $body.on('click', 'button.notice-dismiss', function (e, custom_notice_data) {
375 var $parent = jQuery(this).parents('.give-notice'),
376 custom_notice_data = custom_notice_data || {};
377
378 e.preventDefault();
379
380 var data = {
381 'give-action': 'dismiss_notices',
382 'notice_id': $parent.data('notice-id'),
383 'dismissible_type': $parent.data('dismissible-type'),
384 'dismiss_interval': $parent.data('dismiss-interval'),
385 'dismiss_interval_time': $parent.data('dismiss-interval-time'),
386 '_wpnonce': $parent.data('security')
387 };
388
389 if (Object.keys(custom_notice_data).length) {
390 jQuery.extend(data, custom_notice_data);
391 }
392
393 // Bailout.
394 if (
395 !data.dismiss_interval ||
396 !data.dismissible_type
397 ) {
398 return false;
399 }
400
401 jQuery.post(
402 '<?php echo admin_url(); ?>admin-ajax.php',
403 data,
404 function (response) {
405
406 })
407 })
408 });
409 </script>
410 <?php
411 endif;
412 }
413
414 /**
415 * Show notices
416 * Note: only for internal use
417 *
418 * @since 2.3.0
419 */
420 public function __reveal_notices() {
421 ?>
422 <script>
423 jQuery(document).ready(function($){
424 // Fix notice appearance issue.
425 var give_notices = $('.give-notice');
426 if( give_notices.length ) {
427 give_notices.show();
428 }
429 });
430 </script>
431 <?php
432 }
433
434
435 /**
436 * Hide notice.
437 *
438 * @since 1.8.9
439 * @access public
440 */
441 public function dismiss_notices() {
442 $_post = give_clean( $_POST );
443 $notice_id = esc_attr( $_post['notice_id'] );
444
445 // Bailout.
446 if (
447 empty( $notice_id ) ||
448 empty( $_post['dismissible_type'] ) ||
449 empty( $_post['dismiss_interval'] ) ||
450 ! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' )
451 ) {
452 wp_send_json_error();
453 }
454
455 $notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] );
456 if ( 'user' === $_post['dismissible_type'] ) {
457 $current_user = wp_get_current_user();
458 $notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID );
459 }
460
461 $notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null;
462
463 // Save option to hide notice.
464 Give_Cache::set( $notice_key, true, $notice_dismiss_time, true );
465
466 /**
467 * Fire the action when notice dismissed
468 *
469 * @since 2.2.0
470 */
471 do_action( 'give_notice_dismissed', $_post );
472
473 wp_send_json_success();
474 }
475
476
477 /**
478 * Get notice key.
479 *
480 * @since 1.8.9
481 * @access public
482 *
483 * @param string $notice_id
484 * @param string $dismiss_interval
485 * @param int $user_id
486 *
487 * @return string
488 */
489 public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) {
490 $notice_key = "_give_notice_{$notice_id}";
491
492 if ( ! empty( $dismiss_interval ) ) {
493 $notice_key .= "_{$dismiss_interval}";
494 }
495
496 if ( $user_id ) {
497 $notice_key .= "_{$user_id}";
498 }
499
500 $notice_key = sanitize_key( $notice_key );
501
502 return $notice_key;
503 }
504
505
506 /**
507 * Get notice dismiss link.
508 *
509 * @param $notice_args
510 *
511 * @return string
512 */
513 public function get_dismiss_link( $notice_args ) {
514 $notice_args = wp_parse_args(
515 $notice_args,
516 [
517 'title' => __( 'Click here', 'give' ),
518 'dismissible_type' => '',
519 'dismiss_interval' => '',
520 'dismiss_interval_time' => null,
521 ]
522 );
523
524 return sprintf(
525 '<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>',
526 $notice_args['dismissible_type'],
527 $notice_args['dismiss_interval'],
528 $notice_args['dismiss_interval_time'],
529 $notice_args['title']
530 );
531 }
532
533
534 /**
535 * Check if notice dismissed or not
536 *
537 * @since 1.8.9
538 * @access public
539 *
540 * @param array $notice
541 *
542 * @return bool|null
543 */
544 public function is_notice_dismissed( $notice ) {
545 $notice_key = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] );
546 $is_notice_dismissed = false;
547
548 if ( 'user' === $notice['dismissible_type'] ) {
549 $current_user = wp_get_current_user();
550 $notice_key = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID );
551 }
552
553 $notice_data = Give_Cache::get( $notice_key, true );
554
555 // Find notice dismiss link status if notice has extra dismissible links.
556 if ( ( empty( $notice_data ) || is_wp_error( $notice_data ) ) && ! empty( $notice['extra_links'] ) ) {
557
558 foreach ( $notice['extra_links'] as $extra_link ) {
559 $new_notice_data = wp_parse_args( $extra_link, $notice );
560 unset( $new_notice_data['extra_links'] );
561
562 if ( $is_notice_dismissed = $this->is_notice_dismissed( $new_notice_data ) ) {
563 return $is_notice_dismissed;
564 }
565 }
566 }
567
568 $is_notice_dismissed = ! empty( $notice_data ) && ! is_wp_error( $notice_data );
569
570 return $is_notice_dismissed;
571 }
572
573
574 /**
575 * Print frontend errors.
576 *
577 * @since 1.8.9
578 * @access public
579 *
580 * @param array $errors
581 */
582 public static function print_frontend_errors( $errors ) {
583 // Bailout.
584 if ( ! $errors ) {
585 return;
586 }
587
588 /**
589 * Change auto_dismissible to dismissible and set the value to true
590 *
591 * @since 1.8.14
592 */
593 $default_notice_args = [
594 'dismissible' => true,
595 'dismiss_interval' => 5000,
596 ];
597
598 // Note: we will remove give_errors class in future.
599 $classes = apply_filters( 'give_error_class', [ 'give_notices', 'give_errors' ] );
600
601 echo sprintf( '<div class="%s">', implode( ' ', $classes ) );
602
603 // Loop error codes and display errors.
604 foreach ( $errors as $error_id => $error ) {
605 // Backward compatibility v<1.8.11
606 if ( is_string( $error ) ) {
607 $error = [
608 'message' => $error,
609 'notice_args' => [],
610 ];
611 }
612
613 $notice_args = wp_parse_args( $error['notice_args'], $default_notice_args );
614
615 /**
616 * Filter to modify Frontend Errors args before errors is display.
617 *
618 * @since 1.8.14
619 */
620 $notice_args = apply_filters( 'give_frontend_errors_args', $notice_args );
621
622 echo sprintf(
623 '<div class="give_error give_notice" id="give_error_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d">
624 <p><strong>%4$s</strong>: %5$s</p>
625 </div>',
626 $error_id,
627 give_clean( $notice_args['dismissible'] ),
628 absint( $notice_args['dismiss_interval'] ),
629 esc_html__( 'Error', 'give' ),
630 $error['message']
631 );
632 }
633
634 echo '</div>';
635 }
636
637 /**
638 * Print frontend notice.
639 * Notice: notice type can be success/error/warning
640 *
641 * @since 3.7.0 Escape attributes
642 * @since 1.8.9
643 * @access public
644 *
645 * @param string $message
646 * @param bool $echo
647 * @param string $notice_type
648 * @param array $notice_args
649 *
650 * @return string
651 */
652 public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = [] ) {
653 if ( empty( $message ) ) {
654 return '';
655 }
656
657 /**
658 * Change auto_dismissible to dismissible and set the value to true
659 *
660 * @since 1.8.14
661 */
662 $default_notice_args = [
663 'dismissible' => false,
664 'dismiss_type' => 'auto',
665 'dismiss_interval' => 5000,
666 ];
667
668 $notice_args = wp_parse_args( $notice_args, $default_notice_args );
669
670 // Notice dismissible must be true for dismiss type.
671 $notice_args['dismiss_type'] = ! $notice_args['dismissible'] ? '' : $notice_args['dismiss_type'];
672
673 /**
674 * Filter to modify Frontend notice args before notices is display.
675 *
676 * @since 1.8.14
677 */
678 $notice_args = apply_filters( 'give_frontend_notice_args', $notice_args );
679
680 $close_icon = 'manual' === $notice_args['dismiss_type'] ?
681 sprintf(
682 '<img class="notice-dismiss give-notice-close" src="%s" />',
683 esc_url( GIVE_PLUGIN_URL . 'assets/dist/images/close.svg' )
684 ) :
685 '';
686
687 // Note: we will remove give_errors class in future.
688 $error = sprintf(
689 '<div class="give_notices give_errors" id="give_error_%1$s">
690 <p class="give_notice give_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d" data-dismiss-type="%4$s">
691 %5$s
692 </p>
693 %6$s
694 </div>',
695 esc_attr($notice_type),
696 esc_attr( $notice_args['dismissible'] ),
697 esc_attr( $notice_args['dismiss_interval'] ),
698 esc_attr( $notice_args['dismiss_type'] ),
699 esc_html($message),
700 $close_icon
701 );
702
703 if ( ! $echo ) {
704 return $error;
705 }
706
707 echo $error;
708 }
709
710 /**
711 * Print Inline Notice.
712 * Note: dismissible feature will note work if notice will add to dom by javascript after document load.
713 *
714 * @param array $notice_args An array of notice arguments.
715 *
716 * @todo Implement render_admin_notices function within this function in future.
717 *
718 * @access public
719 * @since 1.8.17
720 *
721 * @return string
722 */
723 public function print_admin_notices( $notice_args = [] ) {
724 // Bailout.
725 if ( empty( $notice_args['description'] ) ) {
726 return '';
727 }
728
729 $defaults = [
730 'id' => '',
731 'echo' => true,
732 'notice_type' => 'warning',
733 'dismissible' => true,
734 ];
735 $notice_args = wp_parse_args( $notice_args, $defaults );
736
737 $output = '';
738 $css_id = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' );
739 $css_class = "notice-{$notice_args['notice_type']} give-notice notice inline";
740 $css_class .= ( $notice_args['dismissible'] ) ? ' is-dismissible' : '';
741 $output .= sprintf(
742 '<div id="%1$s" class="%2$s"><p>%3$s</p></div>',
743 $css_id,
744 $css_class,
745 $notice_args['description']
746 );
747
748 if ( ! $notice_args['echo'] ) {
749 return $output;
750 }
751
752 echo $output;
753 }
754 }
755