$list ) { foreach ( $list as $known ) { if ( $host === $known || str_ends_with( $host, '.' . $known ) ) { return $group; } } } return 'other'; } /** * The known hosts by group, from data/third-party-hosts.json. * * @return array> */ private static function groups(): array { static $groups = null; if ( null === $groups ) { $groups = array(); $json = file_get_contents( dirname( __DIR__, 3 ) . '/data/third-party-hosts.json' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reads a file shipped with the plugin. $data = is_string( $json ) ? json_decode( $json, true ) : null; foreach ( is_array( $data ) ? $data : array() as $name => $list ) { $groups[ (string) $name ] = is_array( $list ) ? array_values( array_map( 'strval', $list ) ) : array(); } } return $groups; } /** * Problems on this page. * * @param ParsedPage $page Parsed page. * @param array $results Not used. * @phpstan-param array $results * @return array */ public function inspect( ParsedPage $page, array $results ): array { $hits = array(); $seen = array(); foreach ( $page->references as $reference ) { if ( ! in_array( $reference->kind, array( Reference::SCRIPT, Reference::STYLE ), true ) || ! str_starts_with( $reference->url, 'http' ) ) { continue; } $host = strtolower( (string) wp_parse_url( $reference->url, PHP_URL_HOST ) ); if ( '' === $host || Url::is_internal( $reference->url, $this->hosts ) ) { continue; } $key = $host . '|' . $reference->source->type . '|' . $reference->source->ref; if ( isset( $seen[ $key ] ) ) { continue; } $seen[ $key ] = true; $group = self::group( $host ); $hits[] = array( 'observation' => new Observation( 'fonts' === $group ? 'perf.google_fonts' : self::ID, Priority::LOW, Priority::SUGGESTION, 'fonts' === $group ? __( 'Fonts are loaded from an outside server', 'forensix-digital' ) /* translators: %s: host name. */ : sprintf( __( 'Files are loaded from %s', 'forensix-digital' ), $host ), $host, $reference->source->type, $reference->source->ref, '', array( 'host' => $host, 'group' => $group, ) ), 'context' => $reference->url, ); } return $hits; } }