path = '' !== $path ? $path : AIBLACKOUT_PATH . 'data/crawlers.json'; } /** * Loads and validates the ruleset. * * @return array Parsed ruleset with 'groups' and 'protected' keys. */ private function load(): array { if ( null !== $this->data ) { return $this->data; } $empty = array( 'version' => '', 'groups' => array(), 'protected' => array(), ); if ( ! is_readable( $this->path ) ) { $this->data = $empty; return $this->data; } $raw = file_get_contents( $this->path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading a bundled plugin file, not a remote resource. $parsed = json_decode( (string) $raw, true ); if ( ! is_array( $parsed ) || ! isset( $parsed['groups'] ) || ! is_array( $parsed['groups'] ) ) { $this->data = $empty; return $this->data; } $protected = isset( $parsed['protected']['agents'] ) && is_array( $parsed['protected']['agents'] ) ? array_filter( $parsed['protected']['agents'] ) : array(); if ( empty( $protected ) ) { $this->data = $empty; return $this->data; } $this->data = array( 'version' => isset( $parsed['version'] ) ? (string) $parsed['version'] : '', 'groups' => $parsed['groups'], 'protected' => $protected, ); return $this->data; } /** * Whether the bundled ruleset loaded successfully. * * @return bool True when at least one group is available. */ public function is_available(): bool { $data = $this->load(); return ! empty( $data['groups'] ); } /** * Returns the ruleset version string. * * @return string Version, or an empty string when unavailable. */ public function get_version(): string { $data = $this->load(); return $data['version']; } /** * Returns all crawler groups. * * @return array List of group arrays. */ public function get_groups(): array { $data = $this->load(); $groups = array(); foreach ( $data['groups'] as $group ) { if ( ! is_array( $group ) ) { continue; } $groups[] = $this->normalize_group( $group ); } return $groups; } /** * Fills in the group fields every caller reads. * * @param array $group Raw group array. * @return array Normalized group array. */ private function normalize_group( array $group ): array { $id = isset( $group['id'] ) ? (string) $group['id'] : ''; return array_merge( $group, array( 'id' => $id, 'label' => isset( $group['label'] ) && '' !== $group['label'] ? (string) $group['label'] : $id, 'docs' => isset( $group['docs'] ) ? (string) $group['docs'] : '', 'agents' => isset( $group['agents'] ) && is_array( $group['agents'] ) ? $group['agents'] : array(), ) ); } /** * Returns a single group by id. * * @param string $group_id Group identifier. * @return array|null Group array, or null when not found. */ public function get_group( string $group_id ): ?array { foreach ( $this->get_groups() as $group ) { if ( isset( $group['id'] ) && $group['id'] === $group_id ) { return $group; } } return null; } /** * Returns every agent across every group, each annotated with its group * id. * * @return array List of agent arrays, each with a 'group' key added. */ public function get_agents(): array { $agents = array(); foreach ( $this->get_groups() as $group ) { if ( empty( $group['agents'] ) || ! is_array( $group['agents'] ) ) { continue; } foreach ( $group['agents'] as $agent ) { if ( empty( $agent['ua'] ) ) { continue; } $agent['group'] = isset( $group['id'] ) ? $group['id'] : ''; $agents[] = $this->normalize_agent( $agent ); } } return $agents; } /** * Returns a single agent by its user-agent token. * * @param string $ua User-agent token. * @return array|null Agent array, or null when not found. */ public function get_agent( string $ua ): ?array { foreach ( $this->get_agents() as $agent ) { if ( 0 === strcasecmp( $agent['ua'], $ua ) ) { return $agent; } } return null; } /** * Returns the user-agent fragments that must never be blocked. * * @return string[] Protected user-agent tokens. */ public function get_protected_agents(): array { $data = $this->load(); return array_values( array_filter( array_map( 'strval', $data['protected'] ) ) ); } /** * Fills in defaults for optional agent fields. * * @param array $agent Raw agent array. * @return array Normalized agent array. */ private function normalize_agent( array $agent ): array { $match = isset( $agent['match'] ) ? (string) $agent['match'] : self::MATCH_ROBOTS; if ( self::MATCH_UA !== $match ) { $match = self::MATCH_ROBOTS; } return array_merge( $agent, array( 'ua' => (string) $agent['ua'], 'purpose' => isset( $agent['purpose'] ) ? (string) $agent['purpose'] : 'training', 'match' => $match, 'search_impact' => isset( $agent['search_impact'] ) ? (string) $agent['search_impact'] : 'none', 'legacy' => ! empty( $agent['legacy'] ), 'description' => isset( $agent['description'] ) ? (string) $agent['description'] : '', ) ); } }