element; core parts hook at 5 (head), 10 (fields), * 15 (honeypot), 20 (recaptcha), 25 (foot). We echo at 12, between fields * and WPForms' own honeypot, and mark the page. * - action `wpforms_process_before` ( array $entry, array $form_data ) * first thing WPForms_Process::process() does after loading form data, * before any field validation. We score here. For `block` we set a * header error; WPForms then stops after validation and shows it. * - filter `wpforms_process_honeypot` ( string $honeypot, array $fields, array $entry, array $form_data ) * returning a non-empty reason makes WPForms take its own spam path * (spam log, and "store spam entries" in 1.8.3+ Pro). Used for `queue`. * If a future WPForms drops this filter, switch queue to the header error. * * @package AgentLane */ namespace AgentLane\Adapters; use AgentLane\Router; if ( ! defined( 'ABSPATH' ) ) { exit; } require_once __DIR__ . '/interface-adapter.php'; /** * Class AdapterWpforms */ class AdapterWpforms implements AdapterInterface { /** * Field types that never carry user text worth scoring or storing. * * @var string[] */ const SKIP_TYPES = array( 'html', 'content', 'divider', 'pagebreak', 'captcha', 'file-upload', 'password', 'payment-total', 'internal-information', 'entry-preview', 'layout', 'repeater', ); /** * Spam reason per form id for this request (consumed by the honeypot filter). * * @var array */ private static $spam_reason = array(); /** * {@inheritDoc} */ public function id(): string { return 'wpforms'; } /** * {@inheritDoc} */ public function register(): void { if ( ! self::is_available() ) { if ( ! did_action( 'init' ) ) { add_action( 'init', array( $this, 'register' ), 1 ); } return; } add_action( 'wpforms_frontend_output', array( $this, 'inject_fields' ), 12, 5 ); add_action( 'wpforms_process_before', array( $this, 'check' ), 5, 2 ); add_filter( 'wpforms_process_honeypot', array( $this, 'honeypot' ), 10, 4 ); } /** * WPForms present? * * @return bool */ public static function is_available(): bool { return function_exists( 'wpforms' ); } /** * Echo hidden fields inside the form and mark the page. * * @param mixed $form_data Form data (unused). * @return void */ public function inject_fields( $form_data = null ) { Router::mark_page_has_form(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- markup is escaped inside Signals::render_hidden_fields(). echo Router::hidden_fields(); } /** * Score before validation; act on the verdict. * * @param mixed $entry Raw entry (unslashed POST data, 'fields' => id => value). * @param mixed $form_data Decoded form definition. * @return void */ public function check( $entry, $form_data ) { if ( ! is_array( $entry ) || ! is_array( $form_data ) ) { return; } $form_id = (int) ( $form_data['id'] ?? ( $entry['id'] ?? 0 ) ); $fields = $this->extract_fields( $entry, $form_data ); $verdict = Router::handle( 'wpforms', (string) $form_id, $fields ); if ( null === $verdict || 'allow' === $verdict->action ) { return; } if ( 'block' === $verdict->action ) { $this->add_header_error( $form_id, Router::block_message() ); return; } // queue: let WPForms' own honeypot path mark it as spam. self::$spam_reason[ $form_id ] = Router::reason_summary( $verdict ); } /** * Hand our spam reason to WPForms' honeypot path. * * @param mixed $honeypot Current reason ('' when clean). * @param mixed $fields Processed fields. * @param mixed $entry Raw entry. * @param mixed $form_data Form definition. * @return mixed */ public function honeypot( $honeypot, $fields = array(), $entry = array(), $form_data = array() ) { if ( ! empty( $honeypot ) ) { return $honeypot; } $form_id = is_array( $form_data ) ? (int) ( $form_data['id'] ?? 0 ) : 0; if ( isset( self::$spam_reason[ $form_id ] ) ) { return self::$spam_reason[ $form_id ]; } return $honeypot; } /** * Map entry values to label => value. * * @param array $entry Raw entry. * @param array $form_data Form definition. * @return array */ private function extract_fields( array $entry, array $form_data ): array { $values = isset( $entry['fields'] ) && is_array( $entry['fields'] ) ? $entry['fields'] : array(); $defs = isset( $form_data['fields'] ) && is_array( $form_data['fields'] ) ? $form_data['fields'] : array(); $out = array(); foreach ( $values as $id => $value ) { $def = isset( $defs[ $id ] ) && is_array( $defs[ $id ] ) ? $defs[ $id ] : array(); $type = (string) ( $def['type'] ?? '' ); if ( in_array( $type, self::SKIP_TYPES, true ) ) { continue; } $label = trim( (string) ( $def['label'] ?? '' ) ); $name = '' !== $label ? $label : 'field_' . $id; if ( isset( $out[ $name ] ) ) { $name .= ' (' . $id . ')'; } $out[ $name ] = $value; } return $out; } /** * Form-level error shown above the form; stops processing after validation. * * @param int $form_id Form id. * @param string $message Plain text. * @return void */ private function add_header_error( int $form_id, string $message ): void { $process = $this->process_object(); if ( null === $process ) { return; } if ( ! isset( $process->errors ) || ! is_array( $process->errors ) ) { $process->errors = array(); } $process->errors[ $form_id ]['header'] = esc_html( $message ); } /** * WPForms_Process instance (its public $errors array is the API). * * @return object|null */ private function process_object() { if ( ! function_exists( 'wpforms' ) ) { return null; } $wpforms = wpforms(); if ( ! is_object( $wpforms ) ) { return null; } if ( method_exists( $wpforms, 'get' ) ) { $process = $wpforms->get( 'process' ); if ( is_object( $process ) ) { return $process; } } if ( isset( $wpforms->process ) && is_object( $wpforms->process ) ) { return $wpforms->process; } return null; } }