is_gravityforms_supported()) { return; } require_once 'class-field-turnstile.php'; } /** * Initialize hooks required for admin. * * @since 1.0 * * @return void */ public function init_admin() { parent::init_admin(); add_action('gform_field_appearance_settings', array($this, 'add_turnstile_theme_field_setting'), 0, 1); } public function scripts() { $enqueue_condition = ! is_admin() ? array(array($this, 'frontend_script_callback')) : array(array($this, 'admin_script_callback')); $turnstile_src = 'https://challenges.cloudflare.com/turnstile/v0/api.js'; // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- Turnstile must load its widget from Cloudflare; disclosed in readme. $scripts = array( array( 'handle' => 'turnstile_script', 'src' => $turnstile_src, 'version' => $this->_version, 'deps' => array(), 'in_footer' => true, 'enqueue' => $enqueue_condition ), array( 'handle' => 'alphagf_turnstile_admin_script', 'src' => trailingslashit($this->get_base_url()) . 'js/turnstile-admin.js', 'version' => $this->_version, 'deps' => array('jquery'), 'in_footer' => true, 'enqueue' => array(array($this, 'admin_script_callback')) ), array( 'handle' => 'alphagf_turnstile_script', 'src' => trailingslashit($this->get_base_url()) . 'js/turnstile.js', 'version' => $this->_version, 'deps' => array('jquery'), 'in_footer' => true, 'enqueue' => $enqueue_condition ), ); return array_merge(parent::scripts(), $scripts); } public function styles() { $styles = array( array( 'handle' => 'alphagf_icon_style', 'src' => ALPHAGF_ASSETS . '/css/font_style.css', 'version' => $this->_version, 'enqueue' => array( array('admin_page' => array('form_editor')), ) ) ); return array_merge(parent::styles(), $styles); } public function plugin_settings_fields() { return array( array( 'title' => esc_html__('Turnstile Settings', 'alpha-addons-for-gravity-forms'), // translators: %1 is an opening tag, and %2 is a closing tag. 'description' => sprintf(esc_html__('To connect your site to Turnstile, create a site on your %1$sCloudflare Dashboard%2$s and enter the associated Site Key and Secret Key below.', 'alpha-addons-for-gravity-forms'), '', ''), // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- Link to the Cloudflare dashboard, not a loaded asset. 'fields' => array( array( 'name' => 'site_key', 'label' => esc_html__('Site Key', 'alpha-addons-for-gravity-forms'), 'type' => 'text', 'class' => 'medium', 'feedback_callback' => [$this, 'is_key_valid'], ), array( 'name' => 'site_secret', 'label' => esc_html__('Secret Key', 'alpha-addons-for-gravity-forms'), 'type' => 'text', 'class' => 'medium', 'feedback_callback' => [$this, 'is_key_valid'], ), ), ), array( 'title' => esc_html__('Field Options', 'alpha-addons-for-gravity-forms'), 'description' => esc_html__('Choose between a Light or Dark theme to tailor the field\'s appearance to your website.', 'alpha-addons-for-gravity-forms'), 'fields' => array( array( 'name' => 'theme', 'label' => esc_html__('Theme', 'alpha-addons-for-gravity-forms'), 'type' => 'select', 'default_value' => 'light', 'choices' => array( array( 'label' => __('Light', 'alpha-addons-for-gravity-forms'), 'value' => 'light', ), array( 'label' => __('Dark', 'alpha-addons-for-gravity-forms'), 'value' => 'dark', ), ), ), ), ), // Preview group array( 'title' => esc_html__('Field Preview', 'alpha-addons-for-gravity-forms'), 'description' => '

' . esc_html__('Below is a preview of how the field will appear in your forms. If you see an error message, check your credentials and try again.', 'alpha-addons-for-gravity-forms') . '

' . esc_html__('Note: ', 'alpha-addons-for-gravity-forms') . '' . esc_html__('Solve the preview and save the settings to activate the Turnstile field on your forms. If your widget is set to the "Invisible" type in Cloudflare, this preview will appear empty.', 'alpha-addons-for-gravity-forms'), 'dependency' => array( 'live' => false, 'fields' => array( array('field' => 'site_key'), array('field' => 'site_secret'), ), ), 'fields' => array( array( 'name' => 'preview', 'type' => 'html', 'html' => array($this, 'get_preview_html'), ), ), ), ); } public function get_preview_html() { $key = $this->get_plugin_setting('site_key'); $secret = $this->get_plugin_setting('site_secret'); $theme = $this->get_plugin_setting('theme'); if (empty($key) || empty($secret)) { return ''; } return '

'; } public function add_turnstile_theme_field_setting($position) { if ((int) $position !== 20) { return; } ?>
  • failed_validation) { return true; } } return false; } public function frontend_script_callback($form) { return $this->has_turnstile_field($form); } public function admin_script_callback($form) { if (! is_admin()) { return false; } $page = rgget('page'); $subview = rgget('subview'); if ($page !== 'gf_edit_forms' && ($page !== 'gf_settings' || $subview !== $this->_slug)) { return false; } return true; } public function get_sanitized_remote_address() { // Check if the index exists if (!isset($_SERVER['REMOTE_ADDR'])) { return ''; } // Get unslashed value (for magic quotes compatibility) $ip = sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])); // Validate and sanitize IP address if (filter_var($ip, FILTER_VALIDATE_IP)) { return $ip; } return ''; } /** * Determine if a given form has a turnstile field. * * @since 1.0 * * @param array $form The form being evaluated. * * @return bool */ public function has_turnstile_field($form) { $fields = \GFAPI::get_fields_by_type($form, array('alphagf_turnstile')); return ! empty($fields); } public function verify_token($token, $is_form_submission = true, $secret = '') { $args = array( 'body' => array( 'secret' => empty($secret) ? $this->get_plugin_setting('site_secret') : $secret, 'response' => $token, 'remoteip' => $this->get_sanitized_remote_address() ), ); $response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', $args); // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- Server-side token verification API. if (is_wp_error($response) || 200 !== (int) wp_remote_retrieve_response_code($response)) { return false; } $body_array = json_decode(wp_remote_retrieve_body($response), true); $is_valid = is_array($body_array) && true === rgar($body_array, 'success'); if (! $is_form_submission) { update_option(self::ALPHAGF_TURNSTILE_SETTINGS_KEY, $is_valid); } return $is_valid; } public function has_valid_credentials() { $secret = $this->get_plugin_setting('site_secret'); // Missing credentials, no need to check further. if (empty($secret) || empty($this->get_plugin_setting('site_key'))) { return false; } $is_valid = get_option(self::ALPHAGF_TURNSTILE_SETTINGS_KEY, false); if ($is_valid) { return (bool) $is_valid; } return false; } public function update_plugin_settings($settings) { $previous = $this->get_plugin_settings(); $keys_changed = rgar($previous, 'site_key') !== rgar($settings, 'site_key') || rgar($previous, 'site_secret') !== rgar($settings, 'site_secret'); $response = rgpost('cf-turnstile-response'); if (! empty($response)) { // Verify against the secret being saved, not the one currently stored. $this->verify_token($response, false, rgar($settings, 'site_secret')); } elseif ($keys_changed) { // New keys must be confirmed by solving the preview again. update_option(self::ALPHAGF_TURNSTILE_SETTINGS_KEY, false); } // Otherwise keep the existing verification, so changing e.g. the theme doesn't silently disable the field. parent::update_plugin_settings($settings); } public function is_key_valid() { $is_valid = get_option(self::ALPHAGF_TURNSTILE_SETTINGS_KEY, false); if ($is_valid) { return true; } return false; } }