Test Failed
Push — feature/background-processing ( ddf6d7 )
by Ravinder
06:04
created

formatting.php ➔ give_human_format_large_amount()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 46
Code Lines 26

Duplication

Lines 19
Ratio 41.3 %

Importance

Changes 0
Metric Value
cc 8
eloc 26
nc 8
nop 2
dl 19
loc 46
rs 5.5555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Formatting functions for taking care of proper number formats and such
4
 *
5
 * @package     Give
6
 * @subpackage  Functions/Formatting
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
18
/**
19
 * Get decimal count
20
 *
21
 * @since 1.6
22
 *
23
 * @return mixed
24
 */
25
function give_get_price_decimals() {
26
	return apply_filters( 'give_sanitize_amount_decimals', give_get_option( 'number_decimals', 0 ) );
27
}
28
29
/**
30
 * Get thousand separator
31
 *
32
 * @since 1.6
33
 *
34
 * @return mixed
35
 */
36
function give_get_price_thousand_separator() {
37
	$give_options       = give_get_settings();
38
	$thousand_separator = isset( $give_options['thousands_separator'] ) ? $give_options['thousands_separator'] : ',';
39
	$thousand_separator = empty( $thousand_separator ) ? ' ' : $thousand_separator;
40
41
	return $thousand_separator;
42
}
43
44
/**
45
 * Get decimal separator
46
 *
47
 * @since 1.6
48
 *
49
 * @return mixed
50
 */
51
function give_get_price_decimal_separator() {
52
	$default_decimal_separators = array(
53
		'.' => ',',
54
		',' => '.',
55
	);
56
57
	$thousand_separator = give_get_price_thousand_separator();
58
	$default_decimal_separator = in_array( $thousand_separator, $default_decimal_separators ) ?
59
		$default_decimal_separators[$thousand_separator] :
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
60
		'.';
61
62
	$decimal_separator = give_get_option( 'decimal_separator', $default_decimal_separator );
63
64
	return $decimal_separator;
65
}
66
67
68
/**
69
 * Sanitize Amount before saving to database
70
 *
71
 * @since      1.8.12
72
 *
73
 * @param  int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
74
 *
75
 * @return string $amount Newly sanitized amount
76
 */
77
function give_sanitize_amount_for_db( $number ) {
78
	return give_maybe_sanitize_amount( $number, 6 );
79
}
80
81
/**
82
 * Sanitize Amount before saving to database
83
 *
84
 * @since      1.8.12
85
 *
86
 * @param  int|float|string $number     Expects either a float or a string with a decimal separator only (no thousands)
87
 * @param  int|bool         $dp         Number of decimals
88
 * @param  bool             $trim_zeros From end of string
89
 *
90
 * @return string $amount Newly sanitized amount
91
 */
