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
%s
%s
%s
%s
%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_attr( self::NONCE_REPAIR === $action ? 'button button-primary' : 'button' ), esc_html( $label ) ); } 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 '| ' . esc_html__( 'Agent', 'agent-lane' ) . ' | '; echo '' . esc_html__( 'Operator', 'agent-lane' ) . ' | '; echo '' . esc_html__( 'Category', 'agent-lane' ) . ' | '; echo '' . esc_html__( 'Default', 'agent-lane' ) . ' | '; echo '' . esc_html__( 'Your rule', 'agent-lane' ) . ' | '; echo '
|---|---|---|---|---|
| ', esc_attr( $id ), esc_html( (string) ( $agent['name'] ?? $agent_id ) ) ); printf( ' | %s | ', esc_html( (string) ( $agent['operator'] ?? '' ) ) ); printf( '%s | ', esc_html( (string) ( $agent['category'] ?? '' ) ) ); printf( '%s | ', esc_html( isset( $options[ $default ] ) ? $options[ $default ] : $default ) ); printf( ''; echo ' |
%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'; } }