| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Currency helper. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Currency |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, 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 |
if ( $this->is_comma_decimal() ) { |
| 207 |
$amount = str_replace( ',', '.', $amount ); |
| 208 |
} else { |
| 209 |
$amount = str_replace( ',', '', $amount ); |
| 210 |
} |
| 211 |
|
| 212 |
return $amount; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Checks whether the comma is being used as the separator. |
| 217 |
* |
| 218 |
* @since 1.0.0 |
| 219 |
* |
| 220 |
* @return boolean |
| 221 |
*/ |
| 222 |
public function is_comma_decimal() { |
| 223 |
return ( ',' == $this->get_decimal_separator() ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Return the currency format based on the position of the currency symbol. |
| 228 |
* |
| 229 |
* @since 1.0.0 |
| 230 |
* @since 1.6.38 Added optional $symbol_position argument. |
| 231 |
* |
| 232 |
* @param string $symbol_position The symbol position. If not set, the site's |
| 233 |
* currency_format setting will be used. |
| 234 |
* @return string |
| 235 |
*/ |
| 236 |
public function get_currency_format( $symbol_position = '' ) { |
| 237 |
if ( empty( $symbol_position ) ) { |
| 238 |
$symbol_position = charitable_get_option( 'currency_format', 'left' ); |
| 239 |
} |
| 240 |
|
| 241 |
switch ( $symbol_position ) { |
| 242 |
case 'left': |
| 243 |
$format = '%1$s%2$s'; |
| 244 |
break; |
| 245 |
case 'right': |
| 246 |
$format = '%2$s%1$s'; |
| 247 |
break; |
| 248 |
case 'left-with-space': |
| 249 |
$format = '%1$s %2$s'; |
| 250 |
break; |
| 251 |
case 'right-with-space': |
| 252 |
$format = '%2$s %1$s'; |
| 253 |
break; |
| 254 |
default: |
| 255 |
/** |
| 256 |
* Filter the fallback currency format. |
| 257 |
* |
| 258 |
* @since 1.0.0 |
| 259 |
* |
| 260 |
* @param string $format The currency format. |
| 261 |
* @param string $symbol_position Where the symbol is positioned in relation to the amount. |
| 262 |
*/ |
| 263 |
$format = apply_filters( 'charitable_currency_format', '%1$s%2$s', $symbol_position ); |
| 264 |
} |
| 265 |
|
| 266 |
return $format; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Get the currency format for accounting.js |
| 271 |
* |
| 272 |
* @since 1.3.0 |
| 273 |
* |
| 274 |
* @return string |
| 275 |
*/ |
| 276 |
public function get_accounting_js_format() { |
| 277 |
switch ( charitable_get_option( 'currency_format', 'left' ) ) { |
| 278 |
case 'right': |
| 279 |
$format = '%v%s'; |
| 280 |
break; |
| 281 |
case 'left-with-space': |
| 282 |
$format = '%s %v'; |
| 283 |
break; |
| 284 |
case 'right-with-space': |
| 285 |
$format = '%v %s'; |
| 286 |
break; |
| 287 |
default: |
| 288 |
$format = '%s%v'; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Filter the currency format to use with accounting.js. |
| 293 |
* |
| 294 |
* @since 1.3.0 |
| 295 |
* |
| 296 |
* @param string $format The currency format. |
| 297 |
*/ |
| 298 |
return apply_filters( 'charitable_accounting_js_currency_format', $format ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Return every currency symbol used with the |
| 303 |
* |
| 304 |
* @since 1.0.0 |
| 305 |
* |
| 306 |
* @return string[] |
| 307 |
*/ |
| 308 |
public function get_all_currencies() { |
| 309 |
if ( empty( $this->currencies ) ) { |
| 310 |
/** |
| 311 |
* Filter the currencies available in Charitable. |
| 312 |
* |
| 313 |
* @since 1.0.0 |
| 314 |
* |
| 315 |
* @param array $currencies All currencies as a key=>index array, with |
| 316 |
* the currency code as the key and the currency |
| 317 |
* name including symbol as value. |
| 318 |
*/ |
| 319 |
$this->currencies = apply_filters( |
| 320 |
'charitable_currencies', |
| 321 |
array( |
| 322 |
'ARS' => sprintf( |
| 323 |
/* translators: %s: currency symbol */ |
| 324 |
__( 'Argentine Peso (%s)', 'charitable' ), |
| 325 |
$this->get_currency_symbol( 'ARS' ) |
| 326 |
), |
| 327 |
'AUD' => sprintf( |
| 328 |
/* translators: %s: currency symbol */ |
| 329 |
__( 'Australian Dollars (%s)', 'charitable' ), |
| 330 |
$this->get_currency_symbol( 'AUD' ) |
| 331 |
), |
| 332 |
'BDT' => sprintf( |
| 333 |
/* translators: %s: currency symbol */ |
| 334 |
__( 'Bangladeshi Taka (%s)', 'charitable' ), |
| 335 |
$this->get_currency_symbol( 'BDT' ) |
| 336 |
), |
| 337 |
'BOB' => sprintf( |
| 338 |
/* translators: %s: currency symbol */ |
| 339 |
__( 'Bolivian Bolíviano (%s)', 'charitable' ), |
| 340 |
$this->get_currency_symbol( 'BOB' ) |
| 341 |
), |
| 342 |
'BRL' => sprintf( |
| 343 |
/* translators: %s: currency symbol */ |
| 344 |
__( 'Brazilian Real (%s)', 'charitable' ), |
| 345 |
$this->get_currency_symbol( 'BRL' ) |
| 346 |
), |
| 347 |
'BND' => sprintf( |
| 348 |
/* translators: %s: currency symbol */ |
| 349 |
__( 'Brunei Dollar (%s)', 'charitable' ), |
| 350 |
$this->get_currency_symbol( 'BND' ) |
| 351 |
), |
| 352 |
'BGN' => sprintf( |
| 353 |
/* translators: %s: currency symbol */ |
| 354 |
__( 'Bulgarian Lev (%s)', 'charitable' ), |
| 355 |
$this->get_currency_symbol( 'BGN' ) |
| 356 |
), |
| 357 |
'CAD' => sprintf( |
| 358 |
/* translators: %s: currency symbol */ |
| 359 |
__( 'Canadian Dollar (%s)', 'charitable' ), |
| 360 |
$this->get_currency_symbol( 'CAD' ) |
| 361 |
), |
| 362 |
'CHF' => sprintf( |
| 363 |
/* translators: %s: currency symbol */ |
| 364 |
__( 'Swiss Franc (%s)', 'charitable' ), |
| 365 |
$this->get_currency_symbol( 'CHF' ) |
| 366 |
), |
| 367 |
'CLP' => sprintf( |
| 368 |
/* translators: %s: currency symbol */ |
| 369 |
__( 'Chilean Peso (%s)', 'charitable' ), |
| 370 |
$this->get_currency_symbol( 'CLP' ) |
| 371 |
), |
| 372 |
'CNY' => sprintf( |
| 373 |
/* translators: %s: currency symbol */ |
| 374 |
__( 'Chinese Yuan Renminbi (%s)', 'charitable' ), |
| 375 |
$this->get_currency_symbol( 'CNY' ) |
| 376 |
), |
| 377 |
'COP' => sprintf( |
| 378 |
/* translators: %s: currency symbol */ |
| 379 |
__( 'Colombian Peso (%s', 'charitable' ), |
| 380 |
$this->get_currency_symbol( 'COP' ) |
| 381 |
), |
| 382 |
'CZK' => sprintf( |
| 383 |
/* translators: %s: currency symbol */ |
| 384 |
__( 'Czech Koruna (%s)', 'charitable' ), |
| 385 |
$this->get_currency_symbol( 'CZK' ) |
| 386 |
), |
| 387 |
'DKK' => sprintf( |
| 388 |
/* translators: %s: currency symbol */ |
| 389 |
__( 'Danish Krone (%s)', 'charitable' ), |
| 390 |
$this->get_currency_symbol( 'DKK' ) |
| 391 |
), |
| 392 |
'EGP' => sprintf( |
| 393 |
/* translators: %s: currency symbol */ |
| 394 |
__( 'Egyptian Pound (%s)', 'charitable' ), |
| 395 |
$this->get_currency_symbol( 'EGP' ) |
| 396 |
), |
| 397 |
'AED' => sprintf( |
| 398 |
/* translators: %s: currency symbol */ |
| 399 |
__( 'Emirati Dirham (%s)', 'charitable' ), |
| 400 |
$this->get_currency_symbol( 'AED' ) |
| 401 |
), |
| 402 |
'EUR' => sprintf( |
| 403 |
/* translators: %s: currency symbol */ |
| 404 |
__( 'Euro (%s)', 'charitable' ), |
| 405 |
$this->get_currency_symbol( 'EUR' ) |
| 406 |
), |
| 407 |
'FJD' => sprintf( |
| 408 |
/* translators: %s: currency symbol */ |
| 409 |
__( 'Fijian Dollar (%s)', 'charitable' ), |
| 410 |
$this->get_currency_symbol( 'FJD' ) |
| 411 |
), |
| 412 |
'GBP' => sprintf( |
| 413 |
/* translators: %s: currency symbol */ |
| 414 |
__( 'British Pound (%s)', 'charitable' ), |
| 415 |
$this->get_currency_symbol( 'GBP' ) |
| 416 |
), |
| 417 |
'GHS' => sprintf( |
| 418 |
/* translators: %s: currency symbol */ |
| 419 |
__( 'Ghanaian Cedi (%s)', 'charitable' ), |
| 420 |
$this->get_currency_symbol( 'GHS' ) |
| 421 |
), |
| 422 |
'HKD' => sprintf( |
| 423 |
/* translators: %s: currency symbol */ |
| 424 |
__( 'Hong Kong Dollar (%s)', 'charitable' ), |
| 425 |
$this->get_currency_symbol( 'HKD' ) |
| 426 |
), |
| 427 |
'HRK' => sprintf( |
| 428 |
/* translators: %s: currency symbol */ |
| 429 |
__( 'Croatian Kuna (%s)', 'charitable' ), |
| 430 |
$this->get_currency_symbol( 'HRK' ) |
| 431 |
), |
| 432 |
'HUF' => sprintf( |
| 433 |
/* translators: %s: currency symbol */ |
| 434 |
__( 'Hungarian Forint (%s)', 'charitable' ), |
| 435 |
$this->get_currency_symbol( 'HUF' ) |
| 436 |
), |
| 437 |
'IDR' => sprintf( |
| 438 |
/* translators: %s: currency symbol */ |
| 439 |
__( 'Indonesian Rupiah (%s)', 'charitable' ), |
| 440 |
$this->get_currency_symbol( 'IDR' ) |
| 441 |
), |
| 442 |
'ILS' => sprintf( |
| 443 |
/* translators: %s: currency symbol */ |
| 444 |
__( 'Israeli Shekel (%s)', 'charitable' ), |
| 445 |
$this->get_currency_symbol( 'ILS' ) |
| 446 |
), |
| 447 |
'INR' => sprintf( |
| 448 |
/* translators: %s: currency symbol */ |
| 449 |
__( 'Indian Rupee (%s)', 'charitable' ), |
| 450 |
$this->get_currency_symbol( 'INR' ) |
| 451 |
), |
| 452 |
'ISK' => sprintf( |
| 453 |
/* translators: %s: currency symbol */ |
| 454 |
__( 'Icelandic Krona (%s)', 'charitable' ), |
| 455 |
$this->get_currency_symbol( 'ISK' ) |
| 456 |
), |
| 457 |
'JPY' => sprintf( |
| 458 |
/* translators: %s: currency symbol */ |
| 459 |
__( 'Japanese Yen (%s)', 'charitable' ), |
| 460 |
$this->get_currency_symbol( 'JPY' ) |
| 461 |
), |
| 462 |
'KWD' => sprintf( |
| 463 |
/* translators: %s: currency symbol */ |
| 464 |
__( 'Kuwaiti Dinar (%s)', 'charitable' ), |
| 465 |
$this->get_currency_symbol( 'KWD' ) |
| 466 |
), |
| 467 |
'MYR' => sprintf( |
| 468 |
/* translators: %s: currency symbol */ |
| 469 |
__( 'Malaysian Ringgit (%s)', 'charitable' ), |
| 470 |
$this->get_currency_symbol( 'MYR' ) |
| 471 |
), |
| 472 |
'MXN' => sprintf( |
| 473 |
/* translators: %s: currency symbol */ |
| 474 |
__( 'Mexican Peso (%s)', 'charitable' ), |
| 475 |
$this->get_currency_symbol( 'MXN' ) |
| 476 |
), |
| 477 |
'NZD' => sprintf( |
| 478 |
/* translators: %s: currency symbol */ |
| 479 |
__( 'New Zealand Dollar (%s)', 'charitable' ), |
| 480 |
$this->get_currency_symbol( 'NZD' ) |
| 481 |
), |
| 482 |
'NGN' => sprintf( |
| 483 |
/* translators: %s: currency symbol */ |
| 484 |
__( 'Nigerian Naira (%s)', 'charitable' ), |
| 485 |
$this->get_currency_symbol( 'NGN' ) |
| 486 |
), |
| 487 |
'NOK' => sprintf( |
| 488 |
/* translators: %s: currency symbol */ |
| 489 |
__( 'Norwegian Krone (%s)', 'charitable' ), |
| 490 |
$this->get_currency_symbol( 'NOK' ) |
| 491 |
), |
| 492 |
'PGK' => sprintf( |
| 493 |
/* translators: %s: currency symbol */ |
| 494 |
__( 'Papua New Guinean Kina (%s)', 'charitable' ), |
| 495 |
$this->get_currency_symbol( 'PGK' ) |
| 496 |
), |
| 497 |
'PHP' => sprintf( |
| 498 |
/* translators: %s: currency symbol */ |
| 499 |
__( 'Philippine Peso (%s)', 'charitable' ), |
| 500 |
$this->get_currency_symbol( 'PHP' ) |
| 501 |
), |
| 502 |
'PLN' => sprintf( |
| 503 |
/* translators: %s: currency symbol */ |
| 504 |
__( 'Polish Zloty (%s)', 'charitable' ), |
| 505 |
$this->get_currency_symbol( 'PLN' ) |
| 506 |
), |
| 507 |
'RON' => sprintf( |
| 508 |
/* translators: %s: currency symbol */ |
| 509 |
__( 'Romanian New Leu (%s)', 'charitable' ), |
| 510 |
$this->get_currency_symbol( 'RON' ) |
| 511 |
), |
| 512 |
'RUB' => sprintf( |
| 513 |
/* translators: %s: currency symbol */ |
| 514 |
__( 'Russian Ruble (%s)', 'charitable' ), |
| 515 |
$this->get_currency_symbol( 'RUB' ) |
| 516 |
), |
| 517 |
'WST' => sprintf( |
| 518 |
/* translators: %s: currency symbol */ |
| 519 |
__( 'Samoan Tālā (%s)', 'charitable' ), |
| 520 |
$this->get_currency_symbol( 'SGD' ) |
| 521 |
), |
| 522 |
'SGD' => sprintf( |
| 523 |
/* translators: %s: currency symbol */ |
| 524 |
__( 'Singapore Dollar (%s)', 'charitable' ), |
| 525 |
$this->get_currency_symbol( 'SGD' ) |
| 526 |
), |
| 527 |
'SBD' => sprintf( |
| 528 |
/* translators: %s: currency symbol */ |
| 529 |
__( 'Solomon Islands Dollar (%s)', 'charitable' ), |
| 530 |
$this->get_currency_symbol( 'SBD' ) |
| 531 |
), |
| 532 |
'ZAR' => sprintf( |
| 533 |
/* translators: %s: currency symbol */ |
| 534 |
__( 'South African Rand (%s)', 'charitable' ), |
| 535 |
$this->get_currency_symbol( 'ZAR' ) |
| 536 |
), |
| 537 |
'KRW' => sprintf( |
| 538 |
/* translators: %s: currency symbol */ |
| 539 |
__( 'South Korean Won (%s)', 'charitable' ), |
| 540 |
$this->get_currency_symbol( 'KRW' ) |
| 541 |
), |
| 542 |
'SEK' => sprintf( |
| 543 |
/* translators: %s: currency symbol */ |
| 544 |
__( 'Swedish Krona (%s)', 'charitable' ), |
| 545 |
$this->get_currency_symbol( 'SEK' ) |
| 546 |
), |
| 547 |
'TWD' => sprintf( |
| 548 |
/* translators: %s: currency symbol */ |
| 549 |
__( 'Taiwan New Dollar (%s)', 'charitable' ), |
| 550 |
$this->get_currency_symbol( 'TWD' ) |
| 551 |
), |
| 552 |
'THB' => sprintf( |
| 553 |
/* translators: %s: currency symbol */ |
| 554 |
__( 'Thai Baht (%s)', 'charitable' ), |
| 555 |
$this->get_currency_symbol( 'THB' ) |
| 556 |
), |
| 557 |
'TOP' => sprintf( |
| 558 |
/* translators: %s: currency symbol */ |
| 559 |
__( 'Tongan Pa‘anga (%s)', 'charitable' ), |
| 560 |
$this->get_currency_symbol( 'TOP' ) |
| 561 |
), |
| 562 |
'TRY' => sprintf( |
| 563 |
/* translators: %s: currency symbol */ |
| 564 |
__( 'Turkish Lira (%s)', 'charitable' ), |
| 565 |
$this->get_currency_symbol( 'TRY' ) |
| 566 |
), |
| 567 |
'USD' => sprintf( |
| 568 |
/* translators: %s: currency symbol */ |
| 569 |
__( 'US Dollar (%s)', 'charitable' ), |
| 570 |
$this->get_currency_symbol( 'USD' ) |
| 571 |
), |
| 572 |
'VUV' => sprintf( |
| 573 |
/* translators: %s: currency symbol */ |
| 574 |
__( 'Vanuatu Vatu (%s)', 'charitable' ), |
| 575 |
$this->get_currency_symbol( 'VUV' ) |
| 576 |
), |
| 577 |
'VND' => sprintf( |
| 578 |
/* translators: %s: currency symbol */ |
| 579 |
__( 'Vietnamese Dong (%s)', 'charitable' ), |
| 580 |
$this->get_currency_symbol( 'VND' ) |
| 581 |
), |
| 582 |
) |
| 583 |
); |
| 584 |
}//end if |
| 585 |
|
| 586 |
return $this->currencies; |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Return the currency symbol for a given currency. |
| 591 |
* |
| 592 |
* This function was changed to a public method in 1.3.7. |
| 593 |
* |
| 594 |
* Credit: This is based on the WooCommerce implemenation. |
| 595 |
* |
| 596 |
* @since 1.0.0 |
| 597 |
* |
| 598 |
* @param string $currency Optional. If not set, currency is based on currently selected currency. |
| 599 |
* @return string |
| 600 |
*/ |
| 601 |
public function get_currency_symbol( $currency = '' ) { |
| 602 |
if ( ! strlen( $currency ) ) { |
| 603 |
$currency = charitable_get_currency(); |
| 604 |
} |
| 605 |
|
| 606 |
switch ( $currency ) { |
| 607 |
case 'AUD': |
| 608 |
case 'ARS': |
| 609 |
case 'BND': |
| 610 |
case 'CAD': |
| 611 |
case 'CLP': |
| 612 |
case 'COP': |
| 613 |
case 'FJD': |
| 614 |
case 'MXN': |
| 615 |
case 'NZD': |
| 616 |
case 'HKD': |
| 617 |
case 'SGD': |
| 618 |
case 'USD': |
| 619 |
$currency_symbol = '$'; |
| 620 |
break; |
| 621 |
|
| 622 |
case 'CNY': |
| 623 |
case 'RMB': |
| 624 |
case 'JPY': |
| 625 |
$currency_symbol = '¥'; |
| 626 |
break; |
| 627 |
case 'AED': |
| 628 |
$currency_symbol = 'د.إ'; |
| 629 |
break; |
| 630 |
case 'BDT': |
| 631 |
$currency_symbol = '৳'; |
| 632 |
break; |
| 633 |
case 'BGN': |
| 634 |
$currency_symbol = 'лв.'; |
| 635 |
break; |
| 636 |
case 'BOB': |
| 637 |
$currency_symbol = 'Bs.'; |
| 638 |
break; |
| 639 |
case 'BRL': |
| 640 |
$currency_symbol = 'R$'; |
| 641 |
break; |
| 642 |
case 'CHF': |
| 643 |
$currency_symbol = 'CHF'; |
| 644 |
break; |
| 645 |
case 'CZK': |
| 646 |
$currency_symbol = 'Kč'; |
| 647 |
break; |
| 648 |
case 'DKK': |
| 649 |
$currency_symbol = 'kr.'; |
| 650 |
break; |
| 651 |
case 'EGP': |
| 652 |
$currency_symbol = 'E£'; |
| 653 |
break; |
| 654 |
case 'EUR': |
| 655 |
$currency_symbol = '€'; |
| 656 |
break; |
| 657 |
case 'GBP': |
| 658 |
$currency_symbol = '£'; |
| 659 |
break; |
| 660 |
case 'GHS': |
| 661 |
$currency_symbol = 'GH₵'; |
| 662 |
break; |
| 663 |
case 'HRK': |
| 664 |
$currency_symbol = 'Kn'; |
| 665 |
break; |
| 666 |
case 'HUF': |
| 667 |
$currency_symbol = 'Ft'; |
| 668 |
break; |
| 669 |
case 'IDR': |
| 670 |
$currency_symbol = 'Rp'; |
| 671 |
break; |
| 672 |
case 'ILS': |
| 673 |
$currency_symbol = '₪'; |
| 674 |
break; |
| 675 |
case 'INR': |
| 676 |
$currency_symbol = 'Rs.'; |
| 677 |
break; |
| 678 |
case 'ISK': |
| 679 |
$currency_symbol = 'Kr.'; |
| 680 |
break; |
| 681 |
case 'KWD': |
| 682 |
$currency_symbol = 'KD'; |
| 683 |
break; |
| 684 |
case 'KRW': |
| 685 |
$currency_symbol = '₩'; |
| 686 |
break; |
| 687 |
case 'MYR': |
| 688 |
$currency_symbol = 'RM'; |
| 689 |
break; |
| 690 |
case 'NGN': |
| 691 |
$currency_symbol = '₦'; |
| 692 |
break; |
| 693 |
case 'NOK': |
| 694 |
$currency_symbol = 'kr'; |
| 695 |
break; |
| 696 |
case 'PGK': |
| 697 |
$currency_symbol = 'K'; |
| 698 |
break; |
| 699 |
case 'PHP': |
| 700 |
$currency_symbol = '₱'; |
| 701 |
break; |
| 702 |
case 'PLN': |
| 703 |
$currency_symbol = 'zł'; |
| 704 |
break; |
| 705 |
case 'RON': |
| 706 |
$currency_symbol = 'lei'; |
| 707 |
break; |
| 708 |
case 'RUB': |
| 709 |
$currency_symbol = 'руб.'; |
| 710 |
break; |
| 711 |
case 'SBD': |
| 712 |
$currency_symbol = 'SI$'; |
| 713 |
break; |
| 714 |
case 'SEK': |
| 715 |
$currency_symbol = 'kr'; |
| 716 |
break; |
| 717 |
case 'THB': |
| 718 |
$currency_symbol = '฿'; |
| 719 |
break; |
| 720 |
case 'TOP': |
| 721 |
$currency_symbol = 'T$'; |
| 722 |
break; |
| 723 |
case 'TRY': |
| 724 |
$currency_symbol = '₺'; |
| 725 |
break; |
| 726 |
case 'TWD': |
| 727 |
$currency_symbol = 'NT$'; |
| 728 |
break; |
| 729 |
case 'VND': |
| 730 |
$currency_symbol = '₫'; |
| 731 |
break; |
| 732 |
case 'VUV': |
| 733 |
$currency_symbol = 'VT'; |
| 734 |
break; |
| 735 |
case 'WST': |
| 736 |
$currency_symbol = 'WS$'; |
| 737 |
break; |
| 738 |
case 'ZAR': |
| 739 |
$currency_symbol = 'R'; |
| 740 |
break; |
| 741 |
default: |
| 742 |
$currency_symbol = ''; |
| 743 |
break; |
| 744 |
}//end switch |
| 745 |
|
| 746 |
return apply_filters( 'charitable_currency_symbol', $currency_symbol, $currency ); |
| 747 |
} |
| 748 |
|
| 749 |
/** |
| 750 |
* Return the thousands separator. |
| 751 |
* |
| 752 |
* @since 1.5.0 |
| 753 |
* |
| 754 |
* @return string |
| 755 |
*/ |
| 756 |
public function get_thousands_separator() { |
| 757 |
$separator = charitable_get_option( 'thousands_separator', ',' ); |
| 758 |
|
| 759 |
if ( 'none' == $separator ) { |
| 760 |
$separator = ''; |
| 761 |
} |
| 762 |
|
| 763 |
return $separator; |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* Return the decimal separator. |
| 768 |
* |
| 769 |
* @since 1.5.0 |
| 770 |
* |
| 771 |
* @return string |
| 772 |
*/ |
| 773 |
public function get_decimal_separator() { |
| 774 |
return charitable_get_option( 'decimal_separator', '.' ); |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
* Return the number of decimals to use. |
| 779 |
* |
| 780 |
* @since 1.0.0 |
| 781 |
* |
| 782 |
* @return int |
| 783 |
*/ |
| 784 |
public function get_decimals() { |
| 785 |
$default = $this->is_zero_decimal_currency() ? 0 : 2; |
| 786 |
return charitable_get_option( 'decimal_count', $default ); |
| 787 |
} |
| 788 |
|
| 789 |
/** |
| 790 |
* Returns a list of currencies that do not use decimals. |
| 791 |
* |
| 792 |
* @since 1.6.38 |
| 793 |
* |
| 794 |
* @return array |
| 795 |
*/ |
| 796 |
public function get_zero_decimal_currencies() { |
| 797 |
/** |
| 798 |
* Filter the list of zero decimal currencies. |
| 799 |
* |
| 800 |
* @since 1.6.38 |
| 801 |
* |
| 802 |
* @param array $currencies All the zero-decimal currencies. |
| 803 |
*/ |
| 804 |
return apply_filters( |
| 805 |
'charitable_zero_decimal_currencies', |
| 806 |
array( |
| 807 |
'BIF', |
| 808 |
'CLP', |
| 809 |
'DJF', |
| 810 |
'GNF', |
| 811 |
'JPY', |
| 812 |
'KMF', |
| 813 |
'KRW', |
| 814 |
'MGA', |
| 815 |
'PYG', |
| 816 |
'RWF', |
| 817 |
'UGX', |
| 818 |
'VND', |
| 819 |
'VUV', |
| 820 |
'XAF', |
| 821 |
'XOF', |
| 822 |
'XPF', |
| 823 |
) |
| 824 |
); |
| 825 |
} |
| 826 |
|
| 827 |
/** |
| 828 |
* Checks whether a given currency is a zero-decimal currency. |
| 829 |
* |
| 830 |
* @since 1.6.38 |
| 831 |
* |
| 832 |
* @param string $currency The currency code to check. |
| 833 |
* @return boolean |
| 834 |
*/ |
| 835 |
public function is_zero_decimal_currency( $currency = '' ) { |
| 836 |
if ( empty( $currency ) ) { |
| 837 |
$currency = charitable_get_currency(); |
| 838 |
} |
| 839 |
|
| 840 |
return in_array( $currency, $this->get_zero_decimal_currencies() ); |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* If we're using a zero-decimal currency, automatically set the |
| 845 |
* number of decimals to equal 0. |
| 846 |
* |
| 847 |
* @since 1.6.38 |
| 848 |
* |
| 849 |
* @param int $decimals The decimal count. |
| 850 |
* @return int |
| 851 |
*/ |
| 852 |
public function maybe_force_zero_decimals( $decimals ) { |
| 853 |
if ( $this->is_zero_decimal_currency() ) { |
| 854 |
$decimals = 0; |
| 855 |
} |
| 856 |
|
| 857 |
return $decimals; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
endif; |
| 862 |
|