PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.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 All 255 releases
← All changes | includes/formatting.php +661 -526 2.3.04.16.9 View file →
@@ -2,86 +2,89 @@
2 2 /**
3 3 * Formatting functions for taking care of proper number formats and such
4 4 *
5 5 * @package Give
6 + * @since 1.0
7 + * @copyright Copyright (c) 2016, GiveWP
8 + * @license https://opensource.org/licenses/gpl-license GNU Public License
6 9 * @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 10 */
11 11
12 12 // Exit if accessed directly.
13 -if ( ! defined( 'ABSPATH' ) ) {
14 - exit;
13 +use Give\Helpers\Utils;
14 +
15 +if ( ! defined('ABSPATH')) {
16 + exit;
15 17 }
16 18
17 19 /**
18 20 * Get Currency Formatting Settings for each donation.
19 21 *
22 + * @since 1.8.15
23 + *
20 24 * @param int|string $id_or_currency_code Donation ID or Currency code.
21 25 *
22 - * @since 1.8.15
23 - *
24 26 * @return mixed
25 27 */
26 -function give_get_currency_formatting_settings( $id_or_currency_code = null ) {
27 - $give_options = give_get_settings();
28 - $setting = array();
28 +function give_get_currency_formatting_settings($id_or_currency_code = null)
29 +{
30 + $give_options = give_get_settings();
31 + $setting = [];
29 32
30 - if ( ! empty( $id_or_currency_code ) ) {
31 - $currencies = give_get_currencies( 'all' );
33 + if ( ! empty($id_or_currency_code)) {
34 + $currencies = give_get_currencies('all');
32 35
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 );
36 + // Set default formatting setting only if currency not set as global currency.
37 + if (
38 + is_string($id_or_currency_code) &&
39 + ! empty($give_options['currency']) &&
40 + $id_or_currency_code !== $give_options['currency'] &&
41 + array_key_exists($id_or_currency_code, $currencies)
42 + ) {
43 + $setting = $currencies[$id_or_currency_code]['setting'];
44 + } elseif (is_numeric($id_or_currency_code) && 'give_payment' === get_post_type($id_or_currency_code)) {
45 + $currency = give_get_meta($id_or_currency_code, '_give_payment_currency', true);
43 46
44 - if (
45 - ! empty( $currency) &&
46 - $give_options['currency'] !== $currency
47 - ) {
48 - $setting = $currencies[ $currency ]['setting'];
49 - }
50 - }
51 - }
47 + if (
48 + ! empty($currency) &&
49 + $give_options['currency'] !== $currency
50 + ) {
51 + $setting = $currencies[$currency]['setting'];
52 + }
53 + }
54 + }
52 55
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;
56 + if (empty($setting)) {
57 + // Set thousand separator.
58 + $thousand_separator = isset($give_options['thousands_separator']) ? $give_options['thousands_separator'] : ',';
59 + $thousand_separator = empty($thousand_separator) ? ' ' : $thousand_separator;
57 60
58 - // Set decimal separator.
59 - $default_decimal_separators = array(
60 - '.' => ',',
61 - ',' => '.',
62 - );
61 + // Set decimal separator.
62 + $default_decimal_separators = [
63 + '.' => ',',
64 + ',' => '.',
65 + ];
63 66
64 - $default_decimal_separator = in_array( $thousand_separator, $default_decimal_separators ) ?
65 - $default_decimal_separators[ $thousand_separator ] :
66 - '.';
67 + $default_decimal_separator = in_array($thousand_separator, $default_decimal_separators) ?
68 + $default_decimal_separators[$thousand_separator] :
69 + '.';
67 70
68 - $decimal_separator = ! empty( $give_options['decimal_separator'] ) ? $give_options['decimal_separator'] : $default_decimal_separator;
71 + $decimal_separator = ! empty($give_options['decimal_separator']) ? $give_options['decimal_separator'] : $default_decimal_separator;
69 72
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 - }
73 + $setting = [
74 + 'currency_position' => give_get_option('currency_position', 'before'),
75 + 'thousands_separator' => $thousand_separator,
76 + 'decimal_separator' => $decimal_separator,
77 + 'number_decimals' => give_get_option('number_decimals', 0),
78 + ];
79 + }
77 80
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 );
81 + /**
82 + * Filter the currency formatting setting.
83 + *
84 + * @since 1.8.15
85 + */
86 + return apply_filters('give_get_currency_formatting_settings', $setting, $id_or_currency_code);
84 87 }
85 88
86 89 /**
87 90 * Get decimal count
@@ -91,67 +94,102 @@
91 94 * @param int|string $id_or_currency_code
92 95 *
93 96 * @return mixed
94 97 */
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 - }
98 +function give_get_price_decimals($id_or_currency_code = null)
99 +{
100 + // Set currency on basis of donation id.
101 + if (empty($id_or_currency_code)) {
102 + $id_or_currency_code = give_get_currency();
103 + }
100 104
101 - $number_of_decimals = 0;
105 + $number_of_decimals = 0;
102 106
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 + if ( ! give_is_zero_based_currency($id_or_currency_code)) {
108 + $setting = give_get_currency_formatting_settings($id_or_currency_code);
109 + $number_of_decimals = $setting['number_decimals'];
110 + }
107 111
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 );
112 + /**
113 + * Filter the number of decimals
114 + *
115 + * @since 1.6
116 + */
117 + return apply_filters('give_sanitize_amount_decimals', $number_of_decimals, $id_or_currency_code);
114 118 }
115 119
116 120 /**
117 121 * Get thousand separator
118 122 *
123 + * @since 1.6
124 + *
119 125 * @param int|string $id_or_currency_code
120 126 *
127 + * @return mixed
128 + */
129 +function give_get_price_thousand_separator($id_or_currency_code = null)
130 +{
131 + $setting = give_get_currency_formatting_settings($id_or_currency_code);
132 +
133 + /**
134 + * Filter the thousand separator
135 + *
136 + * @since 1.6
137 + */
138 + return apply_filters('give_get_price_thousand_separator', $setting['thousands_separator'], $id_or_currency_code);
139 +}
140 +
141 +/**
142 + * Get decimal separator
143 + *
121 144 * @since 1.6
122 145 *
146 + * @param string $id_or_currency_code
147 + *
123 148 * @return mixed
124 149 */
125 -function give_get_price_thousand_separator( $id_or_currency_code = null ) {
126 - $setting = give_get_currency_formatting_settings( $id_or_currency_code );
150 +function give_get_price_decimal_separator($id_or_currency_code = null)
151 +{
152 + $setting = give_get_currency_formatting_settings($id_or_currency_code);
127 153
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 );
154 + /**
155 + * Filter the thousand separator
156 + *
157 + * @since 1.6
158 + */
159 + return apply_filters('give_get_price_decimal_separator', $setting['decimal_separator'], $id_or_currency_code);
134 160 }
135 161
136 162 /**
137 - * Get decimal separator
163 + * Check if amount sanitized
164 + * Note: only for internal purpose
138 165 *
139 - * @param string $id_or_currency_code
166 + * Current this function only check if number is DB sanitize.
140 167 *
141 - * @since 1.6
168 + * @since 2.4.5
142 169 *
143 - * @return mixed
170 + * @param string $amount
171 + *
172 + * @return bool
144 173 */
145 -function give_get_price_decimal_separator( $id_or_currency_code = null ) {
146 - $setting = give_get_currency_formatting_settings( $id_or_currency_code );
174 +function give_is_amount_sanitized($amount)
175 +{
176 + $is_sanitize = false;
147 177
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 );
178 + if (false === strpos($amount, '.')) {
179 + return $is_sanitize;
180 + }
181 +
182 + $number_parts = explode('.', $amount);
183 +
184 + // Handle thousand separator as '.'
185 + // Handle sanitize database values.
186 + $is_sanitize = (2 === count($number_parts) &&
187 + is_numeric($number_parts[0]) &&
188 + is_numeric($number_parts[1]) &&
189 + in_array(strlen($number_parts[1]), [6, 10]));
190 +
191 + return $is_sanitize;
154 192 }
155 193
156 194 /**
157 195 * Sanitize Amount before saving to database
@@ -157,114 +195,112 @@
157 195 * Sanitize Amount before saving to database
158 196 *
159 197 * @since 1.8.12
160 198 *
161 - * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
162 - * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
199 + * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
200 + * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
163 201 *
164 202 * @return string $amount Newly sanitized amount
165 203 */
166 -function give_sanitize_amount_for_db( $number, $args = array() ) {
167 - $args['number_decimals'] = 6;
204 +function give_sanitize_amount_for_db($number, $args = [])
205 +{
206 + $args['number_decimals'] = 6;
168 207
169 - if (
170 - ( isset( $args['currency'] ) && 'BTC' === $args['currency'] )
171 - || 'BTC' === give_get_currency()
172 - ) {
173 - $args['number_decimals'] = 10;
174 - }
208 + if (
209 + (isset($args['currency']) && 'XBT' === $args['currency'])
210 + || 'XBT' === give_get_currency()
211 + ) {
212 + $args['number_decimals'] = 10;
213 + }
175 214
176 - return give_maybe_sanitize_amount( $number, $args );
215 + return give_maybe_sanitize_amount($number, $args);
177 216 }
178 217
179 218 /**
180 219 * Sanitize Amount before saving to database
181 220 *
221 + * @since 4.10.0 Rollback backward compatibility - allows both array and individual parameter calls
222 + * @since 4.9.0 PHP 8 compatibility
182 223 * @since 1.8.12
183 224 *
184 - * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
185 - * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
225 + * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
226 + * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
186 227 *
187 228 * @return string $amount Newly sanitized amount
188 229 */
189 -function give_maybe_sanitize_amount( $number, $args = array() ) {
190 - // Bailout.
191 - if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
192 - return $number;
193 - }
230 +function give_maybe_sanitize_amount($number, $args = [])
231 +{
232 + // Get function arguments first to maintain PHP 7.0+ compatibility.
233 + // func_get_args() must be called before any parameter modifications to avoid warnings.
234 + $func_args = func_get_args();
194 235
195 - $func_args = func_get_args();
236 + // Bailout.
237 + if (empty($number) || ( ! is_numeric($number) && ! is_string($number))) {
238 + return $number;
239 + }
196 240
197 - // Backward compatibility.
198 - if ( isset( $func_args[1] ) && ( is_bool( $func_args[1] ) || is_numeric( $func_args[1] ) ) ) {
199 - $args = array(
200 - 'number_decimals' => $func_args[1],
201 - 'trim_zeros' => isset( $func_args[2] ) ? $func_args[2] : false,
202 - );
203 - }
241 + // Backward compatibility.
242 + if (isset($func_args[1]) && (is_bool($func_args[1]) || is_numeric($func_args[1]))) {
243 + $args = [
244 + 'number_decimals' => $func_args[1],
245 + 'trim_zeros' => isset($func_args[2]) ? $func_args[2] : false,
246 + ];
247 + }
204 248
205 - $args = wp_parse_args(
206 - $args,
207 - array(
208 - 'number_decimals' => false,
209 - 'trim_zeros' => false,
210 - 'currency' => give_get_currency(),
211 - )
212 - );
249 + $args = wp_parse_args(
250 + $args,
251 + [
252 + 'number_decimals' => false,
253 + 'trim_zeros' => false,
254 + 'currency' => give_get_currency(),
255 + ]
256 + );
213 257
214 - $thousand_separator = give_get_price_thousand_separator( $args['currency'] );
215 - $decimal_separator = give_get_price_decimal_separator( $args['currency'] );
216 - $number_decimals = is_bool( $args['number_decimals'] ) ?
217 - give_get_price_decimals( $args['currency'] ) :
218 - $args['number_decimals'];
258 + $thousand_separator = give_get_price_thousand_separator($args['currency']);
259 + $decimal_separator = give_get_price_decimal_separator($args['currency']);
260 + $number_decimals = is_bool($args['number_decimals']) ?
261 + give_get_price_decimals($args['currency']) :
262 + $args['number_decimals'];
219 263
220 - // Explode number by . decimal separator.
221 - $number_parts = explode( '.', $number );
264 + // Explode number by . decimal separator.
265 + $number_parts = explode('.', $number);
222 266
223 - // Remove currency symbols from number if any.
224 - $number = trim( str_replace( give_currency_symbols( true ), '', $number ) );
267 + // Remove currency symbols from number if any.
268 + $number = trim(str_replace(give_currency_symbols(true), '', $number));
225 269
226 - if (
227 - // Non formatted number.
228 - false === strpos( $number, $thousand_separator )
229 - && false === strpos( $number, $decimal_separator )
230 - ) {
231 - return number_format( $number, $number_decimals, '.', '' );
232 - } elseif (
233 - // Decimal formatted number.
234 - // If number of decimal place set to non zero and
235 - // number only contains `.` as separator, precision set to less then or equal to number of decimal
236 - // then number will be consider as decimal formatted which means number is already sanitized.
237 - $number_decimals
238 - && '.' === $thousand_separator
239 - && false !== strpos( $number, $thousand_separator )
240 - && false === strpos( $number, $decimal_separator )
241 - && 2 === count( $number_parts )
242 - && ( $number_decimals >= strlen( $number_parts[1] ) )
243 - ) {
244 - return number_format( $number, $number_decimals, '.', '' );
245 - }
270 + if (
271 + // Non formatted number.
272 + false === strpos($number, $thousand_separator)
273 + && false === strpos($number, $decimal_separator)
274 + ) {
275 + return number_format($number, $number_decimals, '.', '');
276 + } elseif (
277 + // Decimal formatted number.
278 + // If number of decimal place set to non zero and
279 + // number only contains `.` as separator, precision set to less then or equal to number of decimal
280 + // then number will be consider as decimal formatted which means number is already sanitized.
281 + $number_decimals
282 + && '.' === $thousand_separator
283 + && false !== strpos($number, $thousand_separator)
284 + && false === strpos($number, $decimal_separator)
285 + && 2 === count($number_parts)
286 + && ($number_decimals >= strlen($number_parts[1]))
287 + ) {
288 + return number_format($number, $number_decimals, '.', '');
289 + }
246 290
247 - // Handle thousand separator as '.'
248 - // Handle sanitize database values.
249 - $is_db_sanitize_val = ( 2 === count( $number_parts ) &&
250 - is_numeric( $number_parts[0] ) &&
251 - is_numeric( $number_parts[1] ) &&
252 - ( in_array( strlen( $number_parts[1] ), array( 6, 10 ) ) ) );
291 + if (give_is_amount_sanitized($number)) {
292 + // Sanitize database value.
293 + return number_format($number, $number_decimals, '.', '');
294 + } elseif (
295 + '.' === $thousand_separator &&
296 + false !== strpos($number, $thousand_separator)
297 + ) {
298 + // Fix point thousand separator value.
299 + $number = str_replace('.', '', $number);
300 + }
253 301
254 - if ( $is_db_sanitize_val ) {
255 - // Sanitize database value.
256 - return number_format( $number, $number_decimals, '.', '' );
257 -
258 - } elseif (
259 - '.' === $thousand_separator &&
260 - false !== strpos( $number, $thousand_separator )
261 - ) {
262 - // Fix point thousand separator value.
263 - $number = str_replace( '.', '', $number );
264 - }
265 -
266 - return give_sanitize_amount( $number, $args );
302 + return give_sanitize_amount($number, $args);
267 303 }
268 304
269 305 /**
270 306 * Sanitize Amount
@@ -272,105 +308,108 @@
272 308 * Note: Do not this function to sanitize amount instead use give_maybe_sanitize_amount function.
273 309 *
274 310 * Returns a sanitized amount by stripping out thousands separators.
275 311 *
312 + * @since 4.10.0 Rollback backward compatibility - allows both array and individual parameter calls
313 + * @since 4.9.0 PHP 8 compatibility
276 314 * @since 1.0
277 315 *
278 - * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
279 - * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
316 + * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands)
317 + * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'.
280 318 *
281 319 * @return string $amount Newly sanitized amount
282 320 */
283 -function give_sanitize_amount( $number, $args = array() ) {
321 +function give_sanitize_amount($number, $args = [])
322 +{
323 + // Get function arguments first to maintain PHP 7.0+ compatibility.
324 + // func_get_args() must be called before any parameter modifications to avoid warnings.
325 + $func_args = func_get_args();
284 326
285 - // Bailout.
286 - if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) {
287 - return $number;
288 - }
327 + // Bailout.
328 + if (empty($number) || ( ! is_numeric($number) && ! is_string($number))) {
329 + return $number;
330 + }
289 331
290 - // Get function arguments.
291 - $func_args = func_get_args();
332 + // Backward compatibility.
333 + if (isset($func_args[1]) && (is_bool($func_args[1]) || is_numeric($func_args[1]))) {
334 + $args = [
335 + 'number_decimals' => $func_args[1],
336 + 'trim_zeros' => isset($func_args[2]) ? $func_args[2] : false,
337 + ];
338 + }
292 339
293 - // Backward compatibility.
294 - if ( isset( $func_args[1] ) && ( is_bool( $func_args[1] ) || is_numeric( $func_args[1] ) ) ) {
295 - $args = array(
296 - 'number_decimals' => $func_args[1],
297 - 'trim_zeros' => isset( $func_args[2] ) ? $func_args[2] : false,
298 - );
299 - }
340 + $args = wp_parse_args(
341 + $args,
342 + [
343 + 'number_decimals' => false,
344 + 'trim_zeros' => false,
345 + 'currency' => give_get_currency(),
346 + ]
347 + );
300 348
301 - $args = wp_parse_args(
302 - $args,
303 - array(
304 - 'number_decimals' => false,
305 - 'trim_zeros' => false,
306 - 'currency' => give_get_currency(),
307 - )
308 - );
349 + // Remove slash from amount.
350 + // If thousand or decimal separator is set to ' then in $_POST or $_GET param we will get an escaped number.
351 + // To prevent notices and warning remove slash from amount/number.
352 + $number = wp_unslash($number);
309 353
310 - // Remove slash from amount.
311 - // If thousand or decimal separator is set to ' then in $_POST or $_GET param we will get an escaped number.
312 - // To prevent notices and warning remove slash from amount/number.
313 - $number = wp_unslash( $number );
354 + $thousand_separator = give_get_price_thousand_separator($args['currency']);
314 355
315 - $thousand_separator = give_get_price_thousand_separator( $args['currency'] );
356 + $locale = localeconv();
357 + $decimals = [
358 + give_get_price_decimal_separator($args['currency']),
359 + $locale['decimal_point'],
360 + $locale['mon_decimal_point'],
361 + ];
316 362
317 - $locale = localeconv();
318 - $decimals = array(
319 - give_get_price_decimal_separator( $args['currency'] ),
320 - $locale['decimal_point'],
321 - $locale['mon_decimal_point'],
322 - );
363 + // Remove locale from string
364 + if ( ! is_float($number)) {
365 + $number = str_replace($decimals, '.', $number);
366 + }
323 367
324 - // Remove locale from string
325 - if ( ! is_float( $number ) ) {
326 - $number = str_replace( $decimals, '.', $number );
327 - }
368 + // Remove thousand amount formatting if amount has.
369 + // This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db.
370 + // Do not replace thousand separator from price if it is same as decimal separator, because it will be already replace by above code.
371 + if ( ! in_array($thousand_separator, $decimals) && (false !== strpos($number, $thousand_separator))) {
372 + $number = str_replace($thousand_separator, '', $number);
373 + } elseif (in_array($thousand_separator, $decimals)) {
374 + $number = preg_replace('/\.(?=.*\.)/', '', $number);
375 + }
328 376
329 - // Remove thousand amount formatting if amount has.
330 - // This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db.
331 - // Do not replace thousand separator from price if it is same as decimal separator, because it will be already replace by above code.
332 - if ( ! in_array( $thousand_separator, $decimals ) && ( false !== strpos( $number, $thousand_separator ) ) ) {
333 - $number = str_replace( $thousand_separator, '', $number );
334 - } elseif ( in_array( $thousand_separator, $decimals ) ) {
335 - $number = preg_replace( '/\.(?=.*\.)/', '', $number );
336 - }
377 + // Remove non numeric entity before decimal separator.
378 + $number = preg_replace('/[^0-9\.]/', '', $number);
379 + $default_dp = give_get_price_decimals($args['currency']);
337 380
338 - // Remove non numeric entity before decimal separator.
339 - $number = preg_replace( '/[^0-9\.]/', '', $number );
340 - $default_dp = give_get_price_decimals( $args['currency'] );
381 + // Reset negative amount to zero.
382 + if (0 > $number) {
383 + $number = number_format(0, $default_dp, '.');
384 + }
341 385
342 - // Reset negative amount to zero.
343 - if ( 0 > $number ) {
344 - $number = number_format( 0, $default_dp, '.' );
345 - }
386 + // If number does not have decimal then add number of decimals to it.
387 + if (
388 + false === strpos($number, '.')
389 + || ($default_dp > strlen(substr($number, strpos($number, '.') + 1)))
390 + ) {
391 + $number = number_format($number, $default_dp, '.', '');
392 + }
346 393
347 - // If number does not have decimal then add number of decimals to it.
348 - if (
349 - false === strpos( $number, '.' )
350 - || ( $default_dp > strlen( substr( $number, strpos( $number, '.' ) + 1 ) ) )
351 - ) {
352 - $number = number_format( $number, $default_dp, '.', '' );
353 - }
394 + // Format number by custom number of decimals.
395 + if (false !== $args['number_decimals']) {
396 + $dp = intval(is_bool($args['number_decimals']) ? $default_dp : $args['number_decimals']);
397 + $dp = apply_filters('give_sanitize_amount_decimals', $dp, $number);
398 + $number = number_format(floatval($number), $dp, '.', '');
399 + }
354 400
355 - // Format number by custom number of decimals.
356 - if ( false !== $args['number_decimals'] ) {
357 - $dp = intval( is_bool( $args['number_decimals'] ) ? $default_dp : $args['number_decimals'] );
358 - $dp = apply_filters( 'give_sanitize_amount_decimals', $dp, $number );
359 - $number = number_format( floatval( $number ), $dp, '.', '' );
360 - }
401 + // Trim zeros.
402 + if ($args['trim_zeros'] && strstr($number, '.')) {
403 + $number = rtrim(rtrim($number, '0'), '.');
404 + }
361 405
362 - // Trim zeros.
363 - if ( $args['trim_zeros'] && strstr( $number, '.' ) ) {
364 - $number = rtrim( rtrim( $number, '0' ), '.' );
365 - }
366 -
367 - /**
368 - * Filter the sanitize amount
369 - *
370 - * @since 1.0
371 - */
372 - return apply_filters( 'give_sanitize_amount', $number );
406 + /**
407 + * Filter the sanitize amount
408 + *
409 + * @since 1.0
410 + */
411 + return apply_filters('give_sanitize_amount', $number);
373 412 }
374 413
375 414 /**
376 415 * Returns a nicely formatted amount.
@@ -381,86 +420,102 @@
381 420 * @param array $args Array of arguments.
382 421 *
383 422 * @return string $amount Newly formatted amount or Price Not Available
384 423 */
385 -function give_format_amount( $amount, $args = array() ) {
386 - // Backward compatibility.
387 - if ( is_bool( $args ) ) {
388 - $args = array(
389 - 'decimal' => $args,
390 - );
391 - }
424 +function give_format_amount($amount, $args = [])
425 +{
426 + // Backward compatibility.
427 + if (is_bool($args)) {
428 + $args = [
429 + 'decimal' => $args,
430 + ];
431 + }
392 432
393 - $default_args = array(
394 - 'decimal' => true,
395 - 'sanitize' => true,
396 - 'donation_id' => 0,
397 - 'currency' => '',
398 - );
433 + $default_args = [
434 + 'decimal' => true,
435 + 'sanitize' => true,
436 + 'donation_id' => 0,
437 + 'currency' => '',
438 + ];
399 439
400 - $args = wp_parse_args( $args, $default_args );
440 + $args = wp_parse_args($args, $default_args);
401 441
402 - // Set Currency based on donation id, if required.
403 - if ( $args['donation_id'] && empty( $args['currency'] ) ) {
404 - $args['currency'] = give_get_meta( $args['donation_id'], '_give_payment_currency', true );
405 - }
442 + // Set Currency based on donation id, if required.
443 + if ($args['donation_id'] && empty($args['currency'])) {
444 + $args['currency'] = give_get_meta($args['donation_id'], '_give_payment_currency', true);
445 + }
406 446
407 - $formatted = 0;
408 - $currency = ! empty( $args['currency'] ) ? $args['currency'] : give_get_currency( $args['donation_id'] );
409 - $thousands_sep = give_get_price_thousand_separator( $currency );
410 - $decimal_sep = give_get_price_decimal_separator( $currency );
411 - $decimals = ! empty( $args['decimal'] ) ? give_get_price_decimals( $currency ) : 0;
447 + $formatted = 0;
448 + $currency = ! empty($args['currency']) ? $args['currency'] : give_get_currency($args['donation_id']);
449 + $thousands_sep = give_get_price_thousand_separator($currency);
450 + $decimal_sep = give_get_price_decimal_separator($currency);
451 + $decimals = ! empty($args['decimal']) ? give_get_price_decimals($currency) : 0;
412 452
413 - if ( ! empty( $amount ) ) {
414 - // Sanitize amount before formatting.
415 - $amount = ! empty( $args['sanitize'] ) ?
416 - give_maybe_sanitize_amount( $amount, array( 'number_decimals' => $decimals, 'currency' => $currency ) ) :
417 - number_format( $amount, $decimals, '.', '' );
453 + if ( ! empty($amount)) {
454 + // Sanitize amount before formatting.
455 + $amount = ! empty($args['sanitize']) ?
456 + give_maybe_sanitize_amount(
457 + $amount,
458 + [
459 + 'number_decimals' => $decimals,
460 + 'currency' => $currency,
461 + ]
462 + ) :
463 + number_format($amount, $decimals, '.', '');
418 464
419 - switch ( $currency ) {
420 - case 'INR':
421 - $decimal_amount = '';
465 + switch ($currency) {
466 + case 'INR':
467 + $decimal_amount = '';
422 468
423 - // Extract decimals from amount
424 - if ( ( $pos = strpos( $amount, '.' ) ) !== false ) {
425 - if ( ! empty( $decimals ) ) {
426 - $decimal_amount = substr( round( substr( $amount, $pos ), $decimals ), 1 );
427 - $amount = substr( $amount, 0, $pos );
469 + // Extract decimals from amount
470 + if (($pos = strpos($amount, '.')) !== false) {
471 + if ( ! empty($decimals)) {
472 + $decimal_amount = substr(round(substr($amount, $pos), $decimals), 1);
473 + $amount = substr($amount, 0, $pos);
428 474
429 - if ( ! $decimal_amount ) {
430 - $decimal_amount = substr( "{$decimal_sep}0000000000", 0, ( $decimals + 1 ) );
431 - } elseif ( ( $decimals + 1 ) > strlen( $decimal_amount ) ) {
432 - $decimal_amount = substr( "{$decimal_amount}000000000", 0, ( $decimals + 1 ) );
433 - }
434 - } else {
435 - $amount = number_format( $amount, $decimals, $decimal_sep, '' );
436 - }
437 - }
475 + if ( ! $decimal_amount) {
476 + $decimal_amount = substr("{$decimal_sep}0000000000", 0, ($decimals + 1));
477 + } elseif (($decimals + 1) > strlen($decimal_amount)) {
478 + $decimal_amount = substr("{$decimal_amount}000000000", 0, ($decimals + 1));
479 + }
480 + } else {
481 + $amount = number_format($amount, $decimals, $decimal_sep, '');
482 + }
483 + }
438 484
439 - // Extract last 3 from amount
440 - $result = substr( $amount, - 3 );
441 - $amount = substr( $amount, 0, - 3 );
485 + // Extract last 3 from amount
486 + $result = substr($amount, -3);
487 + $amount = substr($amount, 0, -3);
442 488
443 - // Apply digits 2 by 2
444 - while ( strlen( $amount ) > 0 ) {
445 - $result = substr( $amount, - 2 ) . $thousands_sep . $result;
446 - $amount = substr( $amount, 0, - 2 );
447 - }
489 + // Apply digits 2 by 2
490 + while (strlen($amount) > 0) {
491 + $result = substr($amount, -2) . $thousands_sep . $result;
492 + $amount = substr($amount, 0, -2);
493 + }
448 494
449 - $formatted = $result . $decimal_amount;
450 - break;
495 + $formatted = $result . $decimal_amount;
496 + break;
451 497
452 - default:
453 - $formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
454 - }
455 - }
498 + default:
499 + $formatted = number_format($amount, $decimals, $decimal_sep, $thousands_sep);
500 + }
501 + }
456 502
457 - /**
458 - * Filter the formatted amount
459 - *
460 - * @since 1.0
461 - */
462 - return apply_filters( 'give_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $currency, $args );
503 + /**
504 + * Filter the formatted amount
505 + *
506 + * @since 1.0
507 + */
508 + return apply_filters(
509 + 'give_format_amount',
510 + $formatted,
511 + $amount,
512 + $decimals,
513 + $decimal_sep,
514 + $thousands_sep,
515 + $currency,
516 + $args
517 + );
463 518 }
464 519
465 520
466 521 /**
@@ -476,62 +531,82 @@
476 531 * @param array $args Array of arguments.
477 532 *
478 533 * @return string formatted amount number with large number names.
479 534 */
480 -function give_human_format_large_amount( $amount, $args = array() ) {
481 - // Sanitize amount.
482 - $sanitize_amount = give_maybe_sanitize_amount( $amount );
535 +function give_human_format_large_amount($amount, $args = [])
536 +{
537 + // Set default currency;
538 + if (empty($args['currency'])) {
539 + $args['currency'] = give_get_currency();
540 + }
483 541
484 - // Bailout.
485 - if ( ! floatval( $sanitize_amount ) ) {
486 - return '0';
487 - };
542 + // Get thousand separator.
543 + $thousands_sep = give_get_price_thousand_separator($args['currency']);
488 544
489 - // Set default currency;
490 - if ( empty( $args['currency'] ) ) {
491 - $args['currency'] = give_get_currency();
492 - }
545 + // Sanitize amount for calculation purpose.
546 + $sanitize_amount = give_maybe_sanitize_amount(
547 + $amount,
548 + [
549 + 'currency' => $args['currency'],
550 + ]
551 + );
493 552
494 - // Get thousand separator.
495 - $thousands_sep = give_get_price_thousand_separator();
553 + // Bailout.
554 + if ( ! floatval($sanitize_amount)) {
555 + return '0';
556 + };
496 557
497 - // Explode amount to calculate name of large numbers.
498 - $amount_array = explode( $thousands_sep, $amount );
558 + // Explode amount to calculate name of large numbers.
559 + $amount_array = explode($thousands_sep, $amount);
499 560
500 - // Calculate amount parts count.
501 - $amount_count_parts = count( $amount_array );
561 + // Calculate amount parts count.
562 + $amount_count_parts = count($amount_array);
502 563
503 - // Human format amount (default).
504 - $human_format_amount = $amount;
564 + // Human format amount (default).
565 + $human_format_amount = $amount;
505 566
506 - switch ( $args['currency'] ) {
507 - case 'INR':
508 - // Calculate large number formatted amount.
509 - if ( 4 < $amount_count_parts ) {
510 - $human_format_amount = sprintf( esc_html__( '%s arab', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
511 - } elseif ( 3 < $amount_count_parts ) {
512 - $human_format_amount = sprintf( esc_html__( '%s crore', 'give' ), round( ( $sanitize_amount / 10000000 ), 2 ) );
513 - } elseif ( 2 < $amount_count_parts ) {
514 - $human_format_amount = sprintf( esc_html__( '%s lakh', 'give' ), round( ( $sanitize_amount / 100000 ), 2 ) );
515 - }
516 - break;
517 - default:
518 - // Calculate large number formatted amount.
519 - if ( 4 < $amount_count_parts ) {
520 - $human_format_amount = sprintf( esc_html__( '%s trillion', 'give' ), round( ( $sanitize_amount / 1000000000000 ), 2 ) );
521 - } elseif ( 3 < $amount_count_parts ) {
522 - $human_format_amount = sprintf( esc_html__( '%s billion', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) );
523 - } elseif ( 2 < $amount_count_parts ) {
524 - $human_format_amount = sprintf( esc_html__( '%s million', 'give' ), round( ( $sanitize_amount / 1000000 ), 2 ) );
525 - }
526 - }
567 + switch ($args['currency']) {
568 + case 'INR':
569 + // Calculate large number formatted amount.
570 + if (4 < $amount_count_parts) {
571 + $human_format_amount = sprintf(
572 + esc_html__('%s arab', 'give'),
573 + round(($sanitize_amount / 1000000000), 2)
574 + );
575 + } elseif (3 < $amount_count_parts) {
576 + $human_format_amount = sprintf(esc_html__('%s crore', 'give'), round(($sanitize_amount / 10000000), 2));
577 + } elseif (2 < $amount_count_parts) {
578 + $human_format_amount = sprintf(esc_html__('%s lakh', 'give'), round(($sanitize_amount / 100000), 2));
579 + }
580 + break;
581 + default:
582 + // Calculate large number formatted amount.
583 + if (4 < $amount_count_parts) {
584 + $human_format_amount = sprintf(
585 + esc_html__('%s trillion', 'give'),
586 + round(($sanitize_amount / 1000000000000), 2)
587 + );
588 + } elseif (3 < $amount_count_parts) {
589 + $human_format_amount = sprintf(
590 + esc_html__('%s billion', 'give'),
591 + round(($sanitize_amount / 1000000000), 2)
592 + );
593 + } elseif (2 < $amount_count_parts) {
594 + $human_format_amount = sprintf(
595 + esc_html__('%s million', 'give'),
596 + round(($sanitize_amount / 1000000), 2)
597 + );
598 + }
599 + }
527 600
528 - return apply_filters( 'give_human_format_large_amount', $human_format_amount, $amount, $sanitize_amount );
601 + return apply_filters('give_human_format_large_amount', $human_format_amount, $amount, $sanitize_amount);
529 602 }
530 603
531 604 /**
532 605 * Returns a nicely formatted amount with custom decimal separator.
533 606 *
607 + * @since 4.10.0 Rollback backward compatibility - allows both array and individual parameter calls
608 + * @since 4.9.0 PHP 8 compatibility
534 609 * @since 1.0
535 610 *
536 611 * @param array $args {
537 612 *
@@ -539,57 +614,71 @@
539 614 * @type int $donation_id donation amount (optional if set amount, but provide it for better result if formatting decimal amount of donation).
540 615 * @type string $currency donation amount (optional if set donation id). Provide either amount or donation id
541 616 * @type int|bool $dp number of decimals
542 617 * @type bool $sanitize Whether or not sanitize number
543 - * }
618 + * }
544 619 *
545 620 * @return string $amount Newly formatted amount or Price Not Available
546 621 */
547 -function give_format_decimal( $args ) {
548 - // Backward compatibility.
549 - if ( ! is_array( $args ) ) {
550 - $func_args = func_get_args();
551 - $args = array(
552 - 'amount' => $func_args[0],
553 - 'dp' => isset( $func_args[1] ) ? $func_args[1] : false,
554 - 'sanitize' => isset( $func_args[2] ) ? $func_args[2] : true,
555 - );
556 - }
622 +function give_format_decimal($args)
623 +{
624 + // Get function arguments first to maintain PHP 7.0+ compatibility.
625 + // func_get_args() must be called before any parameter modifications to avoid warnings.
626 + $func_args = func_get_args();
557 627
558 - $args = wp_parse_args(
559 - $args,
560 - array(
561 - 'amount' => '',
562 - 'donation_id' => 0,
563 - 'currency' => '',
564 - 'dp' => false,
565 - 'sanitize' => false,
566 - )
567 - );
628 + // Backward compatibility.
629 + if ( ! is_array($args)) {
630 + $args = [
631 + 'amount' => $func_args[0],
632 + 'dp' => isset($func_args[1]) ? $func_args[1] : false,
633 + 'sanitize' => isset($func_args[2]) ? $func_args[2] : true,
634 + ];
635 + }
568 636
569 - if( ! empty( $args['donation_id'] ) ) {
637 + $args = wp_parse_args(
638 + $args,
639 + [
640 + 'amount' => 0,
641 + 'donation_id' => 0,
642 + 'currency' => '',
643 + 'dp' => false,
644 + 'sanitize' => false,
645 + ]
646 + );
570 647
571 - // Set currency if not already done.
572 - if( empty( $args['currency'] ) ) {
573 - $args['currency'] = give_get_payment_currency_code( $args['donation_id'] );
574 - }
648 + if ( ! empty($args['donation_id'])) {
649 + // Set currency if not already done.
650 + if (empty($args['currency'])) {
651 + $args['currency'] = give_get_payment_currency_code($args['donation_id']);
652 + }
575 653
576 - // Set amount if not already done.
577 - if( empty( $args['amount'] ) ) {
578 - $args['amount'] = give_donation_amount( $args['donation_id'] );
579 - }
580 - }
654 + // Set amount if not already done.
655 + if (empty($args['amount'])) {
656 + $args['amount'] = give_donation_amount($args['donation_id']);
657 + }
658 + }
581 659
582 - $decimal_separator = give_get_price_decimal_separator();
583 - $formatted_amount = $args['sanitize'] ?
584 - give_maybe_sanitize_amount( $args['amount'], array( 'number_decimals' => $args['dp'], 'currency' => $args['currency'] ) ) :
585 - number_format( $args['amount'], ( is_bool( $args['dp'] ) ? give_get_price_decimals( $args['currency'] ) : $args['dp'] ), '.', '' );
660 + $decimal_separator = give_get_price_decimal_separator();
661 + $formatted_amount = $args['sanitize'] ?
662 + give_maybe_sanitize_amount(
663 + $args['amount'],
664 + [
665 + 'number_decimals' => $args['dp'],
666 + 'currency' => $args['currency'],
667 + ]
668 + ) :
669 + number_format(
670 + $args['amount'],
671 + (is_bool($args['dp']) ? give_get_price_decimals($args['currency']) : $args['dp']),
672 + '.',
673 + ''
674 + );
586 675
587 - if ( false !== strpos( $formatted_amount, '.' ) ) {
588 - $formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount );
589 - }
676 + if (false !== strpos($formatted_amount, '.')) {
677 + $formatted_amount = str_replace('.', $decimal_separator, $formatted_amount);
678 + }
590 679
591 - return apply_filters( 'give_format_decimal', $formatted_amount, $args['amount'], $decimal_separator, $args );
680 + return apply_filters('give_format_decimal', $formatted_amount, $args['amount'], $decimal_separator, $args);
592 681 }
593 682
594 683 /**
595 684 * Get date format string on basis of given context.
@@ -595,46 +684,47 @@
595 684 * Get date format string on basis of given context.
596 685 *
597 686 * @since 1.7
598 687 *
599 - * @param string $date_context Date format context name.
688 + * @param string $date_context Date format context name.
600 689 *
601 690 * @return string Date format string
602 691 */
603 -function give_date_format( $date_context = '' ) {
604 - /**
605 - * Filter the date context
606 - *
607 - * You can add your own date context or use already exist context.
608 - * For example:
609 - * add_filter( 'give_date_format_contexts', 'add_new_date_contexts' );
610 - * function add_new_date_contexts( $date_format_contexts ) {
611 - * // You can add single context like this $date_format_contexts['checkout'] = 'F j, Y';
612 - * // Instead add multiple date context at once.
613 - * $new_date_format_contexts = array(
614 - * 'checkout' => 'F j, Y',
615 - * 'report' => 'Y-m-d',
616 - * 'email' => 'm/d/Y',
617 - * );
618 - *
619 - * // Merge date contexts array only if you are adding multiple date contexts at once otherwise return $date_format_contexts.
620 - * return array_merge( $new_date_format_contexts, $date_format_contexts );
621 - *
622 - * }
623 - */
624 - $date_format_contexts = apply_filters( 'give_date_format_contexts', array() );
692 +function give_date_format($date_context = '')
693 +{
694 + /**
695 + * Filter the date context
696 + *
697 + * You can add your own date context or use already exist context.
698 + * For example:
699 + * add_filter( 'give_date_format_contexts', 'add_new_date_contexts' );
700 + * function add_new_date_contexts( $date_format_contexts ) {
701 + * // You can add single context like this $date_format_contexts['checkout'] = 'F j, Y';
702 + * // Instead add multiple date context at once.
703 + * $new_date_format_contexts = array(
704 + * 'checkout' => 'F j, Y',
705 + * 'report' => 'Y-m-d',
706 + * 'email' => 'm/d/Y',
707 + * );
708 + *
709 + * // Merge date contexts array only if you are adding multiple date contexts at once otherwise return $date_format_contexts.
710 + * return array_merge( $new_date_format_contexts, $date_format_contexts );
711 + *
712 + * }
713 + */
714 + $date_format_contexts = apply_filters('give_date_format_contexts', []);
625 715
626 - // Set date format to default date format.
627 - $date_format = get_option( 'date_format' );
716 + // Set date format to default date format.
717 + $date_format = get_option('date_format');
628 718
629 - // Update date format if we have non empty date format context array and non empty date format string for that context.
630 - if ( $date_context && ! empty( $date_format_contexts ) && array_key_exists( $date_context, $date_format_contexts ) ) {
631 - $date_format = ! empty( $date_format_contexts[ $date_context ] )
632 - ? $date_format_contexts[ $date_context ]
633 - : $date_format;
634 - }
719 + // Update date format if we have non empty date format context array and non empty date format string for that context.
720 + if ($date_context && ! empty($date_format_contexts) && array_key_exists($date_context, $date_format_contexts)) {
721 + $date_format = ! empty($date_format_contexts[$date_context])
722 + ? $date_format_contexts[$date_context]
723 + : $date_format;
724 + }
635 725
636 - return apply_filters( 'give_date_format', $date_format );
726 + return apply_filters('give_date_format', $date_format);
637 727 }
638 728
639 729 /**
640 730 * Get cache key.
@@ -639,17 +729,19 @@
639 729 /**
640 730 * Get cache key.
641 731 *
642 732 * @since 1.7
643 - * @deprecated 1.8.7 You can access this function from Give_Cache.
644 733 *
645 - * @param string $action Cache key prefix.
734 + * @param string $action Cache key prefix.
646 735 * @param array $query_args Query array.
647 736 *
648 737 * @return string
738 + * @deprecated 1.8.7 You can access this function from Give_Cache.
739 + *
649 740 */
650 -function give_get_cache_key( $action, $query_args ) {
651 - return Give_Cache::get_key( $action, $query_args );
741 +function give_get_cache_key($action, $query_args)
742 +{
743 + return Give_Cache::get_key($action, $query_args);
652 744 }
653 745
654 746 /**
655 747 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
@@ -654,23 +746,56 @@
654 746 /**
655 747 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
656 748 * Non-scalar values are ignored.
657 749 *
750 + * @since 3.19.3 Don't unserialize data by default and return an empty string when data is serialized and $allow_serialized_data is false
751 + * @since 3.17.2 Safe unserialize data by default
658 752 * @since 1.8
659 753 *
660 - * @param string|array $var
754 + * @param string|array $var
661 755 *
662 756 * @return string|array
663 757 */
664 -function give_clean( $var ) {
665 - if ( is_array( $var ) ) {
666 - return array_map( 'give_clean', $var );
667 - } else {
668 - return is_scalar( $var ) ? sanitize_text_field( wp_unslash( $var ) ) : $var;
669 - }
758 +function give_clean($var, $allow_serialized_data = false)
759 +{
760 + if (is_array($var)) {
761 + return array_map('give_clean', $var);
762 + }
763 +
764 + if ( Utils::isSerialized($var)) {
765 + $var = $allow_serialized_data ? Utils::safeUnserialize($var) : '';
766 + }
767 +
768 + return is_scalar($var) ? sanitize_text_field(wp_unslash($var)) : $var;
670 769 }
671 770
672 771 /**
772 + * Strips shortcodes from a string repeatedly until the output stops changing, so nested
773 + * shortcode syntax is fully removed rather than only partially reduced by a single pass.
774 + *
775 + * @since 4.16.9
776 + *
777 + * @param string $content Content that may contain shortcode syntax.
778 + *
779 + * @return string Content with registered shortcodes removed.
780 + */
781 +function give_strip_shortcodes_deep($content): string
782 +{
783 + $content = (string) $content;
784 +
785 + if ('' === $content || false === strpos($content, '[')) {
786 + return $content;
787 + }
788 +
789 + do {
790 + $previous = $content;
791 + $content = strip_shortcodes($content);
792 + } while ($content !== $previous);
793 +
794 + return $content;
795 +}
796 +
797 +/**
673 798 * Transforms php.ini notation for numbers (like '2M') to an integer.
674 799 *
675 800 * @since 1.8
676 801 *
@@ -677,25 +802,26 @@
677 802 * @param $size
678 803 *
679 804 * @return int
680 805 */
681 -function give_let_to_num( $size ) {
682 - $l = substr( $size, - 1 );
683 - $ret = substr( $size, 0, - 1 );
684 - switch ( strtoupper( $l ) ) {
685 - case 'P':
686 - $ret *= 1024;
687 - case 'T':
688 - $ret *= 1024;
689 - case 'G':
690 - $ret *= 1024;
691 - case 'M':
692 - $ret *= 1024;
693 - case 'K':
694 - $ret *= 1024;
695 - }
806 +function give_let_to_num($size)
807 +{
808 + $l = substr($size, -1);
809 + $ret = substr($size, 0, -1);
810 + switch (strtoupper($l)) {
811 + case 'P':
812 + $ret *= 1024;
813 + case 'T':
814 + $ret *= 1024;
815 + case 'G':
816 + $ret *= 1024;
817 + case 'M':
818 + $ret *= 1024;
819 + case 'K':
820 + $ret *= 1024;
821 + }
696 822
697 - return $ret;
823 + return $ret;
698 824 }
699 825
700 826 /**
701 827 * Verify nonce.
@@ -707,38 +833,41 @@
707 833 * @param array $wp_die_args Nonce fail arguments.
708 834 *
709 835 * @return bool
710 836 */
711 -function give_validate_nonce( $nonce, $action = - 1, $wp_die_args = array() ) {
837 +function give_validate_nonce($nonce, $action = -1, $wp_die_args = [])
838 +{
839 + // Verify nonce.
840 + $verify_nonce = wp_verify_nonce($nonce, $action);
712 841
713 - // Verify nonce.
714 - $verify_nonce = wp_verify_nonce( $nonce, $action );
842 + // On ajax request send nonce verification status.
843 + if (wp_doing_ajax()) {
844 + return $verify_nonce;
845 + }
715 846
716 - // On ajax request send nonce verification status.
717 - if ( wp_doing_ajax() ) {
718 - return $verify_nonce;
719 - }
847 + if ( ! $verify_nonce) {
848 + $wp_die_args = wp_parse_args(
849 + $wp_die_args,
850 + [
851 + 'message' => __(
852 + 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.',
853 + 'give'
854 + ),
855 + 'title' => __('Error', 'give'),
856 + 'args' => [
857 + 'response' => 403,
858 + ],
859 + ]
860 + );
720 861
721 - if ( ! $verify_nonce ) {
722 - $wp_die_args = wp_parse_args(
723 - $wp_die_args,
724 - array(
725 - 'message' => __( 'Nonce verification has failed.', 'give' ),
726 - 'title' => __( 'Error', 'give' ),
727 - 'args' => array(
728 - 'response' => 403,
729 - ),
730 - )
731 - );
862 + wp_die(
863 + $wp_die_args['message'],
864 + $wp_die_args['title'],
865 + $wp_die_args['args']
866 + );
867 + }
732 868
733 - wp_die(
734 - $wp_die_args['message'],
735 - $wp_die_args['title'],
736 - $wp_die_args['args']
737 - );
738 - }
739 -
740 - return true;
869 + return true;
741 870 }
742 871
743 872 /**
744 873 * Verify nonce while processing donation form.
@@ -749,21 +878,27 @@
749 878 * @param int $form_id Donation Form ID.
750 879 *
751 880 * @return bool
752 881 */
753 -function give_verify_donation_form_nonce( $nonce = '', $form_id ) {
882 +function give_verify_donation_form_nonce($nonce, $form_id)
883 +{
884 + // Form nonce action.
885 + $nonce_action = "give_donation_form_nonce_{$form_id}";
754 886
755 - // Form nonce action.
756 - $nonce_action = "give_donation_form_nonce_{$form_id}";
887 + // Nonce validation.
888 + $verify_nonce = give_validate_nonce($nonce, $nonce_action);
757 889
758 - // Nonce validation.
759 - $verify_nonce = give_validate_nonce( $nonce, $nonce_action );
890 + if ( ! $verify_nonce) {
891 + give_set_error(
892 + 'donation_form_nonce',
893 + __(
894 + 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.',
895 + 'give'
896 + )
897 + );
898 + }
760 899
761 - if ( ! $verify_nonce ) {
762 - give_set_error( 'donation_form_nonce', __( 'Nonce verification has failed.', 'give' ) );
763 - }
764 -
765 - return $verify_nonce;
900 + return $verify_nonce;
766 901 }
767 902
768 903 /**
769 904 * Check variable and get default or valid value.
@@ -778,47 +913,47 @@
778 913 * @param string (optional) $array_key_name default value: false
779 914 *
780 915 * @return mixed
781 916 */
782 -function give_check_variable( $variable, $conditional = '', $default = false, $array_key_name = '' ) {
783 - // Get value from array if array key non empty.
784 - if( empty( $array_key_name ) ) {
785 - switch ( $conditional ) {
786 - case 'isset_empty':
787 - $variable = ( isset( $variable ) && ! empty( $variable ) ) ? $variable : $default;
788 - break;
917 +function give_check_variable($variable, $conditional = '', $default = false, $array_key_name = '')
918 +{
919 + // Get value from array if array key non empty.
920 + if (empty($array_key_name)) {
921 + switch ($conditional) {
922 + case 'isset_empty':
923 + $variable = (isset($variable) && ! empty($variable)) ? $variable : $default;
924 + break;
789 925
790 - case 'empty':
791 - $variable = ! empty( $variable ) ? $variable : $default;
792 - break;
926 + case 'empty':
927 + $variable = ! empty($variable) ? $variable : $default;
928 + break;
793 929
794 - case 'null':
795 - $variable = ! is_null( $variable ) ? $variable : $default;
796 - break;
930 + case 'null':
931 + $variable = ! is_null($variable) ? $variable : $default;
932 + break;
797 933
798 - default:
799 - $variable = isset( $variable ) ? $variable : $default;
800 - }
801 - } else {
802 - $isset = array_key_exists( $array_key_name, $variable );
934 + default:
935 + $variable = isset($variable) ? $variable : $default;
936 + }
937 + } else {
938 + $isset = is_array($variable) && array_key_exists($array_key_name, $variable);
803 939
804 - switch ( $conditional ) {
805 - case 'isset_empty':
806 - $variable = ( $isset && ! empty( $variable[ $array_key_name ] ) ) ? $variable[ $array_key_name ] : $default;
807 - break;
940 + switch ($conditional) {
941 + case 'isset_empty':
942 + $variable = ($isset && ! empty($variable[$array_key_name])) ? $variable[$array_key_name] : $default;
943 + break;
808 944
809 - case 'empty':
810 - $variable = ! empty( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
811 - break;
945 + case 'empty':
946 + $variable = ! empty($variable[$array_key_name]) ? $variable[$array_key_name] : $default;
947 + break;
812 948
813 - case 'null':
814 - $variable = $isset && ! is_null( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
815 - break;
949 + case 'null':
950 + $variable = $isset && ! is_null($variable[$array_key_name]) ? $variable[$array_key_name] : $default;
951 + break;
816 952
817 - default:
818 - $variable = $isset && isset( $variable[ $array_key_name ] ) ? $variable[ $array_key_name ] : $default;
819 - }
820 - }
953 + default:
954 + $variable = $isset && isset($variable[$array_key_name]) ? $variable[$array_key_name] : $default;
955 + }
956 + }
821 957
822 - return $variable;
823 -
958 + return $variable;
824 959 }