55, 'block_threshold' => 90, 'honeypot_field_name' => '', 'token_min_age' => 3, 'token_max_age' => 86400, 'bind_token_to_client' => true, 'store_full_ip' => false, 'retention_days' => 30, 'verify_signed_agents' => false, 'policy_overrides' => array(), 'declared_agent_action' => 'queue', 'block_message' => 'This form does not accept automated submissions. If you are an agent acting for a person, state your purpose in the agent field.', 'notify_email' => '', 'notify_on_release' => true, 'notify_on_hold' => true, ); /** * Row/bulk actions and the Queue method that handles each. */ const ACTIONS = array( 'release' => 'release', 'delete' => 'delete', 'mark_bot' => 'mark_bot', ); /** * @var Admin|null */ private static $instance = null; /** * @var Queue */ private $queue; /** * @var string Hook suffix of the queue page. */ private $queue_hook = ''; /** * @var string Hook suffix of the settings page. */ private $settings_hook = ''; /** * @var bool Hooks registered? */ private $registered = false; /** * Creates (or returns) the shared instance and registers hooks once. * Plugin (WP1) instead does `new Admin()` + `->register()`; both paths are fine. * * @return Admin */ public static function init(): Admin { if ( null === self::$instance ) { self::$instance = new self(); } self::$instance->register(); return self::$instance; } /** * Constructor. No arguments: Plugin::load_admin() calls `new Admin()`. */ public function __construct() { $this->queue = Queue::instance(); if ( null === self::$instance ) { self::$instance = $this; } } /** * Registers all admin hooks. Safe to call more than once. * * @return void */ public function register() { if ( $this->registered ) { return; } $this->registered = true; add_action( 'admin_menu', array( $this, 'register_menu' ) ); // Priority 20: after Settings::register() (WP1, priority 10) so the option is registered once. add_action( 'admin_init', array( $this, 'register_settings' ), 20 ); add_action( 'admin_init', array( $this, 'privacy_policy_content' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'admin_notices', array( $this, 'admin_notices' ) ); add_action( 'wp_dashboard_setup', array( $this, 'dashboard_widget_setup' ) ); add_action( 'admin_post_' . self::NONCE_REGEN, array( $this, 'handle_regenerate_honeypot' ) ); add_action( 'admin_post_' . self::NONCE_REPAIR, array( $this, 'handle_queue_repair' ) ); add_action( 'admin_post_' . self::NONCE_DISMISS, array( $this, 'handle_queue_failure_dismiss' ) ); add_filter( 'set-screen-option', array( $this, 'save_screen_option' ), 10, 3 ); add_filter( 'set_screen_option_' . self::SCREEN_OPTION, array( $this, 'save_screen_option' ), 10, 3 ); } /** * Suggested text for the site's privacy policy (Settings > Privacy > Policy Guide). * Mirrors docs/privacy-disclosure.md section 3; keep the two in sync. * * @return void */ public function privacy_policy_content() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $name = defined( 'AGENTLANE_NAME' ) ? (string) AGENTLANE_NAME : 'Agent Lane'; $days = (int) $this->get_setting( 'retention_days' ); if ( $days > 0 ) { /* translators: %d: retention period in days */ $held_for = sprintf( __( 'for up to %d days', 'agent-lane' ), $days ); } else { $held_for = __( 'until the site owner deletes them', 'agent-lane' ); } $html = '

' . esc_html__( 'Form spam filtering.', 'agent-lane' ) . ' '; $html .= esc_html__( 'This site uses the Agent Lane plugin to sort form submissions into people and automated senders. When you submit a form, the plugin looks at your browser\'s request headers, the time between loading the page and submitting, and counts of pointer, touch and keyboard activity. It does not record what you type beyond the fields you submit.', 'agent-lane' ) . ' '; $html .= esc_html( sprintf( /* translators: %s: "for up to N days" or "until the site owner deletes them" */ __( 'For submissions it holds for review, it stores the submitted fields, a truncated IP address (the last part removed), your browser\'s user agent and a score, %s. For submissions it delivers, it stores the time, the form, the score, the truncated IP address and the user agent for the last 200 submissions. This data stays on this site and is not shared with third parties.', 'agent-lane' ), $held_for ) ); $html .= '

'; $html .= '

' . esc_html__( 'If "Store full IP address" is enabled: full IP addresses are stored for held submissions for the same period to investigate abuse.', 'agent-lane' ) . '

'; $html .= '

' . esc_html__( 'If "Verify signed agents" is enabled: when an automated agent signs its request, this site may fetch the agent operator\'s public key from the operator\'s website to confirm the signature. That request does not include any information about you.', 'agent-lane' ) . '

