Skip to content

Commit

Permalink
Migrate to GFAddOn API
Browse files Browse the repository at this point in the history
  • Loading branch information
codepuncher committed Jul 25, 2023
1 parent f3838a8 commit a6972be
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 80 deletions.
6 changes: 1 addition & 5 deletions gf-giftaid-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@
require_once __DIR__ . '/vendor/autoload.php';
}

if (! defined('ITINERIS_GF_GIFT_AID_FIELD_URI')) {
define('ITINERIS_GF_GIFT_AID_FIELD_URI', plugins_url(basename(__DIR__)));
}

add_action('gform_loaded', [GfGiftAidField::class, 'load'], 5);
GfGiftAidField::run();
21 changes: 20 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<ruleset name="Plugin" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<ruleset name="Plugin" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<!-- Check only our site MU plugin -->
<file>src</file>

Expand All @@ -24,6 +25,24 @@
</rule>

<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<exclude-pattern>src/AddOn.php</exclude-pattern>
</rule>

<rule ref="PSR2.Classes.PropertyDeclaration.Underscore">
<exclude-pattern>src/AddOn.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
<exclude-pattern>src/AddOn.php</exclude-pattern>
<exclude-pattern>src/GiftAidField.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint">
<exclude-pattern>src/AddOn.php</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation">
<exclude-pattern>src/AddOn.php</exclude-pattern>
<exclude-pattern>src/GiftAidField.php</exclude-pattern>
</rule>
</ruleset>
5 changes: 5 additions & 0 deletions public/js/gf-giftaid-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function SetDefaultValues_gift_aid(field) {
field.label = 'GiftAid';
field.checkboxLabel = 'Yes, please claim GiftAid';
field.descriptionPlaceholder = 'Enter additional description text here.';
}
51 changes: 51 additions & 0 deletions public/js/gf-giftaid-frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function gfGiftAidOnInputChange(elem) {
if (
!(elem instanceof HTMLElement) ||
!elem.classList.contains('ginput_amount')
) {
return;
}

const parentForm = elem.closest('form');
if (!(parentForm instanceof HTMLFormElement)) {
return;
}

const spanTotal = parentForm.querySelectorAll('.gform_donation_total');
if (!(spanTotal instanceof NodeList) || 0 === spanTotal.length) {
return;
}

const spanTotalGift = parentForm.querySelectorAll(
'.gform_donation_gifttotal',
);
if (!(spanTotalGift instanceof NodeList) || 0 === spanTotalGift.length) {
return;
}

const extractFloat = (str) => {
// Regular expression to match floating-point numbers
const regex = /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/g;

// Use match() to find all matching numbers in the string
const matches = str.match(regex);

// If there are matches, convert them to floats and return them; otherwise, return the original str.
return matches ? matches.map(Number) : str;
};

const val = extractFloat(elem.value);
const total = parseFloat(val).toFixed(2);
const totalGift = parseFloat(val * 1.25).toFixed(2);

Array.from(spanTotal).forEach((item) => {
item.innerHTML = total;
});

Array.from(spanTotalGift).forEach((item) => {
item.innerHTML = totalGift;
});
}
document.addEventListener('DOMContentLoaded', () => {
window.gform.addAction('gform_input_change', gfGiftAidOnInputChange, 10);
});
45 changes: 0 additions & 45 deletions public/js/gift-aid.js

This file was deleted.

69 changes: 69 additions & 0 deletions src/AddOn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Itineris\GfGiftaidField;

use GF_Fields;
use GFAddOn;

class AddOn extends GFAddOn
{
private static ?self $_instance = null;

protected $_version = GfGiftAidField::VERSION;
protected $_min_gravityforms_version = '2.7';
protected $_slug = 'gf-giftaid-field';
protected $_path = 'gf-giftaid-field/gf-giftaid-field.php';
protected $_full_path = __DIR__;
protected $_title = 'GF GiftAid Field';
protected $_short_title = 'GF GiftAid Field';
protected $_url = 'https://github.com/ItinerisLtd/gf-giftaid-field';

public static function get_instance(): self
{
if (! self::$_instance instanceof self) {
self::$_instance = new self();
}

return self::$_instance;
}

public function pre_init(): void
{
parent::pre_init();

if ($this->is_gravityforms_supported() && class_exists('GF_Field')) {
GF_Fields::register(new GiftAidField());
}
}

public function scripts(): array
{
return [
...parent::scripts(),
[
'handle' => "{$this->_slug}-frontend",
'src' => $this->get_base_url() . '/public/js/gf-giftaid-frontend.js',
'version' => $this->_version,
'deps' => [],
'in_footer' => true,
'enqueue' => [
'callback' => fn (): bool => ! is_admin(),
],
],
[
'handle' => "{$this->_slug}-admin",
'src' => $this->get_base_url() . '/public/js/gf-giftaid-admin.js',
'version' => $this->_version,
'deps' => [],
'in_footer' => true,
'enqueue' => [
[
'admin_page' => ['form_editor'],
],
],
],
];
}
}
32 changes: 9 additions & 23 deletions src/GfGiftAidField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,20 @@

