Test Failed
Pull Request — master (#1937)
by
unknown
05:41
created

formatting.php ➔ give_maybe_sanitize_amount()   C

Complexity

Conditions 11
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 26.125

Importance

Changes 0
Metric Value
cc 11
eloc 16
nc 5
nop 3
dl 0
loc 28
ccs 7
cts 14
cp 0.5
crap 26.125
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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