'; wp_add_privacy_policy_content( $name, wp_kses_post( $html ) ); } /* ------------------------------------------------------------------ */ /* Menu and pages */ /* ------------------------------------------------------------------ */ /** * Adds the top-level menu (with held-count badge) and its two submenus. * * @return void */ public function register_menu() { $held = $this->queue->count_held(); $badge = ''; if ( $held > 0 ) { $badge = sprintf( ' %3$s', (int) $held, esc_html( number_format_i18n( $held ) ), /* translators: %s: number of held submissions */ esc_html( sprintf( _n( '%s submission awaiting review', '%s submissions awaiting review', $held, 'agent-lane' ), number_format_i18n( $held ) ) ) ); } $this->queue_hook = add_menu_page( __( 'Agent Lane', 'agent-lane' ), esc_html__( 'Agent Lane', 'agent-lane' ) . $badge, self::CAP, self::PAGE_QUEUE, array( $this, 'render_queue_page' ), 'dashicons-shield-alt', 26 ); add_submenu_page( self::PAGE_QUEUE, __( 'Agent Lane queue', 'agent-lane' ), __( 'Queue', 'agent-lane' ), self::CAP, self::PAGE_QUEUE, array( $this, 'render_queue_page' ) ); $this->settings_hook = add_submenu_page( self::PAGE_QUEUE, __( 'Agent Lane settings', 'agent-lane' ), __( 'Settings', 'agent-lane' ), self::CAP, self::PAGE_SETTINGS, array( $this, 'render_settings_page' ) ); if ( $this->queue_hook ) { add_action( 'load-' . $this->queue_hook, array( $this, 'load_queue_page' ) ); } } /** * Runs before the queue page renders: screen options + row/bulk actions. * * @return void */ public function load_queue_page() { add_screen_option( 'per_page', array( 'label' => __( 'Submissions per page', 'agent-lane' ), 'default' => self::DEFAULT_PER_PAGE, 'option' => self::SCREEN_OPTION, ) ); $this->handle_queue_actions(); } /** * Persists the per-page screen option. * * @param mixed $status Current value (false = not handled). * @param string $option Option name. * @param mixed $value Submitted value. * @return mixed */ public function save_screen_option( $status, $option, $value ) { if ( self::SCREEN_OPTION === $option ) { return max( 1, min( 200, (int) $value ) ); } return $status; } /** * Renders the Queue page. * * @return void */ public function render_queue_page() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You do not have permission to view this page.', 'agent-lane' ) ); } $current_tab = QueueListTable::current_tab(); $counts = $this->queue->count_by_status(); $tabs = array( Queue::TAB_PEOPLE => array( 'label' => __( 'People', 'agent-lane' ), 'count' => (int) $counts[ Queue::STATUS_HUMAN ] + (int) $counts[ Queue::STATUS_RELEASED ], 'url' => self::queue_url( Queue::TAB_PEOPLE ), ), Queue::TAB_AUTOMATION => array( 'label' => __( 'Automation', 'agent-lane' ), 'count' => (int) $counts[ Queue::STATUS_HELD ], 'url' => self::queue_url( Queue::TAB_AUTOMATION ), ), ); $per_page = (int) get_user_option( self::SCREEN_OPTION ); if ( $per_page < 1 ) { $per_page = self::DEFAULT_PER_PAGE; } $table = new QueueListTable( $this->queue, $current_tab, $per_page ); $table->prepare_items(); $settings_url = self::settings_url(); $base_url = admin_url( 'admin.php' ); // Bulk actions post to the current list view, so the nonce stays out of the address bar. $list_url = add_query_arg( self::list_state(), self::queue_url( $current_tab ) ); require __DIR__ . '/views/queue.php'; } /** * Renders the Settings page. * * @return void */ public function render_settings_page() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You do not have permission to view this page.', 'agent-lane' ) ); } $queue_url = self::queue_url( Queue::TAB_AUTOMATION ); require __DIR__ . '/views/settings.php'; } /** * Loads admin.css on our pages and on the dashboard (widget). * * @param string $hook Current admin page hook suffix. * @return void */ public function enqueue_assets( $hook ) { $ours = array( $this->queue_hook, $this->settings_hook, 'index.php' ); if ( ! in_array( (string) $hook, $ours, true ) ) { return; } wp_enqueue_style( 'agentlane-admin', self::plugin_url( 'admin/assets/admin.css' ), array(), self::version() ); } /* ------------------------------------------------------------------ */ /* Queue actions */ /* ------------------------------------------------------------------ */ /** * Handles Release / Delete / Mark as bot for one row (GET + nonce) or * for a bulk selection (list table form + bulk nonce), then redirects. * * @return void */ private function handle_queue_actions() { if ( ! current_user_can( self::CAP ) ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- nonces are checked below per action type. $action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : ''; if ( '' === $action || '-1' === $action ) { $action = isset( $_REQUEST['action2'] ) ? sanitize_key( wp_unslash( $_REQUEST['action2'] ) ) : ''; } if ( ! isset( self::ACTIONS[ $action ] ) ) { return; } $tab = QueueListTable::current_tab(); $ids = array(); if ( isset( $_REQUEST['ids'] ) && is_array( $_REQUEST['ids'] ) ) { check_admin_referer( 'bulk-' . QueueListTable::PLURAL ); $ids = array_filter( array_map( 'absint', wp_unslash( $_REQUEST['ids'] ) ) ); } elseif ( isset( $_GET['id'] ) ) { $id = absint( $_GET['id'] ); check_admin_referer( 'agentlane_row_' . $id ); if ( $id > 0 ) { $ids = array( $id ); } } else { return; } // phpcs:enable $method = self::ACTIONS[ $action ]; $done = 0; foreach ( $ids as $id ) { if ( $this->queue->$method( (int) $id ) ) { ++$done; } } $url = add_query_arg( array_merge( self::list_state(), array( 'agentlane_msg' => $action, 'agentlane_n' => $done, ) ), self::queue_url( $tab ) ); wp_safe_redirect( $url ); exit; } /** * Search, sorting and page of the queue list in the current request, so a bulk * action (POST) returns to the same view. * * @return array */ private static function list_state(): array { $state = array(); foreach ( array( 's', 'orderby', 'order', 'paged' ) as $arg ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view state only (search, sort, page); actions check their nonce. $value = isset( $_REQUEST[ $arg ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $arg ] ) ) : ''; if ( '' !== $value ) { $state[ $arg ] = $value; } } return $state; } /** * admin-post handler: generates a new random honeypot field name. * * @return void */ public function handle_regenerate_honeypot() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'agent-lane' ) ); } check_admin_referer( self::NONCE_REGEN ); $settings = self::settings_instance(); if ( $settings && method_exists( $settings, 'regenerate_honeypot_name' ) ) { $settings->regenerate_honeypot_name(); } else { $this->set_setting( 'honeypot_field_name', self::generate_honeypot_name() ); } wp_safe_redirect( add_query_arg( 'agentlane_msg', 'honeypot', self::settings_url() ) ); exit; } /** * Random field name: 10 lowercase alphanumerics starting with a letter. * * @return string */ public static function generate_honeypot_name(): string { $letters = 'abcdefghijklmnopqrstuvwxyz'; $name = $letters[ wp_rand( 0, 25 ) ]; $name .= strtolower( wp_generate_password( 9, false, false ) ); return preg_replace( '/[^a-z0-9]/', 'x', $name ); } /* ------------------------------------------------------------------ */ /* Notices and dashboard widget */ /* ------------------------------------------------------------------ */ /** * Shows result notices after queue actions and honeypot regeneration. * Only on our own pages. * * @return void */ public function admin_notices() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen ) { return; } $ours = in_array( $screen->id, array( $this->queue_hook, $this->settings_hook ), true ); if ( $ours || 'plugins' === $screen->id || 'dashboard' === $screen->id ) { $this->queue_failure_notice(); } if ( $ours || 'plugins' === $screen->id ) { $this->welcome_notice( $ours ); } if ( ! $ours ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- display only. $msg = isset( $_GET['agentlane_msg'] ) ? sanitize_key( wp_unslash( $_GET['agentlane_msg'] ) ) : ''; $n = isset( $_GET['agentlane_n'] ) ? absint( $_GET['agentlane_n'] ) : 0; // phpcs:enable $text = ''; $type = 'success'; switch ( $msg ) { case 'release': /* translators: %s: number of submissions */ $text = sprintf( _n( '%s submission released. The owner was emailed a copy.', '%s submissions released. The owner was emailed a copy.', $n, 'agent-lane' ), number_format_i18n( $n ) ); break; case 'delete': /* translators: %s: number of submissions */ $text = sprintf( _n( '%s submission deleted. It will be removed by the retention cleanup.', '%s submissions deleted. They will be removed by the retention cleanup.', $n, 'agent-lane' ), number_format_i18n( $n ) ); break; case 'mark_bot': /* translators: %s: number of submissions */ $text = sprintf( _n( '%s fingerprint added to the local blocklist.', '%s fingerprints added to the local blocklist.', $n, 'agent-lane' ), number_format_i18n( $n ) ); break; case 'honeypot': $text = __( 'A new honeypot field name was generated. Cached pages will pick it up after the cache clears.', 'agent-lane' ); break; } if ( '' !== $text && 0 === $n && 'honeypot' !== $msg ) { $type = 'warning'; $text = __( 'Nothing changed. The selected submissions may have been processed already.', 'agent-lane' ); } if ( '' !== $text ) { printf( '

