tag; we append hidden fields right after it. * (`gform_field_content` would be the fallback if a theme filters this.) * - filter `gform_pre_validation` ( array $form ) * first line of GFFormDisplay::validate(), before field validation. * We score here (last page only for multi-page forms). * - filter `gform_validation` ( array $validation_result ) * for `block` we set is_valid = false so no entry is created. * - filter `gform_validation_message` ( string $message, array $form ) * replaces the generic "There was a problem" text with the block message. * - filter `gform_entry_is_spam` ( bool $is_spam, array $form, array $entry ) * present since GF 2.4.17; returning true stores the entry as spam * and skips notifications. Used for `queue`. GFCommon::set_spam_filter() * (GF 2.7+) records the reason in the entry notes. * * @package AgentLane */ namespace AgentLane\Adapters; use AgentLane\Router; use AgentLane\Verdict; if ( ! defined( 'ABSPATH' ) ) { exit; } require_once __DIR__ . '/interface-adapter.php'; /** * Class AdapterGravity */ class AdapterGravity implements AdapterInterface { /** * Field types with no user text (or text we must not store). * * @var string[] */ const SKIP_TYPES = array( 'html', 'section', 'page', 'captcha', 'honeypot', 'fileupload', 'password', 'creditcard', 'product', 'total', 'shipping', 'option', 'quantity', 'repeater', ); /** * Verdict per form id for this request. * * @var array */ private static $verdicts = array(); /** * {@inheritDoc} */ public function id(): string { return 'gravity'; } /** * {@inheritDoc} */ public function register(): void { if ( ! self::is_available() ) { if ( ! did_action( 'init' ) ) { add_action( 'init', array( $this, 'register' ), 1 ); } return; } add_filter( 'gform_pre_render', array( $this, 'mark_page' ), 10, 3 ); add_filter( 'gform_form_tag', array( $this, 'inject_fields' ), 10, 2 ); add_filter( 'gform_pre_validation', array( $this, 'score' ), 5 ); add_filter( 'gform_validation', array( $this, 'validation' ), 5 ); add_filter( 'gform_validation_message', array( $this, 'validation_message' ), 10, 2 ); add_filter( 'gform_entry_is_spam', array( $this, 'is_spam' ), 5, 3 ); } /** * Gravity Forms present? * * @return bool */ public static function is_available(): bool { return class_exists( 'GFForms' ); } /** * Mark the page on front-end render. * * @param mixed $form Form array. * @param mixed $ajax Ajax flag. * @param mixed $field_values Prepopulated values. * @return mixed */ public function mark_page( $form, $ajax = false, $field_values = array() ) { if ( ! is_admin() ) { Router::mark_page_has_form(); } return $form; } /** * Append hidden fields after the opening form tag. * * @param mixed $form_tag Opening tag HTML. * @param mixed $form Form array. * @return mixed */ public function inject_fields( $form_tag, $form = array() ) { Router::mark_page_has_form(); if ( ! is_string( $form_tag ) ) { return $form_tag; } return $form_tag . Router::hidden_fields(); } /** * Score once per form per request, on the final page only. * * @param mixed $form Form array. * @return mixed */ public function score( $form ) { if ( ! is_array( $form ) || empty( $form['id'] ) ) { return $form; } $form_id = (int) $form['id']; if ( isset( self::$verdicts[ $form_id ] ) ) { return $form; } if ( ! $this->is_last_page( $form ) ) { return $form; } $verdict = Router::handle( 'gravity', (string) $form_id, $this->extract_fields( $form ) ); if ( null !== $verdict ) { self::$verdicts[ $form_id ] = $verdict; } return $form; } /** * Fail validation for `block` so no entry is created. * * @param mixed $validation_result Array with 'is_valid' and 'form'. * @return mixed */ public function validation( $validation_result ) { if ( ! is_array( $validation_result ) || empty( $validation_result['form']['id'] ) ) { return $validation_result; } $form_id = (int) $validation_result['form']['id']; $verdict = self::$verdicts[ $form_id ] ?? null; if ( null !== $verdict && 'block' === $verdict->action ) { $validation_result['is_valid'] = false; } return $validation_result; } /** * Show the block message instead of the generic validation text. * * @param mixed $message Validation message HTML. * @param mixed $form Form array. * @return mixed */ public function validation_message( $message, $form = array() ) { $form_id = is_array( $form ) ? (int) ( $form['id'] ?? 0 ) : 0; $verdict = self::$verdicts[ $form_id ] ?? null; if ( null === $verdict || 'block' !== $verdict->action ) { return $message; } return '

' . esc_html( Router::block_message() ) . '

'; } /** * Mark the entry as spam for `queue`. * * @param mixed $is_spam Current flag. * @param mixed $form Form array. * @param mixed $entry Entry array. * @return mixed */ public function is_spam( $is_spam, $form = array(), $entry = array() ) { if ( $is_spam || ! is_array( $form ) || empty( $form['id'] ) ) { return $is_spam; } $form_id = (int) $form['id']; $verdict = self::$verdicts[ $form_id ] ?? null; // Entry created without passing through validation (API paths): score now. if ( null === $verdict ) { $verdict = Router::handle( 'gravity', (string) $form_id, $this->extract_fields( $form ) ); if ( null !== $verdict ) { self::$verdicts[ $form_id ] = $verdict; } } if ( null === $verdict || 'allow' === $verdict->action ) { return $is_spam; } if ( class_exists( 'GFCommon' ) && method_exists( 'GFCommon', 'set_spam_filter' ) ) { \GFCommon::set_spam_filter( $form_id, 'Agent Lane', Router::reason_summary( $verdict ) ); } return true; } /** * True on single-page forms and on the last page of multi-page forms. * * @param array $form Form array. * @return bool */ private function is_last_page( array $form ): bool { if ( class_exists( 'GFFormDisplay' ) && method_exists( 'GFFormDisplay', 'is_last_page' ) ) { try { return (bool) \GFFormDisplay::is_last_page( $form ); } catch ( \Throwable $e ) { return true; } } return true; } /** * Read submitted values for each field: label => value. * * @param array $form Form array (fields are GF_Field objects). * @return array */ private function extract_fields( array $form ): array { $out = array(); $fields = isset( $form['fields'] ) && is_array( $form['fields'] ) ? $form['fields'] : array(); foreach ( $fields as $field ) { if ( ! is_object( $field ) ) { continue; } $type = isset( $field->type ) ? (string) $field->type : ''; if ( in_array( $type, self::SKIP_TYPES, true ) ) { continue; } $id = isset( $field->id ) ? (string) $field->id : ''; if ( '' === $id ) { continue; } // Gravity Forms always ships rgpost(); there is no raw $_POST fallback on purpose. if ( method_exists( $field, 'get_value_submission' ) ) { $value = $field->get_value_submission( array() ); } elseif ( function_exists( 'rgpost' ) ) { $value = rgpost( 'input_' . $id ); } else { $value = ''; } $label = isset( $field->label ) ? trim( (string) $field->label ) : ''; $name = '' !== $label ? $label : 'field_' . $id; if ( isset( $out[ $name ] ) ) { $name .= ' (' . $id . ')'; } $out[ $name ] = $value; } return $out; } }