records = self::normalize_records( $source ); return; } $path = ( is_string( $source ) && '' !== $source ) ? $source : self::default_path(); $this->load_file( $path ); } /** * Bundled data file. * * @return string */ public static function default_path(): string { return dirname( __DIR__ ) . '/assets/data/agents.json'; } /** * Categories that mean "automation tool", not "named agent". * * @return string[] */ public static function automation_categories(): array { return array( self::CATEGORY_HEADLESS, self::CATEGORY_HTTP_CLIENT ); } /** * Whether a category is a headless/scripted marker rather than an agent. * * @param string $category Category string. * @return bool */ public static function is_automation_category( string $category ): bool { return in_array( $category, self::automation_categories(), true ); } /** * Match by Signature-Agent first, then by User-Agent. * * @param RequestContext $ctx Request. * @return array|null Record plus 'matched_by' and 'matched_value', or null. */ public function match( RequestContext $ctx ): ?array { if ( is_string( $ctx->sig_agent ) && '' !== trim( $ctx->sig_agent ) ) { $record = $this->match_signature_agent( $ctx->sig_agent ); if ( null !== $record ) { return $record; } } $ua = is_string( $ctx->ua ) ? $ctx->ua : ''; if ( '' !== $ua ) { return $this->match_ua( $ua ); } return null; } /** * Match a Signature-Agent value ("https://chatgpt.com", agent.bot.goog ...). * * @param string $value Raw header value or host. * @return array|null */ public function match_signature_agent( string $value ): ?array { $host = self::normalize_agent_host( $value ); if ( '' === $host ) { return null; } foreach ( $this->records as $record ) { if ( '' !== $record['signature_host'] && $record['signature_host'] === $host ) { return self::with_match( $record, self::MATCHED_BY_SIGNATURE, $host ); } } return null; } /** * Match a User-Agent string. Named agents win over headless markers when * both appear in one UA. * * @param string $ua User agent. * @return array|null */ public function match_ua( string $ua ): ?array { $record = $this->match_ua_in_categories( $ua, false ); if ( null !== $record ) { return $record; } return $this->match_ua_in_categories( $ua, true ); } /** * Match only headless / scripted-client markers. * * @param string $ua User agent. * @return array|null */ public function match_headless( string $ua ): ?array { return $this->match_ua_in_categories( $ua, true ); } /** * Record by id. * * @param string $id Agent id. * @return array|null */ public function get( string $id ): ?array { return isset( $this->records[ $id ] ) ? $this->records[ $id ] : null; } /** * All records as a list (for the settings page policy table). * * @return array */ public function all(): array { return array_values( $this->records ); } /** * Records of one category. * * @param string $category Category. * @return array */ public function by_category( string $category ): array { $out = array(); foreach ( $this->records as $record ) { if ( $record['category'] === $category ) { $out[] = $record; } } return $out; } /** * Number of records. * * @return int */ public function count(): int { return count( $this->records ); } /** * Load error text or null. * * @return string|null */ public function load_error(): ?string { return $this->load_error; } /** * Where the data came from. * * @return string */ public function source_path(): string { return $this->source_path; } /** * Normalize a Signature-Agent value or URL to a bare lowercase host. * Accepts "https://chatgpt.com" (quoted sf-string), https://x.y/path, x.y. * * @param string $value Raw value. * @return string Host or ''. */ public static function normalize_agent_host( string $value ): string { $value = trim( $value ); // Structured-field string: strip the surrounding double quotes. if ( strlen( $value ) >= 2 && '"' === $value[0] && '"' === $value[ strlen( $value ) - 1 ] ) { $value = substr( $value, 1, -1 ); } $value = strtolower( trim( $value ) ); if ( '' === $value ) { return ''; } if ( false !== strpos( $value, '://' ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- pure PHP class, unit-tested without WordPress. $host = parse_url( $value, PHP_URL_HOST ); $value = is_string( $host ) ? $host : ''; } else { $parts = explode( '/', $value, 2 ); $value = $parts[0]; } $value = rtrim( $value, '.' ); // Drop an explicit port. $colon = strpos( $value, ':' ); if ( false !== $colon ) { $value = substr( $value, 0, $colon ); } return $value; } /** * Read and decode the JSON file. */ private function load_file( string $path ): void { $this->source_path = $path; if ( ! is_readable( $path ) ) { $this->load_error = 'agents.json is missing or unreadable: ' . $path; return; } if ( function_exists( 'wp_json_file_decode' ) ) { $decoded = wp_json_file_decode( $path, array( 'associative' => true ) ); } else { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- unit tests run without WordPress; the file ships with the plugin. $raw = file_get_contents( $path ); $decoded = ( false === $raw ) ? null : json_decode( $raw, true ); } if ( ! is_array( $decoded ) ) { $this->load_error = 'agents.json could not be read or is not valid JSON: ' . $path; return; } $list = ( isset( $decoded['agents'] ) && is_array( $decoded['agents'] ) ) ? $decoded['agents'] : $decoded; $this->records = self::normalize_records( $list ); } /** * Fill defaults, drop broken entries, key by id. */ private static function normalize_records( array $list ): array { $out = array(); foreach ( $list as $item ) { if ( ! is_array( $item ) || empty( $item['id'] ) || ! is_string( $item['id'] ) ) { continue; } $id = trim( $item['id'] ); if ( '' === $id ) { continue; } $patterns = array(); if ( isset( $item['ua_patterns'] ) && is_array( $item['ua_patterns'] ) ) { foreach ( $item['ua_patterns'] as $pattern ) { if ( is_string( $pattern ) && '' !== trim( $pattern ) ) { $patterns[] = trim( $pattern ); } } } $signature_agent = ( isset( $item['signature_agent'] ) && is_string( $item['signature_agent'] ) ) ? trim( $item['signature_agent'] ) : null; if ( '' === $signature_agent ) { $signature_agent = null; } $category = ( isset( $item['category'] ) && is_string( $item['category'] ) ) ? strtolower( trim( $item['category'] ) ) : self::CATEGORY_CRAWLER; $default_policy = ( isset( $item['default_policy'] ) && is_string( $item['default_policy'] ) ) ? strtolower( trim( $item['default_policy'] ) ) : 'queue'; if ( ! in_array( $default_policy, array( 'allow', 'queue', 'block' ), true ) ) { $default_policy = 'queue'; } $verified_via = ( isset( $item['verified_via'] ) && is_string( $item['verified_via'] ) ) ? strtolower( trim( $item['verified_via'] ) ) : self::VERIFIED_VIA_NONE; $out[ $id ] = array( 'id' => $id, 'name' => ( isset( $item['name'] ) && is_string( $item['name'] ) && '' !== $item['name'] ) ? $item['name'] : $id, 'operator' => ( isset( $item['operator'] ) && is_string( $item['operator'] ) ) ? $item['operator'] : null, 'ua_patterns' => $patterns, 'signature_agent' => $signature_agent, 'signature_host' => null === $signature_agent ? '' : self::normalize_agent_host( $signature_agent ), 'category' => $category, 'default_policy' => $default_policy, 'verified_via' => $verified_via, 'ip_list_url' => ( isset( $item['ip_list_url'] ) && is_string( $item['ip_list_url'] ) && '' !== $item['ip_list_url'] ) ? $item['ip_list_url'] : null, 'docs_url' => ( isset( $item['docs_url'] ) && is_string( $item['docs_url'] ) && '' !== $item['docs_url'] ) ? $item['docs_url'] : null, 'note' => ( isset( $item['note'] ) && is_string( $item['note'] ) && '' !== $item['note'] ) ? $item['note'] : null, ); } return $out; } /** * Case-insensitive substring match over ua_patterns of one record group. * * @param string $ua User agent. * @param bool $automation True = headless/http-client records only, false = everything else. * @return array|null */ private function match_ua_in_categories( string $ua, bool $automation ): ?array { foreach ( $this->records as $record ) { if ( self::is_automation_category( $record['category'] ) !== $automation ) { continue; } foreach ( $record['ua_patterns'] as $pattern ) { if ( false !== stripos( $ua, $pattern ) ) { return self::with_match( $record, self::MATCHED_BY_UA, $pattern ); } } } return null; } /** * Attach match info to a record copy. */ private static function with_match( array $record, string $by, string $value ): array { $record['matched_by'] = $by; $record['matched_value'] = $value; return $record; } }