; we append hidden fields and mark the page. * - filter `wpcf7_spam` ( bool $spam, WPCF7_Submission $submission ) * two-argument form since CF7 5.1; runs after field validation and * before mail. Returning true makes CF7 treat the submission as spam * (no mail, Flamingo stores it as spam). We hook at priority 5 so the * disallowed-list and Akismet checks see our result first. * - filter `wpcf7_display_message` ( string $message, string $status ) * lets us replace the "spam" response text with the block or held message. * * @package AgentLane */ namespace AgentLane\Adapters; use AgentLane\Router; if ( ! defined( 'ABSPATH' ) ) { exit; } require_once __DIR__ . '/interface-adapter.php'; /** * Class AdapterCf7 */ class AdapterCf7 implements AdapterInterface { /** * Message to show instead of CF7's generic spam text, for this request. * * @var string|null */ private static $message = null; /** * {@inheritDoc} */ public function id(): string { return 'cf7'; } /** * {@inheritDoc} */ public function register(): void { if ( ! self::is_available() ) { // CF7 may simply not be loaded yet; retry once all plugins are in. if ( ! did_action( 'init' ) ) { add_action( 'init', array( $this, 'register' ), 1 ); } return; } add_filter( 'wpcf7_form_elements', array( $this, 'inject_fields' ), 20 ); add_filter( 'wpcf7_spam', array( $this, 'check_spam' ), 5, 2 ); add_filter( 'wpcf7_display_message', array( $this, 'filter_message' ), 10, 2 ); } /** * Contact Form 7 present? * * @return bool */ public static function is_available(): bool { return class_exists( 'WPCF7_ContactForm' ) && class_exists( 'WPCF7_Submission' ); } /** * Append token / honeypot / agent-purpose fields to the form body. * * @param mixed $elements Form HTML. * @return mixed */ public function inject_fields( $elements ) { Router::mark_page_has_form(); if ( ! is_string( $elements ) ) { return $elements; } return $elements . Router::hidden_fields(); } /** * Score the submission; true = spam for CF7. * * @param mixed $spam Current spam flag. * @param object $submission WPCF7_Submission (not type-hinted on purpose). * @return bool */ public function check_spam( $spam, $submission = null ) { $spam = (bool) $spam; if ( ! is_object( $submission ) || ! method_exists( $submission, 'get_posted_data' ) ) { return $spam; } $form_id = ''; if ( method_exists( $submission, 'get_contact_form' ) ) { $form = $submission->get_contact_form(); if ( is_object( $form ) && method_exists( $form, 'id' ) ) { $form_id = (string) $form->id(); } } $posted = $submission->get_posted_data(); $verdict = Router::handle( 'cf7', $form_id, is_array( $posted ) ? $posted : array() ); if ( null === $verdict || 'allow' === $verdict->action ) { return $spam; } self::$message = ( 'block' === $verdict->action ) ? Router::block_message() : Router::held_message(); // CF7 5.3+ keeps a per-submission spam log (visible in Flamingo). if ( method_exists( $submission, 'add_spam_log' ) ) { $submission->add_spam_log( array( 'agent' => 'agent-lane', 'reason' => Router::reason_summary( $verdict ), ) ); } return true; } /** * Replace the generic spam message with ours when we caused the status. * * @param mixed $message Message text. * @param string $status CF7 status key. * @return mixed */ public function filter_message( $message, $status = '' ) { if ( 'spam' === $status && null !== self::$message ) { return self::$message; } return $message; } }