|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Formatting functions for taking care of proper number formats and such |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Functions/Formatting |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Get decimal count |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.6 |
|
22
|
|
|
* |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
function give_get_price_decimals() { |
|
26
|
|
|
return apply_filters( 'give_sanitize_amount_decimals', give_get_option( 'number_decimals', 0 ) ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
52 |
|
/** |
|
30
|
52 |
|
* Get thousand separator |
|
31
|
52 |
|
* |
|
32
|
|
|
* @since 1.6 |
|
33
|
|
|
* |
|
34
|
52 |
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
function give_get_price_thousand_separator() { |
|
37
|
|
|
$give_options = give_get_settings(); |
|
38
|
|
|
$thousand_separator = isset( $give_options['thousands_separator'] ) ? $give_options['thousands_separator'] : ','; |
|
39
|
|
|
$thousand_separator = empty( $thousand_separator ) ? ' ' : $thousand_separator; |
|
40
|
|
|
|
|
41
|
|
|
return $thousand_separator; |
|
42
|
52 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get decimal separator |
|
46
|
52 |
|
* |
|
47
|
|
|
* @since 1.6 |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
52 |
|
*/ |
|
51
|
52 |
|
function give_get_price_decimal_separator() { |
|
52
|
52 |
|
$default_decimal_separators = array( |
|
53
|
|
|
'.' => ',', |
|
54
|
52 |
|
',' => '.', |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$thousand_separator = give_get_price_thousand_separator(); |
|
58
|
52 |
|
$default_decimal_separator = in_array( $thousand_separator, $default_decimal_separators ) ? |
|
59
|
|
|
$default_decimal_separators[$thousand_separator] : |
|
|
|
|
|
|
60
|
|
|
'.'; |
|
61
|
|
|
|
|
62
|
|
|
$decimal_separator = give_get_option( 'decimal_separator', $default_decimal_separator ); |
|
63
|
|
|
|
|
64
|
|
|
return $decimal_separator; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sanitize Amount before saving to database |
|
70
|
|
|
* |
|
71
|
|
|
* @since 1.8.12 |
|
72
|
|
|
* |
|
73
|
43 |
|
* @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands) |
|
74
|
43 |
|
* |
|
75
|
|
|
* @return string $amount Newly sanitized amount |
|
76
|
|
|
*/ |
|
77
|
43 |
|
function give_sanitize_amount_for_db( $number ) { |
|
78
|
|
|
return give_sanitize_amount( $number, 6 ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Sanitize Amount before saving to database |
|
83
|
|
|
* |
|
84
|
43 |
|
* @since 1.8.12 |
|
85
|
|
|
* |
|
86
|
|
|
* @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands) |
|
87
|
|
|
* @param int|bool $dp Number of decimals |
|
88
|
|
|
* @param bool $trim_zeros From end of string |
|
89
|
43 |
|
* |
|
90
|
|
|
* @return string $amount Newly sanitized amount |
|
91
|
|
|
*/ |
|
92
|
|
|
function give_maybe_sanitize_amount( $number, $dp = false, $trim_zeros = false ) { |
|
93
|
|
|
$thousand_separator = give_get_price_thousand_separator(); |
|
94
|
|
|
$decimal_separator = give_get_price_decimal_separator(); |
|
95
|
|
|
|
|
96
|
|
|
// Bailout. |
|
97
|
|
|
if( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) { |
|
|
|
|
|
|
98
|
|
|
return $number; |
|
99
|
43 |
|
}elseif ( |
|
100
|
|
|
( false == strpos( $number, $thousand_separator ) ) && |
|
|
|
|
|
|
101
|
|
|
( false === strpos( $number, $decimal_separator ) ) |
|
102
|
|
|
) { |
|
103
|
43 |
|
return number_format( $number, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' ); |
|
104
|
2 |
|
} |
|
105
|
2 |
|
|
|
106
|
|
|
// Handle thousand separator as '.' |
|
107
|
43 |
|
// Handle sanitize database values. |
|
108
|
|
|
$number_parts = explode( '.', $number ); |
|
109
|
43 |
|
$is_db_sanitize_val = ( 2 === count( $number_parts ) && ( 6 === strlen( $number_parts[1] ) ) ); |
|
110
|
|
|
|
|
111
|
43 |
|
if( $is_db_sanitize_val ) { |
|
|
|
|
|
|
112
|
|
|
// Sanitize database value. |
|
113
|
|
|
return number_format( $number, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' ); |
|
114
|
|
|
|
|
115
|
|
|
} elseif ( |
|
116
|
|
|
'.' === $thousand_separator && |
|
117
|
|
|
false !== strpos( $number, $thousand_separator ) |
|
118
|
|
|
){ |
|
119
|
|
|
// Fix point thousand separator value. |
|
120
|
|
|
if( ! $is_db_sanitize_val ) { |
|
|
|
|
|
|
121
|
|
|
$number = str_replace( '.', '', $number ); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return give_sanitize_amount( $number, $dp, $trim_zeros ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Sanitize Amount |
|
130
|
|
|
* |
|
131
|
|
|
* Returns a sanitized amount by stripping out thousands separators. |
|
132
|
|
|
* |
|
133
|
|
|
* @since 1.0 |
|
134
|
|
|
* |
|
135
|
|
|
* @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands) |
|
136
|
|
|
* @param int|bool $dp Number of decimals |
|
137
|
|
|
* @param bool $trim_zeros From end of string |
|
138
|
|
|
* |
|
139
|
|
|
* @return string $amount Newly sanitized amount |
|
140
|
|
|
*/ |
|
141
|
|
|
function give_sanitize_amount( $number, $dp = false, $trim_zeros = false ) { |
|
142
|
|
|
|
|
143
|
|
|
// Bailout. |
|
144
|
|
|
if ( empty( $number ) || ( ! is_numeric( $number ) && ! is_string( $number ) ) ) { |
|
145
|
|
|
return $number; |
|
146
|
45 |
|
} |
|
147
|
45 |
|
|
|
148
|
45 |
|
// Remove slash from amount. |
|
149
|
|
|
// If thousand or decimal separator is set to ' then in $_POST or $_GET param we will get an escaped number. |
|
150
|
45 |
|
// To prevent notices and warning remove slash from amount/number. |
|
151
|
|
|
$number = wp_unslash( $number ); |
|
152
|
45 |
|
|
|
153
|
|
|
$thousand_separator = give_get_price_thousand_separator(); |
|
154
|
45 |
|
|
|
155
|
|
|
$locale = localeconv(); |
|
156
|
|
|
$decimals = array( give_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] ); |
|
157
|
|
|
|
|
158
|
45 |
|
// Remove locale from string |
|
159
|
|
|
if ( ! is_float( $number ) ) { |
|
160
|
45 |
|
$number = str_replace( $decimals, '.', $number ); |
|
161
|
|
|
} |
|
162
|
45 |
|
|
|
163
|
45 |
|
// Remove thousand amount formatting if amount has. |
|
164
|
45 |
|
// This condition use to add backward compatibility to version before 1.6, because before version 1.6 we were saving formatted amount to db. |
|
165
|
45 |
|
// Do not replace thousand separator from price if it is same as decimal separator, because it will be already replace by above code. |
|
166
|
45 |
|
if ( ! in_array( $thousand_separator, $decimals ) && ( false !== strpos( $number, $thousand_separator ) ) ) { |
|
167
|
45 |
|
$number = str_replace( $thousand_separator, '', $number ); |
|
168
|
45 |
|
} elseif ( in_array( $thousand_separator, $decimals ) ) { |
|
169
|
45 |
|
$number = preg_replace( '/\.(?=.*\.)/', '', $number ); |
|
170
|
45 |
|
} |
|
171
|
45 |
|
|
|
172
|
45 |
|
// Remove non numeric entity before decimal separator. |
|
173
|
45 |
|
$number = preg_replace( '/[^0-9\.]/', '', $number ); |
|
174
|
45 |
|
$default_dp = give_get_price_decimals(); |
|
175
|
45 |
|
|
|
176
|
45 |
|
// Reset negative amount to zero. |
|
177
|
45 |
|
if ( 0 > $number ) { |
|
178
|
45 |
|
$number = number_format( 0, $default_dp, '.' ); |
|
179
|
45 |
|
} |
|
180
|
45 |
|
|
|
181
|
45 |
|
// If number does not have decimal then add number of decimals to it. |
|
182
|
45 |
|
if ( |
|
183
|
45 |
|
false === strpos( $number, '.' ) |
|
184
|
45 |
|
|| ( $default_dp > strlen( substr( $number, strpos( $number, '.' ) + 1 ) ) ) |
|
185
|
45 |
|
) { |
|
186
|
45 |
|
$number = number_format( $number, $default_dp, '.', '' ); |
|
187
|
45 |
|
} |
|
188
|
45 |
|
|
|
189
|
45 |
|
// Format number by custom number of decimals. |
|
190
|
45 |
|
if ( false !== $dp ) { |
|
191
|
45 |
|
$dp = intval( is_bool( $dp ) ? $default_dp : $dp ); |
|
192
|
|
|
$dp = apply_filters( 'give_sanitize_amount_decimals', $dp, $number ); |
|
193
|
|
|
$number = number_format( floatval( $number ), $dp, '.', '' ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// Trim zeros. |
|
197
|
|
|
if ( $trim_zeros && strstr( $number, '.' ) ) { |
|
198
|
|
|
$number = rtrim( rtrim( $number, '0' ), '.' ); |
|
199
|
45 |
|
} |
|
200
|
45 |
|
|
|
201
|
|
|
return apply_filters( 'give_sanitize_amount', $number ); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns a nicely formatted amount. |
|
206
|
|
|
* |
|
207
|
|
|
* @since 1.0 |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $amount Price amount to format |
|
210
|
|
|
* @param array $args Array of arguments. |
|
211
|
|
|
* |
|
212
|
|
|
* @return string $amount Newly formatted amount or Price Not Available |
|
213
|
|
|
*/ |
|
214
|
|
|
function give_format_amount( $amount, $args = array() ) { |
|
215
|
|
|
// Backward compatibility. |
|
216
|
|
|
if( is_bool( $args ) ) { |
|
|
|
|
|
|
217
|
|
|
$args = array( 'decimal' => $args ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$default_args = array( |
|
221
|
|
|
'decimal' => true, |
|
222
|
|
|
'sanitize' => true, |
|
223
|
|
|
'currency' => give_get_currency(), |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
$args = wp_parse_args( $args, $default_args ); |
|
227
|
|
|
|
|
228
|
|
|
$formatted = 0; |
|
229
|
|
|
$thousands_sep = give_get_price_thousand_separator(); |
|
230
|
|
|
$decimal_sep = give_get_price_decimal_separator(); |
|
231
|
|
|
$decimals = ! empty( $args['decimal'] ) ? give_get_price_decimals() : 0; |
|
232
|
|
|
$currency = $args['currency']; |
|
233
|
|
|
|
|
234
|
|
|
if ( ! empty( $amount ) ) { |
|
235
|
|
|
// Sanitize amount before formatting. |
|
236
|
|
|
$amount = ! empty( $args['sanitize'] ) ? |
|
237
|
|
|
give_maybe_sanitize_amount( $amount, $decimals ) : |
|
238
|
45 |
|
number_format( $amount, $decimals, '.', '' ); |
|
239
|
|
|
|
|
240
|
|
|
switch ( $currency ) { |
|
241
|
|
|
case 'INR': |
|
242
|
|
|
$decimal_amount = ''; |
|
243
|
45 |
|
|
|
244
|
|
|
// Extract decimals from amount |
|
245
|
|
|
if ( ( $pos = strpos( $amount, '.' ) ) !== false ) { |
|
|
|
|
|
|
246
|
|
|
if ( ! empty( $decimals ) ) { |
|
247
|
|
|
$decimal_amount = substr( round( substr( $amount, $pos ), $decimals ), 1 ); |
|
248
|
|
|
$amount = substr( $amount, 0, $pos ); |
|
249
|
|
|
|
|
250
|
|
|
if ( ! $decimal_amount ) { |
|
251
|
|
|
$decimal_amount = substr( '.0000000000', 0, ( $decimals + 1 ) ); |
|
252
|
|
|
} elseif ( ( $decimals + 1 ) > strlen( $decimal_amount ) ) { |
|
253
|
|
|
$decimal_amount = substr( "{$decimal_amount}000000000", 0, ( $decimals + 1 ) ); |
|
254
|
|
|
} |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
} else { |
|
257
|
55 |
|
$amount = number_format( $amount, $decimals, $decimal_sep, '' ); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
55 |
|
|
|
261
|
55 |
|
// Extract last 3 from amount |
|
262
|
55 |
|
$result = substr( $amount, - 3 ); |
|
263
|
55 |
|
$amount = substr( $amount, 0, - 3 ); |
|
264
|
|
|
|
|
265
|
|
|
// Apply digits 2 by 2 |
|
266
|
|
|
while ( strlen( $amount ) > 0 ) { |
|
267
|
|
|
$result = substr( $amount, - 2 ) . $thousands_sep . $result; |
|
268
|
|
|
$amount = substr( $amount, 0, - 2 ); |
|
269
|
55 |
|
} |
|
270
|
|
|
|
|
271
|
|
|
$formatted = $result . $decimal_amount; |
|
272
|
|
|
break; |
|
273
|
|
|
|
|
274
|
|
|
default: |
|
275
|
|
|
$formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep ); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return apply_filters( 'give_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep, $currency, $args ); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Get human readable amount. |
|
285
|
|
|
* |
|
286
|
|
|
* Note: This function only support large number formatting from million to trillion |
|
287
|
|
|
* |
|
288
|
|
|
* @since 1.6 |
|
289
|
|
|
* |
|
290
|
|
|
* @use give_get_price_thousand_separator Get thousand separator. |
|
291
|
|
|
* |
|
292
|
|
|
* @param string $amount formatted amount number. |
|
293
|
|
|
* @param array $args Array of arguments. |
|
294
|
|
|
* |
|
295
|
|
|
* @return float|string formatted amount number with large number names. |
|
296
|
|
|
*/ |
|
297
|
|
|
function give_human_format_large_amount( $amount, $args = array() ) { |
|
298
|
|
|
$default_args = array( |
|
299
|
|
|
'currency' => give_get_currency(), |
|
300
|
|
|
); |
|
301
|
|
|
|
|
302
|
|
|
$args = wp_parse_args( $args, $default_args ); |
|
303
|
|
|
|
|
304
|
|
|
// Get thousand separator. |
|
305
|
|
|
$thousands_sep = give_get_price_thousand_separator(); |
|
306
|
|
|
|
|
307
|
|
|
// Sanitize amount. |
|
308
|
|
|
$sanitize_amount = give_maybe_sanitize_amount( $amount ); |
|
309
|
|
|
|
|
310
|
|
|
// Explode amount to calculate name of large numbers. |
|
311
|
|
|
$amount_array = explode( $thousands_sep, $amount ); |
|
312
|
|
|
|
|
313
|
|
|
// Calculate amount parts count. |
|
314
|
|
|
$amount_count_parts = count( $amount_array ); |
|
315
|
|
|
|
|
316
|
|
|
// Human format amount (default). |
|
317
|
|
|
$human_format_amount = $amount; |
|
318
|
|
|
|
|
319
|
|
|
switch ( $args['currency'] ) { |
|
320
|
|
View Code Duplication |
case 'INR': |
|
|
|
|
|
|
321
|
|
|
// Calculate large number formatted amount. |
|
322
|
|
|
if ( 4 < $amount_count_parts ) { |
|
323
|
|
|
$human_format_amount = sprintf( esc_html__( '%s arab', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) ); |
|
324
|
|
|
} elseif ( 3 < $amount_count_parts ) { |
|
325
|
|
|
$human_format_amount = sprintf( esc_html__( '%s crore', 'give' ), round( ( $sanitize_amount / 10000000 ), 2 ) ); |
|
326
|
|
|
} elseif ( 2 < $amount_count_parts ) { |
|
327
|
|
|
$human_format_amount = sprintf( esc_html__( '%s lakh', 'give' ), round( ( $sanitize_amount / 100000 ), 2 ) ); |
|
328
|
|
|
} |
|
329
|
|
|
break; |
|
330
|
|
View Code Duplication |
default: |
|
|
|
|
|
|
331
|
|
|
// Calculate large number formatted amount. |
|
332
|
|
|
if ( 4 < $amount_count_parts ) { |
|
333
|
|
|
$human_format_amount = sprintf( esc_html__( '%s trillion', 'give' ), round( ( $sanitize_amount / 1000000000000 ), 2 ) ); |
|
334
|
|
|
} elseif ( 3 < $amount_count_parts ) { |
|
335
|
|
|
$human_format_amount = sprintf( esc_html__( '%s billion', 'give' ), round( ( $sanitize_amount / 1000000000 ), 2 ) ); |
|
336
|
|
|
} elseif ( 2 < $amount_count_parts ) { |
|
337
|
|
|
$human_format_amount = sprintf( esc_html__( '%s million', 'give' ), round( ( $sanitize_amount / 1000000 ), 2 ) ); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
return apply_filters( 'give_human_format_large_amount', $human_format_amount, $amount, $sanitize_amount ); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Returns a nicely formatted amount with custom decimal separator. |
|
346
|
|
|
* |
|
347
|
|
|
* @since 1.0 |
|
348
|
|
|
* |
|
349
|
|
|
* @param int|float|string $amount Formatted or sanitized price |
|
350
|
|
|
* @param int|bool $dp number of decimals |
|
351
|
|
|
* @param bool $sanitize Whether or not sanitize number |
|
352
|
|
|
* |
|
353
|
|
|
* @return string $amount Newly formatted amount or Price Not Available |
|
354
|
|
|
*/ |
|
355
|
|
|
function give_format_decimal( $amount, $dp = false, $sanitize = true ) { |
|
356
|
|
|
$decimal_separator = give_get_price_decimal_separator(); |
|
357
|
|
|
$formatted_amount = $sanitize ? |
|
358
|
|
|
give_maybe_sanitize_amount( $amount, $dp ) : |
|
359
|
|
|
number_format( $amount, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' ); |
|
360
|
|
|
|
|
361
|
|
|
if ( false !== strpos( $formatted_amount, '.' ) ) { |
|
362
|
|
|
$formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount ); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
return apply_filters( 'give_format_decimal', $formatted_amount, $amount, $decimal_separator ); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Formats the currency displayed. |
|
370
|
|
|
* |
|
371
|
|
|
* @since 1.0 |
|
372
|
|
|
* |
|
373
|
|
|
* @param string $price The donation amount. |
|
374
|
|
|
* @param string $currency The currency code. |
|
375
|
|
|
* @param bool $decode_currency Whether to decode the currency HTML format or not. |
|
376
|
|
|
* |
|
377
|
|
|
* @return mixed|string |
|
378
|
|
|
*/ |
|
379
|
|
|
function give_currency_filter( $price = '', $currency = '', $decode_currency = false ) { |
|
380
|
|
|
|
|
381
|
|
|
if ( empty( $currency ) || ! array_key_exists( (string) $currency, give_get_currencies() ) ) { |
|
382
|
|
|
$currency = give_get_currency(); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$position = give_get_option( 'currency_position', 'before' ); |
|
386
|
|
|
|
|
387
|
|
|
$negative = $price < 0; |
|
388
|
|
|
|
|
389
|
|
|
if ( $negative ) { |
|
390
|
|
|
// Remove proceeding "-". |
|
391
|
|
|
$price = substr( $price, 1 ); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
$symbol = give_currency_symbol( $currency, $decode_currency ); |
|
395
|
|
|
|
|
396
|
|
|
switch ( $currency ) : |
|
397
|
|
|
case 'GBP' : |
|
398
|
|
|
case 'BRL' : |
|
399
|
|
|
case 'EUR' : |
|
400
|
|
|
case 'USD' : |
|
401
|
|
|
case 'AUD' : |
|
402
|
|
|
case 'CAD' : |
|
403
|
|
|
case 'HKD' : |
|
404
|
|
|
case 'MXN' : |
|
405
|
|
|
case 'NZD' : |
|
406
|
|
|
case 'SGD' : |
|
407
|
|
|
case 'JPY' : |
|
408
|
|
|
case 'THB' : |
|
409
|
|
|
case 'INR' : |
|
410
|
|
|
case 'RIAL' : |
|
411
|
|
|
case 'TRY' : |
|
412
|
|
|
case 'RUB' : |
|
413
|
|
|
case 'SEK' : |
|
414
|
|
|
case 'PLN' : |
|
415
|
|
|
case 'PHP' : |
|
416
|
|
|
case 'TWD' : |
|
417
|
|
|
case 'MYR' : |
|
418
|
|
|
case 'CZK' : |
|
419
|
|
|
case 'DKK' : |
|
420
|
|
|
case 'HUF' : |
|
421
|
|
|
case 'ILS' : |
|
422
|
|
|
case 'MAD' : |
|
423
|
|
|
case 'KRW' : |
|
424
|
|
|
case 'ZAR' : |
|
425
|
|
|
$formatted = ( 'before' === $position ? $symbol . $price : $price . $symbol ); |
|
426
|
|
|
break; |
|
427
|
|
View Code Duplication |
case 'NOK' : |
|
|
|
|
|
|
428
|
|
|
$formatted = ( 'before' === $position ? $symbol . ' ' . $price : $price . ' ' . $symbol ); |
|
429
|
|
|
break; |
|
430
|
|
View Code Duplication |
default : |
|
|
|
|
|
|
431
|
|
|
$formatted = ( 'before' === $position ? $currency . ' ' . $price : $price . ' ' . $currency ); |
|
432
|
|
|
break; |
|
433
|
|
|
endswitch; |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* Filter formatted amount with currency |
|
437
|
|
|
* |
|
438
|
|
|
* Filter name depends upon current value of currency and currency position. |
|
439
|
|
|
* For example : |
|
440
|
|
|
* if currency is USD and currency position is before then |
|
441
|
|
|
* filter name will be give_usd_currency_filter_before |
|
442
|
|
|
* |
|
443
|
|
|
* and if currency is USD and currency position is after then |
|
444
|
|
|
* filter name will be give_usd_currency_filter_after |
|
445
|
|
|
*/ |
|
446
|
|
|
$formatted = apply_filters( 'give_' . strtolower( $currency ) . "_currency_filter_{$position}", $formatted, $currency, $price ); |
|
447
|
|
|
|
|
448
|
|
|
if ( $negative ) { |
|
449
|
|
|
// Prepend the minus sign before the currency sign. |
|
450
|
|
|
$formatted = '-' . $formatted; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
return $formatted; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Set the number of decimal places per currency |
|
458
|
|
|
* |
|
459
|
|
|
* @since 1.0 |
|
460
|
|
|
* @since 1.6 $decimals parameter removed from function params |
|
461
|
|
|
* * |
|
462
|
|
|
* @return int $decimals |
|
463
|
|
|
*/ |
|
464
|
|
|
function give_currency_decimal_filter() { |
|
465
|
|
|
|
|
466
|
|
|
remove_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' ); |
|
467
|
|
|
|
|
468
|
|
|
// Set default number of decimals. |
|
469
|
|
|
$decimals = give_get_price_decimals(); |
|
470
|
|
|
|
|
471
|
|
|
add_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' ); |
|
472
|
|
|
|
|
473
|
|
|
// Get number of decimals with backward compatibility ( version < 1.6 ) |
|
474
|
|
|
if ( 1 <= func_num_args() ) { |
|
475
|
|
|
$decimals = ( false === func_get_arg( 0 ) ? $decimals : absint( func_get_arg( 0 ) ) ); |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
$currency = give_get_currency(); |
|
479
|
|
|
|
|
480
|
|
|
switch ( $currency ) { |
|
481
|
|
|
case 'RIAL' : |
|
482
|
|
|
case 'JPY' : |
|
483
|
|
|
case 'TWD' : |
|
484
|
|
|
case 'HUF' : |
|
485
|
|
|
|
|
486
|
|
|
$decimals = 0; |
|
487
|
|
|
break; |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
return apply_filters( 'give_currency_decimal_count', $decimals, $currency ); |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
add_filter( 'give_sanitize_amount_decimals', 'give_currency_decimal_filter' ); |
|
494
|
|
|
add_filter( 'give_format_amount_decimals', 'give_currency_decimal_filter' ); |
|
495
|
|
|
|
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
|
|
* Get date format string on basis of given context. |
|
499
|
|
|
* |
|
500
|
|
|
* @since 1.7 |
|
501
|
|
|
* |
|
502
|
|
|
* @param string $date_context Date format context name. |
|
503
|
|
|
* |
|
504
|
|
|
* @return string Date format string |
|
505
|
|
|
*/ |
|
506
|
|
|
function give_date_format( $date_context = '' ) { |
|
507
|
|
|
/** |
|
508
|
|
|
* Filter the date context |
|
509
|
|
|
* |
|
510
|
|
|
* You can add your own date context or use already exist context. |
|
511
|
|
|
* For example: |
|
512
|
|
|
* add_filter( 'give_date_format_contexts', 'add_new_date_contexts' ); |
|
513
|
|
|
* function add_new_date_contexts( $date_format_contexts ) { |
|
514
|
|
|
* // You can add single context like this $date_format_contexts['checkout'] = 'F j, Y'; |
|
515
|
|
|
* // Instead add multiple date context at once. |
|
516
|
|
|
* $new_date_format_contexts = array( |
|
517
|
|
|
* 'checkout' => 'F j, Y', |
|
518
|
|
|
* 'report' => 'Y-m-d', |
|
519
|
|
|
* 'email' => 'm/d/Y', |
|
520
|
|
|
* ); |
|
521
|
|
|
* |
|
522
|
|
|
* // Merge date contexts array only if you are adding multiple date contexts at once otherwise return $date_format_contexts. |
|
523
|
|
|
* return array_merge( $new_date_format_contexts, $date_format_contexts ); |
|
524
|
|
|
* |
|
525
|
|
|
* } |
|
526
|
|
|
*/ |
|
527
|
|
|
$date_format_contexts = apply_filters( 'give_date_format_contexts', array() ); |
|
528
|
|
|
|
|
529
|
|
|
// Set date format to default date format. |
|
530
|
|
|
$date_format = get_option( 'date_format' ); |
|
531
|
|
|
|
|
532
|
|
|
// Update date format if we have non empty date format context array and non empty date format string for that context. |
|
533
|
|
|
if ( $date_context && ! empty( $date_format_contexts ) && array_key_exists( $date_context, $date_format_contexts ) ) { |
|
534
|
|
|
$date_format = ! empty( $date_format_contexts[ $date_context ] ) |
|
535
|
|
|
? $date_format_contexts[ $date_context ] |
|
536
|
|
|
: $date_format; |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
return apply_filters( 'give_date_format', $date_format ); |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
/** |
|
543
|
|
|
* Get cache key. |
|
544
|
|
|
* |
|
545
|
|
|
* @since 1.7 |
|
546
|
|
|
* @deprecated 1.8.7 You can access this function from Give_Cache. |
|
547
|
|
|
* |
|
548
|
|
|
* @param string $action Cache key prefix. |
|
549
|
|
|
* @param array $query_args Query array. |
|
550
|
|
|
* |
|
551
|
|
|
* @return string |
|
552
|
|
|
*/ |
|
553
|
|
|
function give_get_cache_key( $action, $query_args ) { |
|
554
|
|
|
return Give_Cache::get_key( $action, $query_args ); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* Clean variables using sanitize_text_field. Arrays are cleaned recursively. |
|
559
|
|
|
* Non-scalar values are ignored. |
|
560
|
|
|
* |
|
561
|
|
|
* @since 1.8 |
|
562
|
|
|
* |
|
563
|
|
|
* @param string|array $var |
|
564
|
|
|
* |
|
565
|
|
|
* @return string|array |
|
566
|
|
|
*/ |
|
567
|
|
|
function give_clean( $var ) { |
|
568
|
|
|
if ( is_array( $var ) ) { |
|
569
|
|
|
return array_map( 'give_clean', $var ); |
|
570
|
|
|
} else { |
|
571
|
|
|
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var; |
|
572
|
|
|
} |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
/** |
|
576
|
|
|
* Transforms php.ini notation for numbers (like '2M') to an integer. |
|
577
|
|
|
* |
|
578
|
|
|
* @since 1.8 |
|
579
|
|
|
* |
|
580
|
|
|
* @param $size |
|
581
|
|
|
* |
|
582
|
|
|
* @return int |
|
583
|
|
|
*/ |
|
584
|
|
|
function give_let_to_num( $size ) { |
|
585
|
|
|
$l = substr( $size, - 1 ); |
|
586
|
|
|
$ret = substr( $size, 0, - 1 ); |
|
587
|
|
|
switch ( strtoupper( $l ) ) { |
|
588
|
|
|
case 'P': |
|
589
|
|
|
$ret *= 1024; |
|
590
|
|
|
case 'T': |
|
591
|
|
|
$ret *= 1024; |
|
592
|
|
|
case 'G': |
|
593
|
|
|
$ret *= 1024; |
|
594
|
|
|
case 'M': |
|
595
|
|
|
$ret *= 1024; |
|
596
|
|
|
case 'K': |
|
597
|
|
|
$ret *= 1024; |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
return $ret; |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
/** |
|
604
|
|
|
* Verify nonce. |
|
605
|
|
|
* |
|
606
|
|
|
* @since 1.8 |
|
607
|
|
|
* |
|
608
|
|
|
* @param $nonce |
|
609
|
|
|
* @param int $action |
|
610
|
|
|
* @param array $wp_die_args |
|
611
|
|
|
*/ |
|
612
|
|
|
function give_validate_nonce( $nonce, $action = - 1, $wp_die_args = array() ) { |
|
613
|
|
|
|
|
614
|
|
|
$default_wp_die_args = array( |
|
615
|
|
|
'message' => esc_html__( 'Nonce verification has failed.', 'give' ), |
|
616
|
|
|
'title' => esc_html__( 'Error', 'give' ), |
|
617
|
|
|
'args' => array( |
|
618
|
|
|
'response' => 403, |
|
619
|
|
|
), |
|
620
|
|
|
); |
|
621
|
|
|
|
|
622
|
|
|
$wp_die_args = wp_parse_args( $wp_die_args, $default_wp_die_args ); |
|
623
|
|
|
|
|
624
|
|
|
if ( ! wp_verify_nonce( $nonce, $action ) ) { |
|
625
|
|
|
wp_die( |
|
626
|
|
|
$wp_die_args['message'], |
|
627
|
|
|
$wp_die_args['title'], |
|
628
|
|
|
$wp_die_args['args'] |
|
629
|
|
|
); |
|
630
|
|
|
} |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
/** |
|
634
|
|
|
* Check variable and get default or valid value. |
|
635
|
|
|
* |
|
636
|
|
|
* Helper function to check if a variable is set, empty, etc. |
|
637
|
|
|
* |
|
638
|
|
|
* @since 1.8 |
|
639
|
|
|
* |
|
640
|
|
|
* @param $variable |
|
641
|
|
|
* @param string (optional) $conditional , default value: isset |
|
642
|
|
|
* @param bool (optional) $default , default value: false |
|
643
|
|
|
* |
|
644
|
|
|
* @return mixed |
|
645
|
|
|
*/ |
|
646
|
|
|
function give_check_variable( $variable, $conditional = '', $default = false ) { |
|
647
|
|
|
|
|
648
|
|
|
switch ( $conditional ) { |
|
649
|
|
|
case 'isset_empty': |
|
650
|
|
|
$variable = ( isset( $variable ) && ! empty( $variable ) ) ? $variable : $default; |
|
651
|
|
|
break; |
|
652
|
|
|
|
|
653
|
|
|
case 'empty': |
|
654
|
|
|
$variable = ! empty( $variable ) ? $variable : $default; |
|
655
|
|
|
break; |
|
656
|
|
|
|
|
657
|
|
|
case 'null': |
|
658
|
|
|
$variable = ! is_null( $variable ) ? $variable : $default; |
|
659
|
|
|
break; |
|
660
|
|
|
|
|
661
|
|
|
default: |
|
662
|
|
|
$variable = isset( $variable ) ? $variable : $default; |
|
663
|
|
|
|
|
664
|
|
|
} |
|
665
|
|
|
|
|
666
|
|
|
return $variable; |
|
667
|
|
|
|
|
668
|
|
|
} |
|
669
|
|
|
|