diff --git a/public/js/gf-giftaid-field-frontend.js b/public/js/gf-giftaid-field-frontend.js index f215959..01f9801 100644 --- a/public/js/gf-giftaid-field-frontend.js +++ b/public/js/gf-giftaid-field-frontend.js @@ -12,16 +12,31 @@ function initGiftAid() { if (!totalSelector) { return; } - window.gform.addAction('gform_input_change', function (elem) { - const donationValue = parseFloat(elem.value); + window.gform.addAction( + 'gform_input_change', + function (elem) { + if ( + !(elem instanceof HTMLInputElement) || + !elem.closest(totalSelector) || + !elem.value + ) { + return; + } + const sanitizedValue = elem.value.replace(/[^0-9.]/g, ''); + const donationValue = parseFloat(sanitizedValue); const giftAidValue = donationValue * 1.25; + if (!donationValue || !giftAidValue) { + return; + } const donationRounded = donationValue.toFixed(2); const giftAidRounded = giftAidValue.toFixed(2); if (!donationRounded || !giftAidRounded) { return; } updateGiftAidDisplay(gravityForm, donationRounded, giftAidRounded); - }, 10); + }, + 10, + ); } /** diff --git a/src/GfGiftAidField.php b/src/GfGiftAidField.php index 86c47da..8937141 100644 --- a/src/GfGiftAidField.php +++ b/src/GfGiftAidField.php @@ -23,6 +23,7 @@ public static function run(): void GFAddOn::register(AddOn::class); add_action('gform_field_standard_settings', [static::class, 'addSelectedPriceFieldSetting'], 10, 2); + add_action('gform_editor_js', [static::class, 'addSelectedPriceFieldScript']); } /** @@ -33,10 +34,15 @@ public static function addSelectedPriceFieldSetting(int $position, int $form_id) if (25 !== $position || empty($form_id)) { return; } + $exists = GFAPI::form_id_exists($form_id); + if (empty($exists)) { + return; + } $field_options = static::getFormFields($form_id); if (empty($field_options)) { return; } + ?>