PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.1.0
GiveWP – Donation Plugin and Fundraising Platform v2.1.0
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-sequential-ordering.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-scripts.php 8 years ago class-give-session.php 8 years ago class-give-stats.php 8 years ago class-give-template-loader.php 8 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 shortcodes.php 8 years ago template-functions.php 8 years ago user-functions.php 8 years ago
formatting.php
781 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 $currency = give_get_meta( $id_or_currency_code, '_give_payment_currency', true );
43
44 if (
45 ! empty( $currency) &&
46 $give_options['currency'] !== $currency
47 ) {
48 $setting = $currencies[ $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 $args['currency'] = give_get_meta( $args['donation_id'], '_give_payment_currency', true );
398 }
399
400 $formatted = 0;
401 $currency = ! empty( $args['currency'] ) ? $args['currency'] : give_get_currency( $args['donation_id'] );
402 $thousands_sep = give_get_price_thousand_separator( $currency );
403 $decimal_sep = give_get_price_decimal_separator( $currency );
404 $decimals = ! empty( $args['decimal'] ) ? give_get_price_decimals( $currency ) : 0;
405
406 if ( ! empty( $amount ) ) {
407 // Sanitize amount before formatting.
408 $amount = ! empty( $args['sanitize'] ) ?
409 give_maybe_sanitize_amount( $amount, array( 'number_decimals' => $decimals, 'currency' => $currency ) ) :
410 number_format( $amount, $decimals, '.', '' );
411
412 switch ( $currency ) {
413 case 'INR':
414 $decimal_amount = '';
415
416 // Extract decimals from amount
417 if ( ( $pos = strpos( $amount, '.' ) ) !== false ) {
418 if ( ! empty( $decimals ) ) {
419 $decimal_amount = substr( round( substr( $amount, $pos ), $decimals ), 1 );
420 $amount = substr( $amount, 0, $pos );
421
422 if ( ! $decimal_amount ) {
423 $decimal_amount = substr( "{$decimal_sep}0000000000", 0, ( $decimals + 1 ) );
424 } elseif ( ( $decimals + 1 ) > strlen( $decimal_amount ) ) {
425 $decimal_amount = substr( "{$decimal_amount}000000000", 0, ( $decimals + 1 ) );
426 }
427 } else {
428 $amount = number_format( $amount, $decimals, $decimal_sep, '' );
429 }
430 }
431
432 // Extract last 3 from amount
433 $result = substr( $amount, - 3 );
434 $amount = substr( $amount, 0, - 3 );
435
436 // Apply digits 2 by 2
437 while ( strlen( $amount ) > 0 ) {
438 $result = substr( $amount, - 2 ) . $thousands_sep . $result;
439 $amount = substr( $amount, 0, - 2 );
440 }
441
442 $formatted = $result . $decimal_amount;
443 break;
444
445 default:
446 $formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
447 }
448 }
449
450 /**
451 * Filter the formatted amount
452 *
453 * @since 1.0
454 */
455 return apply_filters( 'give_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $currency, $args );
456 }
457
458
459 /**
460 * Get human readable amount.
461 *
462 * Note: This function only support large number formatting from million to trillion
463 *
464 * @since 1.6
465 *
466 * @use give_get_price_thousand_separator Get thousand separator.
467 *
468 * @param string $amount formatted amount number.
469 * @param array $args Array of arguments.
470 *
471 * @return string formatted amount number with large number names.
472 */
473 function give_human_format_large_amount( $amount, $args = array() ) {
474 // Sanitize amount.
475 $sanitize_amount = give_maybe_sanitize_amount( $amount );
476
477 // Bailout.
478 if ( ! floatval( $sanitize_amount ) ) {
479 return '0';
480 };
481
482 // Set default currency;
483 if ( empty( $args['currency'] ) ) {
484 $args['currency'] = give_get_currency();
485 }
486
487 // Get thousand separator.
488 $thousands_sep = give_get_price_thousand_separator();
489
490 // Explode amount to calculate name of large numbers.
491 $amount_array = explode( $thousands_sep, $amount );
492
493 // Calculate amount parts count.
494 $amount_count_parts = count( $amount_array );
495
496 // Human format amount (default).
497 $human_format_amount = $amount;
498
499 switch ( $args['currency'] ) {
500 case 'INR':
501 // Calculate large number formatted amount.
502 if ( 4 < $amount_count_parts ) {
503 $human_format_amount = sprintf( esc_html__( '%s arab', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
504 } elseif ( 3 < $amount_count_parts ) {
505 $human_format_amount = sprintf( esc_html__( '%s crore', 'give' ), round( ( $sanitize_amount / 10000000 ), 2 ) );
506 } elseif ( 2 < $amount_count_parts ) {
507 $human_format_amount = sprintf( esc_html__( '%s lakh', 'give' ), round( ( $sanitize_amount / 100000 ), 2 ) );
508 }
509 break;
510 default:
511 // Calculate large number formatted amount.
512 if ( 4 < $amount_count_parts ) {
513 $human_format_amount = sprintf( esc_html__( '%s trillion', 'give' ), round( ( $sanitize_amount / 1000000000000 ), 2 ) );
514 } elseif ( 3 < $amount_count_parts ) {
515 $human_format_amount = sprintf( esc_html__( '%s billion', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
516 } elseif ( 2 < $amount_count_parts ) {
517 $human_format_amount = sprintf( esc_html__( '%s million', 'give' ), round( ( $sanitize_amount / 1000000 ), 2 ) );
518 }
519 }
520
521 return apply_filters( 'give_human_format_large_amount', $human_format_amount, $amount, $sanitize_amount );
522 }
523
524 /**
525 * Returns a nicely formatted amount with custom decimal separator.
526 *
527 * @since 1.0
528 *
529 * @param int|float|string $amount Formatted or sanitized price
530 * @param int|bool $dp number of decimals
531 * @param bool $sanitize Whether or not sanitize number
532 *
533 * @return string $amount Newly formatted amount or Price Not Available
534 */
535 function give_format_decimal( $amount, $dp = false, $sanitize = true ) {
536 $decimal_separator = give_get_price_decimal_separator();
537 $formatted_amount = $sanitize ?
538 give_maybe_sanitize_amount( $amount, array( 'number_decimals' => $dp ) ) :
539 number_format( $amount, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' );
540
541 if ( false !== strpos( $formatted_amount, '.' ) ) {
542 $formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount );
543 }
544
545 return apply_filters( 'give_format_decimal', $formatted_amount, $amount, $decimal_separator );
546 }
547
548 /**
549 * Get date format string on basis of given context.
550 *
551 * @since 1.7
552 *
553 * @param string $date_context Date format context name.
554 *
555 * @return string Date format string
556 */
557 function give_date_format( $date_context = '' ) {
558 /**
559 * Filter the date context
560 *
561 * You can add your own date context or use already exist context.
562 * For example:
563 * add_filter( 'give_date_format_contexts', 'add_new_date_contexts' );
564 * function add_new_date_contexts( $date_format_contexts ) {
565 * // You can add single context like this $date_format_contexts['checkout'] = 'F j, Y';
566 * // Instead add multiple date context at once.
567 * $new_date_format_contexts = array(
568 * 'checkout' => 'F j, Y',
569 * 'report' => 'Y-m-d',
570 * 'email' => 'm/d/Y',
571 * );
572 *
573 * // Merge date contexts array only if you are adding multiple date contexts at once otherwise return $date_format_contexts.
574 * return array_merge( $new_date_format_contexts, $date_format_contexts );
575 *
576 * }
577 */
578 $date_format_contexts = apply_filters( 'give_date_format_contexts', array() );
579
580 // Set date format to default date format.
581 $date_format = get_option( 'date_format' );
582
583 // Update date format if we have non empty date format context array and non empty date format string for that context.
584 if ( $date_context && ! empty( $date_format_contexts ) && array_key_exists( $date_context, $date_format_contexts ) ) {
585 $date_format = ! empty( $date_format_contexts[ $date_context ] )
586 ? $date_format_contexts[ $date_context ]
587 : $date_format;
588 }
589
590 return apply_filters( 'give_date_format', $date_format );
591 }
592
593 /**
594 * Get cache key.
595 *
596 * @since 1.7
597 * @deprecated 1.8.7 You can access this function from Give_Cache.
598 *
599 * @param string $action Cache key prefix.
600 * @param array $query_args Query array.
601 *
602 * @return string
603 */
604 function give_get_cache_key( $action, $query_args ) {
605 return Give_Cache::get_key( $action, $query_args );
606 }
607
608 /**
609 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
610 * Non-scalar values are ignored.
611 *
612 * @since 1.8
613 *
614 * @param string|array $var
615 *
616 * @return string|array
617 */
618 function give_clean( $var ) {
619 if ( is_array( $var ) ) {
620 return array_map( 'give_clean', $var );
621 } else {
622 return is_scalar( $var ) ? sanitize_text_field( wp_unslash( $var ) ) : $var;
623 }
624 }
625
626 /**
627 * Transforms php.ini notation for numbers (like '2M') to an integer.
628 *
629 * @since 1.8
630 *
631 * @param $size
632 *
633 * @return int
634 */
635 function give_let_to_num( $size ) {
636 $l = substr( $size, - 1 );
637 $ret = substr( $size, 0, - 1 );
638 switch ( strtoupper( $l ) ) {
639 case 'P':
640 $ret *= 1024;
641 case 'T':
642 $ret *= 1024;
643 case 'G':
644 $ret *= 1024;
645 case 'M':
646 $ret *= 1024;
647 case 'K':
648 $ret *= 1024;
649 }
650
651 return $ret;
652 }
653
654 /**
655 * Verify nonce.
656 *
657 * @since 1.8
658 *
659 * @param $nonce
660 * @param int $action
661 * @param array $wp_die_args
662 *
663 * @return bool
664 */
665 function give_validate_nonce( $nonce, $action = - 1, $wp_die_args = array() ) {
666
667 // Verify nonce.
668 $verify_nonce = wp_verify_nonce( $nonce, $action );
669
670 // On ajax request send nonce verification status.
671 if ( wp_doing_ajax() ) {
672 return $verify_nonce;
673 }
674
675 if ( ! $verify_nonce ) {
676 $wp_die_args = wp_parse_args(
677 $wp_die_args,
678 array(
679 'message' => __( 'Nonce verification has failed.', 'give' ),
680 'title' => __( 'Error', 'give' ),
681 'args' => array(
682 'response' => 403,
683 )
684 )
685 );
686
687 wp_die(
688 $wp_die_args['message'],
689 $wp_die_args['title'],
690 $wp_die_args['args']
691 );
692 }
693
694 return true;
695 }
696
697 /**
698 * Verify nonce while processing donation form.
699 *
700 * @since 2.0
701 *
702 * @param string $nonce Pass nonce value.
703 *
704 * @return bool
705 */
706 function give_verify_donation_form_nonce( $nonce = '' ) {
707 // Get nonce key from donation.
708 $nonce = empty( $nonce ) ? give_clean( $_POST['_wpnonce'] ) : $nonce;
709 $form_id = isset( $_POST['give-form-id'] ) ? absint( $_POST['give-form-id'] ) : 0;
710
711 // Form nonce action.
712 $nonce_action = "donation_form_nonce_{$form_id}";
713
714 // Nonce validation.
715 $verify_nonce = give_validate_nonce( $nonce, $nonce_action );
716
717 if ( ! $verify_nonce ) {
718 give_set_error( 'donation_form_nonce', __( 'Nonce verification has failed.', 'give' ) );
719 }
720
721 return $verify_nonce;
722 }
723
724 /**
725 * Check variable and get default or valid value.
726 *
727 * Helper function to check if a variable is set, empty, etc.
728 *
729 * @since 1.8
730 *
731 * @param $variable
732 * @param string (optional) $conditional default value: isset
733 * @param mixed (optional) $default default value: false
734 * @param string (optional) $array_key_name default value: false
735 *
736 * @return mixed
737 */
738 function give_check_variable( $variable, $conditional = '', $default = false, $array_key_name = '' ) {
739 // Get value from array if array key non empty.
740 if( empty( $array_key_name ) ) {
741 switch ( $conditional ) {
742 case 'isset_empty':
743 $variable = ( isset( $variable ) && ! empty( $variable ) ) ? $variable : $default;
744 break;
745
746 case 'empty':
747 $variable = ! empty( $variable ) ? $variable : $default;
748 break;
749
750 case 'null':
751 $variable = ! is_null( $variable ) ? $variable : $default;
752 break;
753
754 default:
755 $variable = isset( $variable ) ? $variable : $default;
756 }
757 } else {
758 $isset = array_key_exists( $array_key_name, $variable );
759
760 switch ( $conditional ) {
761 case 'isset_empty':
762 $variable = ( $isset && ! empty( $variable[ $array_key_name ] ) ) ? $variable[ $array_key_name ] : $default;
763 break;
764
765 case 'empty':
766 $variable = ! empty( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
767 break;
768
769 case 'null':
770 $variable = $isset && ! is_null( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
771 break;
772
773 default:
774 $variable = $isset && isset( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
775 }
776 }
777
778 return $variable;
779
780 }
781