Completed
Push — issue-2719 ( c98c0f )
by Ravinder
383:18 queued 375:02
created

Give_Notices::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Notices Class.
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Notices
7
 * @copyright   Copyright (c) 2016, WordImpress
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
	 * @var array
26
	 * @since  1.8.9
27
	 * @access private
28
	 */
29
	private static $notices = array();
30
31
32
	/**
33
	 * Flag to check if any notice auto dismissible among all notices
34
	 *
35
	 * @since  1.8.9
36
	 * @access private
37
	 * @var bool
38
	 */
39
	private static $has_auto_dismissible_notice = false;
40
41
	/**
42
	 * Flag to check if any notice has dismiss interval among all notices
43
	 *
44
	 * @since  1.8.9
45
	 * @access private
46
	 * @var bool
47
	 */
48
	private static $has_dismiss_interval_notice = false;
49
50
	/**
51
	 * Get things started.
52
	 *
53
	 * @since 1.8.9
54
	 */
55
	public function __construct() {
56
		add_action( 'admin_notices', array( $this, 'render_admin_notices' ), 999 );
57
		add_action( 'give_dismiss_notices', array( $this, 'dismiss_notices' ) );
58
59
		add_action( 'give_frontend_notices', array( $this, 'render_frontend_notices' ), 999 );
60
		add_action( 'give_pre_form', array( $this, 'render_frontend_notices' ), 11 );
61
		add_action( 'give_ajax_donation_errors', array( $this, 'render_frontend_notices' ) );
62
63
		/**
64
		 * Backward compatibility for deprecated params.
65
		 *
66
		 * @since 1.8.14
67
		 */
68
		add_filter( 'give_register_notice_args', array( $this, 'bc_deprecated_params' ) );
69
		add_filter( 'give_frontend_errors_args', array( $this, 'bc_deprecated_params' ) );
70
		add_filter( 'give_frontend_notice_args', array( $this, 'bc_deprecated_params' ) );
71
	}
72
73
	/**
74
	 * Add backward compatibility to deprecated params.
75
	 *
76
	 * @since  1.8.14
77
	 * @access public
78
	 *
79
	 * @param array $args Array of notice params
80
	 *
81
	 * @return array
82
	 */
83
	public function bc_deprecated_params( $args ) {
84
		/**
85
		 *  Param: auto_dismissible
86
		 *  deprecated in 1.8.14
87
		 *
88
		 *  Check if auto_dismissible is set and it true then unset and change dismissible parameter value to auto
89
		 */
90
		if ( isset( $args['auto_dismissible'] ) ) {
91
			if ( ! empty( $args['auto_dismissible'] ) ) {
92
				$args['dismissible'] = 'auto';
93
			}
94
			// unset auto_dismissible as it has been deprecated.
95
			unset( $args['auto_dismissible'] );
96
		}
97
98
		return $args;
99
	}
100
101
	/**
102
	 * Register notice.
103
	 *
104
	 * @since  1.8.9
105
	 * @access public
106
	 *
107
	 * @param $notice_args
108
	 *
109
	 * @return bool
110
	 */
