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 6 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
16 changes: 12 additions & 4 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 @@ -4,17 +4,25 @@ document.addEventListener('DOMContentLoaded', initGiftAid);
* Updates the gift aid display, based on a chosen field changing.
*/
function initGiftAid() {
const gravityForm = document.querySelector('.gform_wrapper');
const gravityForm = document.querySelector(".gform_wrapper");
if (!(gravityForm instanceof HTMLElement)) {
return;
}
const totalSelector = totalFieldSelector(gravityForm, '.ginput_amount');
const totalSelector = totalFieldSelector(gravityForm, ".ginput_amount");
codepuncher marked this conversation as resolved.
Show resolved Hide resolved
if (!totalSelector) {
return;
}
window.gform.addAction('gform_input_change', function (elem) {
const donationValue = parseFloat(elem.value);
window.gform.addAction("gform_input_change", function (elem) {
codepuncher marked this conversation as resolved.
Show resolved Hide resolved
if (!(elem instanceof HTMLInputElement) || !elem.closest(totalSelector) || !elem.value) {
return;
}
const regex = new RegExp('[^0-9.]', 'g');
const sanitizedValue = elem.value.replace(regex, '');
codepuncher marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
48 changes: 40 additions & 8 deletions src/GfGiftAidField.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,78 @@ 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;
}
$selected_value = static::getExistingFieldValue($form_id, 'selectedPriceField', 'gift_aid') ?? '';

?>
<li class="selected_price_field_setting field_setting">
<label for="selected_price_field_dropdown" class="section_label">
<?php esc_html_e('Price Field', 'itineris-gf-giftaid-field'); ?>
</label>

<select
name="donation_total"
id="selected_price_field_dropdown"
name="donation_total" 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)) {
continue;
}
?>

<option value="<?php echo esc_attr($field_id); ?>"><?php echo esc_html($field_label); ?></option>
<option
value="<?php echo esc_attr($field_id); ?>"
<?php echo $selected_value === $field_id ? 'selected' : ''; ?>
>
<?php echo esc_html($field_label); ?>
</option>
<?php endforeach; ?>
</select>
</li>
<?php
}

/**
* Get the value of a field in a specific form by type.
*/
public static function getExistingFieldValue(int $form_id, string $field_key, string $gf_field_type): mixed
{
$form = GFAPI::get_form($form_id);
if (empty($form) || !is_array($form)) {
return null;
}
$fields = GFAPI::get_fields_by_type($form, $gf_field_type);
if (empty($fields) || !is_array($fields)) {
return null;
}
$field = array_values($fields)[0] ?? null;
if (!($field instanceof GF_Field)) {
return null;
}
$chosen_field = $field[$field_key] ?? '';
if (empty($chosen_field)) {
return null;
}

return $chosen_field;
}

/**
* 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