array( 'label' => __( 'AI training', 'aeoora' ), 'desc' => __( 'Collects pages to train AI models.', 'aeoora' ), 'color' => '#8b5cf6', 'ai' => true, ), 'ai_search' => array( 'label' => __( 'AI search', 'aeoora' ), 'desc' => __( 'Builds the index AI answer engines cite from.', 'aeoora' ), 'color' => '#635bff', 'ai' => true, ), 'ai_assistant' => array( 'label' => __( 'AI assistant (live)', 'aeoora' ), 'desc' => __( 'Fetches a page live because a user asked an AI assistant.', 'aeoora' ), 'color' => '#10b981', 'ai' => true, ), 'ai_agent' => array( 'label' => __( 'AI agent', 'aeoora' ), 'desc' => __( 'Autonomous AI agents browsing on behalf of a user.', 'aeoora' ), 'color' => '#06b6d4', 'ai' => true, ), 'search_engine' => array( 'label' => __( 'Search engine', 'aeoora' ), 'desc' => __( 'Classic search engine crawlers (they also feed AI Overviews and Copilot).', 'aeoora' ), 'color' => '#3b82f6', 'ai' => false, ), 'seo' => array( 'label' => __( 'SEO tool', 'aeoora' ), 'desc' => __( 'Backlink and SEO analysis crawlers.', 'aeoora' ), 'color' => '#f59e0b', 'ai' => false, ), 'social' => array( 'label' => __( 'Social & previews', 'aeoora' ), 'desc' => __( 'Link preview fetchers for social networks and messengers.', 'aeoora' ), 'color' => '#ec4899', 'ai' => false, ), 'monitoring' => array( 'label' => __( 'Monitoring', 'aeoora' ), 'desc' => __( 'Uptime and performance monitors.', 'aeoora' ), 'color' => '#64748b', 'ai' => false, ), 'archive' => array( 'label' => __( 'Archive', 'aeoora' ), 'desc' => __( 'Web archives.', 'aeoora' ), 'color' => '#a16207', 'ai' => false, ), 'feed' => array( 'label' => __( 'Feed reader', 'aeoora' ), 'desc' => __( 'RSS and feed readers.', 'aeoora' ), 'color' => '#f97316', 'ai' => false, ), 'scraper' => array( 'label' => __( 'Scraper / HTTP tool', 'aeoora' ), 'desc' => __( 'Generic HTTP libraries, headless browsers and scraping tools.', 'aeoora' ), 'color' => '#ef4444', 'ai' => false, ), 'other' => array( 'label' => __( 'Other bot', 'aeoora' ), 'desc' => __( 'Other or unidentified automated agents.', 'aeoora' ), 'color' => '#94a3b8', 'ai' => false, ), ); } /** * Is a category an AI category? * * @param string $category Category. * @return bool */ public static function is_ai_category( $category ) { return in_array( $category, array( 'ai_training', 'ai_search', 'ai_assistant', 'ai_agent' ), true ); } /** * All bots, keyed by id, in detection order. * * @return array */ public static function all() { if ( null !== self::$bots ) { return self::$bots; } $list = array(); $custom = get_option( 'aeoora_custom_bots', array() ); foreach ( (array) $custom as $bot ) { if ( ! is_array( $bot ) || empty( $bot['id'] ) || empty( $bot['pattern'] ) ) { continue; } $bot['custom'] = true; $compiles = self::compiles( (string) $bot['pattern'] ); if ( isset( $bot['regex'] ) ) { $bot['regex'] = (bool) $bot['regex'] && $compiles; $bot['either'] = false; } else { // Saved by 1.0.0 (no type chosen): match as text first, then as a // regular expression when it is one, so "Bot (compatible; X)" works. $bot['regex'] = false; $bot['either'] = $compiles && preg_quote( (string) $bot['pattern'], '~' ) !== $bot['pattern']; } // A pattern that matches ordinary browsers would treat every visitor // as a bot (and, if blocked, lock everyone out): it never matches. // The entry stays listed so the owner can see and delete it. if ( self::matches_browser( $bot ) ) { $bot['pattern'] = ''; $bot['desc'] = __( 'Turned off: its pattern also matches ordinary web browsers. Delete it and add it again with text only this bot sends.', 'aeoora' ); } $list[ $bot['id'] ] = $bot; } $data = include AEOORA_DIR . 'data/bots.php'; foreach ( (array) $data as $bot ) { if ( empty( $bot['id'] ) || isset( $list[ $bot['id'] ] ) ) { continue; } $list[ $bot['id'] ] = $bot; } foreach ( $list as $id => $bot ) { $list[ $id ] = wp_parse_args( $bot, array( 'id' => $id, 'name' => $id, 'token' => null, 'pattern' => '', 'operator' => '', 'domain' => '', 'category' => 'other', 'robots' => null, 'docs' => '', 'desc' => '', 'regex' => false, 'either' => false, ) ); } self::$bots = apply_filters( 'aeoora_bots', $list ); return self::$bots; } /** * Is a pattern a valid regular expression? * * @param string $pattern Pattern. * @return bool */ private static function compiles( $pattern ) { return false !== @preg_match( '~' . str_replace( '~', '\~', $pattern ) . '~i', '' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors } /** * Does a bot entry match a user agent? * * @param array $bot Bot entry (pattern, regex, either). * @param string $ua User agent. * @return bool */ public static function matches( array $bot, $ua ) { $pattern = isset( $bot['pattern'] ) ? (string) $bot['pattern'] : ''; if ( '' === $pattern || '' === (string) $ua ) { return false; } $re = '~' . str_replace( '~', '\~', $pattern ) . '~i'; if ( ! empty( $bot['regex'] ) ) { return (bool) @preg_match( $re, $ua ); // phpcs:ignore WordPress.PHP.NoSilencedErrors } if ( false !== stripos( $ua, $pattern ) ) { return true; } return ! empty( $bot['either'] ) && (bool) @preg_match( $re, $ua ); // phpcs:ignore WordPress.PHP.NoSilencedErrors } /** * User agents of ordinary browsers that no bot pattern may match. * * @return string[] */ public static function browser_samples() { return array( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36', 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1', 'Mozilla/5.0 (Linux; Android 14; SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36', 'Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15', ); } /** * Would a bot entry match an ordinary browser? * * @param array $bot Bot entry. * @return bool */ private static function matches_browser( array $bot ) { foreach ( self::browser_samples() as $ua ) { if ( self::matches( $bot, $ua ) ) { return true; } } return false; } /** * Check a custom bot pattern before it is saved. * * @param string $pattern Pattern. * @param bool $regex Regular expression (else plain text). * @return true|WP_Error */ public static function validate_pattern( $pattern, $regex ) { $pattern = (string) $pattern; if ( strlen( $pattern ) < 3 ) { return new WP_Error( 'aeoora_bad_pattern', __( 'The user agent text is too short: use at least 3 characters, such as the bot’s name.', 'aeoora' ), array( 'status' => 400 ) ); } if ( $regex && ! self::compiles( $pattern ) ) { return new WP_Error( 'aeoora_bad_pattern', __( 'That is not a valid regular expression.', 'aeoora' ), array( 'status' => 400 ) ); } $bot = array( 'pattern' => $pattern, 'regex' => (bool) $regex, ); if ( self::matches_browser( $bot ) ) { return new WP_Error( 'aeoora_bad_pattern', __( 'That pattern also matches ordinary web browsers, so every visitor would count as this bot. Use text that only this bot sends.', 'aeoora' ), array( 'status' => 400 ) ); } return true; } /** * One bot by id. * * @param string $id Bot id. * @return array|null */ public static function get( $id ) { $all = self::all(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** * Detect a bot from a User-Agent string. * * @param string $ua User agent. * @return array|null Bot definition or null for humans/unknown. */ public static function detect( $ua ) { $ua = (string) $ua; if ( '' === $ua ) { return null; } foreach ( self::all() as $bot ) { $pattern = $bot['pattern']; if ( '' === $pattern ) { continue; } if ( $bot['regex'] ) { $re = '~' . str_replace( '~', '\~', $pattern ) . '~i'; if ( @preg_match( $re, $ua ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors return $bot; } } elseif ( false !== stripos( $ua, $pattern ) || ( ! empty( $bot['either'] ) && self::matches( $bot, $ua ) ) ) { return $bot; } } // Generic fallback for self-declared bots that are not in the registry. if ( preg_match( '~(bot|crawler|spider|crawl|slurp|scraper|fetcher|httpclient|headless)\b~i', $ua ) && ! preg_match( '~cubot~i', $ua ) ) { return array( 'id' => 'unknown-bot', 'name' => __( 'Unidentified bot', 'aeoora' ), 'token' => null, 'pattern' => '', 'operator' => '', 'domain' => '', 'category' => 'other', 'robots' => null, 'docs' => '', 'desc' => '', 'regex' => false, 'unknown' => true, ); } return null; } /** * Detect the bot of the current request (cached). * * @return array|null */ public static function current() { if ( null === self::$current ) { $bot = self::detect( Aeoora_Util::user_agent() ); // Agents that sign requests (Web Bot Auth) instead of using a distinct // User-Agent. The header is not verified, so it only applies when the // User-Agent itself names no bot: it never overrides a detected (or // blocked) crawler. if ( ! $bot ) { $sig = isset( $_SERVER['HTTP_SIGNATURE_AGENT'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_SIGNATURE_AGENT'] ) ) ) : ''; if ( '' !== $sig && false !== strpos( $sig, 'chatgpt.com' ) ) { $bot = self::get( 'chatgpt-agent' ); } } self::$current = $bot ? $bot : false; } return self::$current ? self::$current : null; } /** * Bots that have a robots.txt token (can be controlled via robots.txt). * * @return array */ public static function controllable() { return array_filter( self::all(), static function ( $bot ) { return ! empty( $bot['token'] ); } ); } /** * Policy of a bot: allow | disallow | block. * * @param string $id Bot id. * @return string */ public static function policy( $id ) { $policies = (array) Aeoora_Settings::get( 'robots', 'policies', array() ); return isset( $policies[ $id ] ) ? $policies[ $id ] : 'allow'; } /** * Generic HTTP clients and browser engines. They identify a tool, not an * actor: payment webhooks, mobile apps, headless front-ends, monitors and * integrations use them too, so presets never block them. * * @param string $id Bot id. * @return bool */ public static function is_library( $id ) { $libs = apply_filters( 'aeoora_http_libraries', array( 'python-requests', 'python-urllib', 'aiohttp', 'python-httpx', 'curl', 'wget', 'go-http-client', 'java-http-client', 'apache-httpclient', 'java', 'okhttp', 'libwww-perl', 'mechanize', 'axios', 'node-fetch', 'guzzlehttp', 'ruby', 'faraday', 'dart', 'deno', 'postmanruntime', 'headlesschrome', 'lightpanda', 'cloudflarebrowserrenderingcrawler' ) ); return in_array( $id, (array) $libs, true ); } /** * Policy presets. * * @return array */ public static function presets() { return array( 'visibility' => array( 'label' => __( 'Maximum AI visibility', 'aeoora' ), 'desc' => __( 'Allow every AI and search crawler. Best for getting cited.', 'aeoora' ), ), 'no_training' => array( 'label' => __( 'Get cited, don’t train', 'aeoora' ), 'desc' => __( 'Allow AI search engines and assistants, disallow AI training crawlers.', 'aeoora' ), ), 'block_ai' => array( 'label' => __( 'Block all AI', 'aeoora' ), 'desc' => __( 'Disallow every AI crawler in robots.txt. Search engines stay allowed.', 'aeoora' ), ), 'block_scrapers' => array( 'label' => __( 'Stop scrapers', 'aeoora' ), 'desc' => __( 'Keep AI allowed, but block dedicated scraping tools and AI trainers that ignore robots.txt. General-purpose HTTP tools are never blocked.', 'aeoora' ), ), ); } /** * Compute policies for a preset. * * @param string $preset Preset id. * @return array id => policy */ public static function preset_policies( $preset ) { $policies = (array) Aeoora_Settings::get( 'robots', 'policies', array() ); foreach ( self::all() as $id => $bot ) { $cat = $bot['category']; switch ( $preset ) { case 'visibility': if ( self::is_ai_category( $cat ) || 'search_engine' === $cat ) { unset( $policies[ $id ] ); } break; case 'no_training': if ( 'ai_training' === $cat && ! empty( $bot['token'] ) ) { $policies[ $id ] = 'disallow'; } elseif ( in_array( $cat, array( 'ai_search', 'ai_assistant', 'ai_agent', 'search_engine' ), true ) ) { unset( $policies[ $id ] ); } break; case 'block_ai': if ( self::is_ai_category( $cat ) && ! empty( $bot['token'] ) ) { $policies[ $id ] = 'disallow'; } break; case 'block_scrapers': // Dedicated scraping tools only — never generic HTTP libraries, // which also power webhooks, mobile apps and integrations. if ( self::is_library( $id ) ) { break; } if ( 'scraper' === $cat ) { $policies[ $id ] = ( true === $bot['robots'] && ! empty( $bot['token'] ) ) ? 'disallow' : 'block'; } elseif ( 'ai_training' === $cat && false === $bot['robots'] ) { $policies[ $id ] = 'block'; } break; } } return $policies; } /** * Public (JS-safe) representation of the registry. * * @return array */ public static function for_js() { $out = array(); foreach ( self::all() as $id => $bot ) { $out[] = array( 'id' => $id, 'name' => $bot['name'], 'token' => $bot['token'], 'operator' => $bot['operator'], 'domain' => $bot['domain'], 'icon' => Aeoora_Favicons::url( $bot['domain'] ), 'category' => $bot['category'], 'robots' => $bot['robots'], 'docs' => $bot['docs'], 'desc' => $bot['desc'], 'policy' => self::policy( $id ), 'custom' => ! empty( $bot['custom'] ), 'library' => self::is_library( $id ), ); } return $out; } }