-
Notifications
You must be signed in to change notification settings - Fork 41
Rock: Enable pricing fields in Lite #3006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0c1e7f4
5fa81a0
7c09d19
89e2dae
5d94d5b
6268636
948977c
8321e20
ae5e9af
eb6ec49
c48d9fd
a508db4
6087e17
8cb8b61
b21d80d
368664a
73b3fe9
631c20e
982b757
e27dea8
c95802c
452a805
a853a77
a717cd4
acf03ec
4934d8a
e6540cc
658045b
631fb35
cf291a5
03bc614
241687c
f5a7b52
fe4c7fe
8d06983
7d237e9
aa15197
d04fd5a
6bd0d9d
e04cd6b
ab5f74f
5de5b51
36fd1ff
a993291
9d8cb8d
4eb7901
2b94c5c
7f5eefc
e533757
0ebc53a
e51fc6f
705aa9b
4707227
4d3e776
b158434
5bd5159
107cd7e
b5c2fed
bc25ec6
30f78ae
a237162
f24ba65
ad6ee31
35ee4e4
a49832e
674d213
4a1ea7d
2e1cc60
a5bf62b
42dbc70
46ee323
88d218c
3b32e4c
bf3bd75
78f55bb
da79ad0
36dccdb
2d98651
f0dd805
8678416
7a41fb5
f9925c0
8988d07
c7ba9aa
85a1263
071184c
99c5361
756c32c
cedd7e5
ced42b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,11 +10,23 @@ | |
| class FrmCurrencyHelper { | ||
|
|
||
| /** | ||
| * @param string $currency | ||
| * Gets the currency data from the currency code. | ||
| * | ||
| * @since x.x The first parameter is optional. | ||
| * | ||
| * @param string|null $currency Currency code. Default is `null`, which use the currency in the global settings. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function get_currency( $currency ) { | ||
| public static function get_currency( $currency = null ) { | ||
| if ( ! $currency ) { | ||
| $currency = trim( FrmAppHelper::get_settings()->currency ); | ||
| } | ||
|
|
||
| if ( ! $currency ) { | ||
| $currency = 'USD'; | ||
| } | ||
|
|
||
| $currency = strtoupper( $currency ); | ||
| $currencies = self::get_currencies(); | ||
|
|
||
|
|
@@ -42,6 +54,107 @@ public static function is_currency_format( $format_value ) { | |
| return in_array( $format_value, array( 'currency', 'number' ), true ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds the currency symbol to the given amount. | ||
| * | ||
| * @param float|string $amount | ||
| * @param array|null $currency | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function format_price( $amount, $currency = null ) { | ||
| if ( ! $currency ) { | ||
| $currency = self::get_currency(); | ||
| } | ||
|
|
||
| if ( is_string( $amount ) ) { | ||
| $amount = floatval( self::prepare_price( $amount, $currency ) ); | ||
| } | ||
|
|
||
| $amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] ); | ||
|
|
||
| if ( '' !== $currency['symbol_left'] ) { | ||
| $amount = $currency['symbol_left'] . $currency['symbol_padding'] . $amount; | ||
| } | ||
|
|
||
| if ( '' !== $currency['symbol_right'] ) { | ||
| $amount .= $currency['symbol_padding'] . $currency['symbol_right']; | ||
| } | ||
|
|
||
| return $amount; | ||
| } | ||
|
|
||
| /** | ||
| * @since x.x | ||
| * | ||
| * @param string $price | ||
| * @param array $currency | ||
| * | ||
| * @return float|string | ||
| */ | ||
| public static function prepare_price( $price, $currency ) { | ||
| $price = trim( $price ); | ||
|
|
||
| if ( ! $price ) { | ||
| return 0; | ||
| } | ||
|
|
||
| preg_match_all( '/[\-]*[0-9,.]*\.?\,?[0-9]+/', $price, $matches ); | ||
| $price = $matches ? end( $matches[0] ) : 0; | ||
|
|
||
| if ( ! $price ) { | ||
| return 0; | ||
| } | ||
|
Comment on lines
+102
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix price parsing for currencies that use spaces or apostrophes as thousand separators. Line 89 only matches digits, commas, and dots, so inputs like 💡 One way to keep the configured separators intact- preg_match_all( '/[\-]*[0-9,.]*\.?\,?[0-9]+/', $price, $matches );
+ $allowed_separators = preg_quote( $currency['thousand_separator'] . $currency['decimal_separator'], '/' );
+ preg_match_all( '/-?[0-9' . $allowed_separators . ']+/u', $price, $matches );
$price = $matches ? end( $matches[0] ) : 0;🤖 Prompt for AI Agents |
||
|
|
||
| $price = self::maybe_use_decimal( $price, $currency ); | ||
| return str_replace( $currency['decimal_separator'], '.', str_replace( $currency['thousand_separator'], '', $price ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @since x.x | ||
| * | ||
| * @param string $amount | ||
| * @param array $currency | ||
| * | ||
| * @return string | ||
| */ | ||
| private static function maybe_use_decimal( $amount, $currency ) { | ||
| if ( $currency['thousand_separator'] !== '.' ) { | ||
| return $amount; | ||
| } | ||
|
|
||
| $amount_parts = explode( '.', $amount ); | ||
| $used_for_decimal = count( $amount_parts ) === 2 && in_array( strlen( $amount_parts[1] ), array( 1, 2 ), true ); | ||
|
|
||
| if ( $used_for_decimal ) { | ||
| return str_replace( '.', $currency['decimal_separator'], $amount ); | ||
| } | ||
|
|
||
| return $amount; | ||
| } | ||
|
|
||
| /** | ||
| * If the currency is needed for this form, add it to the global. | ||
| * This is later included in the footer. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param int|string $form_id Form ID. This is used for Pro compatibility. | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function add_currency_to_global( $form_id ) { | ||
| global $frm_vars; | ||
|
|
||
| if ( ! isset( $frm_vars['currency'] ) ) { | ||
| $frm_vars['currency'] = array(); | ||
| } | ||
|
|
||
| if ( ! isset( $frm_vars['currency'][ $form_id ] ) ) { | ||
| $frm_vars['currency'][ $form_id ] = self::get_currency(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get a list of all supported currencies. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.