Felder wie in Measurement 'html' (ohne asset_headers). */ public static function analyze( $html, $home_url = '' ) { $empty = array( 'is_html' => false, 'bytes' => 0, 'emoji_tags' => 0, 'version_urls' => 0, 'asset_count' => 0, 'blocking_scripts' => 0, 'blocking_handles' => array(), 'external_hosts' => array(), 'hinted_hosts' => array(), 'sample_assets' => array(), ); if ( ! is_string( $html ) || '' === $html ) { return $empty; } $result = $empty; $result['bytes'] = strlen( $html ); // Ohne -Tag ist unklar, was das ist (Fehlerseite, Feed, Teilausgabe). if ( ! preg_match( '/]/i', $html ) ) { return $result; } $result['is_html'] = true; $home_host = self::host( $home_url ); $home_base = self::origin( $home_url ); $head = ''; if ( preg_match( '/]*>(.*?)<\/head>/is', $html, $m ) ) { $head = $m[1]; } $with_inline = array(); $assets = array(); $hosts = array(); $samples = array(); // Stylesheets. preg_match_all( '/]*>/i', $html, $links ); foreach ( $links[0] as $tag ) { $rel = strtolower( (string) self::attr( $tag, 'rel' ) ); $href = (string) self::attr( $tag, 'href' ); if ( '' === $href ) { continue; } if ( false !== strpos( $rel, 'stylesheet' ) ) { $assets[ $href ] = true; self::collect( $href, 'css', $home_host, $home_base, $hosts, $samples ); } elseif ( false !== strpos( $rel, 'preconnect' ) || false !== strpos( $rel, 'dns-prefetch' ) ) { $hint_host = self::host( 0 === strpos( $href, '//' ) ? 'https:' . $href : $href ); if ( '' !== $hint_host && ! in_array( $hint_host, $result['hinted_hosts'], true ) && count( $result['hinted_hosts'] ) < 30 ) { $result['hinted_hosts'][] = $hint_host; } } elseif ( false !== strpos( $rel, 'preload' ) || false !== strpos( $rel, 'icon' ) ) { self::collect( $href, '', $home_host, $home_base, $hosts, $samples ); } } // Skripte. preg_match_all( '/]*>(.*?)<\/script>/is', $html, $scripts, PREG_SET_ORDER ); foreach ( $scripts as $match ) { $tag = substr( $match[0], 0, strpos( $match[0], '>' ) + 1 ); $src = (string) self::attr( $tag, 'src' ); $inline = $match[1]; if ( '' === $src ) { if ( false !== strpos( $inline, '_wpemojiSettings' ) ) { ++$result['emoji_tags']; } // Inline-Skript zu einem eingereihten Skript (id="-js-before" usw.): der Handle darf nicht verzoegert werden. if ( preg_match( '/^([A-Za-z0-9_.\-]+)-js-(before|after|extra|translations)$/', (string) self::attr( $tag, 'id' ), $neighbour ) ) { $with_inline[ $neighbour[1] ] = true; } continue; } $assets[ $src ] = true; if ( false !== strpos( $src, 'emoji-release' ) || false !== strpos( $src, 'wp-emoji' ) ) { ++$result['emoji_tags']; } self::collect( $src, 'js', $home_host, $home_base, $hosts, $samples ); } // Skripte im Kopf ohne defer/async/module blockieren das Rendern. if ( '' !== $head ) { preg_match_all( '/]*>/i', $head, $head_scripts ); foreach ( $head_scripts[0] as $tag ) { if ( '' === (string) self::attr( $tag, 'src' ) ) { continue; } if ( preg_match( '/\s(defer|async)(\s|=|>|\/)/i', $tag ) || preg_match( '/type\s*=\s*["\']?module/i', $tag ) ) { continue; } ++$result['blocking_scripts']; // WordPress traegt den Handle als id="-js" ein. $id = (string) self::attr( $tag, 'id' ); if ( preg_match( '/^([A-Za-z0-9_.\-]+)-js$/', $id, $handle ) && count( $result['blocking_handles'] ) < 20 ) { $result['blocking_handles'][] = $handle[1]; } } } // Bilder (nur fuer Hosts und ein Beispiel). preg_match_all( '/]*>/i', $html, $images ); foreach ( $images[0] as $tag ) { $src = (string) self::attr( $tag, 'src' ); if ( '' !== $src ) { self::collect( $src, 'img', $home_host, $home_base, $hosts, $samples ); } } $versioned = 0; foreach ( array_keys( $assets ) as $url ) { if ( preg_match( '/[?&]ver=/i', $url ) ) { ++$versioned; } } $result['blocking_handles'] = array_values( array_diff( $result['blocking_handles'], array_keys( $with_inline ) ) ); arsort( $hosts ); $result['version_urls'] = $versioned; $result['asset_count'] = count( $assets ); $result['external_hosts'] = array_slice( $hosts, 0, 20, true ); $result['sample_assets'] = array_values( $samples ); return $result; } /** * Merkt Hosts fremder Adressen und je Art die erste Datei der eigenen Website. * * @param array $hosts * @param array $samples */ private static function collect( $url, $type, $home_host, $home_base, array &$hosts, array &$samples ) { $url = html_entity_decode( trim( $url ), ENT_QUOTES ); if ( '' === $url || 0 === strpos( $url, 'data:' ) || 0 === strpos( $url, '#' ) ) { return; } $host = self::host( 0 === strpos( $url, '//' ) ? 'https:' . $url : $url ); if ( '' === $host ) { // Relative Adresse: gehoert zur eigenen Website. if ( '' !== $type && ! isset( $samples[ $type ] ) && '' !== $home_base ) { $samples[ $type ] = array( 'url' => $home_base . '/' . ltrim( $url, '/' ), 'type' => $type, ); } return; } if ( self::same_site( $host, $home_host ) ) { if ( '' !== $type && ! isset( $samples[ $type ] ) ) { $samples[ $type ] = array( 'url' => 0 === strpos( $url, '//' ) ? 'https:' . $url : $url, 'type' => $type, ); } return; } $hosts[ $host ] = isset( $hosts[ $host ] ) ? $hosts[ $host ] + 1 : 1; } private static function same_site( $host, $home_host ) { if ( '' === $home_host ) { return false; } $strip = static function ( $h ) { return 0 === strpos( $h, 'www.' ) ? substr( $h, 4 ) : $h; }; return $strip( $host ) === $strip( $home_host ); } private static function host( $url ) { if ( ! is_string( $url ) || '' === $url ) { return ''; } $host = parse_url( $url, PHP_URL_HOST ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url return is_string( $host ) ? strtolower( $host ) : ''; } private static function origin( $url ) { $host = self::host( $url ); if ( '' === $host ) { return ''; } $scheme = parse_url( $url, PHP_URL_SCHEME ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url $port = parse_url( $url, PHP_URL_PORT ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url return ( is_string( $scheme ) ? $scheme : 'https' ) . '://' . $host . ( $port ? ':' . $port : '' ); } /** * Wert eines Attributs in einem Tag, sonst null. */ private static function attr( $tag, $name ) { if ( preg_match( '/\s' . preg_quote( $name, '/' ) . '\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^\s>]+))/i', $tag, $m ) ) { foreach ( array( 1, 2, 3 ) as $i ) { if ( isset( $m[ $i ] ) && '' !== $m[ $i ] ) { return $m[ $i ]; } } } return null; } }