admin
19 hours ago
api
3 years ago
database
5 months ago
deprecated
1 month ago
donors
5 months ago
emails
9 months ago
forms
19 hours ago
frontend
6 years ago
gateways
9 months ago
libraries
9 months ago
payments
2 months ago
actions.php
9 months ago
ajax-functions.php
2 days ago
class-give-async-process.php
1 year ago
class-give-background-updater.php
9 months ago
class-give-cache-setting.php
1 year ago
class-give-cache.php
9 months ago
class-give-cli-commands.php
1 year ago
class-give-comment.php
9 months ago
class-give-cron.php
9 months ago
class-give-donate-form.php
1 year ago
class-give-donor.php
2 years ago
class-give-email-access.php
5 years ago
class-give-license-handler.php
1 month ago
class-give-logging.php
9 months ago
class-give-readme-parser.php
4 years ago
class-give-roles.php
5 months ago
class-give-scripts.php
2 weeks ago
class-give-session.php
9 months ago
class-give-stats.php
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
4 years ago
class-notices.php
9 months ago
country-functions.php
7 months ago
currencies-list.php
7 months ago
currency-functions.php
3 years ago
error-tracking.php
6 years ago
filters.php
9 months ago
formatting.php
9 months ago
install.php
9 months ago
login-register.php
2 years ago
misc-functions.php
1 month ago
plugin-compatibility.php
6 years ago
post-types.php
1 year ago
price-functions.php
6 years ago
process-donation.php
1 year ago
setting-functions.php
6 years ago
shortcodes.php
1 year ago
template-functions.php
1 year ago
user-functions.php
3 years ago
currency-functions.php
475 lines
| 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 2.21.3 escape the currency symbol to prevent XSS attacks |
| 184 | * @since 1.0 |
| 185 | * |
| 186 | * @param string $currency The currency string. |
| 187 | * @param bool $decode_currency Option to HTML decode the currency symbol. |
| 188 | * |
| 189 | * @return string The symbol to use for the currency |
| 190 | */ |
| 191 | function give_currency_symbol( $currency = '', $decode_currency = false ) { |
| 192 | |
| 193 | if ( empty( $currency ) ) { |
| 194 | $currency = give_get_currency(); |
| 195 | } |
| 196 | |
| 197 | $currencies = give_currency_symbols( $decode_currency ); |
| 198 | $symbol = array_key_exists( $currency, $currencies ) ? $currencies[ $currency ] : $currency; |
| 199 | |
| 200 | /** |
| 201 | * Filter the currency symbol |
| 202 | * |
| 203 | * @since 1.0 |
| 204 | * |
| 205 | * @param string $symbol |
| 206 | * @param string $currency |
| 207 | */ |
| 208 | return apply_filters( 'give_currency_symbol', $symbol, $currency ); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /** |
| 213 | * Get currency name. |
| 214 | * |
| 215 | * @since 1.8.8 |
| 216 | * |
| 217 | * @param string $currency_code |
| 218 | * |
| 219 | * @return string |
| 220 | */ |
| 221 | function give_get_currency_name( $currency_code ) { |
| 222 | $currency_name = ''; |
| 223 | $currency_names = give_get_currencies(); |
| 224 | |
| 225 | if ( $currency_code && array_key_exists( $currency_code, $currency_names ) ) { |
| 226 | $currency_name = explode( '(', $currency_names[ $currency_code ] ); |
| 227 | $currency_name = trim( current( $currency_name ) ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Filter the currency name |
| 232 | * |
| 233 | * @since 1.8.8 |
| 234 | * |
| 235 | * @param string $currency_name |
| 236 | * @param string $currency_code |
| 237 | */ |
| 238 | return apply_filters( 'give_currency_name', $currency_name, $currency_code ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Formats the currency displayed. |
| 243 | * |
| 244 | * @since 1.0 |
| 245 | * |
| 246 | * @param string $price The donation amount. |
| 247 | * @param array $args It accepts 'currency_code', 'decode_currency' and 'form_id'. |
| 248 | * |
| 249 | * @return mixed|string |
| 250 | */ |
| 251 | function give_currency_filter( $price = '', $args = [] ) { |
| 252 | |
| 253 | // Get functions arguments. |
| 254 | $func_args = func_get_args(); |
| 255 | |
| 256 | // Backward compatibility: modify second param to array |
| 257 | if ( isset( $func_args[1] ) && is_string( $func_args[1] ) ) { |
| 258 | $args = [ |
| 259 | 'currency_code' => isset( $func_args[1] ) ? $func_args[1] : '', |
| 260 | 'decode_currency' => isset( $func_args[2] ) ? $func_args[2] : false, |
| 261 | 'form_id' => isset( $func_args[3] ) ? $func_args[3] : '', |
| 262 | ]; |
| 263 | |
| 264 | give_doing_it_wrong( __FUNCTION__, 'Pass second argument as Array.' ); |
| 265 | } |
| 266 | |
| 267 | // Set default values. |
| 268 | $args = wp_parse_args( |
| 269 | $args, |
| 270 | [ |
| 271 | 'currency_code' => '', |
| 272 | 'decode_currency' => false, |
| 273 | 'form_id' => '', |
| 274 | ] |
| 275 | ); |
| 276 | |
| 277 | if ( empty( $args['currency_code'] ) || ! array_key_exists( (string) $args['currency_code'], give_get_currencies() ) ) { |
| 278 | $args['currency_code'] = give_get_currency( $args['form_id'] ); |
| 279 | } |
| 280 | |
| 281 | $args['position'] = give_get_option( 'currency_position', 'before' ); |
| 282 | |
| 283 | /** |
| 284 | * @since 2.16.0 Check for a numeric value before comparing to zero. |
| 285 | * @link https://www.php.net/manual/en/migration80.incompatible.php |
| 286 | */ |
| 287 | $negative = is_numeric( $price ) && $price < 0; |
| 288 | |
| 289 | if ( $negative ) { |
| 290 | // Remove proceeding "-". |
| 291 | $price = substr( $price, 1 ); |
| 292 | } |
| 293 | |
| 294 | $args['symbol'] = give_currency_symbol( $args['currency_code'], $args['decode_currency'] ); |
| 295 | |
| 296 | switch ( $args['currency_code'] ) : |
| 297 | case 'GBP': |
| 298 | case 'BRL': |
| 299 | case 'EUR': |
| 300 | case 'USD': |
| 301 | case 'AUD': |
| 302 | case 'CAD': |
| 303 | case 'HKD': |
| 304 | case 'MXN': |
| 305 | case 'NZD': |
| 306 | case 'SGD': |
| 307 | case 'JPY': |
| 308 | case 'THB': |
| 309 | case 'INR': |
| 310 | case 'IDR': |
| 311 | case 'IRR': |
| 312 | case 'TRY': |
| 313 | case 'RUB': |
| 314 | case 'SEK': |
| 315 | case 'PLN': |
| 316 | case 'PHP': |
| 317 | case 'TWD': |
| 318 | case 'MYR': |
| 319 | case 'CZK': |
| 320 | case 'DKK': |
| 321 | case 'HUF': |
| 322 | case 'ILS': |
| 323 | case 'MAD': |
| 324 | case 'KRW': |
| 325 | case 'ZAR': |
| 326 | $formatted = ( 'before' === $args['position'] ? $args['symbol'] . $price : $price . $args['symbol'] ); |
| 327 | break; |
| 328 | case 'NOK': |
| 329 | $formatted = ( 'before' === $args['position'] ? $args['symbol'] . ' ' . $price : $price . ' ' . $args['symbol'] ); |
| 330 | break; |
| 331 | default: |
| 332 | $formatted = ( 'before' === $args['position'] ? $args['symbol'] . ' ' . $price : $price . ' ' . $args['symbol'] ); |
| 333 | break; |
| 334 | endswitch; |
| 335 | |
| 336 | /** |
| 337 | * Filter formatted amount |
| 338 | * |
| 339 | * @since 1.8.17 |
| 340 | */ |
| 341 | $formatted = apply_filters( 'give_currency_filter', $formatted, $args, $price ); |
| 342 | |
| 343 | /** |
| 344 | * Filter formatted amount with currency |
| 345 | * |
| 346 | * Filter name depends upon current value of currency and currency position. |
| 347 | * For example : |
| 348 | * if currency is USD and currency position is before then |
| 349 | * filter name will be give_usd_currency_filter_before |
| 350 | * |
| 351 | * and if currency is USD and currency position is after then |
| 352 | * filter name will be give_usd_currency_filter_after |
| 353 | */ |
| 354 | $formatted = apply_filters( |
| 355 | 'give_' . strtolower( $args['currency_code'] ) . "_currency_filter_{$args['position']}", |
| 356 | $formatted, |
| 357 | $args['currency_code'], |
| 358 | $price, |
| 359 | $args |
| 360 | ); |
| 361 | |
| 362 | if ( $negative ) { |
| 363 | // Prepend the minus sign before the currency sign. |
| 364 | $formatted = '-' . $formatted; |
| 365 | } |
| 366 | |
| 367 | return $formatted; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * This function is used to fetch list of zero based currencies. |
| 372 | * |
| 373 | * @since 2.3.0 |
| 374 | * |
| 375 | * @return array |
| 376 | */ |
| 377 | function give_get_zero_based_currencies() { |
| 378 | |
| 379 | $zero_based_currencies = [ |
| 380 | 'JPY', // Japanese Yen. |
| 381 | 'KRW', // South Korean Won. |
| 382 | 'CLP', // Chilean peso. |
| 383 | 'ISK', // Icelandic króna. |
| 384 | 'BIF', // Burundian franc. |
| 385 | 'DJF', // Djiboutian franc. |
| 386 | 'GNF', // Guinean franc. |
| 387 | 'KHR', // Cambodian riel. |
| 388 | 'KPW', // North Korean won. |
| 389 | 'LAK', // Lao kip. |
| 390 | 'LKR', // Sri Lankan rupee. |
| 391 | 'MGA', // Malagasy ariary. |
| 392 | 'MZN', // Mozambican metical. |
| 393 | 'VUV', // Vanuatu vatu. |
| 394 | ]; |
| 395 | |
| 396 | /** |
| 397 | * This filter hook can be used to update the list of zero based currencies. |
| 398 | * |
| 399 | * @since 2.3.0 |
| 400 | */ |
| 401 | return apply_filters( 'give_get_zero_based_currencies', $zero_based_currencies ); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Zero Decimal based Currency. |
| 406 | * |
| 407 | * @since 1.8.14 |
| 408 | * @since 2.2.0 Modified list. |
| 409 | * @see https://github.com/impress-org/give/issues/2191 |
| 410 | * |
| 411 | * @param string $currency Currency code |
| 412 | * |
| 413 | * @return bool |
| 414 | */ |
| 415 | function give_is_zero_based_currency( $currency = '' ) { |
| 416 | |
| 417 | $zero_based_currency = give_get_zero_based_currencies(); |
| 418 | |
| 419 | // Set default currency. |
| 420 | if ( empty( $currency ) ) { |
| 421 | $currency = give_get_currency(); |
| 422 | } |
| 423 | |
| 424 | // Check for Zero Based Currency. |
| 425 | if ( in_array( $currency, $zero_based_currency ) ) { |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** |
| 434 | * Check if currency support right to left direction or not. |
| 435 | * |
| 436 | * @param string $currency |
| 437 | * |
| 438 | * @return bool |
| 439 | */ |
| 440 | function give_is_right_to_left_supported_currency( $currency = '' ) { |
| 441 | $zero_based_currency = apply_filters( |
| 442 | 'give_right_to_left_supported_currency', |
| 443 | [ |
| 444 | 'IRR', |
| 445 | 'RIAL', |
| 446 | 'MAD', |
| 447 | 'AED', |
| 448 | 'BHD', |
| 449 | 'KWD', |
| 450 | 'OMR', |
| 451 | 'SAR', |
| 452 | 'TND', // https://en.wikipedia.org/wiki/Tunisian_dinar |
| 453 | 'QAR', // https://en.wikipedia.org/wiki/Qatari_riyal |
| 454 | 'LYD', // https://en.wikipedia.org/wiki/Libyan_dinar |
| 455 | 'LBP', // https://en.wikipedia.org/wiki/Lebanese_pound |
| 456 | 'IRT', // https://en.wikipedia.org/wiki/Iranian_toman |
| 457 | 'IQD', // https://en.wikipedia.org/wiki/Iraqi_dinar |
| 458 | 'DZD', // https://en.wikipedia.org/wiki/Algerian_dinar |
| 459 | 'AFN', // https://en.wikipedia.org/wiki/Afghan_afghani |
| 460 | ] |
| 461 | ); |
| 462 | |
| 463 | // Set default currency. |
| 464 | if ( empty( $currency ) ) { |
| 465 | $currency = give_get_currency(); |
| 466 | } |
| 467 | |
| 468 | // Check for Zero Based Currency. |
| 469 | if ( in_array( $currency, $zero_based_currency ) ) { |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | return false; |
| 474 | } |
| 475 |