PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.32.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.32.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmCurrencyHelper.php

FrmCurrencyHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.32.1, at classes/helpers/FrmCurrencyHelper.php

464 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 die( 'You are not allowed to call this page directly.' );
5 }
6
7 /**
8 * @since 6.5 Added with Stripe Lite.
9 */
10 class FrmCurrencyHelper {
11
12 /**
13 * Gets the currency data from the currency code.
14 *
15 * @since 6.30 The first parameter is optional.
16 *
17 * @param string|null $currency Currency code. Default is `null`, which use the currency in the global settings.
18 *
19 * @return array
20 */
21 public static function get_currency( $currency = null ) {
22 if ( ! $currency ) {
23 $currency = trim( FrmAppHelper::get_settings()->currency );
24 }
25
26 if ( ! $currency ) {
27 $currency = 'USD';
28 }
29
30 $currency = strtoupper( $currency );
31 $currencies = self::get_currencies();
32
33 if ( isset( $currencies[ $currency ] ) ) {
34 $currency = $currencies[ $currency ];
35 } elseif ( isset( $currencies[ strtolower( $currency ) ] ) ) {
36 $currency = $currencies[ strtolower( $currency ) ];
37 } else {
38 $currency = $currencies['USD'];
39 }
40
41 return $currency;
42 }
43
44 /**
45 * Checks if the given format is a valid currency format.
46 *
47 * @since 6.18
48 *
49 * @param string $format_value The format value to check.
50 *
51 * @return bool
52 */
53 public static function is_currency_format( $format_value ) {
54 return in_array( $format_value, array( 'currency', 'number' ), true );
55 }
56
57 /**
58 * Adds the currency symbol to the given amount.
59 *
60 * @param float|string $amount
61 * @param array|null $currency
62 *
63 * @return string
64 */
65 public static function format_price( $amount, $currency = null ) {
66 if ( ! $currency ) {
67 $currency = self::get_currency();
68 }
69
70 if ( is_string( $amount ) ) {
71 $amount = floatval( self::prepare_price( $amount, $currency ) );
72 }
73
74 $amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] );
75
76 if ( '' !== $currency['symbol_left'] ) {
77 $amount = $currency['symbol_left'] . $currency['symbol_padding'] . $amount;
78 }
79
80 if ( '' !== $currency['symbol_right'] ) {
81 $amount .= $currency['symbol_padding'] . $currency['symbol_right'];
82 }
83
84 return $amount;
85 }
86
87 /**
88 * @since 6.30
89 *
90 * @param string $price
91 * @param array $currency
92 *
93 * @return float|string
94 */
95 public static function prepare_price( $price, $currency ) {
96 $price = trim( $price );
97
98 if ( ! $price ) {
99 return 0;
100 }
101
102 preg_match_all( '/[\-]*[0-9,.]*\.?\,?[0-9]+/', $price, $matches );
103 $price = $matches ? end( $matches[0] ) : 0;
104
105 if ( ! $price ) {
106 return 0;
107 }
108
109 $price = self::maybe_use_decimal( $price, $currency );
110 return str_replace( $currency['decimal_separator'], '.', str_replace( $currency['thousand_separator'], '', $price ) );
111 }
112
113 /**
114 * @since 6.30
115 *
116 * @param string $amount
117 * @param array $currency
118 *
119 * @return string
120 */
121 private static function maybe_use_decimal( $amount, $currency ) {
122 if ( $currency['thousand_separator'] !== '.' ) {
123 return $amount;
124 }
125
126 $amount_parts = explode( '.', $amount );
127 $used_for_decimal = count( $amount_parts ) === 2 && in_array( strlen( $amount_parts[1] ), array( 1, 2 ), true );
128
129 if ( $used_for_decimal ) {
130 return str_replace( '.', $currency['decimal_separator'], $amount );
131 }
132
133 return $amount;
134 }
135
136 /**
137 * If the currency is needed for this form, add it to the global.
138 * This is later included in the footer.
139 *
140 * @since 6.30
141 *
142 * @param int|string $form_id Form ID. This is used for Pro compatibility.
143 *
144 * @return void
145 */
146 public static function add_currency_to_global( $form_id ) {
147 global $frm_vars;
148
149 if ( ! isset( $frm_vars['currency'] ) ) {
150 $frm_vars['currency'] = array();
151 }
152
153 if ( ! isset( $frm_vars['currency'][ $form_id ] ) ) {
154 $frm_vars['currency'][ $form_id ] = self::get_currency();
155 }
156 }
157
158 /**
159 * Get a list of all supported currencies.
160 *
161 * @since 6.5.
162 *
163 * @return array
164 */
165 public static function get_currencies() {
166 $currencies = array(
167 'AUD' => array(
168 'name' => __( 'Australian Dollar', 'formidable' ),
169 'symbol_left' => '$',
170 'symbol_right' => '',
171 'symbol_padding' => ' ',
172 'thousand_separator' => ',',
173 'decimal_separator' => '.',
174 'decimals' => 2,
175 ),
176 'BDT' => array(
177 'name' => __( 'Bangladeshi Taka', 'formidable' ),
178 'symbol_left' => '',
179 'symbol_right' => '',
180 'symbol_padding' => ' ',
181 'thousand_separator' => ',',
182 'decimal_separator' => '.',
183 'decimals' => 2,
184 ),
185 'BRL' => array(
186 'name' => __( 'Brazilian Real', 'formidable' ),
187 'symbol_left' => 'R$',
188 'symbol_right' => '',
189 'symbol_padding' => ' ',
190 'thousand_separator' => '.',
191 'decimal_separator' => ',',
192 'decimals' => 2,
193 ),
194 'CAD' => array(
195 'name' => __( 'Canadian Dollar', 'formidable' ),
196 'symbol_left' => '$',
197 'symbol_right' => 'CAD',
198 'symbol_padding' => ' ',
199 'thousand_separator' => ',',
200 'decimal_separator' => '.',
201 'decimals' => 2,
202 ),
203 'CNY' => array(
204 'name' => __( 'Chinese Renminbi Yuan', 'formidable' ),
205 'symbol_left' => '¥',
206 'symbol_right' => '',
207 'symbol_padding' => ' ',
208 'thousand_separator' => ',',
209 'decimal_separator' => '.',
210 'decimals' => 2,
211 ),
212 'CZK' => array(
213 'name' => __( 'Czech Koruna', 'formidable' ),
214 'symbol_left' => '',
215 'symbol_right' => '&#75;&#269;',
216 'symbol_padding' => ' ',
217 'thousand_separator' => ' ',
218 'decimal_separator' => ',',
219 'decimals' => 2,
220 ),
221 'DKK' => array(
222 'name' => __( 'Danish Krone', 'formidable' ),
223 'symbol_left' => 'Kr',
224 'symbol_right' => '',
225 'symbol_padding' => ' ',
226 'thousand_separator' => '.',
227 'decimal_separator' => ',',
228 'decimals' => 2,
229 ),
230 'EUR' => array(
231 'name' => __( 'Euro', 'formidable' ),
232 'symbol_left' => '',
233 'symbol_right' => '&#8364;',
234 'symbol_padding' => ' ',
235 'thousand_separator' => '.',
236 'decimal_separator' => ',',
237 'decimals' => 2,
238 ),
239 'HKD' => array(
240 'name' => __( 'Hong Kong Dollar', 'formidable' ),
241 'symbol_left' => 'HK$',
242 'symbol_right' => '',
243 'symbol_padding' => '',
244 'thousand_separator' => ',',
245 'decimal_separator' => '.',
246 'decimals' => 2,
247 ),
248 'HUF' => array(
249 'name' => __( 'Hungarian Forint', 'formidable' ),
250 'symbol_left' => '',
251 'symbol_right' => 'Ft',
252 'symbol_padding' => ' ',
253 'thousand_separator' => '.',
254 'decimal_separator' => ',',
255 'decimals' => 2,
256 ),
257 'INR' => array(
258 'name' => __( 'Indian Rupee', 'formidable' ),
259 'symbol_left' => '&#8377;',
260 'symbol_right' => '',
261 'symbol_padding' => ' ',
262 'thousand_separator' => ',',
263 'decimal_separator' => '.',
264 'decimals' => 2,
265 ),
266 'ILS' => array(
267 'name' => __( 'Israeli New Sheqel', 'formidable' ),
268 'symbol_left' => '&#8362;',
269 'symbol_right' => '',
270 'symbol_padding' => ' ',
271 'thousand_separator' => ',',
272 'decimal_separator' => '.',
273 'decimals' => 2,
274 ),
275 'JPY' => array(
276 'name' => __( 'Japanese Yen', 'formidable' ),
277 'symbol_left' => '&#165;',
278 'symbol_right' => '',
279 'symbol_padding' => ' ',
280 'thousand_separator' => ',',
281 'decimal_separator' => '',
282 'decimals' => 0,
283 ),
284 'MYR' => array(
285 'name' => __( 'Malaysian Ringgit', 'formidable' ),
286 'symbol_left' => '&#82;&#77;',
287 'symbol_right' => '',
288 'symbol_padding' => ' ',
289 'thousand_separator' => ',',
290 'decimal_separator' => '.',
291 'decimals' => 2,
292 ),
293 'MXN' => array(
294 'name' => __( 'Mexican Peso', 'formidable' ),
295 'symbol_left' => '$',
296 'symbol_right' => '',
297 'symbol_padding' => ' ',
298 'thousand_separator' => ',',
299 'decimal_separator' => '.',
300 'decimals' => 2,
301 ),
302 'NOK' => array(
303 'name' => __( 'Norwegian Krone', 'formidable' ),
304 'symbol_left' => 'Kr',
305 'symbol_right' => '',
306 'symbol_padding' => ' ',
307 'thousand_separator' => '.',
308 'decimal_separator' => ',',
309 'decimals' => 2,
310 ),
311 'NZD' => array(
312 'name' => __( 'New Zealand Dollar', 'formidable' ),
313 'symbol_left' => '$',
314 'symbol_right' => '',
315 'symbol_padding' => ' ',
316 'thousand_separator' => ',',
317 'decimal_separator' => '.',
318 'decimals' => 2,
319 ),
320 'PKR' => array(
321 'name' => __( 'Pakistani Rupee', 'formidable' ),
322 'symbol_left' => '',
323 'symbol_right' => '',
324 'symbol_padding' => ' ',
325 'thousand_separator' => '',
326 'decimal_separator' => '.',
327 'decimals' => 2,
328 ),
329 'PHP' => array(
330 'name' => __( 'Philippine Peso', 'formidable' ),
331 'symbol_left' => 'Php',
332 'symbol_right' => '',
333 'symbol_padding' => ' ',
334 'thousand_separator' => ',',
335 'decimal_separator' => '.',
336 'decimals' => 2,
337 ),
338 'PLN' => array(
339 'name' => __( 'Polish Zloty', 'formidable' ),
340 'symbol_left' => '&#122;&#322;',
341 'symbol_right' => '',
342 'symbol_padding' => ' ',
343 'thousand_separator' => '.',
344 'decimal_separator' => ',',
345 'decimals' => 2,
346 ),
347 'GBP' => array(
348 'name' => __( 'Pound Sterling', 'formidable' ),
349 'symbol_left' => '&#163;',
350 'symbol_right' => '',
351 'symbol_padding' => ' ',
352 'thousand_separator' => ',',
353 'decimal_separator' => '.',
354 'decimals' => 2,
355 ),
356 'SGD' => array(
357 'name' => __( 'Singapore Dollar', 'formidable' ),
358 'symbol_left' => '$',
359 'symbol_right' => '',
360 'symbol_padding' => ' ',
361 'thousand_separator' => ',',
362 'decimal_separator' => '.',
363 'decimals' => 2,
364 ),
365 'ZAR' => array(
366 'name' => __( 'South African Rand', 'formidable' ),
367 'symbol_left' => 'R',
368 'symbol_right' => '',
369 'symbol_padding' => ' ',
370 'thousand_separator' => ' ',
371 'decimal_separator' => '.',
372 'decimals' => 2,
373 ),
374 'LKR' => array(
375 'name' => __( 'Sri Lankan Rupee', 'formidable' ),
376 'symbol_left' => '',
377 'symbol_right' => '',
378 'symbol_padding' => ' ',
379 'thousand_separator' => '',
380 'decimal_separator' => '.',
381 'decimals' => 2,
382 ),
383 'SEK' => array(
384 'name' => __( 'Swedish Krona', 'formidable' ),
385 'symbol_left' => '',
386 'symbol_right' => 'Kr',
387 'symbol_padding' => ' ',
388 'thousand_separator' => ' ',
389 'decimal_separator' => ',',
390 'decimals' => 2,
391 ),
392 'CHF' => array(
393 'name' => __( 'Swiss Franc', 'formidable' ),
394 'symbol_left' => 'Fr.',
395 'symbol_right' => '',
396 'symbol_padding' => ' ',
397 'thousand_separator' => "'",
398 'decimal_separator' => '.',
399 'decimals' => 2,
400 ),
401 'TWD' => array(
402 'name' => __( 'Taiwan New Dollar', 'formidable' ),
403 'symbol_left' => '$',
404 'symbol_right' => '',
405 'symbol_padding' => ' ',
406 'thousand_separator' => ',',
407 'decimal_separator' => '.',
408 'decimals' => 2,
409 ),
410 'THB' => array(
411 'name' => __( 'Thai Baht', 'formidable' ),
412 'symbol_left' => '&#3647;',
413 'symbol_right' => '',
414 'symbol_padding' => ' ',
415 'thousand_separator' => ',',
416 'decimal_separator' => '.',
417 'decimals' => 2,
418 ),
419 'TRY' => array(
420 'name' => __( 'Turkish Liras', 'formidable' ),
421 'symbol_left' => '',
422 'symbol_right' => '&#8378;',
423 'symbol_padding' => ' ',
424 'thousand_separator' => '.',
425 'decimal_separator' => ',',
426 'decimals' => 2,
427 ),
428 'USD' => array(
429 'name' => __( 'U.S. Dollar', 'formidable' ),
430 'symbol_left' => '$',
431 'symbol_right' => '',
432 'symbol_padding' => '',
433 'thousand_separator' => ',',
434 'decimal_separator' => '.',
435 'decimals' => 2,
436 ),
437 'UYU' => array(
438 'name' => __( 'Uruguayan Peso', 'formidable' ),
439 'symbol_left' => '$U',
440 'symbol_right' => '',
441 'symbol_padding' => '',
442 'thousand_separator' => '.',
443 'decimal_separator' => ',',
444 'decimals' => 0,
445 ),
446 );
447
448 /**
449 * @since 6.5
450 *
451 * @param array $currencies
452 */
453 $filtered_currencies = apply_filters( 'frm_currencies', $currencies );
454
455 if ( is_array( $filtered_currencies ) ) {
456 $currencies = $filtered_currencies;
457 } else {
458 _doing_it_wrong( __METHOD__, 'Only arrays should be returned when using the frm_currencies filter.', '6.5' );
459 }
460
461 return $currencies;
462 }
463 }
464