PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.2
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / currency / class-charitable-currency.php

class-charitable-currency.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.2, at includes/currency/class-charitable-currency.php

867 lines 23.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Currency helper.
4 *
5 * @package Charitable/Classes/Charitable_Currency
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.6.55
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Currency' ) ) :
19
20 /**
21 * Charitable_Currency
22 *
23 * @since 1.0.0
24 */
25 final class Charitable_Currency {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_Currency|null
31 */
32 private static $instance = null;
33
34 /**
35 * Every currency available.
36 *
37 * @var string[]
38 */
39 private $currencies = array();
40
41 /**
42 * Create class object. A private constructor, so this is used in a singleton context.
43 *
44 * @since 1.2.3
45 */
46 private function __construct() {
47 add_filter( 'charitable_option_decimal_count', array( $this, 'maybe_force_zero_decimals' ) );
48 }
49
50 /**
51 * Returns and/or create the single instance of this class.
52 *
53 * @since 1.2.3
54 *
55 * @return Charitable_Currency
56 */
57 public static function get_instance() {
58 if ( is_null( self::$instance ) ) {
59 self::$instance = new self();
60 }
61
62 return self::$instance;
63 }
64
65 /**
66 * Return an amount as a monetary string.
67 *
68 * 50.00 -> $50.00
69 *
70 * @since 1.0.0
71 *
72 * @param string $amount The amount to convert.
73 * @param int|false $decimal_count Optional. If not set, default decimal count will be used.
74 * @param boolean $db_format Optional. Whether the amount is in db format (i.e. using decimals
75 * for cents, regardless of site settings).
76 * @param string $symbol_position Optional. If specified, will use this symbol position setting instead
77 * of the site's currency format.
78 * @param string $currency Optional. If passed, will use the given currency's formatting, not the
79 * default currency.
80 * @return string|WP_Error
81 */
82 public function get_monetary_amount( $amount, $decimal_count = false, $db_format = false, $symbol_position = '', $currency = '' ) {
83 if ( false === $decimal_count ) {
84 $decimal_count = charitable_get_option( 'decimal_count', 2 );
85 }
86
87 $amount = $this->sanitize_monetary_amount( strval( $amount ), $db_format );
88
89 $amount = number_format(
90 $amount,
91 (int) $decimal_count,
92 $this->get_decimal_separator(),
93 $this->get_thousands_separator()
94 );
95
96 $formatted = sprintf( $this->get_currency_format( $symbol_position ), $this->get_currency_symbol( $currency ), $amount );
97
98 /**
99 * Filter the amount.
100 *
101 * @since 1.0.0
102 *
103 * @param string $formatted The formatted amount.
104 * @param string $amount The original amount before formatting.
105 */
106 return apply_filters( 'charitable_monetary_amount', $formatted, $amount );
107 }
108
109 /**
110 * Receives unfiltered monetary amount and sanitizes it, returning it as a float.
111 *
112 * $50.00 -> 50.00
113 *
114 * @since 1.0.0
115 *
116 * @param string $amount The amount to sanitize.
117 * @param boolean $db_format Optional. Whether the amount is in db format (i.e. using decimals for cents, regardless of site settings).
118 * @return float|WP_Error
119 */
120 public function sanitize_monetary_amount( $amount, $db_format = false ) {
121 /* Sending anything other than a string can cause unexpected returns, so we require strings. */
122 if ( ! is_string( $amount ) ) {
123 charitable_get_deprecated()->doing_it_wrong(
124 __METHOD__,
125 __( 'Amount must be passed as a string.', 'charitable' ),
126 '1.0.0'
127 );
128
129 return new WP_Error( 'invalid_parameter_type', 'Amount must be passed as a string.' );
130 }
131
132 /**
133 * If we're using commas for decimals, we need to turn any commas into points, and
134 * we need to replace existing points with blank spaces. Example:
135 *
136 * 12.500,50 -> 12500.50
137 */
138 if ( ! $db_format && $this->is_comma_decimal() ) {
139 /* Convert to 12.500_50 */
140 $amount = str_replace( ',', '_', $amount );
141 /* Convert to 12500_50 */
142 $amount = str_replace( '.', '', $amount );
143 /* Convert to 12500.50 */
144 $amount = str_replace( '_', '.', $amount );
145 }
146
147 $amount = str_replace( $this->get_currency_symbol(), '', $amount );
148
149 return floatval( filter_var( $amount, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) );
150 }
151
152 /**
153 * Returns an amount as a localized monetary string, without a currency symbol.
154 *
155 * @since 1.6.11
156 * @since 1.6.49 Added $db_format argument.
157 *
158 * @param string $amount The amount to convert.
159 * @param int|false $decimal_count Optional. If not set, default decimal count will be used.
160 * @param boolean $db_format Optional. Whether the amount is in db format (i.e. using decimals
161 * for cents, regardless of site settings).
162 * @return string
163 */
164 public function get_sanitized_and_localized_amount( $amount, $decimal_count = false, $db_format = false ) {
165 if ( false === $decimal_count ) {
166 $decimal_count = charitable_get_option( 'decimal_count', 2 );
167 }
168
169 $amount = $this->sanitize_monetary_amount( strval( $amount ), $db_format );
170
171 return number_format(
172 $amount,
173 (int) $decimal_count,
174 $this->get_decimal_separator(),
175 $this->get_thousands_separator()
176 );
177 }
178
179 /**
180 * Turns a database amount into an amount formatted for the currency that the site is in.
181 *
182 * @since 1.3.0
183 *
184 * @param string $amount The amount to be sanitized.
185 * @return string
186 */
187 public function sanitize_database_amount( $amount ) {
188 if ( $this->is_comma_decimal() ) {
189 $amount = str_replace( '.', ',', $amount );
190 }
191
192 return $amount;
193 }
194
195 /**
196 * Force a string amount into decimal based format, regardless of the site currency.
197 *
198 * This effectively reverses the effect of Charitable_Currency::sanitize_database_amount.
199 *
200 * @since 1.6.0
201 *
202 * @param string $amount The amount to be cast to decimal format.
203 * @return string
204 */
205 public function cast_to_decimal_format( $amount ) {
206
207 if ( is_wp_error( $amount ) ) {
208 return $amount;
209 }
210
211 if ( $this->is_comma_decimal() ) {
212 $amount = str_replace( ',', '.', $amount );
213 } else {
214 $amount = str_replace( ',', '', $amount );
215 }
216
217 return $amount;
218 }
219
220 /**
221 * Checks whether the comma is being used as the separator.
222 *
223 * @since 1.0.0
224 *
225 * @return boolean
226 */
227 public function is_comma_decimal() {
228 return ( ',' == $this->get_decimal_separator() );
229 }
230
231 /**
232 * Return the currency format based on the position of the currency symbol.
233 *
234 * @since 1.0.0
235 * @since 1.6.38 Added optional $symbol_position argument.
236 *
237 * @param string $symbol_position The symbol position. If not set, the site's
238 * currency_format setting will be used.
239 * @return string
240 */
241 public function get_currency_format( $symbol_position = '' ) {
242 if ( empty( $symbol_position ) ) {
243 $symbol_position = charitable_get_option( 'currency_format', 'left' );
244 }
245
246 switch ( $symbol_position ) {
247 case 'left':
248 $format = '%1$s%2$s';
249 break;
250 case 'right':
251 $format = '%2$s%1$s';
252 break;
253 case 'left-with-space':
254 $format = '%1$s&nbsp;%2$s';
255 break;
256 case 'right-with-space':
257 $format = '%2$s&nbsp;%1$s';
258 break;
259 default:
260 /**
261 * Filter the fallback currency format.
262 *
263 * @since 1.0.0
264 *
265 * @param string $format The currency format.
266 * @param string $symbol_position Where the symbol is positioned in relation to the amount.
267 */
268 $format = apply_filters( 'charitable_currency_format', '%1$s%2$s', $symbol_position );
269 }
270
271 return $format;
272 }
273
274 /**
275 * Get the currency format for accounting.js
276 *
277 * @since 1.3.0
278 *
279 * @return string
280 */
281 public function get_accounting_js_format() {
282 switch ( charitable_get_option( 'currency_format', 'left' ) ) {
283 case 'right':
284 $format = '%v%s';
285 break;
286 case 'left-with-space':
287 $format = '%s %v';
288 break;
289 case 'right-with-space':
290 $format = '%v %s';
291 break;
292 default:
293 $format = '%s%v';
294 }
295
296 /**
297 * Filter the currency format to use with accounting.js.
298 *
299 * @since 1.3.0
300 *
301 * @param string $format The currency format.
302 */
303 return apply_filters( 'charitable_accounting_js_currency_format', $format );
304 }
305
306 /**
307 * Return every currency symbol used with the
308 *
309 * @since 1.0.0
310 *
311 * @return string[]
312 */
313 public function get_all_currencies() {
314 if ( empty( $this->currencies ) ) {
315 /**
316 * Filter the currencies available in Charitable.
317 *
318 * @since 1.0.0
319 *
320 * @param array $currencies All currencies as a key=>index array, with
321 * the currency code as the key and the currency
322 * name including symbol as value.
323 */
324 $this->currencies = apply_filters(
325 'charitable_currencies',
326 array(
327 'ARS' => sprintf(
328 /* translators: %s: currency symbol */
329 __( 'Argentine Peso (%s)', 'charitable' ),
330 $this->get_currency_symbol( 'ARS' )
331 ),
332 'AUD' => sprintf(
333 /* translators: %s: currency symbol */
334 __( 'Australian Dollars (%s)', 'charitable' ),
335 $this->get_currency_symbol( 'AUD' )
336 ),
337 'BDT' => sprintf(
338 /* translators: %s: currency symbol */
339 __( 'Bangladeshi Taka (%s)', 'charitable' ),
340 $this->get_currency_symbol( 'BDT' )
341 ),
342 'BOB' => sprintf(
343 /* translators: %s: currency symbol */
344 __( 'Bolivian Bolíviano (%s)', 'charitable' ),
345 $this->get_currency_symbol( 'BOB' )
346 ),
347 'BRL' => sprintf(
348 /* translators: %s: currency symbol */
349 __( 'Brazilian Real (%s)', 'charitable' ),
350 $this->get_currency_symbol( 'BRL' )
351 ),
352 'BND' => sprintf(
353 /* translators: %s: currency symbol */
354 __( 'Brunei Dollar (%s)', 'charitable' ),
355 $this->get_currency_symbol( 'BND' )
356 ),
357 'BGN' => sprintf(
358 /* translators: %s: currency symbol */
359 __( 'Bulgarian Lev (%s)', 'charitable' ),
360 $this->get_currency_symbol( 'BGN' )
361 ),
362 'CAD' => sprintf(
363 /* translators: %s: currency symbol */
364 __( 'Canadian Dollar (%s)', 'charitable' ),
365 $this->get_currency_symbol( 'CAD' )
366 ),
367 'CHF' => sprintf(
368 /* translators: %s: currency symbol */
369 __( 'Swiss Franc (%s)', 'charitable' ),
370 $this->get_currency_symbol( 'CHF' )
371 ),
372 'CLP' => sprintf(
373 /* translators: %s: currency symbol */
374 __( 'Chilean Peso (%s)', 'charitable' ),
375 $this->get_currency_symbol( 'CLP' )
376 ),
377 'CNY' => sprintf(
378 /* translators: %s: currency symbol */
379 __( 'Chinese Yuan Renminbi (%s)', 'charitable' ),
380 $this->get_currency_symbol( 'CNY' )
381 ),
382 'COP' => sprintf(
383 /* translators: %s: currency symbol */
384 __( 'Colombian Peso (%s)', 'charitable' ),
385 $this->get_currency_symbol( 'COP' )
386 ),
387 'CZK' => sprintf(
388 /* translators: %s: currency symbol */
389 __( 'Czech Koruna (%s)', 'charitable' ),
390 $this->get_currency_symbol( 'CZK' )
391 ),
392 'DKK' => sprintf(
393 /* translators: %s: currency symbol */
394 __( 'Danish Krone (%s)', 'charitable' ),
395 $this->get_currency_symbol( 'DKK' )
396 ),
397 'EGP' => sprintf(
398 /* translators: %s: currency symbol */
399 __( 'Egyptian Pound (%s)', 'charitable' ),
400 $this->get_currency_symbol( 'EGP' )
401 ),
402 'AED' => sprintf(
403 /* translators: %s: currency symbol */
404 __( 'Emirati Dirham (%s)', 'charitable' ),
405 $this->get_currency_symbol( 'AED' )
406 ),
407 'EUR' => sprintf(
408 /* translators: %s: currency symbol */
409 __( 'Euro (%s)', 'charitable' ),
410 $this->get_currency_symbol( 'EUR' )
411 ),
412 'FJD' => sprintf(
413 /* translators: %s: currency symbol */
414 __( 'Fijian Dollar (%s)', 'charitable' ),
415 $this->get_currency_symbol( 'FJD' )
416 ),
417 'GBP' => sprintf(
418 /* translators: %s: currency symbol */
419 __( 'British Pound (%s)', 'charitable' ),
420 $this->get_currency_symbol( 'GBP' )
421 ),
422 'GHS' => sprintf(
423 /* translators: %s: currency symbol */
424 __( 'Ghanaian Cedi (%s)', 'charitable' ),
425 $this->get_currency_symbol( 'GHS' )
426 ),
427 'HKD' => sprintf(
428 /* translators: %s: currency symbol */
429 __( 'Hong Kong Dollar (%s)', 'charitable' ),
430 $this->get_currency_symbol( 'HKD' )
431 ),
432 'HRK' => sprintf(
433 /* translators: %s: currency symbol */
434 __( 'Croatian Kuna (%s)', 'charitable' ),
435 $this->get_currency_symbol( 'HRK' )
436 ),
437 'HUF' => sprintf(
438 /* translators: %s: currency symbol */
439 __( 'Hungarian Forint (%s)', 'charitable' ),
440 $this->get_currency_symbol( 'HUF' )
441 ),
442 'IDR' => sprintf(
443 /* translators: %s: currency symbol */
444 __( 'Indonesian Rupiah (%s)', 'charitable' ),
445 $this->get_currency_symbol( 'IDR' )
446 ),
447 'ILS' => sprintf(
448 /* translators: %s: currency symbol */
449 __( 'Israeli Shekel (%s)', 'charitable' ),
450 $this->get_currency_symbol( 'ILS' )
451 ),
452 'INR' => sprintf(
453 /* translators: %s: currency symbol */
454 __( 'Indian Rupee (%s)', 'charitable' ),
455 $this->get_currency_symbol( 'INR' )
456 ),
457 'ISK' => sprintf(
458 /* translators: %s: currency symbol */
459 __( 'Icelandic Krona (%s)', 'charitable' ),
460 $this->get_currency_symbol( 'ISK' )
461 ),
462 'JPY' => sprintf(
463 /* translators: %s: currency symbol */
464 __( 'Japanese Yen (%s)', 'charitable' ),
465 $this->get_currency_symbol( 'JPY' )
466 ),
467 'KWD' => sprintf(
468 /* translators: %s: currency symbol */
469 __( 'Kuwaiti Dinar (%s)', 'charitable' ),
470 $this->get_currency_symbol( 'KWD' )
471 ),
472 'MYR' => sprintf(
473 /* translators: %s: currency symbol */
474 __( 'Malaysian Ringgit (%s)', 'charitable' ),
475 $this->get_currency_symbol( 'MYR' )
476 ),
477 'MXN' => sprintf(
478 /* translators: %s: currency symbol */
479 __( 'Mexican Peso (%s)', 'charitable' ),
480 $this->get_currency_symbol( 'MXN' )
481 ),
482 'NZD' => sprintf(
483 /* translators: %s: currency symbol */
484 __( 'New Zealand Dollar (%s)', 'charitable' ),
485 $this->get_currency_symbol( 'NZD' )
486 ),
487 'NGN' => sprintf(
488 /* translators: %s: currency symbol */
489 __( 'Nigerian Naira (%s)', 'charitable' ),
490 $this->get_currency_symbol( 'NGN' )
491 ),
492 'NOK' => sprintf(
493 /* translators: %s: currency symbol */
494 __( 'Norwegian Krone (%s)', 'charitable' ),
495 $this->get_currency_symbol( 'NOK' )
496 ),
497 'PGK' => sprintf(
498 /* translators: %s: currency symbol */
499 __( 'Papua New Guinean Kina (%s)', 'charitable' ),
500 $this->get_currency_symbol( 'PGK' )
501 ),
502 'PHP' => sprintf(
503 /* translators: %s: currency symbol */
504 __( 'Philippine Peso (%s)', 'charitable' ),
505 $this->get_currency_symbol( 'PHP' )
506 ),
507 'PLN' => sprintf(
508 /* translators: %s: currency symbol */
509 __( 'Polish Zloty (%s)', 'charitable' ),
510 $this->get_currency_symbol( 'PLN' )
511 ),
512 'RON' => sprintf(
513 /* translators: %s: currency symbol */
514 __( 'Romanian New Leu (%s)', 'charitable' ),
515 $this->get_currency_symbol( 'RON' )
516 ),
517 'RUB' => sprintf(
518 /* translators: %s: currency symbol */
519 __( 'Russian Ruble (%s)', 'charitable' ),
520 $this->get_currency_symbol( 'RUB' )
521 ),
522 'WST' => sprintf(
523 /* translators: %s: currency symbol */
524 __( 'Samoan Tālā (%s)', 'charitable' ),
525 $this->get_currency_symbol( 'SGD' )
526 ),
527 'SGD' => sprintf(
528 /* translators: %s: currency symbol */
529 __( 'Singapore Dollar (%s)', 'charitable' ),
530 $this->get_currency_symbol( 'SGD' )
531 ),
532 'SBD' => sprintf(
533 /* translators: %s: currency symbol */
534 __( 'Solomon Islands Dollar (%s)', 'charitable' ),
535 $this->get_currency_symbol( 'SBD' )
536 ),
537 'ZAR' => sprintf(
538 /* translators: %s: currency symbol */
539 __( 'South African Rand (%s)', 'charitable' ),
540 $this->get_currency_symbol( 'ZAR' )
541 ),
542 'KRW' => sprintf(
543 /* translators: %s: currency symbol */
544 __( 'South Korean Won (%s)', 'charitable' ),
545 $this->get_currency_symbol( 'KRW' )
546 ),
547 'SEK' => sprintf(
548 /* translators: %s: currency symbol */
549 __( 'Swedish Krona (%s)', 'charitable' ),
550 $this->get_currency_symbol( 'SEK' )
551 ),
552 'TWD' => sprintf(
553 /* translators: %s: currency symbol */
554 __( 'Taiwan New Dollar (%s)', 'charitable' ),
555 $this->get_currency_symbol( 'TWD' )
556 ),
557 'THB' => sprintf(
558 /* translators: %s: currency symbol */
559 __( 'Thai Baht (%s)', 'charitable' ),
560 $this->get_currency_symbol( 'THB' )
561 ),
562 'TOP' => sprintf(
563 /* translators: %s: currency symbol */
564 __( 'Tongan Pa&lsquo;anga (%s)', 'charitable' ),
565 $this->get_currency_symbol( 'TOP' )
566 ),
567 'TRY' => sprintf(
568 /* translators: %s: currency symbol */
569 __( 'Turkish Lira (%s)', 'charitable' ),
570 $this->get_currency_symbol( 'TRY' )
571 ),
572 'USD' => sprintf(
573 /* translators: %s: currency symbol */
574 __( 'US Dollar (%s)', 'charitable' ),
575 $this->get_currency_symbol( 'USD' )
576 ),
577 'VUV' => sprintf(
578 /* translators: %s: currency symbol */
579 __( 'Vanuatu Vatu (%s)', 'charitable' ),
580 $this->get_currency_symbol( 'VUV' )
581 ),
582 'VND' => sprintf(
583 /* translators: %s: currency symbol */
584 __( 'Vietnamese Dong (%s)', 'charitable' ),
585 $this->get_currency_symbol( 'VND' )
586 ),
587 )
588 );
589 }//end if
590
591 return $this->currencies;
592 }
593
594 /**
595 * Return the currency symbol for a given currency.
596 *
597 * This function was changed to a public method in 1.3.7.
598 *
599 * Credit: This is based on the WooCommerce implemenation.
600 *
601 * @since 1.0.0
602 *
603 * @param string $currency Optional. If not set, currency is based on currently selected currency.
604 * @return string
605 */
606 public function get_currency_symbol( $currency = '' ) {
607 if ( ! strlen( $currency ) ) {
608 $currency = charitable_get_currency();
609 }
610
611 switch ( $currency ) {
612 case 'AUD':
613 case 'ARS':
614 case 'BND':
615 case 'CAD':
616 case 'CLP':
617 case 'COP':
618 case 'FJD':
619 case 'MXN':
620 case 'NZD':
621 case 'HKD':
622 case 'SGD':
623 case 'USD':
624 $currency_symbol = '&#36;';
625 break;
626
627 case 'CNY':
628 case 'RMB':
629 case 'JPY':
630 $currency_symbol = '&yen;';
631 break;
632 case 'AED':
633 $currency_symbol = 'د.إ';
634 break;
635 case 'BDT':
636 $currency_symbol = '&#2547;';
637 break;
638 case 'BGN':
639 $currency_symbol = '&#1083;&#1074;.';
640 break;
641 case 'BOB':
642 $currency_symbol = '&#66;&#115;&#46;';
643 break;
644 case 'BRL':
645 $currency_symbol = '&#82;&#36;';
646 break;
647 case 'CHF':
648 $currency_symbol = '&#67;&#72;&#70;';
649 break;
650 case 'CZK':
651 $currency_symbol = '&#75;&#269;';
652 break;
653 case 'DKK':
654 $currency_symbol = 'kr.';
655 break;
656 case 'EGP':
657 $currency_symbol = 'E&pound;';
658 break;
659 case 'EUR':
660 $currency_symbol = '&euro;';
661 break;
662 case 'GBP':
663 $currency_symbol = '&pound;';
664 break;
665 case 'GHS':
666 $currency_symbol = 'GH&#8373;';
667 break;
668 case 'HRK':
669 $currency_symbol = 'Kn';
670 break;
671 case 'HUF':
672 $currency_symbol = '&#70;&#116;';
673 break;
674 case 'IDR':
675 $currency_symbol = 'Rp';
676 break;
677 case 'ILS':
678 $currency_symbol = '&#8362;';
679 break;
680 case 'INR':
681 $currency_symbol = 'Rs.';
682 break;
683 case 'ISK':
684 $currency_symbol = 'Kr.';
685 break;
686 case 'KWD':
687 $currency_symbol = 'KD';
688 break;
689 case 'KRW':
690 $currency_symbol = '&#8361;';
691 break;
692 case 'MYR':
693 $currency_symbol = '&#82;&#77;';
694 break;
695 case 'NGN':
696 $currency_symbol = '&#8358;';
697 break;
698 case 'NOK':
699 $currency_symbol = '&#107;&#114;';
700 break;
701 case 'PGK':
702 $currency_symbol = 'K';
703 break;
704 case 'PHP':
705 $currency_symbol = '&#8369;';
706 break;
707 case 'PLN':
708 $currency_symbol = '&#122;&#322;';
709 break;
710 case 'RON':
711 $currency_symbol = 'lei';
712 break;
713 case 'RUB':
714 $currency_symbol = '&#1088;&#1091;&#1073;.';
715 break;
716 case 'SBD':
717 $currency_symbol = 'SI&#36;';
718 break;
719 case 'SEK':
720 $currency_symbol = '&#107;&#114;';
721 break;
722 case 'THB':
723 $currency_symbol = '&#3647;';
724 break;
725 case 'TOP':
726 $currency_symbol = 'T&#36;';
727 break;
728 case 'TRY':
729 $currency_symbol = '&#8378;';
730 break;
731 case 'TWD':
732 $currency_symbol = '&#78;&#84;&#36;';
733 break;
734 case 'VND':
735 $currency_symbol = '&#8363;';
736 break;
737 case 'VUV':
738 $currency_symbol = 'VT';
739 break;
740 case 'WST':
741 $currency_symbol = 'WS&#36;';
742 break;
743 case 'ZAR':
744 $currency_symbol = '&#82;';
745 break;
746 default:
747 $currency_symbol = '';
748 break;
749 }//end switch
750
751 return apply_filters( 'charitable_currency_symbol', $currency_symbol, $currency );
752 }
753
754 /**
755 * Return the thousands separator.
756 *
757 * @since 1.5.0
758 *
759 * @return string
760 */
761 public function get_thousands_separator() {
762 $separator = charitable_get_option( 'thousands_separator', ',' );
763
764 if ( 'none' == $separator ) {
765 $separator = '';
766 }
767
768 return $separator;
769 }
770
771 /**
772 * Return the decimal separator.
773 *
774 * @since 1.5.0
775 *
776 * @return string
777 */
778 public function get_decimal_separator() {
779 return charitable_get_option( 'decimal_separator', '.' );
780 }
781
782 /**
783 * Return the number of decimals to use.
784 *
785 * @since 1.0.0
786 *
787 * @return int
788 */
789 public function get_decimals() {
790 $default = $this->is_zero_decimal_currency() ? 0 : 2;
791 return charitable_get_option( 'decimal_count', $default );
792 }
793
794 /**
795 * Returns a list of currencies that do not use decimals.
796 *
797 * @since 1.6.38
798 *
799 * @return array
800 */
801 public function get_zero_decimal_currencies() {
802 /**
803 * Filter the list of zero decimal currencies.
804 *
805 * @since 1.6.38
806 *
807 * @param array $currencies All the zero-decimal currencies.
808 */
809 return apply_filters(
810 'charitable_zero_decimal_currencies',
811 array(
812 'BIF',
813 'CLP',
814 'DJF',
815 'GNF',
816 'JPY',
817 'KMF',
818 'KRW',
819 'MGA',
820 'PYG',
821 'RWF',
822 'UGX',
823 'VND',
824 'VUV',
825 'XAF',
826 'XOF',
827 'XPF',
828 )
829 );
830 }
831
832 /**
833 * Checks whether a given currency is a zero-decimal currency.
834 *
835 * @since 1.6.38
836 *
837 * @param string $currency The currency code to check.
838 * @return boolean
839 */
840 public function is_zero_decimal_currency( $currency = '' ) {
841 if ( empty( $currency ) ) {
842 $currency = charitable_get_currency();
843 }
844
845 return in_array( $currency, $this->get_zero_decimal_currencies() );
846 }
847
848 /**
849 * If we're using a zero-decimal currency, automatically set the
850 * number of decimals to equal 0.
851 *
852 * @since 1.6.38
853 *
854 * @param int $decimals The decimal count.
855 * @return int
856 */
857 public function maybe_force_zero_decimals( $decimals ) {
858 if ( $this->is_zero_decimal_currency() ) {
859 $decimals = 0;
860 }
861
862 return $decimals;
863 }
864 }
865
866 endif;
867