PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.2.2
GiveWP – Donation Plugin and Fundraising Platform v2.2.2
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 7 years ago api 7 years ago deprecated 7 years ago donors 7 years ago emails 7 years ago forms 7 years ago gateways 7 years ago libraries 7 years ago payments 7 years ago actions.php 7 years ago ajax-functions.php 7 years ago class-give-async-process.php 8 years ago class-give-background-updater.php 7 years ago class-give-cache.php 7 years ago class-give-cli-commands.php 8 years ago class-give-comment.php 7 years ago class-give-cron.php 8 years ago class-give-db-donor-meta.php 8 years ago class-give-db-donors.php 7 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 7 years ago class-give-db-meta.php 7 years ago class-give-db-payment-meta.php 7 years ago class-give-db-sequential-ordering.php 7 years ago class-give-db-sessions.php 7 years ago class-give-db.php 8 years ago class-give-donate-form.php 8 years ago class-give-donor-wall-widget.php 7 years ago class-give-donor.php 7 years ago class-give-email-access.php 7 years ago class-give-html-elements.php 7 years ago class-give-license-handler.php 7 years ago class-give-logging.php 8 years ago class-give-readme-parser.php 8 years ago class-give-roles.php 8 years ago class-give-scripts.php 7 years ago class-give-session.php 7 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 7 years ago country-functions.php 8 years ago currency-functions.php 7 years ago error-tracking.php 8 years ago filters.php 7 years ago formatting.php 7 years ago import-functions.php 7 years ago install.php 7 years ago login-register.php 8 years ago misc-functions.php 7 years ago plugin-compatibility.php 8 years ago post-types.php 8 years ago price-functions.php 7 years ago process-donation.php 7 years ago shortcodes.php 7 years ago template-functions.php 8 years ago user-functions.php 7 years ago
formatting.php
780 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 * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
164 *
165 * @return string $amount Newly sanitized amount
166 */
167 function give_sanitize_amount_for_db( $number, $args = array() ) {
168 $args['number_decimals'] = 6;
169
170 return give_maybe_sanitize_amount( $number, $args );
171 }
172
173 /**
174 * Sanitize Amount before saving to database
175 *
176 * @since 1.8.12
177 *
178 * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
179 * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
180 *
181 * @return string $amount Newly sanitized amount
182 */
183 function give_maybe_sanitize_amount( $number, $args = array() ) {
184 // Bailout.
185 if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
186 return $number;
187 }
188
189 $func_args = func_get_args();
190
191 // Backward compatibility.
192 if ( isset( $func_args[1] ) && ( is_bool( $func_args[1] ) || is_numeric( $func_args[1] ) ) ) {
193 $args = array(
194 'number_decimals' => $func_args[1],
195 'trim_zeros' => isset( $func_args[2] ) ? $func_args[2] : false,
196 );
197 }
198
199 $args = wp_parse_args(
200 $args,
201 array(
202 'number_decimals' => false,
203 'trim_zeros' => false,
204 'currency' => give_get_currency(),
205 )
206 );
207
208 $thousand_separator = give_get_price_thousand_separator( $args['currency'] );
209 $decimal_separator = give_get_price_decimal_separator( $args['currency'] );
210 $number_decimals = is_bool( $args['number_decimals'] ) ?
211 give_get_price_decimals() :
212 $args['number_decimals'];
213
214 // Explode number by . decimal separator.
215 $number_parts = explode( '.', $number );
216
217 // Remove currency symbols from number if any.
218 $number = trim( str_replace( give_currency_symbols( true ), '', $number ) );
219
220 if (
221 // Non formatted number.
222 false === strpos( $number, $thousand_separator )
223 && false === strpos( $number, $decimal_separator )
224 ) {
225 return number_format( $number, $number_decimals, '.', '' );
226 } elseif (
227 // Decimal formatted number.
228 // If number of decimal place set to non zero and
229 // number only contains `.` as separator, precision set to less then or equal to number of decimal
230 // then number will be consider as decimal formatted which means number is already sanitized.
231 $number_decimals
232 && '.' === $thousand_separator
233 && false !== strpos( $number, $thousand_separator )
234 && false === strpos( $number, $decimal_separator )
235 && 2 === count( $number_parts )
236 && ( $number_decimals >= strlen( $number_parts[1] ) )
237 ){
238 return number_format( $number, $number_decimals, '.', '' );
239 }
240
241 // Handle thousand separator as '.'
242 // Handle sanitize database values.
243 $is_db_sanitize_val = ( 2 === count( $number_parts ) &&
244 is_numeric( $number_parts[0] ) &&
245 is_numeric( $number_parts[1] ) &&
246 ( 6 === strlen( $number_parts[1] ) ) );
247
248 if ( $is_db_sanitize_val ) {
249 // Sanitize database value.
250 return number_format( $number, $number_decimals, '.', '' );
251
252 } elseif (
253 '.' === $thousand_separator &&
254 false !== strpos( $number, $thousand_separator )
255 ) {
256 // Fix point thousand separator value.
257 $number = str_replace( '.', '', $number );
258 }
259
260 return give_sanitize_amount( $number, $args );
261 }
262
263 /**
264 * Sanitize Amount
265 *
266 * Note: Do not this function to sanitize amount instead use give_maybe_sanitize_amount function.
267 *
268 * Returns a sanitized amount by stripping out thousands separators.
269 *
270 * @since 1.0
271 *
272 * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
273 * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
274 *
275 * @return string $amount Newly sanitized amount
276 */
277 function give_sanitize_amount( $number, $args = array() ) {
278
279 // Bailout.
280 if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
281 return $number;
282 }
283
284 // Get function arguments.
285 $func_args = func_get_args();
286
287 // Backward compatibility.
288 if ( isset( $func_args[1] ) && ( is_bool( $func_args[1] ) || is_numeric( $func_args[1] ) ) ) {
289 $args = array(
290 'number_decimals' => $func_args[1],
291 'trim_zeros' => isset( $func_args[2] ) ? $func_args[2] : false,
292 );
293 }
294
295 $args = wp_parse_args(
296 $args,
297 array(
298 'number_decimals' => false,
299 'trim_zeros' => false,
300 'currency' => give_get_currency(),
301 )
302 );
303
304 // Remove slash from amount.
305 // If thousand or decimal separator is set to ' then in $_POST or $_GET param we will get an escaped number.
306 // To prevent notices and warning remove slash from amount/number.
307 $number = wp_unslash( $number );
308
309 $thousand_separator = give_get_price_thousand_separator( $args['currency'] );
310
311 $locale = localeconv();
312 $decimals = array(
313 give_get_price_decimal_separator( $args['currency'] ),
314 $locale['decimal_point'],
315 $locale['mon_decimal_point'],
316 );
317
318 // Remove locale from string
319 if ( ! is_float( $number ) ) {
320 $number = str_replace( $decimals, '.', $number );
321 }
322
323 // Remove thousand amount formatting if amount has.
324 // This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db.
325 // Do not replace thousand separator from price if it is same as decimal separator, because it will be already replace by above code.
326 if ( ! in_array( $thousand_separator, $decimals ) && ( false !== strpos( $number, $thousand_separator ) ) ) {
327 $number = str_replace( $thousand_separator, '', $number );
328 } elseif ( in_array( $thousand_separator, $decimals ) ) {
329 $number = preg_replace( '/\.(?=.*\.)/', '', $number );
330 }
331
332 // Remove non numeric entity before decimal separator.
333 $number = preg_replace( '/[^0-9\.]/', '', $number );
334 $default_dp = give_get_price_decimals( $args['currency'] );
335
336 // Reset negative amount to zero.
337 if ( 0 > $number ) {
338 $number = number_format( 0, $default_dp, '.' );
339 }
340
341 // If number does not have decimal then add number of decimals to it.
342 if (
343 false === strpos( $number, '.' )
344 || ( $default_dp > strlen( substr( $number, strpos( $number, '.' ) + 1 ) ) )
345 ) {
346 $number = number_format( $number, $default_dp, '.', '' );
347 }
348
349 // Format number by custom number of decimals.
350 if ( false !== $args['number_decimals'] ) {
351 $dp = intval( is_bool( $args['number_decimals'] ) ? $default_dp : $args['number_decimals'] );
352 $dp = apply_filters( 'give_sanitize_amount_decimals', $dp, $number );
353 $number = number_format( floatval( $number ), $dp, '.', '' );
354 }
355
356 // Trim zeros.
357 if ( $args['trim_zeros'] && strstr( $number, '.' ) ) {
358 $number = rtrim( rtrim( $number, '0' ), '.' );
359 }
360
361 /**
362 * Filter the sanitize amount
363 *
364 * @since 1.0
365 */
366 return apply_filters( 'give_sanitize_amount', $number );
367 }
368
369 /**
370 * Returns a nicely formatted amount.
371 *
372 * @since 1.0
373 *
374 * @param string $amount Price amount to format
375 * @param array $args Array of arguments.
376 *
377 * @return string $amount Newly formatted amount or Price Not Available
378 */
379 function give_format_amount( $amount, $args = array() ) {
380 // Backward compatibility.
381 if ( is_bool( $args ) ) {
382 $args = array(
383 'decimal' => $args,
384 );
385 }
386
387 $default_args = array(
388 'decimal' => true,
389 'sanitize' => true,
390 'donation_id' => 0,
391 'currency' => '',
392 );
393
394 $args = wp_parse_args( $args, $default_args );
395
396 // Set Currency based on donation id, if required.
397 if ( $args['donation_id'] && empty( $args['currency'] ) ) {
398 $args['currency'] = give_get_meta( $args['donation_id'], '_give_payment_currency', true );
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( wp_unslash( $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 string $nonce Nonce Hash.
661 * @param int $action Nonce verification action.
662 * @param array $wp_die_args Nonce fail arguments.
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 Nonce value.
704 * @param int $form_id Donation Form ID.
705 *
706 * @return bool
707 */
708 function give_verify_donation_form_nonce( $nonce = '', $form_id ) {
709
710 // Form nonce action.
711 $nonce_action = "give_donation_form_nonce_{$form_id}";
712
713 // Nonce validation.
714 $verify_nonce = give_validate_nonce( $nonce, $nonce_action );
715
716 if ( ! $verify_nonce ) {
717 give_set_error( 'donation_form_nonce', __( 'Nonce verification has failed.', 'give' ) );
718 }
719
720 return $verify_nonce;
721 }
722
723 /**
724 * Check variable and get default or valid value.
725 *
726 * Helper function to check if a variable is set, empty, etc.
727 *
728 * @since 1.8
729 *
730 * @param $variable
731 * @param string (optional) $conditional default value: isset
732 * @param mixed (optional) $default default value: false
733 * @param string (optional) $array_key_name default value: false
734 *
735 * @return mixed
736 */
737 function give_check_variable( $variable, $conditional = '', $default = false, $array_key_name = '' ) {
738 // Get value from array if array key non empty.
739 if( empty( $array_key_name ) ) {
740 switch ( $conditional ) {
741 case 'isset_empty':
742 $variable = ( isset( $variable ) && ! empty( $variable ) ) ? $variable : $default;
743 break;
744
745 case 'empty':
746 $variable = ! empty( $variable ) ? $variable : $default;
747 break;
748
749 case 'null':
750 $variable = ! is_null( $variable ) ? $variable : $default;
751 break;
752
753 default:
754 $variable = isset( $variable ) ? $variable : $default;
755 }
756 } else {
757 $isset = array_key_exists( $array_key_name, $variable );
758
759 switch ( $conditional ) {
760 case 'isset_empty':
761 $variable = ( $isset && ! empty( $variable[ $array_key_name ] ) ) ? $variable[ $array_key_name ] : $default;
762 break;
763
764 case 'empty':
765 $variable = ! empty( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
766 break;
767
768 case 'null':
769 $variable = $isset && ! is_null( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
770 break;
771
772 default:
773 $variable = $isset && isset( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
774 }
775 }
776
777 return $variable;
778
779 }
780