| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Currency helper. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Currency |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2019, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 15 |
|
| 16 |
if ( ! class_exists( 'Charitable_Currency' ) ) : |
| 17 |
|
| 18 |
/** |
| 19 |
* Charitable_Currency |
| 20 |
* |
| 21 |
* @final |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
final class Charitable_Currency { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @var Charitable_Currency|null |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Every currency available. |
| 35 |
* |
| 36 |
* @var string[] |
| 37 |
*/ |
| 38 |
private $currencies = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Create class object. A private constructor, so this is used in a singleton context. |
| 42 |
* |
| 43 |
* @since 1.2.3 |
| 44 |
*/ |
| 45 |
private function __construct() { |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns and/or create the single instance of this class. |
| 50 |
* |
| 51 |
* @since 1.2.3 |
| 52 |
* |
| 53 |
* @return Charitable_Currency |
| 54 |
*/ |
| 55 |
public static function get_instance() { |
| 56 |
if ( is_null( self::$instance ) ) { |
| 57 |
self::$instance = new self(); |
| 58 |
} |
| 59 |
|
| 60 |
return self::$instance; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Return an amount as a monetary string. |
| 65 |
* |
| 66 |
* 50.00 -> $50.00 |
| 67 |
* |
| 68 |
* @since 1.0.0 |
| 69 |
* |
| 70 |
* @param string $amount The amount to convert. |
| 71 |
* @param int|false $decimal_count Optional. If not set, default decimal count will be used. |
| 72 |
* @param boolean $db_format Optional. Whether the amount is in db format (i.e. using decimals for cents, regardless of site settings). |
| 73 |
* @return string|WP_Error |
| 74 |
*/ |
| 75 |
public function get_monetary_amount( $amount, $decimal_count = false, $db_format = false ) { |
| 76 |
if ( false === $decimal_count ) { |
| 77 |
$decimal_count = charitable_get_option( 'decimal_count', 2 ); |
| 78 |
} |
| 79 |
|
| 80 |
$amount = $this->sanitize_monetary_amount( strval( $amount ), $db_format ); |
| 81 |
|
| 82 |
$amount = number_format( |
| 83 |
$amount, |
| 84 |
(int) $decimal_count, |
| 85 |
$this->get_decimal_separator(), |
| 86 |
$this->get_thousands_separator() |
| 87 |
); |
| 88 |
|
| 89 |
$formatted = sprintf( $this->get_currency_format(), $this->get_currency_symbol(), $amount ); |
| 90 |
|
| 91 |
return apply_filters( 'charitable_monetary_amount', $formatted, $amount ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Receives unfiltered monetary amount and sanitizes it, returning it as a float. |
| 96 |
* |
| 97 |
* $50.00 -> 50.00 |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* |
| 101 |
* @param string $amount The amount to sanitize. |
| 102 |
* @param boolean $db_format Optional. Whether the amount is in db format (i.e. using decimals for cents, regardless of site settings). |
| 103 |
* @return float|WP_Error |
| 104 |
*/ |
| 105 |
public function sanitize_monetary_amount( $amount, $db_format = false ) { |
| 106 |
/* Sending anything other than a string can cause unexpected returns, so we require strings. */ |
| 107 |
if ( ! is_string( $amount ) ) { |
| 108 |
|
| 109 |
charitable_get_deprecated()->doing_it_wrong( |
| 110 |
__METHOD__, |
| 111 |
__( 'Amount must be passed as a string.', 'charitable' ), |
| 112 |
'1.0.0' |
| 113 |
); |
| 114 |
|
| 115 |
return new WP_Error( 'invalid_parameter_type', 'Amount must be passed as a string.' ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* If we're using commas for decimals, we need to turn any commas into points, and |
| 120 |
* we need to replace existing points with blank spaces. Example: |
| 121 |
* |
| 122 |
* 12.500,50 -> 12500.50 |
| 123 |
*/ |
| 124 |
if ( ! $db_format && $this->is_comma_decimal() ) { |
| 125 |
/* Convert to 12.500_50 */ |
| 126 |
$amount = str_replace( ',', '_', $amount ); |
| 127 |
/* Convert to 12500_50 */ |
| 128 |
$amount = str_replace( '.', '', $amount ); |
| 129 |
/* Convert to 12500.50 */ |
| 130 |
$amount = str_replace( '_', '.', $amount ); |
| 131 |
} |
| 132 |
|
| 133 |
$amount = str_replace( $this->get_currency_symbol(), '', $amount ); |
| 134 |
|
| 135 |
return floatval( filter_var( $amount, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Turns a database amount into an amount formatted for the currency that the site is in. |
| 140 |
* |
| 141 |
* @since 1.3.0 |
| 142 |
* |
| 143 |
* @param string $amount The amount to be sanitized. |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function sanitize_database_amount( $amount ) { |
| 147 |
if ( $this->is_comma_decimal() ) { |
| 148 |
$amount = str_replace( '.', ',', $amount ); |
| 149 |
} |
| 150 |
|
| 151 |
return $amount; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Force a string amount into decimal based format, regardless of the site currency. |
| 156 |
* |
| 157 |
* This effectively reverses the effect of Charitable_Currency::sanitize_database_amount. |
| 158 |
* |
| 159 |
* @since 1.6.0 |
| 160 |
* |
| 161 |
* @param string $amount The amount to be cast to decimal format. |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
public function cast_to_decimal_format( $amount ) { |
| 165 |
if ( $this->is_comma_decimal() ) { |
| 166 |
$amount = str_replace( ',', '.', $amount ); |
| 167 |
} else { |
| 168 |
$amount = str_replace( ',', '', $amount ); |
| 169 |
} |
| 170 |
|
| 171 |
return $amount; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Checks whether the comma is being used as the separator. |
| 176 |
* |
| 177 |
* @since 1.0.0 |
| 178 |
* |
| 179 |
* @return boolean |
| 180 |
*/ |
| 181 |
public function is_comma_decimal() { |
| 182 |
return ( ',' == $this->get_decimal_separator() ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return the currency format based on the position of the currency symbol. |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function get_currency_format() { |
| 193 |
$symbol_position = charitable_get_option( 'currency_format', 'left' ); |
| 194 |
|
| 195 |
switch ( $symbol_position ) { |
| 196 |
case 'left': |
| 197 |
$format = '%1$s%2$s'; |
| 198 |
break; |
| 199 |
case 'right': |
| 200 |
$format = '%2$s%1$s'; |
| 201 |
break; |
| 202 |
case 'left-with-space': |
| 203 |
$format = '%1$s %2$s'; |
| 204 |
break; |
| 205 |
case 'right-with-space': |
| 206 |
$format = '%2$s %1$s'; |
| 207 |
break; |
| 208 |
default: |
| 209 |
$format = apply_filters( 'charitable_currency_format', '%1$s%2$s', $symbol_position ); |
| 210 |
} |
| 211 |
|
| 212 |
return $format; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get the currency format for accounting.js |
| 217 |
* |
| 218 |
* @since 1.3.0 |
| 219 |
* |
| 220 |
* @return string |
| 221 |
*/ |
| 222 |
public function get_accounting_js_format() { |
| 223 |
return apply_filters( 'charitable_accounting_js_currency_format', '%s%v' ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Return every currency symbol used with the |
| 228 |
* |
| 229 |
* @since 1.0.0 |
| 230 |
* |
| 231 |
* @return string[] |
| 232 |
*/ |
| 233 |
public function get_all_currencies() { |
| 234 |
if ( empty( $this->currencies ) ) { |
| 235 |
$this->currencies = apply_filters( 'charitable_currencies', array( |
| 236 |
'AED' => sprintf( __( 'Emirati Dirham (%s)', 'charitable' ), $this->get_currency_symbol( 'AED' ) ), |
| 237 |
'AUD' => sprintf( __( 'Australian Dollars (%s)', 'charitable' ), $this->get_currency_symbol( 'AUD' ) ), |
| 238 |
'ARS' => sprintf( __( 'Argentine Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'ARS' ) ), |
| 239 |
'BDT' => sprintf( __( 'Bangladeshi Taka (%s)', 'charitable' ), $this->get_currency_symbol( 'BDT' ) ), |
| 240 |
'BOB' => sprintf( __( 'Bolivian Bolíviano (%s)', 'charitable' ), $this->get_currency_symbol( 'BOB' ) ), |
| 241 |
'BRL' => sprintf( __( 'Brazilian Real (%s)', 'charitable' ), $this->get_currency_symbol( 'BRL' ) ), |
| 242 |
'BGN' => sprintf( __( 'Bulgarian Lev (%s)', 'charitable' ), $this->get_currency_symbol( 'BGN' ) ), |
| 243 |
'CAD' => sprintf( __( 'Canadian Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'CAD' ) ), |
| 244 |
'CHF' => sprintf( __( 'Swiss Franc (%s)', 'charitable' ), $this->get_currency_symbol( 'CHF' ) ), |
| 245 |
'CLP' => sprintf( __( 'Chilean Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'CLP' ) ), |
| 246 |
'CNY' => sprintf( __( 'Chinese Yuan Renminbi (%s)', 'charitable' ), $this->get_currency_symbol( 'CNY' ) ), |
| 247 |
'CZK' => sprintf( __( 'Czech Koruna (%s)', 'charitable' ), $this->get_currency_symbol( 'CZK' ) ), |
| 248 |
'DKK' => sprintf( __( 'Danish Krone (%s)', 'charitable' ), $this->get_currency_symbol( 'DKK' ) ), |
| 249 |
'EGP' => sprintf( __( 'Egyptian Pound (%s)', 'charitable' ), $this->get_currency_symbol( 'EGP' ) ), |
| 250 |
'EUR' => sprintf( __( 'Euro (%s)', 'charitable' ), $this->get_currency_symbol( 'EUR' ) ), |
| 251 |
'GBP' => sprintf( __( 'British Pound (%s)', 'charitable' ), $this->get_currency_symbol( 'GBP' ) ), |
| 252 |
'GHS' => sprintf( __( 'Ghanaian Cedi (%s)', 'charitable' ), $this->get_currency_symbol( 'GHS' ) ), |
| 253 |
'HKD' => sprintf( __( 'Hong Kong Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'HKD' ) ), |
| 254 |
'HRK' => sprintf( __( 'Croatian Kuna (%s)', 'charitable' ), $this->get_currency_symbol( 'HRK' ) ), |
| 255 |
'HUF' => sprintf( __( 'Hungarian Forint (%s)', 'charitable' ), $this->get_currency_symbol( 'HUF' ) ), |
| 256 |
'IDR' => sprintf( __( 'Indonesian Rupiah (%s)', 'charitable' ), $this->get_currency_symbol( 'IDR' ) ), |
| 257 |
'ILS' => sprintf( __( 'Israeli Shekel (%s)', 'charitable' ), $this->get_currency_symbol( 'ILS' ) ), |
| 258 |
'INR' => sprintf( __( 'Indian Rupee (%s)', 'charitable' ), $this->get_currency_symbol( 'INR' ) ), |
| 259 |
'ISK' => sprintf( __( 'Icelandic Krona (%s)', 'charitable' ), $this->get_currency_symbol( 'ISK' ) ), |
| 260 |
'JPY' => sprintf( __( 'Japanese Yen (%s)', 'charitable' ), $this->get_currency_symbol( 'JPY' ) ), |
| 261 |
'KRW' => sprintf( __( 'South Korean Won (%s)', 'charitable' ), $this->get_currency_symbol( 'KRW' ) ), |
| 262 |
'MXN' => sprintf( __( 'Mexican Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'MXN' ) ), |
| 263 |
'MYR' => sprintf( __( 'Malaysian Ringgit (%s)', 'charitable' ), $this->get_currency_symbol( 'MYR' ) ), |
| 264 |
'NGN' => sprintf( __( 'Nigerian Naira (%s)', 'charitable' ), $this->get_currency_symbol( 'NGN' ) ), |
| 265 |
'NOK' => sprintf( __( 'Norwegian Krone (%s)', 'charitable' ), $this->get_currency_symbol( 'NOK' ) ), |
| 266 |
'NZD' => sprintf( __( 'New Zealand Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'NZD' ) ), |
| 267 |
'PHP' => sprintf( __( 'Philippine Peso (%s)', 'charitable' ), $this->get_currency_symbol( 'PHP' ) ), |
| 268 |
'PLN' => sprintf( __( 'Polish Zloty (%s)', 'charitable' ), $this->get_currency_symbol( 'PLN' ) ), |
| 269 |
'RON' => sprintf( __( 'Romanian New Leu (%s)', 'charitable' ), $this->get_currency_symbol( 'RON' ) ), |
| 270 |
'RUB' => sprintf( __( 'Russian Ruble (%s)', 'charitable' ), $this->get_currency_symbol( 'RUB' ) ), |
| 271 |
'SEK' => sprintf( __( 'Swedish Krona (%s)', 'charitable' ), $this->get_currency_symbol( 'SEK' ) ), |
| 272 |
'SGD' => sprintf( __( 'Singapore Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'SGD' ) ), |
| 273 |
'THB' => sprintf( __( 'Thai Baht (%s)', 'charitable' ), $this->get_currency_symbol( 'THB' ) ), |
| 274 |
'TRY' => sprintf( __( 'Turkish Lira (%s)', 'charitable' ), $this->get_currency_symbol( 'TRY' ) ), |
| 275 |
'TWD' => sprintf( __( 'Taiwan New Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'TWD' ) ), |
| 276 |
'USD' => sprintf( __( 'US Dollar (%s)', 'charitable' ), $this->get_currency_symbol( 'USD' ) ), |
| 277 |
'VND' => sprintf( __( 'Vietnamese Dong (%s)', 'charitable' ), $this->get_currency_symbol( 'VND' ) ), |
| 278 |
'ZAR' => sprintf( __( 'South African Rand (%s)', 'charitable' ), $this->get_currency_symbol( 'ZAR' ) ), |
| 279 |
) ); |
| 280 |
}//end if |
| 281 |
|
| 282 |
return $this->currencies; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Return the currency symbol for a given currency. |
| 287 |
* |
| 288 |
* This function was changed to a public method in 1.3.7. |
| 289 |
* |
| 290 |
* Credit: This is based on the WooCommerce implemenation. |
| 291 |
* |
| 292 |
* @since 1.0.0 |
| 293 |
* |
| 294 |
* @param string $currency Optional. If not set, currency is based on currently selected currency. |
| 295 |
* @return string |
| 296 |
*/ |
| 297 |
public function get_currency_symbol( $currency = '' ) { |
| 298 |
if ( ! strlen( $currency ) ) { |
| 299 |
$currency = charitable_get_option( 'currency', 'AUD' ); |
| 300 |
} |
| 301 |
|
| 302 |
switch ( $currency ) { |
| 303 |
case 'AED': |
| 304 |
$currency_symbol = 'د.إ'; |
| 305 |
break; |
| 306 |
case 'BDT': |
| 307 |
$currency_symbol = '৳'; |
| 308 |
break; |
| 309 |
case 'BOB': |
| 310 |
$currency_symbol = 'Bs.'; |
| 311 |
break; |
| 312 |
case 'BRL': |
| 313 |
$currency_symbol = 'R$'; |
| 314 |
break; |
| 315 |
case 'BGN': |
| 316 |
$currency_symbol = 'лв.'; |
| 317 |
break; |
| 318 |
case 'AUD': |
| 319 |
case 'ARS': |
| 320 |
case 'CAD': |
| 321 |
case 'CLP': |
| 322 |
case 'MXN': |
| 323 |
case 'NZD': |
| 324 |
case 'HKD': |
| 325 |
case 'SGD': |
| 326 |
case 'USD': |
| 327 |
$currency_symbol = '$'; |
| 328 |
break; |
| 329 |
case 'EGP': |
| 330 |
$currency_symbol = 'E£'; |
| 331 |
break; |
| 332 |
case 'EUR': |
| 333 |
$currency_symbol = '€'; |
| 334 |
break; |
| 335 |
case 'GHS': |
| 336 |
$currency_symbol = 'GH₵'; |
| 337 |
break; |
| 338 |
case 'CNY': |
| 339 |
case 'RMB': |
| 340 |
case 'JPY': |
| 341 |
$currency_symbol = '¥'; |
| 342 |
break; |
| 343 |
case 'RUB': |
| 344 |
$currency_symbol = 'руб.'; |
| 345 |
break; |
| 346 |
case 'KRW': |
| 347 |
$currency_symbol = '₩'; |
| 348 |
break; |
| 349 |
case 'TRY': |
| 350 |
$currency_symbol = '₺'; |
| 351 |
break; |
| 352 |
case 'NOK': |
| 353 |
$currency_symbol = 'kr'; |
| 354 |
break; |
| 355 |
case 'ZAR': |
| 356 |
$currency_symbol = 'R'; |
| 357 |
break; |
| 358 |
case 'CZK': |
| 359 |
$currency_symbol = 'Kč'; |
| 360 |
break; |
| 361 |
case 'MYR': |
| 362 |
$currency_symbol = 'RM'; |
| 363 |
break; |
| 364 |
case 'DKK': |
| 365 |
$currency_symbol = 'kr.'; |
| 366 |
break; |
| 367 |
case 'HUF': |
| 368 |
$currency_symbol = 'Ft'; |
| 369 |
break; |
| 370 |
case 'IDR': |
| 371 |
$currency_symbol = 'Rp'; |
| 372 |
break; |
| 373 |
case 'INR': |
| 374 |
$currency_symbol = 'Rs.'; |
| 375 |
break; |
| 376 |
case 'ISK': |
| 377 |
$currency_symbol = 'Kr.'; |
| 378 |
break; |
| 379 |
case 'ILS': |
| 380 |
$currency_symbol = '₪'; |
| 381 |
break; |
| 382 |
case 'PHP': |
| 383 |
$currency_symbol = '₱'; |
| 384 |
break; |
| 385 |
case 'PLN': |
| 386 |
$currency_symbol = 'zł'; |
| 387 |
break; |
| 388 |
case 'SEK': |
| 389 |
$currency_symbol = 'kr'; |
| 390 |
break; |
| 391 |
case 'CHF': |
| 392 |
$currency_symbol = 'CHF'; |
| 393 |
break; |
| 394 |
case 'TWD': |
| 395 |
$currency_symbol = 'NT$'; |
| 396 |
break; |
| 397 |
case 'THB': |
| 398 |
$currency_symbol = '฿'; |
| 399 |
break; |
| 400 |
case 'GBP': |
| 401 |
$currency_symbol = '£'; |
| 402 |
break; |
| 403 |
case 'RON': |
| 404 |
$currency_symbol = 'lei'; |
| 405 |
break; |
| 406 |
case 'VND': |
| 407 |
$currency_symbol = '₫'; |
| 408 |
break; |
| 409 |
case 'NGN': |
| 410 |
$currency_symbol = '₦'; |
| 411 |
break; |
| 412 |
case 'HRK': |
| 413 |
$currency_symbol = 'Kn'; |
| 414 |
break; |
| 415 |
default : |
| 416 |
$currency_symbol = ''; |
| 417 |
break; |
| 418 |
}//end switch |
| 419 |
|
| 420 |
return apply_filters( 'charitable_currency_symbol', $currency_symbol, $currency ); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Return the thousands separator. |
| 425 |
* |
| 426 |
* @since 1.5.0 |
| 427 |
* |
| 428 |
* @return string |
| 429 |
*/ |
| 430 |
public function get_thousands_separator() { |
| 431 |
$separator = charitable_get_option( 'thousands_separator', ',' ); |
| 432 |
|
| 433 |
if ( 'none' == $separator ) { |
| 434 |
$separator = ''; |
| 435 |
} |
| 436 |
|
| 437 |
return $separator; |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* Return the decimal separator. |
| 442 |
* |
| 443 |
* @since 1.5.0 |
| 444 |
* |
| 445 |
* @return string |
| 446 |
*/ |
| 447 |
public function get_decimal_separator() { |
| 448 |
return charitable_get_option( 'decimal_separator', '.' ); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Return the number of decimals to use. |
| 453 |
* |
| 454 |
* @since 1.0.0 |
| 455 |
* |
| 456 |
* @return int |
| 457 |
*/ |
| 458 |
public function get_decimals() { |
| 459 |
return charitable_get_option( 'decimal_count', 2 ); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
endif; |
| 464 |
|