111
	public function register_notice( $notice_args ) {
112
		// Bailout.
113
		if ( empty( $notice_args['id'] ) || array_key_exists( $notice_args['id'], self::$notices ) ) {
114
			return false;
115
		}
116
117
		$notice_args = wp_parse_args(
118
			$notice_args,
119
			array(
120
				'id'                    => '',
121
				'description'           => '',
122
123
				/*
124
				 * Add custom notice html
125
				 * Note: This param has more priority then description, so if you have  both param then this one will be use
126
				 *       for generating notice html. Most of feature of notice attach to core generated html, so if you set
127
				 *       custom html then please add required classes and data attribute which help to apply feature on notice.
128
				 *
129
				 * @since 1.8.16
130
				 */
131
				'description_html'      => '',
132
133
				/*
134
				 * Add New Parameter and remove the auto_dismissible parameter.
135
				 * Value: auto/true/false
136
				 *
137
				 * @since 1.8.14
138
				 */
139
				'dismissible'           => true,
140
141
				// Value: error/warning/success/info/updated
142
				'type'                  => 'error',
143
144
				// Value: null/user/all
145
				'dismissible_type'      => null,
146
147
				// Value: shortly/permanent/null/custom
148
				'dismiss_interval'      => null,
149
150
				// Only set it when custom is defined.
151
				'dismiss_interval_time' => null,
152
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
153
154
			)
155
		);
156
157
		/**
158
		 * Filter to modify Notice args before it get add
159
		 *
160
		 * @since 1.8.14
161
		 */
162
		$notice_args = apply_filters( 'give_register_notice_args', $notice_args );
163
164
		// Set extra dismiss links if any.
165
		if ( false !== strpos( $notice_args['description'], 'data-dismiss-interval' ) ) {
166
167
			preg_match_all( "/data-([^\"]*)=\"([^\"]*)\"/", $notice_args['description'], $extra_notice_dismiss_link );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /data-([^\"]*)=\"([^\"]*)\"/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
168
169
			if ( ! empty( $extra_notice_dismiss_link ) ) {
170
				$extra_notice_dismiss_links = array_chunk( current( $extra_notice_dismiss_link ), 3 );
171
				foreach ( $extra_notice_dismiss_links as $extra_notice_dismiss_link ) {
172
					// Create array og key ==> value by parsing query string created after renaming data attributes.
173
					$data_attribute_query_str = str_replace( array( 'data-', '-', '"' ), array(
174
						'',
175
						'_',
176
						'',
177
					), implode( '&', $extra_notice_dismiss_link ) );
178
179
					$notice_args['extra_links'][] = wp_parse_args( $data_attribute_query_str );
180
				}
181
			}
182
		}
183
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
184
185
		self::$notices[ $notice_args['id'] ] = $notice_args;
186
187
		// Auto set show param if not already set.
188
		if ( ! isset( self::$notices[ $notice_args['id'] ]['show'] ) ) {
189
			self::$notices[ $notice_args['id'] ]['show'] = $this->is_notice_dismissed( $notice_args ) ? false : true;
190
		}
191
192
		// Auto set time interval for shortly.
193
		if ( 'shortly' === self::$notices[ $notice_args['id'] ]['dismiss_interval'] ) {
194
			self::$notices[ $notice_args['id'] ]['dismiss_interval_time'] = DAY_IN_SECONDS;
195
		}
196
197
		return true;
198
	}
199
200
	/**
201
	 * Display notice.
202
	 *
203
	 * @since 1.8.9
204
	 *
205
	 */
206
	public function render_admin_notices() {
207
		// Bailout.
208
		if ( empty( self::$notices ) ) {
209
			return;
210
		}
211
212
		$output = '';
213
214
		foreach ( self::$notices as $notice_id => $notice ) {
215
			// Check flag set to true to show notice.
216
			if ( ! $notice['show'] ) {
217
				continue;
218
			}
219
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
220
221
			// Render custom html.
222
			if( ! empty( $notice['description_html'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
223
				$output .= "{$notice['description_html']} \n";
224
				continue;
225
			}
226
227
			// Check if notice dismissible or not.
228
			if ( ! self::$has_auto_dismissible_notice ) {
229
				self::$has_auto_dismissible_notice = ( 'auto' === $notice['dismissible'] );
230
			}
231
232
			// Check if notice dismissible or not.
233
			if ( ! self::$has_dismiss_interval_notice ) {
234
				self::$has_dismiss_interval_notice = $notice['dismiss_interval'];
235
			}
236
237
			$css_id = ( false === strpos( $notice['id'], 'give' ) ? "give-{$notice['id']}" : $notice['id'] );
238
239
			$css_class = 'give-notice notice ' . ( empty( $notice['dismissible'] ) ? 'non' : 'is' ) . "-dismissible {$notice['type']} notice-{$notice['type']}";
240
			$output    .= sprintf(
241
				'<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",
242
				$css_id,
243
				$css_class,
244
				give_clean( $notice['dismissible'] ),
245
				$notice['dismissible_type'],
246
				$notice['dismiss_interval'],
247
				$notice['id'],
248
				empty( $notice['dismissible_type'] ) ? '' : wp_create_nonce( "give_edit_{$notice_id}_notice" ),
249
				$notice['dismiss_interval_time']
250
			);
251
252
			$output .= ( 0 === strpos( $notice['description'], '<div' ) || 0 === strpos( $notice['description'], '<p' ) ? $notice['description'] : "<p>{$notice['description']}</p>" );
253
			$output .= "</div> \n";
254
		}
255
256
		echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
257
258
		$this->print_js();
259
	}
260
261
262
	/**
263
	 * Render give frontend notices.
264
	 *
265
	 * @since  1.8.9
266
	 * @access public
267
	 *
268
	 * @param int $form_id
269
	 */
270
	public function render_frontend_notices( $form_id = 0 ) {
271
		$errors = give_get_errors();
272
273
		$request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
274
275
		// Sanity checks first: Ensure that gateway returned errors display on the appropriate form.
276
		if ( ! isset( $_POST['give_ajax'] ) && $request_form_id !== $form_id ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
277
			return;
278
		}
279
280
		if ( $errors ) {
281
			self::print_frontend_errors( $errors );
0 ignored issues
show
Bug introduced by
It seems like $errors defined by give_get_errors() on line 271 can also be of type string; however, Give_Notices::print_frontend_errors() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
282
283
			give_clear_errors();
284
		}
285
	}
286
287
	/**
288
	 * Print notice js.
289
	 *
290
	 * @since  1.8.9
291
	 * @access private
292
	 */
293
	private function print_js() {
294
		if ( self::$has_auto_dismissible_notice ) :
295
			?>
296
			<script>
297
				jQuery(document).ready(function () {
298
					// auto hide setting message in 5 seconds.
299
					window.setTimeout(
300
						function () {
301
							jQuery('.give-notice[data-dismissible="auto"]').slideUp();
302
						},
303
						5000
304
					);
305
				})
306
			</script>
307
			<?php
308
		endif;
309
310
		if ( self::$has_dismiss_interval_notice ) :
311
			?>
312
			<script>
313
				jQuery(document).ready(function () {
314
					var $body = jQuery('body');
315
316
					$body.on('click', '.give_dismiss_notice', function (e) {
317
						var $parent            = jQuery(this).parents('.give-notice'),
318
							custom_notice_data = {
319
								'dismissible_type': jQuery(this).data('dismissible-type'),
320
								'dismiss_interval': jQuery(this).data('dismiss-interval'),
321
								'dismiss_interval_time': jQuery(this).data('dismiss-interval-time')
322
							};
323
324
						$parent.find('button.notice-dismiss').trigger('click', [custom_notice_data]);
325
						return false;
326
					});
327
328
					$body.on('click', 'button.notice-dismiss', function (e, custom_notice_data) {
329
						var $parent            = jQuery(this).parents('.give-notice'),
330
							custom_notice_data = custom_notice_data || {};
331
332
						e.preventDefault();
333
334
						var data = {
335
							'give-action': 'dismiss_notices',
336
							'notice_id': $parent.data('notice-id'),
337
							'dismissible_type': $parent.data('dismissible-type'),
338
							'dismiss_interval': $parent.data('dismiss-interval'),
339
							'dismiss_interval_time': $parent.data('dismiss-interval-time'),
340
							'_wpnonce': $parent.data('security')
341
						};
342
343
						if (Object.keys(custom_notice_data).length) {
344
							jQuery.extend(data, custom_notice_data);
345
						}
346
347
						// Bailout.
348
						if (
349
							!data.dismiss_interval ||
350
							!data.dismissible_type
351
						) {
352
							return false;
353
						}
354
355
						jQuery.post(
356
							'<?php echo admin_url(); ?>admin-ajax.php',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'admin_url'
Loading history...
357
							data,
358
							function (response) {
359
360
							})
361
					})
362
				});
363
			</script>
364
			<?php
365
		endif;
366
		?>
367
		<script>
368
			jQuery(document).ready(function($){
369
				// Fix notice appearance issue.
370
				window.setTimeout(
371
					function(){
372
						$('.give-notice').slideDown();
373
					},
374
					1000
375
				);
376
			});
377
		</script>
378
		<?php
379
	}
380
381
382
	/**
383
	 * Hide notice.
384
	 *
385
	 * @since  1.8.9
386
	 * @access public
387
	 */
388
	public function dismiss_notices() {
389
		$_post     = give_clean( $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
390
		$notice_id = esc_attr( $_post['notice_id'] );
391
392
		// Bailout.
393
		if (
394
			empty( $notice_id ) ||
395
			empty( $_post['dismissible_type'] ) ||
396
			empty( $_post['dismiss_interval'] ) ||
397
			! check_ajax_referer( "give_edit_{$notice_id}_notice", '_wpnonce' )
398
		) {
399
			wp_send_json_error();
400
		}
401
402
		$notice_key = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'] );
403 View Code Duplication
		if ( 'user' === $_post['dismissible_type'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
404
			$current_user = wp_get_current_user();
405
			$notice_key   = Give()->notices->get_notice_key( $notice_id, $_post['dismiss_interval'], $current_user->ID );
406
		}
407
408
		$notice_dismiss_time = ! empty( $_post['dismiss_interval_time'] ) ? $_post['dismiss_interval_time'] : null;
409
410
		// Save option to hide notice.
411
		Give_Cache::set( $notice_key, true, $notice_dismiss_time, true );
412
413
		/**
414
		 * Fire the action when notice dismissed
415
		 *
416
		 * @since 2.0
417
		 */
418
		do_action( 'give_dismiss_notices', $_post );
419
420
		wp_send_json_success();
421
	}
422
423
424
	/**
425
	 * Get notice key.
426
	 *
427
	 * @since  1.8.9
428
	 * @access public
429
	 *
430
	 * @param string $notice_id
431
	 * @param string $dismiss_interval
432
	 * @param int    $user_id
433
	 *
434
	 * @return string
435
	 */
436
	public function get_notice_key( $notice_id, $dismiss_interval = null, $user_id = 0 ) {
437
		$notice_key = "_give_notice_{$notice_id}";
438
439
		if ( ! empty( $dismiss_interval ) ) {
440
			$notice_key .= "_{$dismiss_interval}";
441
		}
442
443
		if ( $user_id ) {
444
			$notice_key .= "_{$user_id}";
445
		}
446
447
		$notice_key = sanitize_key( $notice_key );
448
449
		return $notice_key;
450
	}
451
452
453
	/**
454
	 * Get notice dismiss link.
455
	 *
456
	 * @param $notice_args
457
	 *
458
	 * @return string
459
	 */
460
	public function get_dismiss_link( $notice_args ) {
461
		$notice_args = wp_parse_args(
462
			$notice_args,
463
			array(
464
				'title'                 => __( 'Click here', 'give' ),
465
				'dismissible_type'      => '',
466
				'dismiss_interval'      => '',
467
				'dismiss_interval_time' => null,
468
			)
469
		);
470
471
		return sprintf(
472
			'<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>',
473
			$notice_args['dismissible_type'],
474
			$notice_args['dismiss_interval'],
475
			$notice_args['dismiss_interval_time'],
476
			$notice_args['title']
477
		);
478
	}
479
480
481
	/**
482
	 * Check if notice dismissed or not
483
	 *
484
	 * @since  1.8.9
485
	 * @access public
486
	 *
487
	 * @param array $notice
488
	 *
489
	 * @return bool|null
490
	 */
491
	public function is_notice_dismissed( $notice ) {
492
		$notice_key          = $this->get_notice_key( $notice['id'], $notice['dismiss_interval'] );
493
		$is_notice_dismissed = false;
0 ignored issues
show
Unused Code introduced by
$is_notice_dismissed is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
494
495 View Code Duplication
		if ( 'user' === $notice['dismissible_type'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
496
			$current_user = wp_get_current_user();
497
			$notice_key   = Give()->notices->get_notice_key( $notice['id'], $notice['dismiss_interval'], $current_user->ID );
498
		}
499
500
		$notice_data = Give_Cache::get( $notice_key, true );
501
502
		// Find notice dismiss link status if notice has extra dismissible links.
503
		if ( ( empty( $notice_data ) || is_wp_error( $notice_data ) ) && ! empty( $notice['extra_links'] ) ) {
504
505
			foreach ( $notice['extra_links'] as $extra_link ) {
506
				$new_notice_data = wp_parse_args( $extra_link, $notice );
507
				unset( $new_notice_data['extra_links'] );
508
509
				if ( $is_notice_dismissed = $this->is_notice_dismissed( $new_notice_data ) ) {
510
					return $is_notice_dismissed;
511
				}
512
			}
513
		}
514
515
		$is_notice_dismissed = ! empty( $notice_data ) && ! is_wp_error( $notice_data );
516
517
		return $is_notice_dismissed;
518
	}
519
520
521
	/**
522
	 * Print frontend errors.
523
	 *
524
	 * @since  1.8.9
525
	 * @access public
526
	 *
527
	 * @param array $errors
528
	 */
529
	public static function print_frontend_errors( $errors ) {
530
		// Bailout.
531
		if ( ! $errors ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
532
			return;
533
		}
534
535
		/**
536
		 * Change auto_dismissible to dismissible and set the value to true
537
		 *
538
		 * @since 1.8.14
539
		 */
540
		$default_notice_args = array(
541
			'dismissible'      => true,
542
			'dismiss_interval' => 5000,
543
		);
544
545
		// Note: we will remove give_errors class in future.
546
		$classes = apply_filters( 'give_error_class', array( 'give_notices', 'give_errors' ) );
547
548
		echo sprintf( '<div class="%s">', implode( ' ', $classes ) );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
549
550
		// Loop error codes and display errors.
551
		foreach ( $errors as $error_id => $error ) {
552
			// Backward compatibility v<1.8.11
553
			if ( is_string( $error ) ) {
554
				$error = array(
555
					'message'     => $error,
556
					'notice_args' => array(),
557
				);
558
			}
559
560
			$notice_args = wp_parse_args( $error['notice_args'], $default_notice_args );
561
562
			/**
563
			 * Filter to modify Frontend Errors args before errors is display.
564
			 *
565
			 * @since 1.8.14
566
			 */
567
			$notice_args = apply_filters( 'give_frontend_errors_args', $notice_args );
568
569
			echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
570
				'<div class="give_error give_notice" id="give_error_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d">
571
						<p><strong>%4$s</strong>: %5$s</p>
572
					</div>',
573
				$error_id,
574
				give_clean( $notice_args['dismissible'] ),
575
				absint( $notice_args['dismiss_interval'] ),
576
				esc_html__( 'Error', 'give' ),
577
				$error['message']
578
			);
579
		}
580
581
		echo '</div>';
582
	}
583
584
	/**
585
	 * Print frontend notice.
586
	 * Notice: notice type can be success/error/warning
587
	 *
588
	 * @since  1.8.9
589
	 * @access public
590
	 *
591
	 * @param string $message
592
	 * @param bool   $echo
593
	 * @param string $notice_type
594
	 * @param array  $notice_args
595
	 *
596
	 * @return  string
597
	 */
598
	public static function print_frontend_notice( $message, $echo = true, $notice_type = 'warning', $notice_args = array() ) {
599
		if ( empty( $message ) ) {
600
			return '';
601
		}
602
603
		/**
604
		 * Change auto_dismissible to dismissible and set the value to true
605
		 *
606
		 * @since 1.8.14
607
		 */
608
		$default_notice_args = array(
609
			'dismissible'      => false,
610
			'dismiss_type'     => 'auto',
611
			'dismiss_interval' => 5000,
612
		);
613
614
		$notice_args = wp_parse_args( $notice_args, $default_notice_args );
615
616
		// Notice dismissible must be true for dismiss type.
617
		$notice_args['dismiss_type'] = ! $notice_args['dismissible'] ? '' : $notice_args['dismiss_type'];
618
619
		/**
620
		 * Filter to modify Frontend notice args before notices is display.
621
		 *
622
		 * @since 1.8.14
623
		 */
624
		$notice_args = apply_filters( 'give_frontend_notice_args', $notice_args );
625
626
		$close_icon = 'manual' === $notice_args['dismiss_type'] ?
627
			sprintf(
628
				'<img class="notice-dismiss give-notice-close" src="%s" />',
629
				esc_url( GIVE_PLUGIN_URL . 'assets/images/svg/close.svg' )
630
0 ignored issues
show
Coding Style introduced by
There should be no empty lines in a multi-line function call.
Loading history...
631
			) :
632
			'';
633
634
		// Note: we will remove give_errors class in future.
635
		$error = sprintf(
636
			'<div class="give_notices give_errors" id="give_error_%1$s">
637
						<p class="give_error give_notice give_%1$s" data-dismissible="%2$s" data-dismiss-interval="%3$d" data-dismiss-type="%4$s">
638
							%5$s
639
						</p>
640
						%6$s
641
					</div>',
642
			$notice_type,
643
			give_clean( $notice_args['dismissible'] ),
644
			absint( $notice_args['dismiss_interval'] ),
645
			give_clean( $notice_args['dismiss_type'] ),
646
			$message,
647
			$close_icon
648
0 ignored issues
show
Coding Style introduced by
There should be no empty lines in a multi-line function call.
Loading history...
649
		);
650
651
		if ( ! $echo ) {
652
			return $error;
653
		}
654
655
		echo $error;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$error'
Loading history...
656
	}
657
658
	/**
659
	 * Print Inline Notice.
660
	 * Note: dismissible feature will note work if notice will add to dom by javascript after document load.
661
	 *
662
	 * @param array $notice_args An array of notice arguments.
663
	 *
664
	 * @todo   Implement render_admin_notices function within this function in future.
665
	 *
666
	 * @access public
667
	 * @since  1.8.17
668
	 *
669
	 * @return string
670
	 */
671
	public function print_admin_notices( $notice_args = array() ) {
672
		// Bailout.
673
		if ( empty( $notice_args['description'] ) ) {
674
			return '';
675
		}
676
677
		$defaults    = array(
678
			'id'          => '',
679
			'echo'        => true,
680
			'notice_type' => 'warning',
681
			'dismissible' => true,
682
		);
683
		$notice_args = wp_parse_args( $notice_args, $defaults );
684
685
		$output    = '';
686
		$css_id    = ! empty( $notice_args['id'] ) ? $notice_args['id'] : uniqid( 'give-inline-notice-' );
687
		$css_class = "notice-{$notice_args['notice_type']} give-notice notice inline";
688
		$css_class .= ( $notice_args['dismissible'] ) ? ' is-dismissible' : '';
689
		$output    .= sprintf(
690
			'<div id="%1$s" class="%2$s"><p>%3$s</p></div>',
691
			$css_id,
692
			$css_class,
693
			$notice_args['description']
694
		);
695
696
		if ( ! $notice_args['echo'] ) {
697
			return $output;
698
		}
699
700
		echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
701
	}
702
}