path = '' !== $path ? $path : AIBLACKOUT_PATH . 'data/ai-providers.json'; } /** * Loads and validates the list. * * @return array Parsed list with 'version' and 'providers' keys. */ private function load(): array { if ( null !== $this->data ) { return $this->data; } $empty = array( 'version' => '', 'providers' => 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 ) || empty( $parsed['providers'] ) || ! is_array( $parsed['providers'] ) ) { $this->data = $empty; return $this->data; } $providers = array(); foreach ( $parsed['providers'] as $provider ) { if ( empty( $provider['id'] ) ) { continue; } $providers[] = $this->normalize( $provider ); } $this->data = array( 'version' => isset( $parsed['version'] ) ? (string) $parsed['version'] : '', 'providers' => $providers, ); return $this->data; } /** * Fills in defaults for optional provider fields. * * @param array $provider Raw provider array. * @return array Normalized provider array. */ private function normalize( array $provider ): array { $group = isset( $provider['group'] ) ? (string) $provider['group'] : self::GROUP_PROVIDER; if ( self::GROUP_PROXY !== $group ) { $group = self::GROUP_PROVIDER; } return array( 'id' => (string) $provider['id'], 'label' => isset( $provider['label'] ) ? (string) $provider['label'] : (string) $provider['id'], 'group' => $group, 'default' => ! empty( $provider['default'] ), 'hosts' => $this->string_list( $provider, 'hosts' ), 'host_suffixes' => $this->string_list( $provider, 'host_suffixes' ), 'path_prefix' => isset( $provider['path_prefix'] ) ? (string) $provider['path_prefix'] : '', 'docs' => isset( $provider['docs'] ) ? (string) $provider['docs'] : '', 'warning' => isset( $provider['warning'] ) ? (string) $provider['warning'] : '', ); } /** * Returns a provider field as a list of lower-cased strings. * * @param array $provider Provider array. * @param string $key Field name. * @return string[] Values. */ private function string_list( array $provider, string $key ): array { if ( empty( $provider[ $key ] ) || ! is_array( $provider[ $key ] ) ) { return array(); } $values = array_map( static function ( $value ) { return strtolower( trim( (string) $value ) ); }, $provider[ $key ] ); return array_values( array_filter( $values ) ); } /** * Whether the bundled list loaded successfully. * * @return bool True when at least one provider is available. */ public function is_available(): bool { $data = $this->load(); return ! empty( $data['providers'] ); } /** * Returns the list version string. * * @return string Version, or an empty string when unavailable. */ public function get_version(): string { $data = $this->load(); return $data['version']; } /** * Returns every provider. * * @return array List of provider arrays. */ public function get_providers(): array { $data = $this->load(); return $data['providers']; } /** * Returns providers in one group. * * @param string $group One of the GROUP_* constants. * @return array List of provider arrays. */ public function get_group( string $group ): array { return array_values( array_filter( $this->get_providers(), static function ( array $provider ) use ( $group ) { return $provider['group'] === $group; } ) ); } /** * Returns the ids of providers watched out of the box. * * @return string[] Provider identifiers. */ public function get_default_ids(): array { $ids = array(); foreach ( $this->get_providers() as $provider ) { if ( $provider['default'] ) { $ids[] = $provider['id']; } } return $ids; } /** * Finds the provider a URL belongs to. * * @param string $url The request URL. * @param string[] $enabled_ids Provider ids to consider. An empty list matches none. * @return array|null The matching provider, or null when the URL is not an AI endpoint. */ public function match_url( string $url, array $enabled_ids ): ?array { if ( empty( $enabled_ids ) ) { return null; } $parts = wp_parse_url( $url ); if ( empty( $parts['host'] ) ) { return null; } $host = strtolower( $parts['host'] ); $path = isset( $parts['path'] ) ? $parts['path'] : '/'; foreach ( $this->get_providers() as $provider ) { if ( ! in_array( $provider['id'], $enabled_ids, true ) ) { continue; } if ( ! $this->host_matches( $host, $provider ) ) { continue; } if ( '' !== $provider['path_prefix'] && 0 !== strpos( $path, $provider['path_prefix'] ) ) { continue; } return $provider; } return null; } /** * Whether a host matches a provider's host rules. * * @param string $host Lower-cased request host. * @param array $provider Normalized provider array. * @return bool True on a match. */ private function host_matches( string $host, array $provider ): bool { if ( in_array( $host, $provider['hosts'], true ) ) { return true; } foreach ( $provider['host_suffixes'] as $suffix ) { $offset = strlen( $host ) - strlen( $suffix ); if ( $offset > 0 && substr( $host, $offset ) === $suffix ) { return true; } } return false; } }