is_gravityforms_supported()) { return; } require_once 'class-hcaptcha-field.php'; } public function get_menu_icon() { return 'gform-icon alphagf_icon icon-hcaptcha'; } /** * Initialize required hooks for admin and theme. * * @since 1.0 * * @return void */ public function init() { parent::init(); add_filter('gform_pre_validation', array($this, 'move_hcaptcha_field_to_last'), 999, 1); add_filter('gform_validation', array($this, 'reset_hcaptcha_field_position'), 999, 1); } /** * 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_hcaptcha_theme_field_setting'), 0, 1); } /** * Return the scripts which should be enqueued. * * @return array */ public function scripts() { $enqueue_condition = ! is_admin() ? array(array($this, 'frontend_script_callback')) : array(array($this, 'admin_script_callback')); $hcaptcha_src = 'https://js.hcaptcha.com/1/api.js'; $scripts = array( array( 'handle' => 'hcaptcha_script', 'src' => $hcaptcha_src, 'version' => $this->_version, 'deps' => array(), 'in_footer' => true, 'enqueue' => $enqueue_condition ), array( 'handle' => 'alphagf_hcaptcha_admin_script', 'src' => trailingslashit($this->get_base_url()) . 'js/hcaptcha-admin.js', 'version' => $this->_version, 'deps' => array('jquery'), 'in_footer' => true, 'enqueue' => array(array($this, 'admin_script_callback')) ), array( 'handle' => 'alphagf_hcaptcha_script', 'src' => trailingslashit($this->get_base_url()) . 'js/hcaptcha.js', 'version' => $this->_version, 'deps' => array('jquery'), 'in_footer' => true, 'enqueue' => $enqueue_condition ), ); return array_merge(parent::scripts(), $scripts); } public function add_hcaptcha_theme_field_setting($position) { if ((int) $position !== 20) { return; } ?>
' . 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 hCaptcha field on your forms.', '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'), ), ), ), ); } /** * Get the HTML to display when previewing the widget on the settings page. * * @since 1.0 * * @return string */ 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 move_hcaptcha_field_to_last($form) { if (! $this->has_hcaptcha_field($form)) { return $form; } $idx = null; $fields = $form['fields']; foreach ($fields as $i => $field) { if ($field->type === 'alphagf_hcaptcha') { $form['alphagf_hcaptcha_original_position'] = $i; $idx = $i; break; } } if (is_null($idx)) { return $form; } unset($fields[$idx]); $fields[] = $field; $form['fields'] = $fields; return $form; } public function reset_hcaptcha_field_position($validation_result) { $form = $validation_result['form']; if (! $this->has_hcaptcha_field($form)) { return $validation_result; } $field_position = rgar($form, 'alphagf_hcaptcha_original_position'); unset($form['alphagf_hcaptcha_original_position']); if ($field_position !== 0 && ! $field_position) { return $validation_result; } $fields = $form['fields']; $hcaptcha_field = array_pop($fields); // Put the field back to its original index. $fields = array_merge( array_slice( $fields, 0, $field_position ), array($hcaptcha_field), array_slice($fields, $field_position) ); $form['fields'] = $fields; $validation_result['form'] = $form; return $validation_result; } public function form_has_errors($form) { foreach ($form['fields'] as $field) { if ($field->failed_validation) { return true; } } return false; } public function frontend_script_callback($form) { return $this->has_hcaptcha_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; } /** * Determine if a given form has a turnstile field. * * @since 1.0 * * @param array $form The form being evaluated. * * @return bool */ public function has_hcaptcha_field($form) { $fields = \GFAPI::get_fields_by_type($form, array('alphagf_hcaptcha')); return ! empty($fields); } 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 ''; } 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://hcaptcha.com/siteverify', $args); if (is_wp_error($response)) { return false; } if (wp_remote_retrieve_response_code($response) !== 200) { 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_HCAPTCHA_CACHE_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_HCAPTCHA_CACHE_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('h-captcha-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_HCAPTCHA_CACHE_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_HCAPTCHA_CACHE_KEY, false); if ($is_valid) { return true; } return false; } }