%2$s

', esc_attr( $type ), esc_html( $text ) ); } if ( ! self::form_plugin_detected() ) { printf( '

%s

', esc_html__( 'No supported form plugin detected (Contact Form 7, WPForms, Gravity Forms, Elementor Pro). Agent Lane still watches comments.', 'agent-lane' ) ); } } /** * Error notice while submissions are delivered because the queue cannot store them * (Queue::OPTION_FAILURE), plus the result of Repair and Dismiss. Shown to users who * can manage options, on the plugin's screens, the Dashboard and the Plugins screen * only. It says how to fix the problem and goes away on repair or dismissal. * * @return void */ public function queue_failure_notice() { if ( ! current_user_can( self::CAP ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only: the result of a nonce-checked admin-post action. $result = isset( $_GET['agentlane_queue'] ) ? sanitize_key( wp_unslash( $_GET['agentlane_queue'] ) ) : ''; if ( 'repaired' === $result ) { printf( '

%s

', esc_html__( 'Agent Lane: the queue table was repaired. Submissions are held again.', 'agent-lane' ) ); } elseif ( 'dismissed' === $result ) { printf( '

%s

', esc_html__( 'Agent Lane: the notice was dismissed. It comes back if a submission cannot be stored again.', 'agent-lane' ) ); } elseif ( 'repair_failed' === $result ) { printf( '

%s

', esc_html__( 'Agent Lane: the queue table could not be repaired. Ask your host to check that the database user may create tables.', 'agent-lane' ) ); } $record = Queue::failure_record(); if ( empty( $record ) ) { return; } $count = (int) $record['count']; $since = $record['first'] > 0 ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $record['first'] ) : ''; echo '
'; printf( '

%s

', esc_html__( 'Agent Lane cannot store submissions in its queue.', 'agent-lane' ) ); printf( '

%s

', esc_html( sprintf( /* translators: 1: number of submissions, 2: date and time of the first one */ _n( '%1$s submission that should have been held was delivered without being held (first one: %2$s). Until the queue is repaired, everything is delivered, bots included.', '%1$s submissions that should have been held were delivered without being held (first one: %2$s). Until the queue is repaired, everything is delivered, bots included.', $count, 'agent-lane' ), number_format_i18n( $count ), $since ) ) ); if ( '' !== $record['error'] ) { /* translators: %s: database error message */ printf( '

%s

', esc_html( sprintf( __( 'Database error: %s', 'agent-lane' ), $record['error'] ) ) ); } printf( '

%s

', esc_html__( 'To fix it, repair the queue table. If that does not help, ask your host to check that the database user may create tables and that the table uses the utf8mb4 character set.', 'agent-lane' ) ); echo '

'; foreach ( array( self::NONCE_REPAIR => __( 'Repair the queue table', 'agent-lane' ), self::NONCE_DISMISS => __( 'Dismiss', 'agent-lane' ) ) as $action => $label ) { printf( '

', esc_url( admin_url( 'admin-post.php' ) ) ); printf( '', esc_attr( $action ) ); wp_nonce_field( $action ); printf( '
', esc_attr( self::NONCE_REPAIR === $action ? 'button button-primary' : 'button' ), esc_html( $label ) ); } echo '

'; } /** * admin-post handler: creates the queue table for the current site again and checks it. * * @return void */ public function handle_queue_repair() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'agent-lane' ) ); } check_admin_referer( self::NONCE_REPAIR ); $ok = false; try { \AgentLane\Installer::create_table(); $ok = \AgentLane\Installer::table_exists(); } catch ( \Throwable $e ) { $ok = false; } if ( $ok ) { Queue::clear_failure(); } wp_safe_redirect( add_query_arg( 'agentlane_queue', $ok ? 'repaired' : 'repair_failed', self::notice_return_url() ) ); exit; } /** * admin-post handler: clears the failure record. * * @return void */ public function handle_queue_failure_dismiss() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'agent-lane' ) ); } check_admin_referer( self::NONCE_DISMISS ); Queue::clear_failure(); wp_safe_redirect( add_query_arg( 'agentlane_queue', 'dismissed', self::notice_return_url() ) ); exit; } /** * Where Repair and Dismiss return to: the screen the button was on, or the Dashboard. * * @return string */ private static function notice_return_url(): string { $url = wp_get_referer(); if ( ! $url ) { $url = admin_url( 'index.php' ); } return remove_query_arg( array( 'agentlane_queue', 'agentlane_msg', 'agentlane_n' ), $url ); } /** * One-time explanation after the first activation. The plugin changes nothing a * visitor can see, so without this the owner has no sign that it works or where * to look. Shown on the Plugins screen until one of the plugin's own screens was * opened, where it is shown for the last time. * * @param bool $own_screen True on the Queue and Settings screens. * @return void */ private function welcome_notice( $own_screen ) { if ( ! current_user_can( self::CAP ) || ! get_option( 'agentlane_welcome' ) ) { return; } printf( '

%1$s %2$s

%4$s %6$s

', esc_html__( 'Agent Lane is on.', 'agent-lane' ), esc_html__( 'Your visitors will not notice anything, and there is nothing to set up. Form submissions and comments that look automated are held under Agent Lane > Queue instead of being delivered. Once a day you get a short email if something new is waiting there, so a real message held by mistake is not lost.', 'agent-lane' ), esc_url( self::queue_url( Queue::TAB_AUTOMATION ) ), esc_html__( 'Open the queue', 'agent-lane' ), esc_url( self::settings_url() ), esc_html__( 'Settings', 'agent-lane' ) ); if ( $own_screen ) { delete_option( 'agentlane_welcome' ); } } /** * Registers the dashboard widget. * * @return void */ public function dashboard_widget_setup() { if ( ! current_user_can( self::CAP ) ) { return; } wp_add_dashboard_widget( 'agentlane_dashboard', __( 'Agent Lane: last 7 days', 'agent-lane' ), array( $this, 'render_dashboard_widget' ) ); } /** * Dashboard widget body: 7-day counts per class + held now. * * @return void */ public function render_dashboard_widget() { $stats = $this->queue->stats( 7 ); $held = $this->queue->count_held(); $people = (int) $stats['human']; $automation = (int) $stats['total'] - $people; echo '
'; $failure = Queue::failure_record(); if ( ! empty( $failure ) ) { printf( '

%s

', /* translators: %s: number of submissions */ esc_html( sprintf( _n( 'The queue is not storing submissions: %s was delivered without being held.', 'The queue is not storing submissions: %s were delivered without being held.', (int) $failure['count'], 'agent-lane' ), number_format_i18n( (int) $failure['count'] ) ) ) ); } echo '
'; printf( '%2$s%3$s', esc_url( self::queue_url( Queue::TAB_PEOPLE ) ), esc_html( number_format_i18n( $people ) ), esc_html__( 'People', 'agent-lane' ) ); printf( '%2$s%3$s', esc_url( self::queue_url( Queue::TAB_AUTOMATION ) ), esc_html( number_format_i18n( $automation ) ), esc_html__( 'Automation', 'agent-lane' ) ); echo '
'; echo ''; if ( $held > 0 ) { printf( '

%2$s

', esc_url( self::queue_url( Queue::TAB_AUTOMATION ) ), /* translators: %s: number of held submissions */ esc_html( sprintf( _n( '%s submission is waiting for your review', '%s submissions are waiting for your review', $held, 'agent-lane' ), number_format_i18n( $held ) ) ) ); } else { printf( '

%s

', esc_html__( 'Nothing is waiting for review.', 'agent-lane' ) ); } echo '
'; } /* ------------------------------------------------------------------ */ /* Settings API */ /* ------------------------------------------------------------------ */ /** * Registers the option, sections and fields. * * @return void */ public function register_settings() { $this->register_option(); $page = self::PAGE_SETTINGS; /* Scoring */ add_settings_section( 'agentlane_scoring', __( 'Scoring', 'agent-lane' ), array( $this, 'section_scoring' ), $page ); $this->add_field( 'queue_threshold', __( 'Queue threshold', 'agent-lane' ), 'agentlane_scoring', array( 'type' => 'number', 'min' => 1, 'max' => 100, 'desc' => __( 'Submissions scoring at or above this value go to the Automation queue. 0 = certainly a person, 100 = certainly automated. Default 55.', 'agent-lane' ), ) ); $this->add_field( 'block_threshold', __( 'Block threshold', 'agent-lane' ), 'agentlane_scoring', array( 'type' => 'number', 'min' => 1, 'max' => 100, 'desc' => __( 'Submissions scoring at or above this value are rejected with the block message and kept for 7 days. Default 90.', 'agent-lane' ), ) ); /* Form fields */ add_settings_section( 'agentlane_fields', __( 'Hidden form fields', 'agent-lane' ), array( $this, 'section_fields' ), $page ); $this->add_field( 'honeypot_field_name', __( 'Honeypot field name', 'agent-lane' ), 'agentlane_fields', array( 'type' => 'honeypot' ) ); $this->add_field( 'token_min_age', __( 'Minimum form age (seconds)', 'agent-lane' ), 'agentlane_fields', array( 'type' => 'number', 'min' => 0, 'max' => 60, 'desc' => __( 'A submission sent sooner than this after the page was rendered counts as scripted. Default 3.', 'agent-lane' ), ) ); $this->add_field( 'token_max_age', __( 'Maximum form age (seconds)', 'agent-lane' ), 'agentlane_fields', array( 'type' => 'number', 'min' => 600, 'max' => 86400, 'desc' => __( 'After this many seconds the page token expires. Between 600 (10 minutes) and 86400 (24 hours). Default 86400.', 'agent-lane' ), ) ); $this->add_field( 'bind_token_to_client', __( 'Tie the token to the visitor', 'agent-lane' ), 'agentlane_fields', array( 'type' => 'checkbox', 'label' => __( 'Only accept the page token from the same network range and browser family that loaded the page', 'agent-lane' ), 'desc' => __( 'Uses the first half of the IP address and the browser family (Chrome, Firefox, Safari...). A token carried from one visitor to another counts as invalid. Nothing extra is stored. Default on.', 'agent-lane' ), 'note' => __( 'Works with a full-page cache (WP Super Cache, W3 Total Cache, LiteSpeed Cache, Cloudflare APO, host-level caching): when a cached page is shown, the page script fetches a fresh token for the visitor in the background on the first interaction with a form. That needs JavaScript and a reachable admin-ajax.php. If your site blocks admin-ajax.php for visitors, turn this off: a cached page then carries one token for every visitor, and with this on most of them would score as "invalid token".', 'agent-lane' ), ) ); /* Privacy */ add_settings_section( 'agentlane_privacy', __( 'Privacy and retention', 'agent-lane' ), array( $this, 'section_privacy' ), $page ); $this->add_field( 'store_full_ip', __( 'Store full IP address', 'agent-lane' ), 'agentlane_privacy', array( 'type' => 'checkbox', 'label' => __( 'Keep the complete IP address of each submission', 'agent-lane' ), 'desc' => __( 'Off by default: the last octet (IPv4) or the last 64 bits (IPv6) are zeroed before storing.', 'agent-lane' ), ) ); $this->add_field( 'retention_days', __( 'Keep held submissions for (days)', 'agent-lane' ), 'agentlane_privacy', array( 'type' => 'number', 'min' => 0, 'max' => 3650, 'desc' => __( 'Automation queue items older than this are removed by a daily cleanup. 0 = keep forever. Default 30.', 'agent-lane' ), ) ); /* Agents */ add_settings_section( 'agentlane_agents', __( 'AI agents', 'agent-lane' ), array( $this, 'section_agents' ), $page ); $this->add_field( 'verify_signed_agents', __( 'Verify signed agents', 'agent-lane' ), 'agentlane_agents', array( 'type' => 'checkbox', 'label' => __( 'Verify Web Bot Auth signatures from agents such as ChatGPT agent', 'agent-lane' ), 'desc' => __( 'When enabled, the plugin fetches public signing keys from the agent operator\'s website to verify signatures', 'agent-lane' ), 'note' => __( 'This is the only outbound request the plugin ever makes. Keys are cached for 24 hours. Off by default.', 'agent-lane' ), ) ); $this->add_field( 'declared_agent_action', __( 'Declared and verified agents', 'agent-lane' ), 'agentlane_agents', array( 'type' => 'select', 'options' => self::policy_options(), 'desc' => __( 'What to do with submissions from agents that state their purpose or carry a valid signature from an operator in the agent directory. Per-agent rules below apply to verified signatures only.', 'agent-lane' ), 'note' => __( 'Allow delivers every submission that fills the hidden agent field, including spam bots that found it. Submissions that also look scripted are still held.', 'agent-lane' ), ) ); $this->add_field( 'policy_overrides', __( 'Per-agent policy', 'agent-lane' ), 'agentlane_agents', array( 'type' => 'policy_table' ) ); /* Messages */ add_settings_section( 'agentlane_messages', __( 'Messages and notifications', 'agent-lane' ), array( $this, 'section_messages' ), $page ); $this->add_field( 'block_message', __( 'Block message', 'agent-lane' ), 'agentlane_messages', array( 'type' => 'textarea', 'desc' => __( 'Shown to blocked submitters in place of the form plugin\'s success message.', 'agent-lane' ), ) ); $this->add_field( 'notify_email', __( 'Notification email', 'agent-lane' ), 'agentlane_messages', array( 'type' => 'email', 'desc' => __( 'Released submissions are sent here. Empty = site admin email.', 'agent-lane' ), ) ); $this->add_field( 'notify_on_release', __( 'Email on release', 'agent-lane' ), 'agentlane_messages', array( 'type' => 'checkbox', 'label' => __( 'Send the full submission by email when I release it from the Automation queue', 'agent-lane' ), ) ); $this->add_field( 'notify_on_hold', __( 'Daily summary', 'agent-lane' ), 'agentlane_messages', array( 'type' => 'checkbox', 'label' => __( 'Email me once a day when new submissions are waiting in the Automation queue', 'agent-lane' ), 'desc' => __( 'One short email with the number of held submissions and a link to the queue. Nothing is sent on days when nothing new was held. Blocked submissions are not counted.', 'agent-lane' ), ) ); } /** * Registers the option exactly once. Prefers AgentLane\Settings::register() * (WP1, hooked on admin_init priority 10, own sanitizer); falls back to a * local register_setting() with sanitize_settings() when Settings is absent. * * @return void */ private function register_option() { global $wp_registered_settings; if ( is_array( $wp_registered_settings ) && isset( $wp_registered_settings[ self::OPTION ] ) ) { return; } $settings = self::settings_instance(); if ( $settings && method_exists( $settings, 'register' ) ) { $settings->register(); return; } register_setting( self::option_group(), self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => self::DEFAULTS, ) ); } /** * Settings group for settings_fields(): AgentLane\Settings::GROUP when defined. * * @return string */ public static function option_group(): string { if ( class_exists( 'AgentLane\Settings' ) && defined( 'AgentLane\Settings::GROUP' ) ) { return (string) constant( 'AgentLane\Settings::GROUP' ); } return self::OPTION_GROUP; } /** * add_settings_field wrapper with a shared renderer. * * @param string $key Setting key. * @param string $title Field label. * @param string $section Section id. * @param array $args Renderer args. * @return void */ private function add_field( $key, $title, $section, array $args ) { $args['key'] = $key; $args['label_for'] = ( 'policy_table' === $args['type'] || 'honeypot' === $args['type'] ) ? '' : 'agentlane_' . $key; add_settings_field( 'agentlane_' . $key, $title, array( $this, 'render_field' ), self::PAGE_SETTINGS, $section, $args ); } /** Section intro: scoring. */ public function section_scoring() { echo '

' . esc_html__( 'Every submission gets a score from 0 to 100 built from request signals and the hidden fields. Thresholds decide where it goes.', 'agent-lane' ) . '

'; } /** Section intro: fields. */ public function section_fields() { echo '

' . esc_html__( 'Agent Lane adds a page token, a honeypot and an agent-purpose field to supported forms. People never see them.', 'agent-lane' ) . '

'; } /** Section intro: privacy. */ public function section_privacy() { echo '

' . esc_html__( 'Nothing is sent to third parties. Held submissions stay in your database until released, deleted or expired.', 'agent-lane' ) . '

'; } /** Section intro: agents. */ public function section_agents() { echo '

' . esc_html__( 'Honest AI agents can declare themselves through the agent-purpose field or a Web Bot Auth signature. Decide what happens to them.', 'agent-lane' ) . '

'; } /** Section intro: messages. */ public function section_messages() { echo '

' . esc_html__( 'What blocked submitters see, and where released submissions are sent.', 'agent-lane' ) . '

'; } /** * Renders one settings field by type. * * @param array $args Field args (key, type, ...). * @return void */ public function render_field( array $args ) { $key = (string) $args['key']; $type = (string) $args['type']; $id = 'agentlane_' . $key; $name = self::OPTION . '[' . $key . ']'; $value = $this->get_setting( $key ); switch ( $type ) { case 'number': printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) (int) $value ), (int) $args['min'], (int) $args['max'] ); break; case 'checkbox': // Hidden 0 first so the key is always posted and unchecking works through the sanitizer. printf( '', esc_attr( $name ) ); printf( '', esc_attr( $id ), esc_attr( $name ), checked( (bool) $value, true, false ), esc_html( (string) ( $args['label'] ?? '' ) ) ); break; case 'select': printf( ''; break; case 'textarea': printf( '', esc_attr( $id ), esc_attr( $name ), esc_textarea( (string) $value ) ); break; case 'email': // The administrator email is the default: show it as the placeholder, not as a value // that a save would pin. if ( 0 === strcasecmp( (string) $value, (string) get_option( 'admin_email' ) ) ) { $value = ''; } printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ), esc_attr( (string) get_option( 'admin_email' ) ) ); break; case 'honeypot': $this->render_honeypot_field( (string) $value ); break; case 'policy_table': $this->render_policy_table( is_array( $value ) ? $value : array() ); break; } if ( ! empty( $args['desc'] ) ) { printf( '

%s

', esc_html( (string) $args['desc'] ) ); } if ( ! empty( $args['note'] ) ) { printf( '

%s

', esc_html( (string) $args['note'] ) ); } } /** * Read-only honeypot name with a nonce-protected Regenerate link. * * @param string $value Current name. * @return void */ private function render_honeypot_field( $value ) { $regen_url = wp_nonce_url( admin_url( 'admin-post.php?action=' . self::NONCE_REGEN ), self::NONCE_REGEN ); printf( '%1$s %3$s', esc_html( '' !== $value ? $value : __( '(not set yet)', 'agent-lane' ) ), esc_url( $regen_url ), esc_html__( 'Regenerate', 'agent-lane' ) ); printf( '

%s

', esc_html__( 'A random name generated for this site. Regenerate it if bots start skipping the honeypot. Existing cached pages keep the old name until the cache clears.', 'agent-lane' ) ); } /** * Per-agent policy table from AgentLane\AgentDirectory::all(). * * @param array $overrides agent_id => allow|queue|block. * @return void */ private function render_policy_table( array $overrides ) { $agents = self::directory_agents(); if ( empty( $agents ) ) { printf( '

%s

', esc_html__( 'The agent directory is not loaded, so per-agent rules are not available. The setting above applies to all agents.', 'agent-lane' ) ); return; } $options = self::policy_options(); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ( $agents as $agent ) { $agent_id = sanitize_key( (string) ( $agent['id'] ?? '' ) ); if ( '' === $agent_id ) { continue; } $default = (string) ( $agent['default_policy'] ?? 'queue' ); $current = isset( $overrides[ $agent_id ] ) ? (string) $overrides[ $agent_id ] : ''; $name = self::OPTION . '[policy_overrides][' . $agent_id . ']'; $id = 'agentlane_policy_' . $agent_id; echo ''; printf( '', esc_attr( $id ), esc_html( (string) ( $agent['name'] ?? $agent_id ) ) ); printf( '', esc_html( (string) ( $agent['operator'] ?? '' ) ) ); printf( '', esc_html( (string) ( $agent['category'] ?? '' ) ) ); printf( '', esc_html( isset( $options[ $default ] ) ? $options[ $default ] : $default ) ); printf( ''; echo ''; } echo '
' . esc_html__( 'Agent', 'agent-lane' ) . '' . esc_html__( 'Operator', 'agent-lane' ) . '' . esc_html__( 'Category', 'agent-lane' ) . '' . esc_html__( 'Default', 'agent-lane' ) . '' . esc_html__( 'Your rule', 'agent-lane' ) . '
%s%s%s
'; printf( '

%s

', esc_html__( 'Allow = deliver as usual. Queue = hold in the Automation tab. Block = reject with the block message.', 'agent-lane' ) ); } /** * Sanitizes the whole settings array. Keys that are not posted keep their * current value, so programmatic Settings::set() calls and partial forms * are safe. * * @param mixed $input Raw submitted array. * @return array */ public function sanitize_settings( $input ) { $current = $this->get_all_settings(); if ( ! is_array( $input ) ) { return $current; } $out = $current; $ints = array( 'queue_threshold' => array( 1, 100 ), 'block_threshold' => array( 1, 100 ), 'token_min_age' => array( 0, 60 ), 'token_max_age' => array( 600, 86400 ), 'retention_days' => array( 0, 3650 ), ); foreach ( $ints as $key => $range ) { if ( array_key_exists( $key, $input ) ) { $out[ $key ] = max( $range[0], min( $range[1], (int) $input[ $key ] ) ); } } if ( (int) $out['block_threshold'] < (int) $out['queue_threshold'] ) { $out['block_threshold'] = (int) $out['queue_threshold']; add_settings_error( self::OPTION, 'agentlane_thresholds', __( 'The block threshold cannot be lower than the queue threshold. It was raised to match.', 'agent-lane' ), 'warning' ); } foreach ( array( 'bind_token_to_client', 'store_full_ip', 'verify_signed_agents', 'notify_on_release', 'notify_on_hold' ) as $key ) { if ( array_key_exists( $key, $input ) ) { $out[ $key ] = self::to_bool( $input[ $key ] ); } } if ( array_key_exists( 'declared_agent_action', $input ) ) { $action = sanitize_key( (string) $input['declared_agent_action'] ); $out['declared_agent_action'] = isset( self::policy_options()[ $action ] ) ? $action : 'queue'; } if ( array_key_exists( 'policy_overrides', $input ) ) { $overrides = array(); if ( is_array( $input['policy_overrides'] ) ) { foreach ( $input['policy_overrides'] as $agent_id => $policy ) { $agent_id = sanitize_key( (string) $agent_id ); $policy = sanitize_key( (string) $policy ); if ( '' !== $agent_id && isset( self::policy_options()[ $policy ] ) ) { $overrides[ $agent_id ] = $policy; } } } $out['policy_overrides'] = $overrides; } if ( array_key_exists( 'block_message', $input ) ) { $message = sanitize_textarea_field( (string) $input['block_message'] ); if ( '' === trim( $message ) ) { $message = self::DEFAULTS['block_message']; } $out['block_message'] = mb_substr( $message, 0, 500 ); } if ( array_key_exists( 'notify_email', $input ) ) { $email = sanitize_email( (string) $input['notify_email'] ); if ( '' === $email || 0 === strcasecmp( $email, (string) get_option( 'admin_email' ) ) ) { // Empty = the administrator email at send time, so a later change of that email is followed. $out['notify_email'] = ''; } elseif ( is_email( $email ) ) { $out['notify_email'] = $email; } else { add_settings_error( self::OPTION, 'agentlane_email', __( 'The notification email is not valid. The previous value was kept.', 'agent-lane' ), 'error' ); } } // Never editable from the form; accepted from code (installer, regenerate). if ( array_key_exists( 'honeypot_field_name', $input ) ) { $hp = sanitize_key( (string) $input['honeypot_field_name'] ); if ( strlen( $hp ) >= 3 && strlen( $hp ) <= 32 ) { $out['honeypot_field_name'] = $hp; } } return $out; } /* ------------------------------------------------------------------ */ /* Settings access (through AgentLane\Settings when available) */ /* ------------------------------------------------------------------ */ /** * Reads one setting. * * @param string $key Setting key. * @return mixed */ public function get_setting( $key ) { $all = $this->get_all_settings(); return array_key_exists( $key, $all ) ? $all[ $key ] : null; } /** * Reads all settings merged over defaults. * * @return array */ public function get_all_settings(): array { $defaults = self::DEFAULTS; $defaults['notify_email'] = (string) get_option( 'admin_email' ); $stored = array(); $settings = self::settings_instance(); if ( $settings && method_exists( $settings, 'all' ) ) { $stored = $settings->all(); } else { $stored = get_option( self::OPTION, array() ); } if ( ! is_array( $stored ) ) { $stored = array(); } return array_merge( $defaults, $stored ); } /** * AgentLane\Settings singleton (WP1) or null when the class is absent. * * @return object|null */ private static function settings_instance() { $class = '\AgentLane\Settings'; if ( ! class_exists( $class ) || ! method_exists( $class, 'instance' ) ) { return null; } $instance = $class::instance(); return is_object( $instance ) ? $instance : null; } /** * Writes one setting. * * @param string $key Setting key. * @param mixed $value Value. * @return void */ private function set_setting( $key, $value ) { $settings = self::settings_instance(); if ( $settings && method_exists( $settings, 'set' ) ) { $settings->set( $key, $value ); return; } $stored = get_option( self::OPTION, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } $stored[ $key ] = $value; update_option( self::OPTION, $stored ); } /* ------------------------------------------------------------------ */ /* Helpers */ /* ------------------------------------------------------------------ */ /** * Policy labels. * * @return array */ public static function policy_options(): array { return array( 'allow' => __( 'Allow', 'agent-lane' ), 'queue' => __( 'Queue', 'agent-lane' ), 'block' => __( 'Block', 'agent-lane' ), ); } /** * Agent records from AgentLane\AgentDirectory::all() (static or instance), or []. * * @return array */ private static function directory_agents(): array { $class = '\AgentLane\AgentDirectory'; if ( ! class_exists( $class ) || ! method_exists( $class, 'all' ) ) { return array(); } try { $method = new \ReflectionMethod( $class, 'all' ); $agents = $method->isStatic() ? $class::all() : ( new $class() )->all(); } catch ( \Throwable $e ) { return array(); } return is_array( $agents ) ? array_values( array_filter( $agents, 'is_array' ) ) : array(); } /** * True when at least one supported form plugin is active. * * @return bool */ private static function form_plugin_detected(): bool { return class_exists( 'WPCF7' ) || function_exists( 'wpforms' ) || class_exists( 'GFForms' ) || defined( 'ELEMENTOR_PRO_VERSION' ); } /** * Loose boolean from a posted value. * * @param mixed $value Raw value. * @return bool */ private static function to_bool( $value ): bool { if ( is_string( $value ) ) { return in_array( strtolower( $value ), array( '1', 'true', 'yes', 'on' ), true ); } return (bool) $value; } /** * URL of the queue page for a tab. * * @param string $tab people|automation. * @return string */ public static function queue_url( $tab = Queue::TAB_AUTOMATION ): string { return add_query_arg( array( 'page' => self::PAGE_QUEUE, 'tab' => ( Queue::TAB_PEOPLE === $tab ) ? Queue::TAB_PEOPLE : Queue::TAB_AUTOMATION, ), admin_url( 'admin.php' ) ); } /** * URL of the settings page. * * @return string */ public static function settings_url(): string { return add_query_arg( 'page', self::PAGE_SETTINGS, admin_url( 'admin.php' ) ); } /** * Public URL of a file inside the plugin. Uses the constants from * agent-lane.php (WP1); the last fallback assumes admin/ sits next to it. * * @param string $rel Relative path. * @return string */ private static function plugin_url( $rel ): string { if ( defined( 'AGENTLANE_PLUGIN_URL' ) ) { return trailingslashit( AGENTLANE_PLUGIN_URL ) . ltrim( $rel, '/' ); } if ( defined( 'AGENTLANE_PLUGIN_FILE' ) ) { return plugins_url( $rel, AGENTLANE_PLUGIN_FILE ); } return plugins_url( $rel, dirname( __DIR__ ) . '/agent-lane.php' ); } /** * Plugin version for cache busting. * * @return string */ private static function version(): string { return defined( 'AGENTLANE_VERSION' ) ? (string) AGENTLANE_VERSION : '0.1.0'; } }