92
function give_maybe_sanitize_amount( $number, $dp = false, $trim_zeros = false ) {
93
	$thousand_separator = give_get_price_thousand_separator();
94
	$decimal_separator  = give_get_price_decimal_separator();
95
96
	// Bailout.
97
	if( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
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...
98
		return $number;
99
	}elseif (
100
		( false == strpos( $number, $thousand_separator ) ) &&
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($number, $thousand_separator) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
101
		( false === strpos( $number, $decimal_separator ) )
102
	) {
103
		return number_format( $number, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' );
104
	}
105
106
	// Handle thousand separator as '.'
107
	// Handle sanitize database values.
108
	$number_parts = explode( '.', $number );
109
	$is_db_sanitize_val = ( 2 === count( $number_parts ) &&
110
	                        is_numeric( $number_parts[0] ) &&
111
	                        is_numeric( $number_parts[1] ) &&
112
	                        ( 6 === strlen( $number_parts[1] ) ) );
113
114
	if( $is_db_sanitize_val ) {
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...
115
		// Sanitize database value.
116
		return number_format( $number, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' );
117
118
	} elseif (
119
		'.' === $thousand_separator &&
120
		false !== strpos( $number, $thousand_separator )
121
	){
122
		// Fix point thousand separator value.
123
		$number = str_replace( '.', '', $number );
124
	}
125
126
	return give_sanitize_amount( $number, $dp, $trim_zeros );
127
}
128
129
/**
130
 * Sanitize Amount
131
 *
132
 * Returns a sanitized amount by stripping out thousands separators.
133
 *
134
 * @since      1.0
135
 *
136
 * @param  int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
137
 * @param  int|bool         $dp Number of decimals
138
 * @param  bool             $trim_zeros From end of string
139
 *
140
 * @return string $amount Newly sanitized amount
141
 */
142
function give_sanitize_amount( $number, $dp = false, $trim_zeros = false ) {
143
144
	// Bailout.
145
	if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
146
		return $number;
147
	}
148
149
	// Remove slash from amount.
150
	// If thousand or decimal separator is set to ' then in $_POST or $_GET param we will get an escaped number.
151
	// To prevent notices and warning remove slash from amount/number.
152
	$number = wp_unslash( $number );
153
154
	$thousand_separator = give_get_price_thousand_separator();
155
156
	$locale   = localeconv();
157
	$decimals = array( give_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );
158
159
	// Remove locale from string
160
	if ( ! is_float( $number ) ) {
161
		$number = str_replace( $decimals, '.', $number );
162
	}
163
164
	// Remove thousand amount formatting if amount has.
165
	// This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db.
166
	// Do not replace thousand separator from price if it is same as decimal separator, because it will be already replace by above code.
167
	if ( ! in_array( $thousand_separator, $decimals ) && ( false !== strpos( $number, $thousand_separator ) ) ) {
168
		$number = str_replace( $thousand_separator, '', $number );
169
	} elseif ( in_array( $thousand_separator, $decimals ) ) {
170
		$number = preg_replace( '/\.(?=.*\.)/', '', $number );
171
	}
172
173
	// Remove non numeric entity before decimal separator.
174
	$number     = preg_replace( '/[^0-9\.]/', '', $number );
175
	$default_dp = give_get_price_decimals();
176
177
	// Reset negative amount to zero.
178
	if ( 0 > $number ) {
179
		$number = number_format( 0, $default_dp, '.' );
180
	}
181
182
	// If number does not have decimal then add number of decimals to it.
183
	if (
184
		false === strpos( $number, '.' )
185
		|| ( $default_dp > strlen( substr( $number, strpos( $number, '.' ) + 1 ) ) )
186
	) {
187
		$number = number_format( $number, $default_dp, '.', '' );
188
	}
189
190
	// Format number by custom number of decimals.
191
	if ( false !== $dp ) {
192
		$dp     = intval( is_bool( $dp ) ? $default_dp : $dp );
193
		$dp     = apply_filters( 'give_sanitize_amount_decimals', $dp, $number );
194
		$number = number_format( floatval( $number ), $dp, '.', '' );
195
	}
196
197
	// Trim zeros.
198
	if ( $trim_zeros && strstr( $number, '.' ) ) {
199
		$number = rtrim( rtrim( $number, '0' ), '.' );
200
	}
201
202
	return apply_filters( 'give_sanitize_amount', $number );
203
}
204
205
/**
206
 * Returns a nicely formatted amount.
207
 *
208
 * @since 1.0
209
 *
210
 * @param string $amount   Price amount to format
211
 * @param array  $args     Array of arguments.
212
 *
213
 * @return string $amount   Newly formatted amount or Price Not Available
214
 */
215
function give_format_amount( $amount, $args = array() ) {
216
	// Backward compatibility.
217
	if( is_bool( $args ) ) {
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...
218
		$args = array( 'decimal' => $args );
219
	}
220
221
	$default_args = array(
222
		'decimal'  => true,
223
		'sanitize' => true,
224
		'currency' => give_get_currency(),
225
	);
226
227
	$args = wp_parse_args( $args, $default_args );
228
229
	$formatted     = 0;
230
	$thousands_sep = give_get_price_thousand_separator();
231
	$decimal_sep   = give_get_price_decimal_separator();
232
	$decimals      = ! empty( $args['decimal'] ) ? give_get_price_decimals() : 0;
233
	$currency      = $args['currency'];
234
235
	if ( ! empty( $amount ) ) {
236
		// Sanitize amount before formatting.
237
		$amount = ! empty( $args['sanitize'] ) ?
238
			give_maybe_sanitize_amount( $amount, $decimals ) :
239
			number_format( $amount, $decimals, '.', '' );
240
241
		switch ( $currency ) {
242
			case 'INR':
243
				$decimal_amount = '';
244
245
				// Extract decimals from amount
246
				if ( ( $pos = strpos( $amount, '.' ) ) !== false ) {
0 ignored issues
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
247
					if ( ! empty( $decimals ) ) {
248
						$decimal_amount = substr( round( substr( $amount, $pos ), $decimals ), 1 );
249
						$amount         = substr( $amount, 0, $pos );
250
251
						if ( ! $decimal_amount ) {
252
							$decimal_amount = substr( '.0000000000', 0, ( $decimals + 1 ) );
253
						} elseif ( ( $decimals + 1 ) > strlen( $decimal_amount ) ) {
254
							$decimal_amount = substr( "{$decimal_amount}000000000", 0, ( $decimals + 1 ) );
255
						}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
256
257
					} else {
258
						$amount = number_format( $amount, $decimals, $decimal_sep, '' );
259
					}
260
				}
261
262
				// Extract last 3 from amount
263
				$result = substr( $amount, - 3 );
264
				$amount = substr( $amount, 0, - 3 );
265
266
				// Apply digits 2 by 2
267
				while ( strlen( $amount ) > 0 ) {
268
					$result = substr( $amount, - 2 ) . $thousands_sep . $result;
269
					$amount = substr( $amount, 0, - 2 );
270
				}
271
272
				$formatted = $result . $decimal_amount;
273
				break;
274
275
			default:
276
				$formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
277
		}
278
	}
279
280
	return apply_filters( 'give_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $currency, $args );
281
}
282
283
284
/**
285
 * Get human readable amount.
286
 *
287
 * Note: This function only support large number formatting from million to trillion
288
 *
289
 * @since 1.6
290
 *
291
 * @use   give_get_price_thousand_separator Get thousand separator.
292
 *
293
 * @param string $amount formatted amount number.
294
 * @param array  $args   Array of arguments.
295
 *
296
 * @return float|string  formatted amount number with large number names.
297
 */
298
function give_human_format_large_amount( $amount, $args = array() ) {
299
	$default_args = array(
300
		'currency' => give_get_currency(),
301
	);
302
303
	$args = wp_parse_args( $args, $default_args );
304
305
	// Get thousand separator.
306
	$thousands_sep = give_get_price_thousand_separator();
307
308
	// Sanitize amount.
309
	$sanitize_amount = give_maybe_sanitize_amount( $amount );
310
311
	// Explode amount to calculate name of large numbers.
312
	$amount_array = explode( $thousands_sep, $amount );
313
314
	// Calculate amount parts count.
315
	$amount_count_parts = count( $amount_array );
316
317
	// Human format amount (default).
318
	$human_format_amount = $amount;
319
320
	switch ( $args['currency'] ) {
321 View Code Duplication
		case 'INR':
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...
322
			// Calculate large number formatted amount.
323
			if ( 4 < $amount_count_parts ) {
324
				$human_format_amount = sprintf( esc_html__( '%s arab', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
325
			} elseif ( 3 < $amount_count_parts ) {
326
				$human_format_amount = sprintf( esc_html__( '%s crore', 'give' ), round( ( $sanitize_amount / 10000000 ), 2 ) );
327
			} elseif ( 2 < $amount_count_parts ) {
328
				$human_format_amount = sprintf( esc_html__( '%s lakh', 'give' ), round( ( $sanitize_amount / 100000 ), 2 ) );
329
			}
330
			break;
331 View Code Duplication
		default:
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...
332
			// Calculate large number formatted amount.
333
			if ( 4 < $amount_count_parts ) {
334
				$human_format_amount = sprintf( esc_html__( '%s trillion', 'give' ), round( ( $sanitize_amount / 1000000000000 ), 2 ) );
335
			} elseif ( 3 < $amount_count_parts ) {
336
				$human_format_amount = sprintf( esc_html__( '%s billion', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
337
			} elseif ( 2 < $amount_count_parts ) {
338
				$human_format_amount = sprintf( esc_html__( '%s million', 'give' ), round( ( $sanitize_amount / 1000000 ), 2 ) );
339
			}
340
	}
341
342
	return apply_filters( 'give_human_format_large_amount', $human_format_amount, $amount, $sanitize_amount );
343
}
344
345
/**
346
 * Returns a nicely formatted amount with custom decimal separator.
347
 *
348
 * @since 1.0
349
 *
350
 * @param int|float|string $amount   Formatted or sanitized price
351
 * @param int|bool         $dp       number of decimals
352
 * @param bool             $sanitize Whether or not sanitize number
353
 *
354
 * @return string $amount Newly formatted amount or Price Not Available
355
 */
356
function give_format_decimal( $amount, $dp = false, $sanitize = true ) {
357
	$decimal_separator = give_get_price_decimal_separator();
358
	$formatted_amount  = $sanitize ?
359
		give_maybe_sanitize_amount( $amount, $dp ) :
360
		number_format( $amount, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' );
361
362
	if ( false !== strpos( $formatted_amount, '.' ) ) {
363
		$formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount );
364
	}
365
366
	return apply_filters( 'give_format_decimal', $formatted_amount, $amount, $decimal_separator );
367
}
368
369
/**
370
 * Formats the currency displayed.
371
 *
372
 * @since 1.0
373
 *
374
 * @param string $price The donation amount.
375
 * @param string $currency The currency code.
376
 * @param bool   $decode_currency Whether to decode the currency HTML format or not.
377
 *
378
 * @return mixed|string
379
 */
380
function give_currency_filter( $price = '', $currency = '', $decode_currency = false ) {
381
382
	if ( empty( $currency ) || ! array_key_exists( (string) $currency, give_get_currencies() ) ) {
383
		$currency = give_get_currency();
384
	}
385
386
	$position = give_get_option( 'currency_position', 'before' );
387
388
	$negative = $price < 0;
389
390
	if ( $negative ) {
391
		// Remove proceeding "-".
392
		$price = substr( $price, 1 );
393
	}
394
395
	$symbol = give_currency_symbol( $currency, $decode_currency );
396
397
	switch ( $currency ) :
398
		case 'GBP' :
399
		case 'BRL' :
400
		case 'EUR' :
401
		case 'USD' :
402
		case 'AUD' :
403
		case 'CAD' :
404
		case 'HKD' :
405
		case 'MXN' :
406
		case 'NZD' :
407
		case 'SGD' :
408
		case 'JPY' :
409
		case 'THB' :
410
		case 'INR' :
411
		case 'RIAL' :
412
		case 'TRY' :
413
		case 'RUB' :
414
		case 'SEK' :
415
		case 'PLN' :
416
		case 'PHP' :
417
		case 'TWD' :
418
		case 'MYR' :
419
		case 'CZK' :
420
		case 'DKK' :
421
		case 'HUF' :
422
		case 'ILS' :
423
		case 'MAD' :
424
		case 'KRW' :
425
		case 'ZAR' :
426
			$formatted = ( 'before' === $position ? $symbol . $price : $price . $symbol );
427
			break;
428 View Code Duplication
		case 'NOK' :
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...
429
			$formatted = ( 'before' === $position ? $symbol . ' ' . $price : $price . ' ' . $symbol );
430
			break;
431 View Code Duplication
		default :
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...
432
			$formatted = ( 'before' === $position ? $currency . ' ' . $price : $price . ' ' . $currency );
433
			break;
434
	endswitch;
435
436
	/**
437
	 * Filter formatted amount with currency
438
	 *
439
	 * Filter name depends upon current value of currency and currency position.
440
	 * For example :
441
	 *           if currency is USD and currency position is before then
442
	 *           filter name will be give_usd_currency_filter_before
443
	 *
444
	 *           and if currency is USD and currency position is after then
445
	 *           filter name will be give_usd_currency_filter_after
446
	 */
447
	$formatted = apply_filters( 'give_' . strtolower( $currency ) . "_currency_filter_{$position}", $formatted, $currency, $price );
448
449
	if ( $negative ) {
450
		// Prepend the minus sign before the currency sign.
451
		$formatted = '-' . $formatted;
452
	}
453
454
	return $formatted;
455
}
456
457
/**
458
 * Set the number of decimal places per currency
459
 *
460
 * @since 1.0
461
 * @since 1.6 $decimals parameter removed from function params
462
 * *
463
 * @return int $decimals
464
 */
465
function give_currency_decimal_filter() {
466
467
	remove_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' );
468
469
	// Set default number of decimals.
470
	$decimals = give_get_price_decimals();
471
472
	add_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' );
473
474
	// Get number of decimals with backward compatibility ( version < 1.6 )
475
	if ( 1 <= func_num_args() ) {
476
		$decimals = ( false === func_get_arg( 0 ) ? $decimals : absint( func_get_arg( 0 ) ) );
477
	}
478
479
	$currency = give_get_currency();
480
481
	switch ( $currency ) {
482
		case 'RIAL' :
483
		case 'JPY' :
484
		case 'TWD' :
485
		case 'HUF' :
486
487
			$decimals = 0;
488
			break;
489
	}
490
491
	return apply_filters( 'give_currency_decimal_count', $decimals, $currency );
492
}
493
494
add_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' );
495
add_filter( 'give_format_amount_decimals', 'give_currency_decimal_filter' );
496
497
498
/**
499
 * Get date format string on basis of given context.
500
 *
501
 * @since 1.7
502
 *
503
 * @param  string $date_context Date format context name.
504
 *
505
 * @return string                  Date format string
506
 */
507
function give_date_format( $date_context = '' ) {
508
	/**
509
	 * Filter the date context
510
	 *
511
	 * You can add your own date context or use already exist context.
512
	 * For example:
513
	 *    add_filter( 'give_date_format_contexts', 'add_new_date_contexts' );
514
	 *    function add_new_date_contexts( $date_format_contexts ) {
515
	 *        // You can add single context like this $date_format_contexts['checkout'] = 'F j, Y';
516
	 *        // Instead add multiple date context at once.
517
	 *        $new_date_format_contexts = array(
518
	 *            'checkout' => 'F j, Y',
519
	 *            'report'   => 'Y-m-d',
520
	 *            'email'    => 'm/d/Y',
521
	 *        );
522
	 *
523
	 *       // Merge date contexts array only if you are adding multiple date contexts at once otherwise return  $date_format_contexts.
524
	 *       return array_merge( $new_date_format_contexts, $date_format_contexts );
525
	 *
526
	 *    }
527
	 */
528
	$date_format_contexts = apply_filters( 'give_date_format_contexts', array() );
529
530
	// Set date format to default date format.
531
	$date_format = get_option( 'date_format' );
532
533
	// Update date format if we have non empty date format context array and non empty date format string for that context.
534
	if ( $date_context && ! empty( $date_format_contexts ) && array_key_exists( $date_context, $date_format_contexts ) ) {
535
		$date_format = ! empty( $date_format_contexts[ $date_context ] )
536
			? $date_format_contexts[ $date_context ]
537
			: $date_format;
538
	}
539
540
	return apply_filters( 'give_date_format', $date_format );
541
}
542
543
/**
544
 * Get cache key.
545
 *
546
 * @since  1.7
547
 * @deprecated 1.8.7 You can access this function from Give_Cache.
548
 *
549
 * @param  string $action Cache key prefix.
550
 * @param array  $query_args Query array.
551
 *
552
 * @return string
553
 */
554
function give_get_cache_key( $action, $query_args ) {
555
	return Give_Cache::get_key( $action, $query_args );
556
}
557
558
/**
559
 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
560
 * Non-scalar values are ignored.
561
 *
562
 * @since  1.8
563
 *
564
 * @param  string|array $var
565
 *
566
 * @return string|array
567
 */
568
function give_clean( $var ) {
569
	if ( is_array( $var ) ) {
570
		return array_map( 'give_clean', $var );
571
	} else {
572
		return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
573
	}
574
}
575
576
/**
577
 * Transforms php.ini notation for numbers (like '2M') to an integer.
578
 *
579
 * @since 1.8
580
 *
581
 * @param $size
582
 *
583
 * @return int
584
 */
585
function give_let_to_num( $size ) {
586
	$l   = substr( $size, - 1 );
587
	$ret = substr( $size, 0, - 1 );
588
	switch ( strtoupper( $l ) ) {
589
		case 'P':
590
			$ret *= 1024;
591
		case 'T':
592
			$ret *= 1024;
593
		case 'G':
594
			$ret *= 1024;
595
		case 'M':
596
			$ret *= 1024;
597
		case 'K':
598
			$ret *= 1024;
599
	}
600
601
	return $ret;
602
}
603
604
/**
605
 * Verify nonce.
606
 *
607
 * @since 1.8
608
 *
609
 * @param        $nonce
610
 * @param int   $action
611
 * @param array $wp_die_args
612
 */
613
function give_validate_nonce( $nonce, $action = - 1, $wp_die_args = array() ) {
614
615
	$default_wp_die_args = array(
616
		'message' => esc_html__( 'Nonce verification has failed.', 'give' ),
617
		'title'   => esc_html__( 'Error', 'give' ),
618
		'args'    => array(
619
			'response' => 403,
620
		),
621
	);
622
623
	$wp_die_args = wp_parse_args( $wp_die_args, $default_wp_die_args );
624
625
	if ( ! wp_verify_nonce( $nonce, $action ) ) {
626
		wp_die(
627
			$wp_die_args['message'],
628
			$wp_die_args['title'],
629
			$wp_die_args['args']
630
		);
631
	}
632
}
633
634
/**
635
 * Check variable and get default or valid value.
636
 *
637
 * Helper function to check if a variable is set, empty, etc.
638
 *
639
 * @since 1.8
640
 *
641
 * @param                   $variable
642
 * @param string (optional) $conditional , default value: isset
643
 * @param bool (optional)   $default , default value: false
644
 *
645
 * @return mixed
646
 */
647
function give_check_variable( $variable, $conditional = '', $default = false ) {
648
649
	switch ( $conditional ) {
650
		case 'isset_empty':
651
			$variable = ( isset( $variable ) && ! empty( $variable ) ) ? $variable : $default;
652
			break;
653
654
		case 'empty':
655
			$variable = ! empty( $variable ) ? $variable : $default;
656
			break;
657
658
		case 'null':
659
			$variable = ! is_null( $variable ) ? $variable : $default;
660
			break;
661
662
		default:
663
			$variable = isset( $variable ) ? $variable : $default;
664
665
	}
666
667
	return $variable;
668
669
}
670