This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-captcha
|
||||
* @author cleverogre
|
||||
* @copyright 2026 CleverOgre
|
||||
* @license GLP-3.0-or-later
|
||||
* @version 1.2.8
|
||||
* @since 1.1.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Consent\Integrations;
|
||||
|
||||
use Ogre\Singleton;
|
||||
use Ogre\Consent as Plugin;
|
||||
use Ogre\Captcha\Settings;
|
||||
use GFForms;
|
||||
use GFAddOn;
|
||||
use GFFormDisplay;
|
||||
use GFCommon;
|
||||
use GP_Nested_Forms;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
GFForms::include_addon_framework();
|
||||
|
||||
final class GravityForms extends GFAddOn {
|
||||
use Singleton;
|
||||
|
||||
protected $_min_gravityforms_version = '2.6.3.2';
|
||||
|
||||
public static function get_instance(): GFAddOn {
|
||||
return self::instance();
|
||||
}
|
||||
|
||||
function __construct() {
|
||||
$this->_version = Plugin::get_version();
|
||||
$this->_slug = Plugin::get_id();
|
||||
$this->_path = sprintf(
|
||||
'%1$s/%1$s.php',
|
||||
Plugin::get_id()
|
||||
);
|
||||
$this->_full_path = Plugin::get_file();
|
||||
$this->_title = sprintf(
|
||||
Plugin::__('%s Add-On'),
|
||||
Plugin::get_title()
|
||||
);
|
||||
$this->_short_title = Plugin::get_title();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function init() {
|
||||
add_filter('gform_submit_button', [$this, 'submit_button'], 10, 2);
|
||||
add_filter('gform_validation', [$this, 'validation'], 10, 2);
|
||||
|
||||
parent::init();
|
||||
}
|
||||
|
||||
public function get_menu_icon() {
|
||||
return 'gform-icon--lock';
|
||||
}
|
||||
|
||||
public function plugin_settings_fields() {
|
||||
return [
|
||||
[
|
||||
'title' => esc_html(sprintf(
|
||||
Plugin::__('%s Settings'),
|
||||
Plugin::get_title()
|
||||
)),
|
||||
'description' => sprintf(
|
||||
Plugin::__('Looking to configure the rest of Ogre Consent? You can find the settings on <a href="%s">this page</a>.'),
|
||||
esc_url(Settings::get_url())
|
||||
),
|
||||
'fields' => [
|
||||
[
|
||||
'label' => sprintf(
|
||||
Plugin::esc_html__('Enable %s'),
|
||||
Plugin::get_title()
|
||||
),
|
||||
'type' => 'checkbox',
|
||||
'name' => 'enabled',
|
||||
'choices' => [
|
||||
[
|
||||
'label' => Plugin::esc_html__('Enable consent on all forms by default.'),
|
||||
'name' => 'enabled',
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function form_settings_fields($form) {
|
||||
return [
|
||||
[
|
||||
'title' => esc_html(sprintf(
|
||||
Plugin::__('%s Settings'),
|
||||
Plugin::get_title()
|
||||
)),
|
||||
'fields' => [
|
||||
[
|
||||
'type' => 'checkbox',
|
||||
'name' => 'disabled',
|
||||
'choices' => [
|
||||
[
|
||||
'label' => Plugin::esc_html__('Disable consent for this form.'),
|
||||
'name' => 'disabled',
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function is_enabled(array|null $form = null): bool {
|
||||
// Check to make sure that we're enabled on the plugin level first
|
||||
$plugin_enabled = $this->get_plugin_setting('enabled') === '1';
|
||||
if (is_null($form) || !$plugin_enabled) return $plugin_enabled;
|
||||
|
||||
$form_id = !is_null($form) ? absint(rgar($form, 'id')) : 0;
|
||||
|
||||
// If Gravity Perks Nested Forms is active and the current form is nested, disable entirely (it's not compatible at the moment)
|
||||
if (class_exists('GP_Nested_Forms')) {
|
||||
$gp_nested_forms = GP_Nested_Forms::get_instance();
|
||||
if ($gp_nested_forms->is_nested_form_submission() && $gp_nested_forms->get_parent_form_id() !== $form_id) return false;
|
||||
}
|
||||
|
||||
// Now check to see if the opt-out is enabled within the form settings
|
||||
$form_settings = $this->get_form_settings($form);
|
||||
$form_disabled = (bool) rgar($form_settings, 'disabled');
|
||||
return !$form_disabled;
|
||||
}
|
||||
|
||||
public function set_enabled(bool $value): bool {
|
||||
$settings = (array) $this->get_plugin_settings();
|
||||
$settings['enabled'] = !!$value ? '1' : '0';
|
||||
$this->update_plugin_settings($settings);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function submit_button(string $button_input, array $form): string {
|
||||
if (!Settings::is_connected() || !$this->is_enabled($form)) return $button_input;
|
||||
if ((is_admin() && !defined('DOING_AJAX')) || GFCommon::is_form_editor()) return $button_input;
|
||||
|
||||
$id = sprintf(
|
||||
'gform_cap_%d',
|
||||
absint(rgar($form, 'id'))
|
||||
);
|
||||
|
||||
ob_start();
|
||||
Widget::render([
|
||||
'id' => $id,
|
||||
]);
|
||||
$button_input = ob_get_clean() . $button_input;
|
||||
if (Settings::is_widget_floating()) {
|
||||
$button_input = Widget::set_button_floating(
|
||||
$button_input,
|
||||
[
|
||||
'id' => $id,
|
||||
]
|
||||
);
|
||||
}
|
||||
return $button_input;
|
||||
}
|
||||
|
||||
public function validation(array $validation_result, string $context): array {
|
||||
if ($context != 'form-submit') return $validation_result;
|
||||
|
||||
$form = $validation_result['form'];
|
||||
|
||||
// Make sure that we are actually processing the end of the form
|
||||
if (!GFFormDisplay::is_last_page($form) || rgpost('action') == 'heartbeat') return $validation_result;
|
||||
|
||||
// Make sure that we are connected and enabled
|
||||
if (!Settings::is_connected() || !$this->is_enabled($form)) return $validation_result;
|
||||
|
||||
$errors = API::process(Widget::get_token());
|
||||
if ($errors->has_errors()) {
|
||||
$validation_result['is_valid'] = false;
|
||||
}
|
||||
|
||||
// TODO: Mark as spam (optional)
|
||||
return $validation_result;
|
||||
}
|
||||
|
||||
public function plugin_settings_link( $links, $file ) {
|
||||
// Remove plugin action link
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function get_url(): string {
|
||||
return add_query_arg(
|
||||
[
|
||||
'page' => 'gf_settings',
|
||||
'subview' => $this->get_slug()
|
||||
],
|
||||
admin_url('admin.php')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user