| @@ -9,25 +9,154 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | class FrmCurrencyHelper { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * @param string $currency | |
| 13 | + * Gets the currency data from the currency code. | |
| 14 | + * | |
| 15 | + * @since 6.30 The first parameter is optional. | |
| 16 | + * | |
| 17 | + * @param string|null $currency Currency code. Default is `null`, which use the currency in the global settings. | |
| 18 | + * | |
| 14 | 19 | * @return array |
| 15 | 20 | */ |
| 16 | - public static function get_currency( $currency ) { | |
| 21 | + public static function get_currency( $currency = null ) { | |
| 22 | + if ( ! $currency ) { | |
| 23 | + $currency = trim( FrmAppHelper::get_settings()->currency ); | |
| 24 | + } | |
| 25 | + | |
| 26 | + if ( ! $currency ) { | |
| 27 | + $currency = 'USD'; | |
| 28 | + } | |
| 29 | + | |
| 17 | 30 | $currency = strtoupper( $currency ); |
| 18 | 31 | $currencies = self::get_currencies(); |
| 32 | + | |
| 19 | 33 | if ( isset( $currencies[ $currency ] ) ) { |
| 20 | 34 | $currency = $currencies[ $currency ]; |
| 21 | 35 | } elseif ( isset( $currencies[ strtolower( $currency ) ] ) ) { |
| 22 | 36 | $currency = $currencies[ strtolower( $currency ) ]; |
| 23 | 37 | } else { |
| 24 | - $currency = $currencies['usd']; | |
| 38 | + $currency = $currencies['USD']; | |
| 25 | 39 | } |
| 40 | + | |
| 26 | 41 | return $currency; |
| 27 | 42 | } |
| 28 | 43 | |
| 29 | 44 | /** |
| 45 | + * Checks if the given format is a valid currency format. | |
| 46 | + * | |
| 47 | + * @since 6.18 | |
| 48 | + * | |
| 49 | + * @param string $format_value The format value to check. | |
| 50 | + * | |
| 51 | + * @return bool | |
| 52 | + */ | |
| 53 | + public static function is_currency_format( $format_value ) { | |
| 54 | + return in_array( $format_value, array( 'currency', 'number' ), true ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Adds the currency symbol to the given amount. | |
| 59 | + * | |
| 60 | + * @param float|string $amount | |
| 61 | + * @param array|null $currency | |
| 62 | + * | |
| 63 | + * @return string | |
| 64 | + */ | |
| 65 | + public static function format_price( $amount, $currency = null ) { | |
| 66 | + if ( ! $currency ) { | |
| 67 | + $currency = self::get_currency(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + if ( is_string( $amount ) ) { | |
| 71 | + $amount = floatval( self::prepare_price( $amount, $currency ) ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + $amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] ); | |
| 75 | + | |
| 76 | + if ( '' !== $currency['symbol_left'] ) { | |
| 77 | + $amount = $currency['symbol_left'] . $currency['symbol_padding'] . $amount; | |
| 78 | + } | |
| 79 | + | |
| 80 | + if ( '' !== $currency['symbol_right'] ) { | |
| 81 | + $amount .= $currency['symbol_padding'] . $currency['symbol_right']; | |
| 82 | + } | |
| 83 | + | |
| 84 | + return $amount; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * @since 6.30 | |
| 89 | + * | |
| 90 | + * @param string $price | |
| 91 | + * @param array $currency | |
| 92 | + * | |
| 93 | + * @return float|string | |
| 94 | + */ | |
| 95 | + public static function prepare_price( $price, $currency ) { | |
| 96 | + $price = trim( $price ); | |
| 97 | + | |
| 98 | + if ( ! $price ) { | |
| 99 | + return 0; | |
| 100 | + } | |
| 101 | + | |
| 102 | + preg_match_all( '/[\-]*[0-9,.]*\.?\,?[0-9]+/', $price, $matches ); | |
| 103 | + $price = $matches ? end( $matches[0] ) : 0; | |
| 104 | + | |
| 105 | + if ( ! $price ) { | |
| 106 | + return 0; | |
| 107 | + } | |
| 108 | + | |
| 109 | + $price = self::maybe_use_decimal( $price, $currency ); | |
| 110 | + return str_replace( $currency['decimal_separator'], '.', str_replace( $currency['thousand_separator'], '', $price ) ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * @since 6.30 | |
| 115 | + * | |
| 116 | + * @param string $amount | |
| 117 | + * @param array $currency | |
| 118 | + * | |
| 119 | + * @return string | |
| 120 | + */ | |
| 121 | + private static function maybe_use_decimal( $amount, $currency ) { | |
| 122 | + if ( $currency['thousand_separator'] !== '.' ) { | |
| 123 | + return $amount; | |
| 124 | + } | |
| 125 | + | |
| 126 | + $amount_parts = explode( '.', $amount ); | |
| 127 | + $used_for_decimal = count( $amount_parts ) === 2 && in_array( strlen( $amount_parts[1] ), array( 1, 2 ), true ); | |
| 128 | + | |
| 129 | + if ( $used_for_decimal ) { | |
| 130 | + return str_replace( '.', $currency['decimal_separator'], $amount ); | |
| 131 | + } | |
| 132 | + | |
| 133 | + return $amount; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * If the currency is needed for this form, add it to the global. | |
| 138 | + * This is later included in the footer. | |
| 139 | + * | |
| 140 | + * @since 6.30 | |
| 141 | + * | |
| 142 | + * @param int|string $form_id Form ID. This is used for Pro compatibility. | |
| 143 | + * | |
| 144 | + * @return void | |
| 145 | + */ | |
| 146 | + public static function add_currency_to_global( $form_id ) { | |
| 147 | + global $frm_vars; | |
| 148 | + | |
| 149 | + if ( ! isset( $frm_vars['currency'] ) ) { | |
| 150 | + $frm_vars['currency'] = array(); | |
| 151 | + } | |
| 152 | + | |
| 153 | + if ( ! isset( $frm_vars['currency'][ $form_id ] ) ) { | |
| 154 | + $frm_vars['currency'][ $form_id ] = self::get_currency(); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 30 | 159 | * Get a list of all supported currencies. |
| 31 | 160 | * |
| 32 | 161 | * @since 6.5. |
| 33 | 162 | * |
| @@ -44,9 +173,9 @@ | ||
| 44 | 173 | 'decimal_separator' => '.', |
| 45 | 174 | 'decimals' => 2, |
| 46 | 175 | ), |
| 47 | 176 | 'BDT' => array( |
| 48 | - 'name' => __( 'Bangladeshi Taka', 'formidable' ), | |
| 177 | + 'name' => __( 'Bangladeshi Taka', 'formidable' ), | |
| 49 | 178 | 'symbol_left' => '৳', |
| 50 | 179 | 'symbol_right' => '', |
| 51 | 180 | 'symbol_padding' => ' ', |
| 52 | 181 | 'thousand_separator' => ',', |
| @@ -53,9 +182,9 @@ | ||
| 53 | 182 | 'decimal_separator' => '.', |
| 54 | 183 | 'decimals' => 2, |
| 55 | 184 | ), |
| 56 | 185 | 'BRL' => array( |
| 57 | - 'name' => __( 'Brazilian Real', 'formidable' ), | |
| 186 | + 'name' => __( 'Brazilian Real', 'formidable' ), | |
| 58 | 187 | 'symbol_left' => 'R$', |
| 59 | 188 | 'symbol_right' => '', |
| 60 | 189 | 'symbol_padding' => ' ', |
| 61 | 190 | 'thousand_separator' => '.', |
| @@ -170,9 +299,9 @@ | ||
| 170 | 299 | 'decimal_separator' => '.', |
| 171 | 300 | 'decimals' => 2, |
| 172 | 301 | ), |
| 173 | 302 | 'NOK' => array( |
| 174 | - 'name' => __( 'Norwegian Krone', 'formidable' ), | |
| 303 | + 'name' => __( 'Norwegian Krone', 'formidable' ), | |
| 175 | 304 | 'symbol_left' => 'Kr', |
| 176 | 305 | 'symbol_right' => '', |
| 177 | 306 | 'symbol_padding' => ' ', |
| 178 | 307 | 'thousand_separator' => '.', |
| @@ -251,24 +380,24 @@ | ||
| 251 | 380 | 'decimal_separator' => '.', |
| 252 | 381 | 'decimals' => 2, |
| 253 | 382 | ), |
| 254 | 383 | 'SEK' => array( |
| 255 | - 'name' => __( 'Swedish Krona', 'formidable' ), | |
| 256 | - 'symbol_left' => '', | |
| 257 | - 'symbol_right' => 'Kr', | |
| 258 | - 'symbol_padding' => ' ', | |
| 384 | + 'name' => __( 'Swedish Krona', 'formidable' ), | |
| 385 | + 'symbol_left' => '', | |
| 386 | + 'symbol_right' => 'Kr', | |
| 387 | + 'symbol_padding' => ' ', | |
| 259 | 388 | 'thousand_separator' => ' ', |
| 260 | - 'decimal_separator' => ',', | |
| 261 | - 'decimals' => 2, | |
| 389 | + 'decimal_separator' => ',', | |
| 390 | + 'decimals' => 2, | |
| 262 | 391 | ), |
| 263 | 392 | 'CHF' => array( |
| 264 | - 'name' => __( 'Swiss Franc', 'formidable' ), | |
| 265 | - 'symbol_left' => 'Fr.', | |
| 266 | - 'symbol_right' => '', | |
| 267 | - 'symbol_padding' => ' ', | |
| 393 | + 'name' => __( 'Swiss Franc', 'formidable' ), | |
| 394 | + 'symbol_left' => 'Fr.', | |
| 395 | + 'symbol_right' => '', | |
| 396 | + 'symbol_padding' => ' ', | |
| 268 | 397 | 'thousand_separator' => "'", |
| 269 | - 'decimal_separator' => '.', | |
| 270 | - 'decimals' => 2, | |
| 398 | + 'decimal_separator' => '.', | |
| 399 | + 'decimals' => 2, | |
| 271 | 400 | ), |
| 272 | 401 | 'TWD' => array( |
| 273 | 402 | 'name' => __( 'Taiwan New Dollar', 'formidable' ), |
| 274 | 403 | 'symbol_left' => '$', |
| @@ -278,20 +407,20 @@ | ||
| 278 | 407 | 'decimal_separator' => '.', |
| 279 | 408 | 'decimals' => 2, |
| 280 | 409 | ), |
| 281 | 410 | 'THB' => array( |
| 282 | - 'name' => __( 'Thai Baht', 'formidable' ), | |
| 283 | - 'symbol_left' => '฿', | |
| 284 | - 'symbol_right' => '', | |
| 285 | - 'symbol_padding' => ' ', | |
| 411 | + 'name' => __( 'Thai Baht', 'formidable' ), | |
| 412 | + 'symbol_left' => '฿', | |
| 413 | + 'symbol_right' => '', | |
| 414 | + 'symbol_padding' => ' ', | |
| 286 | 415 | 'thousand_separator' => ',', |
| 287 | - 'decimal_separator' => '.', | |
| 288 | - 'decimals' => 2, | |
| 416 | + 'decimal_separator' => '.', | |
| 417 | + 'decimals' => 2, | |
| 289 | 418 | ), |
| 290 | 419 | 'TRY' => array( |
| 291 | 420 | 'name' => __( 'Turkish Liras', 'formidable' ), |
| 292 | 421 | 'symbol_left' => '', |
| 293 | - 'symbol_right' => '€', | |
| 422 | + 'symbol_right' => '₺', | |
| 294 | 423 | 'symbol_padding' => ' ', |
| 295 | 424 | 'thousand_separator' => '.', |
| 296 | 425 | 'decimal_separator' => ',', |
| 297 | 426 | 'decimals' => 2, |
| @@ -325,9 +454,9 @@ | ||
| 325 | 454 | |
| 326 | 455 | if ( is_array( $filtered_currencies ) ) { |
| 327 | 456 | $currencies = $filtered_currencies; |
| 328 | 457 | } else { |
| 329 | - _doing_it_wrong( __FUNCTION__, 'Only arrays should be returned when using the frm_currencies filter.', 'x.x' ); | |
| 458 | + _doing_it_wrong( __METHOD__, 'Only arrays should be returned when using the frm_currencies filter.', '6.5' ); | |
| 330 | 459 | } |
| 331 | 460 | |
| 332 | 461 | return $currencies; |
| 333 | 462 | } |