| @@ -9,14 +9,28 @@ | ||
| 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 ) ]; |
| @@ -22,8 +36,9 @@ | ||
| 22 | 36 | $currency = $currencies[ strtolower( $currency ) ]; |
| 23 | 37 | } else { |
| 24 | 38 | $currency = $currencies['USD']; |
| 25 | 39 | } |
| 40 | + | |
| 26 | 41 | return $currency; |
| 27 | 42 | } |
| 28 | 43 | |
| 29 | 44 | /** |
| @@ -31,12 +46,114 @@ | ||
| 31 | 46 | * |
| 32 | 47 | * @since 6.18 |
| 33 | 48 | * |
| 34 | 49 | * @param string $format_value The format value to check. |
| 50 | + * | |
| 35 | 51 | * @return bool |
| 36 | 52 | */ |
| 37 | 53 | public static function is_currency_format( $format_value ) { |
| 38 | 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 | + } | |
| 39 | 156 | } |
| 40 | 157 | |
| 41 | 158 | /** |
| 42 | 159 | * Get a list of all supported currencies. |