class-evf-field-address.php
1 year ago
class-evf-field-ai.php
1 year ago
class-evf-field-captcha.php
3 weeks ago
class-evf-field-checkbox.php
2 months ago
class-evf-field-color.php
3 weeks ago
class-evf-field-country.php
2 months ago
class-evf-field-credit-card.php
3 weeks ago
class-evf-field-date-time.php
3 weeks ago
class-evf-field-divider.php
2 years ago
class-evf-field-email.php
1 year ago
class-evf-field-file-upload.php
1 year ago
class-evf-field-first-name.php
1 year ago
class-evf-field-hcaptcha.php
4 months ago
class-evf-field-hidden.php
1 year ago
class-evf-field-html.php
9 months ago
class-evf-field-image-upload.php
1 year ago
class-evf-field-last-name.php
1 year ago
class-evf-field-likert.php
1 year ago
class-evf-field-lookup.php
3 weeks ago
class-evf-field-number.php
1 year ago
class-evf-field-password.php
3 weeks ago
class-evf-field-payment-authorize-net.php
3 weeks ago
class-evf-field-payment-checkbox.php
3 weeks ago
class-evf-field-payment-coupon.php
3 weeks ago
class-evf-field-payment-gateway-selector.php
3 weeks ago
class-evf-field-payment-quantity.php
3 weeks ago
class-evf-field-payment-radio.php
3 weeks ago
class-evf-field-payment-single.php
3 weeks ago
class-evf-field-payment-square.php
1 year ago
class-evf-field-payment-subscription-plan.php
3 weeks ago
class-evf-field-payment-subtotal.php
3 weeks ago
class-evf-field-payment-total.php
3 weeks ago
class-evf-field-phone.php
1 year ago
class-evf-field-privacy-policy.php
2 months ago
class-evf-field-private-note.php
1 year ago
class-evf-field-progress.php
3 weeks ago
class-evf-field-radio.php
2 months ago
class-evf-field-range-slider.php
3 weeks ago
class-evf-field-rating.php
2 months ago
class-evf-field-recaptcha.php
4 months ago
class-evf-field-repeater.php
3 weeks ago
class-evf-field-reset.php
3 weeks ago
class-evf-field-scale-rating.php
1 year ago
class-evf-field-select.php
1 year ago
class-evf-field-signature.php
3 weeks ago
class-evf-field-text.php
1 year ago
class-evf-field-textarea.php
1 year ago
class-evf-field-title.php
2 years ago
class-evf-field-turnstile.php
4 months ago
class-evf-field-url.php
1 year ago
class-evf-field-wysiwyg.php
2 months ago
class-evf-field-yes-no.php
1 year ago
payment-amount-helpers.php
3 weeks ago
payment-amount-helpers.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Builder-scope payment amount helpers. |
| 4 | * |
| 5 | * Currency-aware fallbacks for evf_sanitize_amount() / evf_format_amount() so the |
| 6 | * free plugin can render LOCKED payment fields' previews IDENTICALLY to Everest |
| 7 | * Forms Pro (e.g. "$ 0.00"). The real helpers ship in Pro |
| 8 | * (includes/payments/functions.php, unguarded); these are only ever loaded from |
| 9 | * a free payment-field class file, which is itself only autoloaded when the Pro |
| 10 | * class is absent — so there is never a redeclaration conflict. |
| 11 | * |
| 12 | * Logic mirrors Pro's evf_format_amount()/evf_sanitize_amount() and reads the |
| 13 | * same `everest_forms_currency` option, so the rendered amount matches Pro. |
| 14 | * |
| 15 | * @package EverestForms\Fields |
| 16 | */ |
| 17 | |
| 18 | defined( 'ABSPATH' ) || exit; |
| 19 | |
| 20 | if ( ! function_exists( 'evf_get_currencies' ) ) { |
| 21 | /** |
| 22 | * Currency definitions (subset mirroring Everest Forms Pro values). Falls back |
| 23 | * to USD for any currency not listed. |
| 24 | * |
| 25 | * @return array |
| 26 | */ |
| 27 | function evf_get_currencies() { |
| 28 | return array( |
| 29 | 'USD' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 30 | 'GBP' => array( 'symbol' => '£', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 31 | 'EUR' => array( 'symbol' => '€', 'symbol_pos' => 'right', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 32 | 'AUD' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 33 | 'BRL' => array( 'symbol' => 'R$', 'symbol_pos' => 'left', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 34 | 'CAD' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 35 | 'CHF' => array( 'symbol' => 'CHF', 'symbol_pos' => 'left', 'thousands_separator' => "'", 'decimal_separator' => '.', 'decimals' => 2 ), |
| 36 | 'CZK' => array( 'symbol' => 'Kč', 'symbol_pos' => 'right', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 37 | 'DKK' => array( 'symbol' => 'kr.', 'symbol_pos' => 'left', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 38 | 'HKD' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 39 | 'HUF' => array( 'symbol' => 'Ft', 'symbol_pos' => 'right', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 40 | 'ILS' => array( 'symbol' => '₪', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 41 | 'INR' => array( 'symbol' => '₹', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 42 | 'JPY' => array( 'symbol' => '¥', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 0 ), |
| 43 | 'MXN' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 44 | 'MYR' => array( 'symbol' => 'RM', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 45 | 'NOK' => array( 'symbol' => 'kr', 'symbol_pos' => 'left', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 46 | 'NZD' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 47 | 'PHP' => array( 'symbol' => '₱', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 48 | 'PLN' => array( 'symbol' => 'zł', 'symbol_pos' => 'right', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 49 | 'SEK' => array( 'symbol' => 'kr', 'symbol_pos' => 'left', 'thousands_separator' => '.', 'decimal_separator' => ',', 'decimals' => 2 ), |
| 50 | 'SGD' => array( 'symbol' => '$', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 51 | 'THB' => array( 'symbol' => '฿', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 52 | 'ZAR' => array( 'symbol' => 'R', 'symbol_pos' => 'left', 'thousands_separator' => ',', 'decimal_separator' => '.', 'decimals' => 2 ), |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if ( ! function_exists( 'evf_sanitize_amount' ) ) { |
| 58 | /** |
| 59 | * Sanitize a price amount (builder-scope mirror of the Pro helper). |
| 60 | * |
| 61 | * @param string $amount Amount value. |
| 62 | * @param string $currency Currency code. |
| 63 | * @return string |
| 64 | */ |
| 65 | function evf_sanitize_amount( $amount, $currency = '' ) { |
| 66 | if ( empty( $currency ) ) { |
| 67 | $currency = get_option( 'everest_forms_currency', 'USD' ); |
| 68 | } |
| 69 | |
| 70 | $currency = strtoupper( $currency ); |
| 71 | $currencies = evf_get_currencies(); |
| 72 | $thousands_sep = isset( $currencies[ $currency ]['thousands_separator'] ) ? $currencies[ $currency ]['thousands_separator'] : ','; |
| 73 | $decimal_sep = isset( $currencies[ $currency ]['decimal_separator'] ) ? $currencies[ $currency ]['decimal_separator'] : '.'; |
| 74 | |
| 75 | if ( is_string( $amount ) ) { |
| 76 | if ( ',' === $decimal_sep && false !== strpos( $amount, $decimal_sep ) ) { |
| 77 | if ( ( '.' === $thousands_sep || ' ' === $thousands_sep ) && false !== strpos( $amount, $thousands_sep ) ) { |
| 78 | $amount = str_replace( $thousands_sep, '', $amount ); |
| 79 | } elseif ( empty( $thousands_sep ) && false !== strpos( $amount, '.' ) ) { |
| 80 | $amount = str_replace( '.', '', $amount ); |
| 81 | } |
| 82 | $amount = str_replace( $decimal_sep, '.', $amount ); |
| 83 | } elseif ( ',' === $thousands_sep && false !== strpos( $amount, $thousands_sep ) ) { |
| 84 | $amount = str_replace( $thousands_sep, '', $amount ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | $amount = preg_replace( '/[^0-9\.]/', '', (string) $amount ); |
| 89 | $decimals = apply_filters( 'evf_sanitize_amount_decimals', 2, $amount ); |
| 90 | |
| 91 | return number_format( (float) $amount, $decimals, '.', '' ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if ( ! function_exists( 'evf_format_amount' ) ) { |
| 96 | /** |
| 97 | * Format a price amount (builder-scope mirror of the Pro helper). |
| 98 | * |
| 99 | * @param string $amount Amount. |
| 100 | * @param boolean $symbol Whether to prepend/append the currency symbol. |
| 101 | * @param string $currency Currency code. |
| 102 | * @return string |
| 103 | */ |
| 104 | function evf_format_amount( $amount, $symbol = false, $currency = '' ) { |
| 105 | if ( empty( $currency ) ) { |
| 106 | $currency = get_option( 'everest_forms_currency', 'USD' ); |
| 107 | } |
| 108 | |
| 109 | $currency = strtoupper( $currency ); |
| 110 | $currencies = evf_get_currencies(); |
| 111 | $thousands_sep = isset( $currencies[ $currency ]['thousands_separator'] ) ? $currencies[ $currency ]['thousands_separator'] : ','; |
| 112 | $decimal_sep = isset( $currencies[ $currency ]['decimal_separator'] ) ? $currencies[ $currency ]['decimal_separator'] : '.'; |
| 113 | $sep_found = ! empty( $decimal_sep ) ? strpos( $amount, $decimal_sep ) : false; |
| 114 | |
| 115 | if ( ',' === $decimal_sep && false !== $sep_found ) { |
| 116 | $whole = substr( $amount, 0, $sep_found ); |
| 117 | $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) ); |
| 118 | $amount = $whole . '.' . $part; |
| 119 | } |
| 120 | |
| 121 | if ( ',' === $thousands_sep && false !== strpos( (string) $amount, $thousands_sep ) ) { |
| 122 | $amount = (float) floatval( str_replace( ',', '', $amount ) ); |
| 123 | } |
| 124 | |
| 125 | if ( empty( $amount ) ) { |
| 126 | $amount = 0; |
| 127 | } |
| 128 | |
| 129 | $decimals = apply_filters( 'evf_sanitize_amount_decimals', 2, $amount ); |
| 130 | $number = number_format( (float) $amount, $decimals, $decimal_sep, $thousands_sep ); |
| 131 | |
| 132 | if ( $symbol && isset( $currencies[ $currency ]['symbol'], $currencies[ $currency ]['symbol_pos'] ) ) { |
| 133 | $symbol_padding = apply_filters( 'evf_currency_symbol_padding', ' ' ); |
| 134 | if ( 'right' === $currencies[ $currency ]['symbol_pos'] ) { |
| 135 | $number .= $symbol_padding . $currencies[ $currency ]['symbol']; |
| 136 | } else { |
| 137 | $number = $currencies[ $currency ]['symbol'] . $symbol_padding . $number; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if ( 'EUR' === $currency ) { |
| 142 | $number = str_replace( '.', '', $number ); |
| 143 | } |
| 144 | |
| 145 | return $number; |
| 146 | } |
| 147 | } |
| 148 |