Skip to content
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

Fix Bugs 05/04/2024 #15

Merged
merged 11 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions public/js/gf-giftaid-field-frontend.js
codepuncher marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +15 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what theeeee

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what the formatter wanted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just leaves in the breeze,
drifting onwards, no control,
formatter decides

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eww

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,
);
}

/**
Expand Down
38 changes: 33 additions & 5 deletions src/GfGiftAidField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand All @@ -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;
}
Comment on lines +37 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$exists = GFAPI::form_id_exists($form_id);
if (empty($exists)) {
return;
}
if (! GFAPI::form_id_exists($form_id)) {
return;
}

stop doing empty checks on boolssssss!!!!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theres literally no harm in doing it - it just adds an extra layer of safety in case the function doesnt do what its docblock says

$field_options = static::getFormFields($form_id);
if (empty($field_options)) {
return;
}

?>
<li class="selected_price_field_setting field_setting">
<label for="selected_price_field_dropdown" class="section_label">
Expand All @@ -48,6 +54,8 @@ public static function addSelectedPriceFieldSetting(int $position, int $form_id)
id="selected_price_field_dropdown"
onchange="SetFieldProperty('selectedPriceField', this.value);"
>
<option value="">--Please select a field--</option>

Comment on lines +57 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<option value="">--Please select a field--</option>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

<?php foreach ($field_options as $field_id => $field_label) : ?>
<?php
if (empty($field_id) || empty($field_label)) {
Expand All @@ -62,17 +70,37 @@ public static function addSelectedPriceFieldSetting(int $position, int $form_id)
<?php
}

public static function addSelectedPriceFieldScript(): void
{
?>
<script type='text/javascript'>
//adding setting to fields of type "text"
fieldSettings.text += ', .selected_price_field_setting';
// Update the field settings when the field is loaded in the form editor
jQuery(document).on('gform_load_field_settings', function(event, field, form) {
if (!field || !field.type || 'gift_aid' !== field.type) {
return;
}
var savedValue = rgar(field, 'selectedPriceField');
if (!savedValue) {
return;
}
var selectedPriceDropdown = document.querySelector('#selected_price_field_dropdown');
if (!(selectedPriceDropdown instanceof HTMLSelectElement)) {
return;
}
selectedPriceDropdown.value = savedValue;
});
</script>
<?php
}

/**
* Get all the fields in a gravity form in the format:
* ['field class' => 'field_label', ...]
*/
public static function getFormFields(int $form_id): array
{
$exists = GFAPI::form_id_exists($form_id);
if (empty($exists)) {
return [];
}

$form = GFAPI::get_form($form_id);
$fields = $form['fields'] ?? [];
if (empty($fields) || !is_array($fields)) {
Expand Down
Loading