PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.3
GiveWP – Donation Plugin and Fundraising Platform v2.0.3
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 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 / formatting.php
give / includes Last commit date
admin 8 years ago api 8 years ago deprecated 8 years ago donors 8 years ago emails 8 years ago forms 8 years ago gateways 8 years ago libraries 8 years ago payments 8 years ago actions.php 8 years ago ajax-functions.php 8 years ago class-give-async-process.php 8 years ago class-give-background-updater.php 8 years ago class-give-cache.php 8 years ago class-give-cli-commands.php 8 years ago class-give-cron.php 8 years ago class-give-db-donor-meta.php 8 years ago class-give-db-donors.php 8 years ago class-give-db-form-meta.php 8 years ago class-give-db-logs-meta.php 8 years ago class-give-db-logs.php 8 years ago class-give-db-meta.php 8 years ago class-give-db-payment-meta.php 8 years ago class-give-db.php 8 years ago class-give-donate-form.php 8 years ago class-give-donor.php 8 years ago class-give-email-access.php 8 years ago class-give-gravatars.php 8 years ago class-give-html-elements.php 8 years ago class-give-license-handler.php 8 years ago class-give-logging.php 8 years ago class-give-roles.php 8 years ago class-give-session.php 8 years ago class-give-stats.php 8 years ago class-give-template-loader.php 9 years ago class-give-tooltips.php 8 years ago class-give-translation.php 8 years ago class-notices.php 8 years ago country-functions.php 8 years ago currency-functions.php 8 years ago error-tracking.php 8 years ago filters.php 8 years ago formatting.php 8 years ago import-functions.php 8 years ago install.php 8 years ago login-register.php 8 years ago misc-functions.php 8 years ago plugin-compatibility.php 8 years ago post-types.php 8 years ago price-functions.php 8 years ago process-donation.php 8 years ago scripts.php 8 years ago shortcodes.php 8 years ago template-functions.php 8 years ago user-functions.php 8 years ago
formatting.php
782 lines
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 * Get Currency Formatting Settings for each donation.
19 *
20 * @param int|string $id_or_currency_code Donation ID or Currency code.
21 *
22 * @since 1.8.15
23 *
24 * @return mixed
25 */
26 function give_get_currency_formatting_settings( $id_or_currency_code = null ) {
27 $give_options = give_get_settings();
28 $setting = array();
29
30 if ( ! empty( $id_or_currency_code ) ) {
31 $currencies = give_get_currencies( 'all' );
32
33 // Set default formatting setting only if currency not set as global currency.
34 if (
35 is_string( $id_or_currency_code ) &&
36 ! empty( $give_options['currency'] ) &&
37 $id_or_currency_code !== $give_options['currency'] &&
38 array_key_exists( $id_or_currency_code, $currencies )
39 ) {
40 $setting = $currencies[ $id_or_currency_code ]['setting'];
41 } elseif ( is_numeric( $id_or_currency_code ) && 'give_payment' === get_post_type( $id_or_currency_code ) ) {
42 $donation_meta = give_get_meta( $id_or_currency_code, '_give_payment_meta', true );
43
44 if (
45 ! empty( $donation_meta['currency'] ) &&
46 $give_options['currency'] !== $donation_meta['currency']
47 ) {
48 $setting = $currencies[ $donation_meta['currency'] ]['setting'];
49 }
50 }
51 }
52
53 if ( empty( $setting ) ) {
54 // Set thousand separator.
55 $thousand_separator = isset( $give_options['thousands_separator'] ) ? $give_options['thousands_separator'] : ',';
56 $thousand_separator = empty( $thousand_separator ) ? ' ' : $thousand_separator;
57
58 // Set decimal separator.
59 $default_decimal_separators = array(
60 '.' => ',',
61 ',' => '.',
62 );
63
64 $default_decimal_separator = in_array( $thousand_separator, $default_decimal_separators ) ?
65 $default_decimal_separators[ $thousand_separator ] :
66 '.';
67
68 $decimal_separator = ! empty( $give_options['decimal_separator'] ) ? $give_options['decimal_separator'] : $default_decimal_separator;
69
70 $setting = array(
71 'currency_position' => give_get_option( 'currency_position', 'before' ),
72 'thousands_separator' => $thousand_separator,
73 'decimal_separator' => $decimal_separator,
74 'number_decimals' => give_get_option( 'number_decimals', 0 ),
75 );
76 }
77
78 /**
79 * Filter the currency formatting setting.
80 *
81 * @since 1.8.15
82 */
83 return apply_filters( 'give_get_currency_formatting_settings', $setting, $id_or_currency_code );
84 }
85
86 /**
87 * Get decimal count
88 *
89 * @since 1.6
90 *
91 * @param int|string $id_or_currency_code
92 *
93 * @return mixed
94 */
95 function give_get_price_decimals( $id_or_currency_code = null ) {
96 // Set currency on basis of donation id.
97 if ( empty( $id_or_currency_code ) ) {
98 $id_or_currency_code = give_get_currency();
99 }
100
101 $number_of_decimals = 0;
102
103 if ( ! give_is_zero_based_currency( $id_or_currency_code ) ) {
104 $setting = give_get_currency_formatting_settings( $id_or_currency_code );
105 $number_of_decimals = $setting['number_decimals'];
106 }
107
108 /**
109 * Filter the number of decimals
110 *
111 * @since 1.6
112 */
113 return apply_filters( 'give_sanitize_amount_decimals', $number_of_decimals, $id_or_currency_code );
114 }
115
116 /**
117 * Get thousand separator
118 *
119 * @param int|string $id_or_currency_code
120 *
121 * @since 1.6
122 *
123 * @return mixed
124 */
125 function give_get_price_thousand_separator( $id_or_currency_code = null ) {
126 $setting = give_get_currency_formatting_settings( $id_or_currency_code );
127
128 /**
129 * Filter the thousand separator
130 *
131 * @since 1.6
132 */
133 return apply_filters( 'give_get_price_thousand_separator', $setting['thousands_separator'], $id_or_currency_code );
134 }
135
136 /**
137 * Get decimal separator
138 *
139 * @param string $id_or_currency_code
140 *
141 * @since 1.6
142 *
143 * @return mixed
144 */
145 function give_get_price_decimal_separator( $id_or_currency_code = null ) {
146 $setting = give_get_currency_formatting_settings( $id_or_currency_code );
147
148 /**
149 * Filter the thousand separator
150 *
151 * @since 1.6
152 */
153 return apply_filters( 'give_get_price_decimal_separator', $setting['decimal_separator'], $id_or_currency_code );
154 }
155
156
157 /**
158 * Sanitize Amount before saving to database
159 *
160 * @since 1.8.12
161 *
162 * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
163 *
164 * @return string $amount Newly sanitized amount
165 */
166 function give_sanitize_amount_for_db( $number ) {
167 return give_maybe_sanitize_amount( $number, array( 'number_decimals' => 6 ) );
168 }
169
170 /**
171 * Sanitize Amount before saving to database
172 *
173 * @since 1.8.12
174 *
175 * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
176 * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
177 *
178 * @return string $amount Newly sanitized amount
179 */
180 function give_maybe_sanitize_amount( $number, $args = array() ) {
181 // Bailout.
182 if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
183 return $number;
184 }
185
186 $func_args = func_get_args();
187
188 // Backward compatibility.
189 if ( isset( $func_args[1] ) && ( is_bool( $func_args[1] ) || is_numeric( $func_args[1] ) ) ) {
190 $args = array(
191 'number_decimals' => $func_args[1],
192 'trim_zeros' => isset( $func_args[2] ) ? $func_args[2] : false,
193 );
194 }
195
196 $args = wp_parse_args(
197 $args,
198 array(
199 'number_decimals' => false,
200 'trim_zeros' => false,
201 'currency' => give_get_currency(),
202 )
203 );
204
205 $thousand_separator = give_get_price_thousand_separator( $args['currency'] );
206 $decimal_separator = give_get_price_decimal_separator( $args['currency'] );
207 $number_decimals = is_bool( $args['number_decimals'] ) ?
208 give_get_price_decimals() :
209 $args['number_decimals'];
210
211 // Explode number by . decimal separator.
212 $number_parts = explode( '.', $number );
213
214 // Remove currency symbols from number if any.
215 $number = trim( str_replace( give_currency_symbols( true ), '', $number ) );
216
217 if (
218 // Non formatted number.
219 (
220 ( false === strpos( $number, $thousand_separator ) ) &&
221 ( false === strpos( $number, $decimal_separator ) )
222 ) ||
223
224 // Decimal formatted number.
225 // If number of decimal place set to non zero and
226 // number only contains `.` as separator, precision set to less then or equal to number of decimal
227 // then number will be consider as decimal formatted which means number is already sanitized.
228 (
229 $number_decimals &&
230 '.' === $thousand_separator &&
231 false !== strpos( $number, $thousand_separator ) &&
232 false === strpos( $number, $decimal_separator ) &&
233 2 === count( $number_parts ) &&
234 ( $number_decimals >= strlen( $number_parts[1] ) )
235 )
236 ) {
237 return number_format( $number, $number_decimals, '.', '' );
238 }
239
240 // Handle thousand separator as '.'
241 // Handle sanitize database values.
242 $is_db_sanitize_val = ( 2 === count( $number_parts ) &&
243 is_numeric( $number_parts[0] ) &&
244 is_numeric( $number_parts[1] ) &&
245 ( 6 === strlen( $number_parts[1] ) ) );
246
247 if ( $is_db_sanitize_val ) {
248 // Sanitize database value.
249 return number_format( $number, $number_decimals, '.', '' );
250
251 } elseif (
252 '.' === $thousand_separator &&
253 false !== strpos( $number, $thousand_separator )
254 ) {
255 // Fix point thousand separator value.
256 $number = str_replace( '.', '', $number );
257 }
258
259 return give_sanitize_amount( $number, $args );
260 }
261
262 /**
263 * Sanitize Amount
264 *
265 * Note: Do not this function to sanitize amount instead use give_maybe_sanitize_amount function.
266 *
267 * Returns a sanitized amount by stripping out thousands separators.
268 *
269 * @since 1.0
270 *
271 * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
272 * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
273 *
274 * @return string $amount Newly sanitized amount
275 */
276 function give_sanitize_amount( $number, $args = array() ) {
277
278 // Bailout.
279 if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
280 return $number;
281 }
282
283 // Get function arguments.
284 $func_args = func_get_args();
285
286 // Backward compatibility.
287 if ( isset( $func_args[1] ) && ( is_bool( $func_args[1] ) || is_numeric( $func_args[1] ) ) ) {
288 $args = array(
289 'number_decimals' => $func_args[1],
290 'trim_zeros' => isset( $func_args[2] ) ? $func_args[2] : false,
291 );
292 }
293
294 $args = wp_parse_args(
295 $args,
296 array(
297 'number_decimals' => false,
298 'trim_zeros' => false,
299 'currency' => give_get_currency(),
300 )
301 );
302
303 // Remove slash from amount.
304 // If thousand or decimal separator is set to ' then in $_POST or $_GET param we will get an escaped number.
305 // To prevent notices and warning remove slash from amount/number.
306 $number = wp_unslash( $number );
307
308 $thousand_separator = give_get_price_thousand_separator( $args['currency'] );
309
310 $locale = localeconv();
311 $decimals = array(
312 give_get_price_decimal_separator( $args['currency'] ),
313 $locale['decimal_point'],
314 $locale['mon_decimal_point'],
315 );
316
317 // Remove locale from string
318 if ( ! is_float( $number ) ) {
319 $number = str_replace( $decimals, '.', $number );
320 }
321
322 // Remove thousand amount formatting if amount has.
323 // This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db.
324 // Do not replace thousand separator from price if it is same as decimal separator, because it will be already replace by above code.
325 if ( ! in_array( $thousand_separator, $decimals ) && ( false !== strpos( $number, $thousand_separator ) ) ) {
326 $number = str_replace( $thousand_separator, '', $number );
327 } elseif ( in_array( $thousand_separator, $decimals ) ) {
328 $number = preg_replace( '/\.(?=.*\.)/', '', $number );
329 }
330
331 // Remove non numeric entity before decimal separator.
332 $number = preg_replace( '/[^0-9\.]/', '', $number );
333 $default_dp = give_get_price_decimals( $args['currency'] );
334
335 // Reset negative amount to zero.
336 if ( 0 > $number ) {
337 $number = number_format( 0, $default_dp, '.' );
338 }
339
340 // If number does not have decimal then add number of decimals to it.
341 if (
342 false === strpos( $number, '.' )
343 || ( $default_dp > strlen( substr( $number, strpos( $number, '.' ) + 1 ) ) )
344 ) {
345 $number = number_format( $number, $default_dp, '.', '' );
346 }
347
348 // Format number by custom number of decimals.
349 if ( false !== $args['number_decimals'] ) {
350 $dp = intval( is_bool( $args['number_decimals'] ) ? $default_dp : $args['number_decimals'] );
351 $dp = apply_filters( 'give_sanitize_amount_decimals', $dp, $number );
352 $number = number_format( floatval( $number ), $dp, '.', '' );
353 }
354
355 // Trim zeros.
356 if ( $args['trim_zeros'] && strstr( $number, '.' ) ) {
357 $number = rtrim( rtrim( $number, '0' ), '.' );
358 }
359
360 /**
361 * Filter the sanitize amount
362 *
363 * @since 1.0
364 */
365 return apply_filters( 'give_sanitize_amount', $number );
366 }
367
368 /**
369 * Returns a nicely formatted amount.
370 *
371 * @since 1.0
372 *
373 * @param string $amount Price amount to format
374 * @param array $args Array of arguments.
375 *
376 * @return string $amount Newly formatted amount or Price Not Available
377 */
378 function give_format_amount( $amount, $args = array() ) {
379 // Backward compatibility.
380 if ( is_bool( $args ) ) {
381 $args = array(
382 'decimal' => $args,
383 );
384 }
385
386 $default_args = array(
387 'decimal' => true,
388 'sanitize' => true,
389 'donation_id' => 0,
390 'currency' => '',
391 );
392
393 $args = wp_parse_args( $args, $default_args );
394
395 // Set Currency based on donation id, if required.
396 if ( $args['donation_id'] && empty( $args['currency'] ) ) {
397 $donation_meta = give_get_meta( $args['donation_id'], '_give_payment_meta', true );
398 $args['currency'] = $donation_meta['currency'];
399 }
400
401 $formatted = 0;
402 $currency = ! empty( $args['currency'] ) ? $args['currency'] : give_get_currency( $args['donation_id'] );
403 $thousands_sep = give_get_price_thousand_separator( $currency );
404 $decimal_sep = give_get_price_decimal_separator( $currency );
405 $decimals = ! empty( $args['decimal'] ) ? give_get_price_decimals( $currency ) : 0;
406
407 if ( ! empty( $amount ) ) {
408 // Sanitize amount before formatting.
409 $amount = ! empty( $args['sanitize'] ) ?
410 give_maybe_sanitize_amount( $amount, array( 'number_decimals' => $decimals, 'currency' => $currency ) ) :
411 number_format( $amount, $decimals, '.', '' );
412
413 switch ( $currency ) {
414 case 'INR':
415 $decimal_amount = '';
416
417 // Extract decimals from amount
418 if ( ( $pos = strpos( $amount, '.' ) ) !== false ) {
419 if ( ! empty( $decimals ) ) {
420 $decimal_amount = substr( round( substr( $amount, $pos ), $decimals ), 1 );
421 $amount = substr( $amount, 0, $pos );
422
423 if ( ! $decimal_amount ) {
424 $decimal_amount = substr( "{$decimal_sep}0000000000", 0, ( $decimals + 1 ) );
425 } elseif ( ( $decimals + 1 ) > strlen( $decimal_amount ) ) {
426 $decimal_amount = substr( "{$decimal_amount}000000000", 0, ( $decimals + 1 ) );
427 }
428 } else {
429 $amount = number_format( $amount, $decimals, $decimal_sep, '' );
430 }
431 }
432
433 // Extract last 3 from amount
434 $result = substr( $amount, - 3 );
435 $amount = substr( $amount, 0, - 3 );
436
437 // Apply digits 2 by 2
438 while ( strlen( $amount ) > 0 ) {
439 $result = substr( $amount, - 2 ) . $thousands_sep . $result;
440 $amount = substr( $amount, 0, - 2 );
441 }
442
443 $formatted = $result . $decimal_amount;
444 break;
445
446 default:
447 $formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
448 }
449 }
450
451 /**
452 * Filter the formatted amount
453 *
454 * @since 1.0
455 */
456 return apply_filters( 'give_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $currency, $args );
457 }
458
459
460 /**
461 * Get human readable amount.
462 *
463 * Note: This function only support large number formatting from million to trillion
464 *
465 * @since 1.6
466 *
467 * @use give_get_price_thousand_separator Get thousand separator.
468 *
469 * @param string $amount formatted amount number.
470 * @param array $args Array of arguments.
471 *
472 * @return string formatted amount number with large number names.
473 */
474 function give_human_format_large_amount( $amount, $args = array() ) {
475 // Sanitize amount.
476 $sanitize_amount = give_maybe_sanitize_amount( $amount );
477
478 // Bailout.
479 if ( ! floatval( $sanitize_amount ) ) {
480 return '0';
481 };
482
483 // Set default currency;
484 if ( empty( $args['currency'] ) ) {
485 $args['currency'] = give_get_currency();
486 }
487
488 // Get thousand separator.
489 $thousands_sep = give_get_price_thousand_separator();
490
491 // Explode amount to calculate name of large numbers.
492 $amount_array = explode( $thousands_sep, $amount );
493
494 // Calculate amount parts count.
495 $amount_count_parts = count( $amount_array );
496
497 // Human format amount (default).
498 $human_format_amount = $amount;
499
500 switch ( $args['currency'] ) {
501 case 'INR':
502 // Calculate large number formatted amount.
503 if ( 4 < $amount_count_parts ) {
504 $human_format_amount = sprintf( esc_html__( '%s arab', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
505 } elseif ( 3 < $amount_count_parts ) {
506 $human_format_amount = sprintf( esc_html__( '%s crore', 'give' ), round( ( $sanitize_amount / 10000000 ), 2 ) );
507 } elseif ( 2 < $amount_count_parts ) {
508 $human_format_amount = sprintf( esc_html__( '%s lakh', 'give' ), round( ( $sanitize_amount / 100000 ), 2 ) );
509 }
510 break;
511 default:
512 // Calculate large number formatted amount.
513 if ( 4 < $amount_count_parts ) {
514 $human_format_amount = sprintf( esc_html__( '%s trillion', 'give' ), round( ( $sanitize_amount / 1000000000000 ), 2 ) );
515 } elseif ( 3 < $amount_count_parts ) {
516 $human_format_amount = sprintf( esc_html__( '%s billion', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
517 } elseif ( 2 < $amount_count_parts ) {
518 $human_format_amount = sprintf( esc_html__( '%s million', 'give' ), round( ( $sanitize_amount / 1000000 ), 2 ) );
519 }
520 }
521
522 return apply_filters( 'give_human_format_large_amount', $human_format_amount, $amount, $sanitize_amount );
523 }
524
525 /**
526 * Returns a nicely formatted amount with custom decimal separator.
527 *
528 * @since 1.0
529 *
530 * @param int|float|string $amount Formatted or sanitized price
531 * @param int|bool $dp number of decimals
532 * @param bool $sanitize Whether or not sanitize number
533 *
534 * @return string $amount Newly formatted amount or Price Not Available
535 */
536 function give_format_decimal( $amount, $dp = false, $sanitize = true ) {
537 $decimal_separator = give_get_price_decimal_separator();
538 $formatted_amount = $sanitize ?
539 give_maybe_sanitize_amount( $amount, array( 'number_decimals' => $dp ) ) :
540 number_format( $amount, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' );
541
542 if ( false !== strpos( $formatted_amount, '.' ) ) {
543 $formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount );
544 }
545
546 return apply_filters( 'give_format_decimal', $formatted_amount, $amount, $decimal_separator );
547 }
548
549 /**
550 * Get date format string on basis of given context.
551 *
552 * @since 1.7
553 *
554 * @param string $date_context Date format context name.
555 *
556 * @return string Date format string
557 */
558 function give_date_format( $date_context = '' ) {
559 /**
560 * Filter the date context
561 *
562 * You can add your own date context or use already exist context.
563 * For example:
564 * add_filter( 'give_date_format_contexts', 'add_new_date_contexts' );
565 * function add_new_date_contexts( $date_format_contexts ) {
566 * // You can add single context like this $date_format_contexts['checkout'] = 'F j, Y';
567 * // Instead add multiple date context at once.
568 * $new_date_format_contexts = array(
569 * 'checkout' => 'F j, Y',
570 * 'report' => 'Y-m-d',
571 * 'email' => 'm/d/Y',
572 * );
573 *
574 * // Merge date contexts array only if you are adding multiple date contexts at once otherwise return $date_format_contexts.
575 * return array_merge( $new_date_format_contexts, $date_format_contexts );
576 *
577 * }
578 */
579 $date_format_contexts = apply_filters( 'give_date_format_contexts', array() );
580
581 // Set date format to default date format.
582 $date_format = get_option( 'date_format' );
583
584 // Update date format if we have non empty date format context array and non empty date format string for that context.
585 if ( $date_context && ! empty( $date_format_contexts ) && array_key_exists( $date_context, $date_format_contexts ) ) {
586 $date_format = ! empty( $date_format_contexts[ $date_context ] )
587 ? $date_format_contexts[ $date_context ]
588 : $date_format;
589 }
590
591 return apply_filters( 'give_date_format', $date_format );
592 }
593
594 /**
595 * Get cache key.
596 *
597 * @since 1.7
598 * @deprecated 1.8.7 You can access this function from Give_Cache.
599 *
600 * @param string $action Cache key prefix.
601 * @param array $query_args Query array.
602 *
603 * @return string
604 */
605 function give_get_cache_key( $action, $query_args ) {
606 return Give_Cache::get_key( $action, $query_args );
607 }
608
609 /**
610 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
611 * Non-scalar values are ignored.
612 *
613 * @since 1.8
614 *
615 * @param string|array $var
616 *
617 * @return string|array
618 */
619 function give_clean( $var ) {
620 if ( is_array( $var ) ) {
621 return array_map( 'give_clean', $var );
622 } else {
623 return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
624 }
625 }
626
627 /**
628 * Transforms php.ini notation for numbers (like '2M') to an integer.
629 *
630 * @since 1.8
631 *
632 * @param $size
633 *
634 * @return int
635 */
636 function give_let_to_num( $size ) {
637 $l = substr( $size, - 1 );
638 $ret = substr( $size, 0, - 1 );
639 switch ( strtoupper( $l ) ) {
640 case 'P':
641 $ret *= 1024;
642 case 'T':
643 $ret *= 1024;
644 case 'G':
645 $ret *= 1024;
646 case 'M':
647 $ret *= 1024;
648 case 'K':
649 $ret *= 1024;
650 }
651
652 return $ret;
653 }
654
655 /**
656 * Verify nonce.
657 *
658 * @since 1.8
659 *
660 * @param $nonce
661 * @param int $action
662 * @param array $wp_die_args
663 *
664 * @return bool
665 */
666 function give_validate_nonce( $nonce, $action = - 1, $wp_die_args = array() ) {
667
668 // Verify nonce.
669 $verify_nonce = wp_verify_nonce( $nonce, $action );
670
671 // On ajax request send nonce verification status.
672 if ( wp_doing_ajax() ) {
673 return $verify_nonce;
674 }
675
676 if ( ! $verify_nonce ) {
677 $wp_die_args = wp_parse_args(
678 $wp_die_args,
679 array(
680 'message' => __( 'Nonce verification has failed.', 'give' ),
681 'title' => __( 'Error', 'give' ),
682 'args' => array(
683 'response' => 403,
684 )
685 )
686 );
687
688 wp_die(
689 $wp_die_args['message'],
690 $wp_die_args['title'],
691 $wp_die_args['args']
692 );
693 }
694
695 return true;
696 }
697
698 /**
699 * Verify nonce while processing donation form.
700 *
701 * @since 2.0
702 *
703 * @param string $nonce Pass nonce value.
704 *
705 * @return bool
706 */
707 function give_verify_donation_form_nonce( $nonce = '' ) {
708 // Get nonce key from donation.
709 $nonce = empty( $nonce ) ? give_clean( $_POST['_wpnonce'] ) : $nonce;
710 $form_id = isset( $_POST['give-form-id'] ) ? absint( $_POST['give-form-id'] ) : 0;
711
712 // Form nonce action.
713 $nonce_action = "donation_form_nonce_{$form_id}";
714
715 // Nonce validation.
716 $verify_nonce = give_validate_nonce( $nonce, $nonce_action );
717
718 if ( ! $verify_nonce ) {
719 give_set_error( 'donation_form_nonce', __( 'Nonce verification has failed.', 'give' ) );
720 }
721
722 return $verify_nonce;
723 }
724
725 /**
726 * Check variable and get default or valid value.
727 *
728 * Helper function to check if a variable is set, empty, etc.
729 *
730 * @since 1.8
731 *
732 * @param $variable
733 * @param string (optional) $conditional default value: isset
734 * @param mixed (optional) $default default value: false
735 * @param string (optional) $array_key_name default value: false
736 *
737 * @return mixed
738 */
739 function give_check_variable( $variable, $conditional = '', $default = false, $array_key_name = '' ) {
740 // Get value from array if array key non empty.
741 if( empty( $array_key_name ) ) {
742 switch ( $conditional ) {
743 case 'isset_empty':
744 $variable = ( isset( $variable ) && ! empty( $variable ) ) ? $variable : $default;
745 break;
746
747 case 'empty':
748 $variable = ! empty( $variable ) ? $variable : $default;
749 break;
750
751 case 'null':
752 $variable = ! is_null( $variable ) ? $variable : $default;
753 break;
754
755 default:
756 $variable = isset( $variable ) ? $variable : $default;
757 }
758 } else {
759 $isset = array_key_exists( $array_key_name, $variable );
760
761 switch ( $conditional ) {
762 case 'isset_empty':
763 $variable = ( $isset && ! empty( $variable[ $array_key_name ] ) ) ? $variable[ $array_key_name ] : $default;
764 break;
765
766 case 'empty':
767 $variable = ! empty( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
768 break;
769
770 case 'null':
771 $variable = $isset && ! is_null( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
772 break;
773
774 default:
775 $variable = $isset && isset( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
776 }
777 }
778
779 return $variable;
780
781 }
782