| 1 |
<?php |
| 2 |
/** |
| 3 |
* Currency Functions |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Functions |
| 7 |
* @copyright Copyright (c) 2017, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.8.17 |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Get the set currency |
| 14 |
* |
| 15 |
* @since 1.0 |
| 16 |
* @since 1.8.15 Upgrade function to handle dynamic currency |
| 17 |
* |
| 18 |
* @param int $donation_or_form_id Donation or Form ID |
| 19 |
* @param array|object $args Additional data |
| 20 |
* |
| 21 |
* @return string The currency code |
| 22 |
*/ |
| 23 |
function give_get_currency( $donation_or_form_id = null, $args = [] ) { |
| 24 |
|
| 25 |
// Get currency from donation |
| 26 |
if ( is_numeric( $donation_or_form_id ) && 'give_payment' === get_post_type( $donation_or_form_id ) ) { |
| 27 |
$currency = give_get_meta( $donation_or_form_id, '_give_payment_currency', true ); |
| 28 |
|
| 29 |
if ( empty( $currency ) ) { |
| 30 |
$currency = give_get_option( 'currency', 'USD' ); |
| 31 |
} |
| 32 |
} else { |
| 33 |
$currency = give_get_option( 'currency', 'USD' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Filter the currency on basis of donation, form id, or additional data. |
| 38 |
* |
| 39 |
* @since 1.0 |
| 40 |
*/ |
| 41 |
return apply_filters( 'give_currency', $currency, $donation_or_form_id, $args ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the set currency position |
| 46 |
* |
| 47 |
* @since 1.3.6 |
| 48 |
* |
| 49 |
* @return string The currency code |
| 50 |
*/ |
| 51 |
function give_get_currency_position() { |
| 52 |
|
| 53 |
$currency_pos = give_get_option( 'currency_position', 'before' ); |
| 54 |
|
| 55 |
return apply_filters( 'give_currency_position', $currency_pos ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get Currencies List |
| 60 |
* |
| 61 |
* @since 1.8.17 |
| 62 |
* |
| 63 |
* @return array $currencies A list of the available currencies |
| 64 |
*/ |
| 65 |
function give_get_currencies_list() { |
| 66 |
$currencies = Give_Cache_Setting::get_option( 'currencies' ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Filter the currencies |
| 70 |
* Note: you can register new currency by using this filter |
| 71 |
* array( |
| 72 |
* 'admin_label' => '', // required |
| 73 |
* 'symbol' => '', // required |
| 74 |
* 'setting' => '' // required |
| 75 |
* .... |
| 76 |
* ) |
| 77 |
* |
| 78 |
* @since 1.8.15 |
| 79 |
* @deprecated 2.10.4 Use give_register_currency filter hook to register new currency. |
| 80 |
* Example code to register new currency: |
| 81 |
* |
| 82 |
* add_filter( 'give_register_currency', 'give_add_costarican_currency', 10, 1 ); |
| 83 |
* function give_add_costarican_currency( $currencies ) { |
| 84 |
* $currencies['VND'] = array( |
| 85 |
* 'admin_label' => __( 'Vietnamese đồng (₫)', 'give' ), |
| 86 |
* 'symbol' => '₫', |
| 87 |
* 'setting' => array( |
| 88 |
* 'currency_position' => 'after', |
| 89 |
* 'thousands_separator' => '.', |
| 90 |
* 'decimal_separator' => ',', |
| 91 |
* 'number_decimals' => 2, |
| 92 |
* ) |
| 93 |
* ); |
| 94 |
* |
| 95 |
* return $currencies; |
| 96 |
* } |
| 97 |
* |
| 98 |
* @param array $currencies |
| 99 |
*/ |
| 100 |
return (array) apply_filters( 'give_currencies', $currencies ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get Currencies |
| 105 |
* |
| 106 |
* @since 1.0 |
| 107 |
* |
| 108 |
* @param string $info Specify currency info |
| 109 |
* |
| 110 |
* @return array $currencies A list of the available currencies |
| 111 |
*/ |
| 112 |
function give_get_currencies( $info = 'admin_label' ) { |
| 113 |
|
| 114 |
$currencies = give_get_currencies_list(); |
| 115 |
|
| 116 |
// Backward compatibility: handle old way of currency registration. |
| 117 |
// Backward compatibility: Return desired result. |
| 118 |
if ( ! empty( $currencies ) ) { |
| 119 |
foreach ( $currencies as $currency_code => $currency_setting ) { |
| 120 |
if ( is_string( $currency_setting ) ) { |
| 121 |
$currencies[ $currency_code ] = [ |
| 122 |
'admin_label' => $currency_setting, |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
$currencies[ $currency_code ] = wp_parse_args( |
| 127 |
$currencies[ $currency_code ], |
| 128 |
[ |
| 129 |
'admin_label' => '', |
| 130 |
'symbol' => $currency_code, |
| 131 |
'setting' => [], |
| 132 |
] |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
if ( ! empty( $info ) && is_string( $info ) && 'all' !== $info ) { |
| 137 |
$currencies = wp_list_pluck( $currencies, $info ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $currencies; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Get all currency symbols |
| 147 |
* |
| 148 |
* @since 1.8.14 |
| 149 |
* |
| 150 |
* @param bool $decode_currencies |
| 151 |
* |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
function give_currency_symbols( $decode_currencies = false ) { |
| 155 |
$currencies = give_get_currencies( 'symbol' ); |
| 156 |
|
| 157 |
if ( $decode_currencies ) { |
| 158 |
array_walk( |
| 159 |
$currencies, |
| 160 |
function ( &$currency_symbol ) { |
| 161 |
$currency_symbol = html_entity_decode( $currency_symbol, ENT_COMPAT, 'UTF-8' ); |
| 162 |
} |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Filter the currency symbols |
| 168 |
* |
| 169 |
* @since 1.8.14 |
| 170 |
* |
| 171 |
* @param array $currencies |
| 172 |
*/ |
| 173 |
return apply_filters( 'give_currency_symbols', $currencies ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Give Currency Symbol |
| 179 |
* |
| 180 |
* Given a currency determine the symbol to use. If no currency given, site default is used. If no symbol is determine, |
| 181 |
* the currency string is returned. |
| 182 |
* |
| 183 |
* @since 1.0 |
| 184 |
* |
| 185 |
* @param string $currency The currency string. |
| 186 |
* @param bool $decode_currency Option to HTML decode the currency symbol. |
| 187 |
* |
| 188 |
* @return string The symbol to use for the currency |
| 189 |
*/ |
| 190 |
function give_currency_symbol( $currency = '', $decode_currency = false ) { |
| 191 |
|
| 192 |
if ( empty( $currency ) ) { |
| 193 |
$currency = give_get_currency(); |
| 194 |
} |
| 195 |
|
| 196 |
$currencies = give_currency_symbols( $decode_currency ); |
| 197 |
$symbol = array_key_exists( $currency, $currencies ) ? $currencies[ $currency ] : $currency; |
| 198 |
|
| 199 |
/** |
| 200 |
* Filter the currency symbol |
| 201 |
* |
| 202 |
* @since 1.0 |
| 203 |
* |
| 204 |
* @param string $symbol |
| 205 |
* @param string $currency |
| 206 |
*/ |
| 207 |
return apply_filters( 'give_currency_symbol', $symbol, $currency ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Get currency name. |
| 213 |
* |
| 214 |
* @since 1.8.8 |
| 215 |
* |
| 216 |
* @param string $currency_code |
| 217 |
* |
| 218 |
* @return string |
| 219 |
*/ |
| 220 |
function give_get_currency_name( $currency_code ) { |
| 221 |
$currency_name = ''; |
| 222 |
$currency_names = give_get_currencies(); |
| 223 |
|
| 224 |
if ( $currency_code && array_key_exists( $currency_code, $currency_names ) ) { |
| 225 |
$currency_name = explode( '(', $currency_names[ $currency_code ] ); |
| 226 |
$currency_name = trim( current( $currency_name ) ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Filter the currency name |
| 231 |
* |
| 232 |
* @since 1.8.8 |
| 233 |
* |
| 234 |
* @param string $currency_name |
| 235 |
* @param string $currency_code |
| 236 |
*/ |
| 237 |
return apply_filters( 'give_currency_name', $currency_name, $currency_code ); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Formats the currency displayed. |
| 242 |
* |
| 243 |
* @since 1.0 |
| 244 |
* |
| 245 |
* @param string $price The donation amount. |
| 246 |
* @param array $args It accepts 'currency_code', 'decode_currency' and 'form_id'. |
| 247 |
* |
| 248 |
* @return mixed|string |
| 249 |
*/ |
| 250 |
function give_currency_filter( $price = '', $args = [] ) { |
| 251 |
|
| 252 |
// Get functions arguments. |
| 253 |
$func_args = func_get_args(); |
| 254 |
|
| 255 |
// Backward compatibility: modify second param to array |
| 256 |
if ( isset( $func_args[1] ) && is_string( $func_args[1] ) ) { |
| 257 |
$args = [ |
| 258 |
'currency_code' => isset( $func_args[1] ) ? $func_args[1] : '', |
| 259 |
'decode_currency' => isset( $func_args[2] ) ? $func_args[2] : false, |
| 260 |
'form_id' => isset( $func_args[3] ) ? $func_args[3] : '', |
| 261 |
]; |
| 262 |
|
| 263 |
give_doing_it_wrong( __FUNCTION__, 'Pass second argument as Array.' ); |
| 264 |
} |
| 265 |
|
| 266 |
// Set default values. |
| 267 |
$args = wp_parse_args( |
| 268 |
$args, |
| 269 |
[ |
| 270 |
'currency_code' => '', |
| 271 |
'decode_currency' => false, |
| 272 |
'form_id' => '', |
| 273 |
] |
| 274 |
); |
| 275 |
|
| 276 |
if ( empty( $args['currency_code'] ) || ! array_key_exists( (string) $args['currency_code'], give_get_currencies() ) ) { |
| 277 |
$args['currency_code'] = give_get_currency( $args['form_id'] ); |
| 278 |
} |
| 279 |
|
| 280 |
$args['position'] = give_get_option( 'currency_position', 'before' ); |
| 281 |
|
| 282 |
$negative = $price < 0; |
| 283 |
|
| 284 |
if ( $negative ) { |
| 285 |
// Remove proceeding "-". |
| 286 |
$price = substr( $price, 1 ); |
| 287 |
} |
| 288 |
|
| 289 |
$args['symbol'] = give_currency_symbol( $args['currency_code'], $args['decode_currency'] ); |
| 290 |
|
| 291 |
switch ( $args['currency_code'] ) : |
| 292 |
case 'GBP': |
| 293 |
case 'BRL': |
| 294 |
case 'EUR': |
| 295 |
case 'USD': |
| 296 |
case 'AUD': |
| 297 |
case 'CAD': |
| 298 |
case 'HKD': |
| 299 |
case 'MXN': |
| 300 |
case 'NZD': |
| 301 |
case 'SGD': |
| 302 |
case 'JPY': |
| 303 |
case 'THB': |
| 304 |
case 'INR': |
| 305 |
case 'IDR': |
| 306 |
case 'IRR': |
| 307 |
case 'TRY': |
| 308 |
case 'RUB': |
| 309 |
case 'SEK': |
| 310 |
case 'PLN': |
| 311 |
case 'PHP': |
| 312 |
case 'TWD': |
| 313 |
case 'MYR': |
| 314 |
case 'CZK': |
| 315 |
case 'DKK': |
| 316 |
case 'HUF': |
| 317 |
case 'ILS': |
| 318 |
case 'MAD': |
| 319 |
case 'KRW': |
| 320 |
case 'ZAR': |
| 321 |
$formatted = ( 'before' === $args['position'] ? $args['symbol'] . $price : $price . $args['symbol'] ); |
| 322 |
break; |
| 323 |
case 'NOK': |
| 324 |
$formatted = ( 'before' === $args['position'] ? $args['symbol'] . ' ' . $price : $price . ' ' . $args['symbol'] ); |
| 325 |
break; |
| 326 |
default: |
| 327 |
$formatted = ( 'before' === $args['position'] ? $args['symbol'] . ' ' . $price : $price . ' ' . $args['symbol'] ); |
| 328 |
break; |
| 329 |
endswitch; |
| 330 |
|
| 331 |
/** |
| 332 |
* Filter formatted amount |
| 333 |
* |
| 334 |
* @since 1.8.17 |
| 335 |
*/ |
| 336 |
$formatted = apply_filters( 'give_currency_filter', $formatted, $args, $price ); |
| 337 |
|
| 338 |
/** |
| 339 |
* Filter formatted amount with currency |
| 340 |
* |
| 341 |
* Filter name depends upon current value of currency and currency position. |
| 342 |
* For example : |
| 343 |
* if currency is USD and currency position is before then |
| 344 |
* filter name will be give_usd_currency_filter_before |
| 345 |
* |
| 346 |
* and if currency is USD and currency position is after then |
| 347 |
* filter name will be give_usd_currency_filter_after |
| 348 |
*/ |
| 349 |
$formatted = apply_filters( |
| 350 |
'give_' . strtolower( $args['currency_code'] ) . "_currency_filter_{$args['position']}", |
| 351 |
$formatted, |
| 352 |
$args['currency_code'], |
| 353 |
$price, |
| 354 |
$args |
| 355 |
); |
| 356 |
|
| 357 |
if ( $negative ) { |
| 358 |
// Prepend the minus sign before the currency sign. |
| 359 |
$formatted = '-' . $formatted; |
| 360 |
} |
| 361 |
|
| 362 |
return $formatted; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* This function is used to fetch list of zero based currencies. |
| 367 |
* |
| 368 |
* @since 2.3.0 |
| 369 |
* |
| 370 |
* @return array |
| 371 |
*/ |
| 372 |
function give_get_zero_based_currencies() { |
| 373 |
|
| 374 |
$zero_based_currencies = [ |
| 375 |
'JPY', // Japanese Yen. |
| 376 |
'KRW', // South Korean Won. |
| 377 |
'CLP', // Chilean peso. |
| 378 |
'ISK', // Icelandic króna. |
| 379 |
'BIF', // Burundian franc. |
| 380 |
'DJF', // Djiboutian franc. |
| 381 |
'GNF', // Guinean franc. |
| 382 |
'KHR', // Cambodian riel. |
| 383 |
'KPW', // North Korean won. |
| 384 |
'LAK', // Lao kip. |
| 385 |
'LKR', // Sri Lankan rupee. |
| 386 |
'MGA', // Malagasy ariary. |
| 387 |
'MZN', // Mozambican metical. |
| 388 |
'VUV', // Vanuatu vatu. |
| 389 |
]; |
| 390 |
|
| 391 |
/** |
| 392 |
* This filter hook can be used to update the list of zero based currencies. |
| 393 |
* |
| 394 |
* @since 2.3.0 |
| 395 |
*/ |
| 396 |
return apply_filters( 'give_get_zero_based_currencies', $zero_based_currencies ); |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Zero Decimal based Currency. |
| 401 |
* |
| 402 |
* @since 1.8.14 |
| 403 |
* @since 2.2.0 Modified list. |
| 404 |
* @see https://github.com/impress-org/give/issues/2191 |
| 405 |
* |
| 406 |
* @param string $currency Currency code |
| 407 |
* |
| 408 |
* @return bool |
| 409 |
*/ |
| 410 |
function give_is_zero_based_currency( $currency = '' ) { |
| 411 |
|
| 412 |
$zero_based_currency = give_get_zero_based_currencies(); |
| 413 |
|
| 414 |
// Set default currency. |
| 415 |
if ( empty( $currency ) ) { |
| 416 |
$currency = give_get_currency(); |
| 417 |
} |
| 418 |
|
| 419 |
// Check for Zero Based Currency. |
| 420 |
if ( in_array( $currency, $zero_based_currency ) ) { |
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/** |
| 429 |
* Check if currency support right to left direction or not. |
| 430 |
* |
| 431 |
* @param string $currency |
| 432 |
* |
| 433 |
* @return bool |
| 434 |
*/ |
| 435 |
function give_is_right_to_left_supported_currency( $currency = '' ) { |
| 436 |
$zero_based_currency = apply_filters( |
| 437 |
'give_right_to_left_supported_currency', |
| 438 |
[ |
| 439 |
'IRR', |
| 440 |
'RIAL', |
| 441 |
'MAD', |
| 442 |
'AED', |
| 443 |
'BHD', |
| 444 |
'KWD', |
| 445 |
'OMR', |
| 446 |
'SAR', |
| 447 |
'TND', // https://en.wikipedia.org/wiki/Tunisian_dinar |
| 448 |
'QAR', // https://en.wikipedia.org/wiki/Qatari_riyal |
| 449 |
'LYD', // https://en.wikipedia.org/wiki/Libyan_dinar |
| 450 |
'LBP', // https://en.wikipedia.org/wiki/Lebanese_pound |
| 451 |
'IRT', // https://en.wikipedia.org/wiki/Iranian_toman |
| 452 |
'IQD', // https://en.wikipedia.org/wiki/Iraqi_dinar |
| 453 |
'DZD', // https://en.wikipedia.org/wiki/Algerian_dinar |
| 454 |
'AFN', // https://en.wikipedia.org/wiki/Afghan_afghani |
| 455 |
] |
| 456 |
); |
| 457 |
|
| 458 |
// Set default currency. |
| 459 |
if ( empty( $currency ) ) { |
| 460 |
$currency = give_get_currency(); |
| 461 |
} |
| 462 |
|
| 463 |
// Check for Zero Based Currency. |
| 464 |
if ( in_array( $currency, $zero_based_currency ) ) { |
| 465 |
return true; |
| 466 |
} |
| 467 |
|
| 468 |
return false; |
| 469 |
} |
| 470 |
|