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