namespace Itineris\GfGiftaidField;

use GF_Fields;
use Itineris\GfGiftaidField\GiftAidField;
use GFAddOn;
use GFForms;

class GfGiftAidField
{
public static function load(): void
{
static::hooks();

GF_Fields::register(new GiftAidField());
}
public const VERSION = '0.1.0';

public static function hooks(): void
public static function run(): void
{
add_action('wp_enqueue_scripts', [static::class, 'enqueueGiftAidScripts']);
add_action('gform_enqueue_scripts', function (): void {
wp_enqueue_script('gf-gift-aid-field');
});
}
if (! method_exists('GFForms', 'include_addon_framework')) {
return;
}

public static function enqueueGiftAidScripts(): void
{
wp_register_script(
'gf-gift-aid-field',
ITINERIS_GF_GIFT_AID_FIELD_URI . '/public/js/gift-aid.js',
[],
null,
true,
);
GFForms::include_addon_framework();
GFAddOn::register(AddOn::class);
}
}
69 changes: 63 additions & 6 deletions src/GiftAidField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Itineris\GfGiftaidField;

use GF_Field;
use GFCommon;
use GFFormsModel;

class GiftAidField extends GF_Field
{
Expand All @@ -15,6 +17,11 @@ public function get_form_editor_field_title(): string
return esc_attr__('Gift aid field', 'itineris-gf-giftaid-field');
}

public function get_form_editor_field_description(): string
{
return esc_attr__('Offers a “yes/no” checkbox to allow a user to give permission for you to claim GiftAid on their behalf.', 'itineris-gf-giftaid-field');
}

public function get_form_editor_button(): array
{
return [
Expand All @@ -36,21 +43,39 @@ public function get_form_editor_field_settings(): array
];
}

public function is_value_submission_array(): bool
public function is_conditional_logic_supported(): bool
{
return true;
}

public function get_form_editor_field_icon(): string
{
return 'gform-icon--consent';
}

/**
* @param array $form
*/
public function get_field_container_tag($form): string
{
if (GFCommon::is_legacy_markup_enabled($form)) {
return parent::get_field_container_tag($form);
}

return 'fieldset';
}

/**
* @param array $form
* @param string|array $value
* @param null|array $entry
* @param ?array $entry
*
* @return string
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
public function get_field_input($form, $value = '', $entry = null): string
{
$id = (int) $this->id;
$giftaidImage = ITINERIS_GF_GIFT_AID_FIELD_URI . '/public/img/giftaid.svg';
$giftaidImage = plugins_url('public/img/giftaid.svg', __DIR__);

ob_start();
?>
Expand All @@ -59,7 +84,7 @@ public function get_field_input($form, $value = '', $entry = null): string
<img src="<?php echo esc_url($giftaidImage); ?>" alt="GiftAid logo">
</div>
<div class="description text-primary font-medium text-xl mb-2">
<?php //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo wpautop(wp_kses_post($this->get_calculated_gift())); ?>
</div>
<div class="gift-box-form-wrapper my-6 pb-6 border-b border-b-gray-200">
Expand All @@ -83,6 +108,7 @@ class="gfield-choice-input"
<div class="details-description"><?php echo wp_kses_post($this->description); ?></div>
</div>
<?php

return ob_get_clean();
}

Expand All @@ -91,7 +117,6 @@ class="gfield-choice-input"
* @param bool $force_frontend_label
* @param array $form
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
public function get_field_content($value, $force_frontend_label, $form): string
{
$form_id = $form['id'];
Expand Down Expand Up @@ -141,4 +166,36 @@ public function sanitize_settings(): void
parent::sanitize_settings();
$this->checkboxLabel = $this->maybe_wp_kses($this->checkboxLabel);
}

public function get_filter_settings(): array
{
$filter_settings = [
'key' => $this->id,
'text' => GFFormsModel::get_label($this),
'preventMultiple' => false,
'operators' => $this->get_filter_operators(),
];

$values = $this->get_filter_values();
if (empty($values)) {
$filter_settings['values'] = $values;
}

return $filter_settings;
}

public function get_filter_operators(): array
{
return ['is', 'isnot'];
}

public function get_filter_values(): array
{
return [
[
'value' => '1',
'text' => esc_html__('Checked', 'itineris-gf-giftaid-field'),
],
];
}
}

0 comments on commit a6972be

Please sign in to comment.