| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Exit if accessed directly |
| 5 |
*/ |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
if ( ! class_exists( 'Dfrapi_Currency' ) ) { |
| 11 |
|
| 12 |
class Dfrapi_Currency { |
| 13 |
|
| 14 |
/** @var string Default currency code to use when $currency_code does not exist. */ |
| 15 |
const DEFAULT_CURRENCY_CODE = 'USD'; |
| 16 |
|
| 17 |
/** @var string $currency_code 3-character ISO 4217 currency code. */ |
| 18 |
private $currency_code; |
| 19 |
|
| 20 |
/** @var mixed $context The context we are displaying the price in. */ |
| 21 |
private $context; |
| 22 |
|
| 23 |
/** |
| 24 |
* Dfrapi_Currency constructor. |
| 25 |
* |
| 26 |
* @param string $currency_code 3-character ISO 4217 currency code. |
| 27 |
* @param mixed $context Optional. |
| 28 |
*/ |
| 29 |
public function __construct( $currency_code, $context = null ) { |
| 30 |
$this->context = $context; |
| 31 |
$valid_currency_code = $this->validate_currency_code( $currency_code ); |
| 32 |
$this->currency_code = is_wp_error( $valid_currency_code ) ? self::DEFAULT_CURRENCY_CODE : $valid_currency_code; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns the initial $context passed in the constructor. |
| 37 |
* |
| 38 |
* @return mixed |
| 39 |
*/ |
| 40 |
public function get_context() { |
| 41 |
return $this->context; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the 3-character ISO 4217 currency code (ie. USD, EUR, GBP, etc...). |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_currency_code() { |
| 50 |
return apply_filters( 'dfrapi_currency_code', $this->currency_code, $this ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the currency symbol (ie. $, €, £, etc...) for the $this->currency_code. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function get_currency_symbol() { |
| 59 |
return apply_filters( 'dfrapi_currency_symbol', $this->get_currency_field( 'symbol' ), $this ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the currency name (ie. US Dollar, Euro, Great Britain Pound Sterling, etc..) for the $this->currency_code. |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function get_currency_name() { |
| 68 |
return apply_filters( 'dfrapi_currency_name', $this->get_currency_field( 'name' ), $this ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns the decimal point to use for the $this->currency_code. |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function get_decimal_point() { |
| 77 |
return apply_filters( 'dfrapi_currency_decimal_point', $this->get_currency_field( 'decimal_point' ), $this ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the thousands separator to use for the $this->currency_code. |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
public function get_thousands_sep() { |
| 86 |
return apply_filters( 'dfrapi_currency_thousands_sep', $this->get_currency_field( 'thousands_sep' ), $this ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns an array of data about the $this->currency_code. |
| 91 |
* |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
public function get_currency() { |
| 95 |
return self::currencies()[ $this->currency_code ]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Return a field from the currencies array for the $this->currency_code. |
| 100 |
* |
| 101 |
* This will return an empty string if the $field is not found. |
| 102 |
* |
| 103 |
* This function is safe to use to display data as it only returns a string no matter if $field doesn't exist. |
| 104 |
* |
| 105 |
* @param string $field Item in currencies array to get. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function get_currency_field( $field ) { |
| 110 |
$currency = $this->get_currency(); |
| 111 |
|
| 112 |
return ( isset( $currency[ $field ] ) ) ? $currency[ $field ] : ''; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns the format that this currency should be displayed in when the price is positive or zero. |
| 117 |
* |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
public function get_format() { |
| 121 |
return apply_filters( 'dfrapi_currency_format', $this->get_currency_field( 'format' ), $this ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the format that this currency should be displayed in when the price is negative. |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public function get_neg_format() { |
| 130 |
return apply_filters( 'dfrapi_currency_neg_format', $this->get_currency_field( '-format' ), $this ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns true if the currency code is supported. Else returns false. |
| 135 |
* |
| 136 |
* @param string $currency_code 3-character ISO 4217 currency code |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public function currency_code_is_supported( $currency_code ) { |
| 141 |
return in_array( $currency_code, self::get_supported_currency_codes() ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns a valid currency code trimmed and uppercased or WP_Error if code is invalid or unsupported. |
| 146 |
* |
| 147 |
* @param string $currency_code 3-character ISO 4217 currency code |
| 148 |
* |
| 149 |
* @return string|WP_Error Returns 3-character ISO 4217 code or WP_Error if it doesn't exist or is invalid. |
| 150 |
*/ |
| 151 |
private function validate_currency_code( $currency_code ) { |
| 152 |
|
| 153 |
$code = strtoupper( trim( $currency_code ) ); |
| 154 |
|
| 155 |
if ( strlen( $code ) !== 3 ) { |
| 156 |
return new WP_Error( |
| 157 |
'invalid_currency_code', |
| 158 |
'The currency code "' . esc_html( $currency_code ) . '" is invalid. Currency codes must be exactly 3 characters.' |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! $this->currency_code_is_supported( $currency_code ) ) { |
| 163 |
return new WP_Error( |
| 164 |
'unsupported_currency_code', |
| 165 |
'The currency code "' . esc_html( $currency_code ) . '" is unsupported.' |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
return $code; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns an array of all currently supported Currency Codes. |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public static function get_supported_currency_codes() { |
| 178 |
return array_keys( self::currencies() ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns an array of supported currencies and relevant data about each currency. |
| 183 |
* |
| 184 |
* @link https://www.thefinancials.com/Default.aspx?SubSectionID=curformat |
| 185 |
* @link https://fastspring.com/blog/how-to-format-30-currencies-from-countries-all-over-the-world/ |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
public static function currencies() { |
| 190 |
return apply_filters( 'dfrapi_currencies', [ |
| 191 |
'ARS' => [ |
| 192 |
'symbol' => '$', |
| 193 |
'code' => 'ARS', |
| 194 |
'format' => '{symbol} {price}', |
| 195 |
'-format' => '-{symbol} {price}', |
| 196 |
'decimal_point' => ',', |
| 197 |
'thousands_sep' => '.', |
| 198 |
'name' => __( 'Argentine Peso', 'datafeedr-api' ), |
| 199 |
], |
| 200 |
'AUD' => [ |
| 201 |
'symbol' => '$', |
| 202 |
'code' => 'AUD', |
| 203 |
'format' => '{symbol} {price}', |
| 204 |
'-format' => '-{symbol} {price}', |
| 205 |
'decimal_point' => '.', |
| 206 |
'thousands_sep' => ',', |
| 207 |
'name' => __( 'Australian Dollar', 'datafeedr-api' ), |
| 208 |
], |
| 209 |
'BRL' => [ |
| 210 |
'symbol' => 'R$', |
| 211 |
'code' => 'BRL', |
| 212 |
'format' => '{symbol} {price}', |
| 213 |
'-format' => '-{symbol} {price}', |
| 214 |
'decimal_point' => ',', |
| 215 |
'thousands_sep' => '.', |
| 216 |
'name' => __( 'Brazilian Real', 'datafeedr-api' ), |
| 217 |
], |
| 218 |
'CAD' => [ |
| 219 |
'symbol' => '$', |
| 220 |
'code' => 'CAD', |
| 221 |
'format' => '{symbol} {price}', |
| 222 |
'-format' => '-{symbol} {price}', |
| 223 |
'decimal_point' => '.', |
| 224 |
'thousands_sep' => ',', |
| 225 |
'name' => __( 'Canadian Dollar', 'datafeedr-api' ), |
| 226 |
], |
| 227 |
'CLP' => [ |
| 228 |
'symbol' => '$', |
| 229 |
'code' => 'CLP', |
| 230 |
'format' => '{symbol} {price}', |
| 231 |
'-format' => '-{symbol} {price}', |
| 232 |
'decimal_point' => '.', |
| 233 |
'thousands_sep' => ',', |
| 234 |
'name' => __( 'Chilean Peso', 'datafeedr-api' ), |
| 235 |
], |
| 236 |
'CNY' => [ |
| 237 |
'symbol' => '� |
| 238 |
�', |
| 239 |
'code' => 'CNY', |
| 240 |
'format' => '{symbol} {price}', |
| 241 |
'-format' => '-{symbol} {price}', |
| 242 |
'decimal_point' => '.', |
| 243 |
'thousands_sep' => ',', |
| 244 |
'name' => __( 'Chinese Yuan Renminbi', 'datafeedr-api' ), |
| 245 |
], |
| 246 |
'COP' => [ |
| 247 |
'symbol' => '$', |
| 248 |
'code' => 'COP', |
| 249 |
'format' => '{symbol} {price}', |
| 250 |
'-format' => '-{symbol} {price}', |
| 251 |
'decimal_point' => '.', |
| 252 |
'thousands_sep' => ',', |
| 253 |
'name' => __( 'Colombian Peso', 'datafeedr-api' ), |
| 254 |
], |
| 255 |
'CHF' => [ |
| 256 |
'symbol' => 'fr.', |
| 257 |
'code' => 'CHF', |
| 258 |
'format' => '{symbol} {price}', |
| 259 |
'-format' => '-{symbol} {price}', |
| 260 |
'decimal_point' => '.', |
| 261 |
'thousands_sep' => '’', |
| 262 |
'name' => __( 'Swiss Franc', 'datafeedr-api' ), |
| 263 |
], |
| 264 |
'CZK' => [ |
| 265 |
'symbol' => 'Kč', |
| 266 |
'code' => 'CZK', |
| 267 |
'format' => '{price} {symbol}', |
| 268 |
'-format' => '-{price} {symbol}', |
| 269 |
'decimal_point' => ',', |
| 270 |
'thousands_sep' => '.', |
| 271 |
'name' => __( 'Czech Koruna', 'datafeedr-api' ), |
| 272 |
], |
| 273 |
'DKK' => [ |
| 274 |
'symbol' => 'kr.', |
| 275 |
'code' => 'DKK', |
| 276 |
'format' => '{symbol} {price}', |
| 277 |
'-format' => '-{symbol} {price}', |
| 278 |
'decimal_point' => ',', |
| 279 |
'thousands_sep' => '.', |
| 280 |
'name' => __( 'Danish Krone', 'datafeedr-api' ), |
| 281 |
], |
| 282 |
'EUR' => [ |
| 283 |
'symbol' => '€', |
| 284 |
'code' => 'EUR', |
| 285 |
'format' => '{symbol}{price}', |
| 286 |
'-format' => '-{symbol}{price}', |
| 287 |
'decimal_point' => ',', |
| 288 |
'thousands_sep' => '.', |
| 289 |
'name' => __( 'Euro', 'datafeedr-api' ), |
| 290 |
], |
| 291 |
'HKD' => [ |
| 292 |
'symbol' => 'HK$', |
| 293 |
'code' => 'HKD', |
| 294 |
'format' => '{symbol} {price}', |
| 295 |
'-format' => '-{symbol} {price}', |
| 296 |
'decimal_point' => '.', |
| 297 |
'thousands_sep' => ',', |
| 298 |
'name' => __( 'Hong Kong Dollar', 'datafeedr-api' ), |
| 299 |
], |
| 300 |
'GBP' => [ |
| 301 |
'symbol' => '£', |
| 302 |
'code' => 'GBP', |
| 303 |
'format' => '{symbol}{price}', |
| 304 |
'-format' => '-{symbol}{price}', |
| 305 |
'decimal_point' => '.', |
| 306 |
'thousands_sep' => ',', |
| 307 |
'name' => __( 'Great Britain Pound Sterling', 'datafeedr-api' ), |
| 308 |
], |
| 309 |
'HUF' => [ |
| 310 |
'symbol' => 'Ft', |
| 311 |
'code' => 'HUF', |
| 312 |
'format' => '{price} {symbol}', |
| 313 |
'-format' => '-{price} {symbol}', |
| 314 |
'decimal_point' => ',', |
| 315 |
'thousands_sep' => '.', |
| 316 |
'name' => __( 'Hungarian Forint', 'datafeedr-api' ), |
| 317 |
], |
| 318 |
'INR' => [ |
| 319 |
'symbol' => '₹', |
| 320 |
'code' => 'INR', |
| 321 |
'format' => '{symbol} {price}', |
| 322 |
'-format' => '-{symbol} {price}', |
| 323 |
'decimal_point' => '.', |
| 324 |
'thousands_sep' => ',', |
| 325 |
'name' => __( 'Indian Rupee', 'datafeedr-api' ), |
| 326 |
], |
| 327 |
'ILS' => [ |
| 328 |
'symbol' => '₪', |
| 329 |
'code' => 'ILS', |
| 330 |
'format' => '{symbol} {price}', |
| 331 |
'-format' => '-{symbol} {price}', |
| 332 |
'decimal_point' => ',', |
| 333 |
'thousands_sep' => '.', |
| 334 |
'name' => __( 'New Israeli Shekel', 'datafeedr-api' ), |
| 335 |
], |
| 336 |
'JPY' => [ |
| 337 |
'symbol' => '¥', |
| 338 |
'code' => 'JPY', |
| 339 |
'format' => '{symbol} {price}', |
| 340 |
'-format' => '-{symbol} {price}', |
| 341 |
'decimal_point' => '.', |
| 342 |
'thousands_sep' => ',', |
| 343 |
'name' => __( 'Japanese Yen', 'datafeedr-api' ), |
| 344 |
], |
| 345 |
'KRW' => [ |
| 346 |
'symbol' => '₩', |
| 347 |
'code' => 'KRW', |
| 348 |
'format' => '{symbol} {price}', |
| 349 |
'-format' => '-{symbol} {price}', |
| 350 |
'decimal_point' => '.', |
| 351 |
'thousands_sep' => ',', |
| 352 |
'name' => __( 'Korean Won', 'datafeedr-api' ), |
| 353 |
], |
| 354 |
'MYR' => [ |
| 355 |
'symbol' => 'RM', |
| 356 |
'code' => 'MYR', |
| 357 |
'format' => '{symbol} {price}', |
| 358 |
'-format' => '-{symbol} {price}', |
| 359 |
'decimal_point' => '.', |
| 360 |
'thousands_sep' => ',', |
| 361 |
'name' => __( 'Malaysian Ringgit', 'datafeedr-api' ), |
| 362 |
], |
| 363 |
'MXN' => [ |
| 364 |
'symbol' => '$', |
| 365 |
'code' => 'MXN', |
| 366 |
'format' => '{symbol} {price}', |
| 367 |
'-format' => '-{symbol} {price}', |
| 368 |
'decimal_point' => '.', |
| 369 |
'thousands_sep' => ',', |
| 370 |
'name' => __( 'Mexican Peso', 'datafeedr-api' ), |
| 371 |
], |
| 372 |
'NOK' => [ |
| 373 |
'symbol' => 'kr', |
| 374 |
'code' => 'NOK', |
| 375 |
'format' => '{symbol} {price}', |
| 376 |
'-format' => '-{symbol} {price}', |
| 377 |
'decimal_point' => '.', |
| 378 |
'thousands_sep' => ',', |
| 379 |
'name' => __( 'Norwegian Krone', 'datafeedr-api' ), |
| 380 |
], |
| 381 |
'NZD' => [ |
| 382 |
'symbol' => '$', |
| 383 |
'code' => 'NZD', |
| 384 |
'format' => '{symbol} {price}', |
| 385 |
'-format' => '-{symbol} {price}', |
| 386 |
'decimal_point' => '.', |
| 387 |
'thousands_sep' => ',', |
| 388 |
'name' => __( 'New Zealand Dollar', 'datafeedr-api' ), |
| 389 |
], |
| 390 |
'PLN' => [ |
| 391 |
'symbol' => 'zł', |
| 392 |
'code' => 'PLN', |
| 393 |
'format' => '{price} {symbol}', |
| 394 |
'-format' => '-{price} {symbol}', |
| 395 |
'decimal_point' => ',', |
| 396 |
'thousands_sep' => '.', |
| 397 |
'name' => __( 'Polish Zloty', 'datafeedr-api' ), |
| 398 |
], |
| 399 |
'PHP' => [ |
| 400 |
'symbol' => '₱', |
| 401 |
'code' => 'PHP', |
| 402 |
'format' => '{symbol} {price}', |
| 403 |
'-format' => '-{symbol} {price}', |
| 404 |
'decimal_point' => '.', |
| 405 |
'thousands_sep' => ',', |
| 406 |
'name' => __( 'Philippine Peso', 'datafeedr-api' ), |
| 407 |
], |
| 408 |
'RON' => [ |
| 409 |
'symbol' => 'L', |
| 410 |
'code' => 'RON', |
| 411 |
'format' => '{price} {symbol}', |
| 412 |
'-format' => '-{price} {symbol}', |
| 413 |
'decimal_point' => ',', |
| 414 |
'thousands_sep' => '.', |
| 415 |
'name' => __( 'Romanian Leu', 'datafeedr-api' ), |
| 416 |
], |
| 417 |
'RUB' => [ |
| 418 |
'symbol' => 'p.', |
| 419 |
'code' => 'RUB', |
| 420 |
'format' => '{price} {symbol}', |
| 421 |
'-format' => '-{price} {symbol}', |
| 422 |
'decimal_point' => ',', |
| 423 |
'thousands_sep' => '.', |
| 424 |
'name' => __( 'Russian Ruble', 'datafeedr-api' ), |
| 425 |
], |
| 426 |
'SGD' => [ |
| 427 |
'symbol' => '$', |
| 428 |
'code' => 'SGD', |
| 429 |
'format' => '{symbol}{price}', |
| 430 |
'-format' => '-{symbol}{price}', |
| 431 |
'decimal_point' => '.', |
| 432 |
'thousands_sep' => ',', |
| 433 |
'name' => __( 'Singapore Dollar', 'datafeedr-api' ), |
| 434 |
], |
| 435 |
'ZAR' => [ |
| 436 |
'symbol' => 'R', |
| 437 |
'code' => 'ZAR', |
| 438 |
'format' => '{symbol} {price}', |
| 439 |
'-format' => '-{symbol} {price}', |
| 440 |
'decimal_point' => '.', |
| 441 |
'thousands_sep' => ',', |
| 442 |
'name' => __( 'South African Rand', 'datafeedr-api' ), |
| 443 |
], |
| 444 |
'SEK' => [ |
| 445 |
'symbol' => 'kr', |
| 446 |
'code' => 'SEK', |
| 447 |
'format' => '{price} {symbol}', |
| 448 |
'-format' => '-{price} {symbol}', |
| 449 |
'decimal_point' => ',', |
| 450 |
'thousands_sep' => '.', |
| 451 |
'name' => __( 'Swedish Krona', 'datafeedr-api' ), |
| 452 |
], |
| 453 |
'TWD' => [ |
| 454 |
'symbol' => '� |
| 455 |
�', |
| 456 |
'code' => 'TWD', |
| 457 |
'format' => '{symbol} {price}', |
| 458 |
'-format' => '-{symbol} {price}', |
| 459 |
'decimal_point' => '.', |
| 460 |
'thousands_sep' => ',', |
| 461 |
'name' => __( 'New Taiwan Dollar', 'datafeedr-api' ), |
| 462 |
], |
| 463 |
'THB' => [ |
| 464 |
'symbol' => '฿', |
| 465 |
'code' => 'THB', |
| 466 |
'format' => '{price} {symbol}', |
| 467 |
'-format' => '-{price} {symbol}', |
| 468 |
'decimal_point' => '.', |
| 469 |
'thousands_sep' => ',', |
| 470 |
'name' => __( 'Thai Baht', 'datafeedr-api' ), |
| 471 |
], |
| 472 |
'TRY' => [ |
| 473 |
'symbol' => '₺', |
| 474 |
'code' => 'TRY', |
| 475 |
'format' => '{price} {symbol}', |
| 476 |
'-format' => '-{price} {symbol}', |
| 477 |
'decimal_point' => '.', |
| 478 |
'thousands_sep' => ',', |
| 479 |
'name' => __( 'Turkish Lira', 'datafeedr-api' ), |
| 480 |
], |
| 481 |
'UAH' => [ |
| 482 |
'symbol' => 'грн', |
| 483 |
'code' => 'UAH', |
| 484 |
'format' => '{price} {symbol}', |
| 485 |
'-format' => '-{price} {symbol}', |
| 486 |
'decimal_point' => '.', |
| 487 |
'thousands_sep' => ' ', |
| 488 |
'name' => __( 'Ukrainian Hryvnia', 'datafeedr-api' ), |
| 489 |
], |
| 490 |
'USD' => [ |
| 491 |
'symbol' => '$', |
| 492 |
'code' => 'USD', |
| 493 |
'format' => '{symbol}{price}', |
| 494 |
'-format' => '-{symbol}{price}', |
| 495 |
'decimal_point' => '.', |
| 496 |
'thousands_sep' => ',', |
| 497 |
'name' => __( 'US Dollar', 'datafeedr-api' ), |
| 498 |
], |
| 499 |
'VND' => [ |
| 500 |
'symbol' => '₫', |
| 501 |
'code' => 'VND', |
| 502 |
'format' => '{price} {symbol}', |
| 503 |
'-format' => '-{price} {symbol}', |
| 504 |
'decimal_point' => ',', |
| 505 |
'thousands_sep' => '.', |
| 506 |
'name' => __( 'Vietnamese Dong', 'datafeedr-api' ), |
| 507 |
], |
| 508 |
] ); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|