Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeboy-test-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"tests/smoke-validation-runtime-diagnostics.php": { "environment": "standalone-php" },
"tests/smoke-visual-repair-css.php": { "environment": "standalone-php" },
"tests/smoke-webfont-producer-consumer.php": { "environment": "standalone-php" },
"tests/smoke-google-fonts-cap-fallback.php": { "environment": "standalone-php" },
"tests/smoke-website-artifact-import-input.php": { "environment": "standalone-php" },
"tests/smoke-wordpress-site-plan-materializer.php": { "environment": "standalone-php" },
"tests/smoke-artifact-run-primitives.php": { "environment": "standalone-php" },
Expand Down
220 changes: 187 additions & 33 deletions includes/class-static-site-importer-font-materializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,59 @@ public static function prepare_overlay( array $plan, array $resolved_plan ) {
}

$font_faces = self::resolve_google_font_faces( $plan, $families, $diagnostics );
if ( '' === $font_faces ) {
if ( 'preserved' === $font_faces['state'] ) {
$preserved_url = (string) $font_faces['url'];
$fallback_url = self::is_google_stylesheet_url( $preserved_url ) ? $preserved_url : '';
$imports = array();
foreach ( $plan['stylesheets'] ?? array() as $stylesheet ) {
$content = is_array( $stylesheet ) && is_scalar( $stylesheet['content'] ?? null ) ? (string) $stylesheet['content'] : '';
if ( preg_match_all( '/@import\s+(?:url\()?\s*["\']?([^"\'\s\)]+)["\']?\s*\)?/i', $content, $matches ) ) {
foreach ( $matches[1] as $candidate ) {
if ( self::is_google_stylesheet_url( $candidate ) ) {
$imports[] = $candidate;
}
}
}
}
$imports = array_values( array_unique( $imports ) );
if ( '' === $fallback_url && ! empty( $imports ) ) {
$fallback_url = $imports[0];
}
$css_lines = array();
foreach ( $imports as $import_url ) {
$css_lines[] = '@import "' . addcslashes( $import_url, "\"\\\n" ) . '";';
}
if ( empty( $css_lines ) && '' !== $fallback_url ) {
$css_lines[] = '@import "' . addcslashes( $fallback_url, "\"\\\n" ) . '";';
}
$css_body = empty( $css_lines ) ? '' : trim( implode( "\n", $css_lines ) ) . "\n";
$outer_detail = array(
'reason' => (string) $font_faces['reason'],
'observed_bytes' => (int) $font_faces['observed_bytes'],
'url' => $preserved_url,
);
if ( 'google_fonts_payloads_partial_preserved' === $outer_detail['reason'] ) {
$outer_detail['limit_bytes'] = self::TOTAL_FONT_LIMIT;
$outer_detail['aggregate_bytes'] = (int) ( $font_faces['aggregate_bytes'] ?? $font_faces['observed_bytes'] );
} elseif ( 'google_fonts_stylesheet_preserved_due_to_size' === $outer_detail['reason'] ) {
$outer_detail['limit_bytes'] = self::CSS_LIMIT;
}
$diagnostics[] = self::diagnostic_with_detail( 'font_materialization_partial_preserved', $outer_detail );
if ( '' === $css_body ) {
return new WP_Error( 'static_site_importer_font_materialization_failed', '', $diagnostics );
}
$writes[] = self::write( 'assets/css/embedded-fonts.css', $css_body, 'theme.font_materialization' );
return self::with_runtime_registration( $writes, $resolved_plan, array(), $diagnostics );
}
$embedded_css = (string) $font_faces['css'];
if ( '' === trim( $embedded_css ) ) {
return new WP_Error( 'static_site_importer_font_materialization_failed', '', $diagnostics );
}

$writes[] = self::write( 'assets/css/embedded-fonts.css', trim( $font_faces ) . "\n", 'theme.font_materialization' );
$writes[] = self::write( 'assets/css/embedded-fonts.css', trim( $embedded_css ) . "\n", 'theme.font_materialization' );
foreach ( $svg_writes as $svg_write ) {
$writes[] = self::write(
$svg_write['target_path'],
self::embed_svg_font_faces( $svg_write['content'], $font_faces ),
self::embed_svg_font_faces( $svg_write['content'], $embedded_css ),
$svg_write['source_path']
);
}
Expand Down Expand Up @@ -532,8 +576,11 @@ private static function matching_svg_writes( array $writes, array $families ): a
return $matches;
}

/** @param array<int,string> $families @param array<int,array<string,string>> $diagnostics */
private static function resolve_google_font_faces( array $plan, array $families, array &$diagnostics ): string {
/** @param array<int,string> $families @param array<int,array<string,string>> $diagnostics
* @return array{state:'embedded',css:string}
* | array{state:'preserved',reason:string,observed_bytes:int,url:string,aggregate_bytes?:int}
*/
private static function resolve_google_font_faces( array $plan, array $families, array &$diagnostics ): array {
$imports = array();
foreach ( $plan['stylesheets'] ?? array() as $stylesheet ) {
$content = is_array( $stylesheet ) && is_scalar( $stylesheet['content'] ?? null ) ? (string) $stylesheet['content'] : '';
Expand All @@ -544,12 +591,22 @@ private static function resolve_google_font_faces( array $plan, array $families,
$imports = array_values( array_unique( $imports ) );
if ( empty( $imports ) ) {
$diagnostics[] = self::diagnostic( 'stylesheet_import_missing' );
return '';
return array(
'state' => 'preserved',
'reason' => 'stylesheet_import_missing',
'observed_bytes' => 0,
'url' => '',
);
}
foreach ( $imports as $import ) {
if ( ! self::is_google_stylesheet_url( $import ) ) {
$diagnostics[] = self::diagnostic( 'untrusted_stylesheet_url' );
return '';
return array(
'state' => 'preserved',
'reason' => 'untrusted_stylesheet_url',
'observed_bytes' => 0,
'url' => (string) $import,
);
}
}

Expand All @@ -560,58 +617,142 @@ private static function resolve_google_font_faces( array $plan, array $families,
$response = self::request( $import, self::CSS_LIMIT );
$css = is_wp_error( $response ) ? '' : (string) wp_remote_retrieve_body( $response );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) || '' === $css ) {
$diagnostics[] = self::diagnostic( 'stylesheet_fetch_failed' );
return '';
$diagnostics[] = self::diagnostic_with_detail(
'stylesheet_fetch_failed',
array(
'url' => $import,
'observed_bytes' => strlen( $css ),
'limit_bytes' => self::CSS_LIMIT,
)
);
return array(
'state' => 'preserved',
'reason' => 'stylesheet_fetch_failed',
'observed_bytes' => strlen( $css ),
'url' => $import,
);
}
if ( strlen( $css ) > self::CSS_LIMIT ) {
$diagnostics[] = self::diagnostic( 'stylesheet_response_too_large' );
return '';
$diagnostics[] = self::diagnostic_with_detail(
'google_fonts_stylesheet_preserved_due_to_size',
array(
'url' => $import,
'observed_bytes' => strlen( $css ),
'limit_bytes' => self::CSS_LIMIT,
)
);
return array(
'state' => 'preserved',
'reason' => 'google_fonts_stylesheet_preserved_due_to_size',
'observed_bytes' => strlen( $css ),
'url' => $import,
);
}
$embedded = self::embed_font_sources( $css, $families, $font_payloads, $font_payload_bytes, $diagnostics );
if ( '' === $embedded ) {
return '';
if ( 'preserved' === $embedded['state'] ) {
if ( '' === (string) $embedded['url'] ) {
$embedded['url'] = $import;
}
return $embedded;
}
$faces[] = $embedded;
$faces[] = (string) $embedded['css'];
}
return implode( "\n", $faces );
return array(
'state' => 'embedded',
'css' => implode( "\n", $faces ),
);
}

/** @param array<int,string> $families @param array<string,string> $payloads @param array<int,array<string,string>> $diagnostics */
private static function embed_font_sources( string $css, array $families, array &$payloads, int &$payload_bytes, array &$diagnostics ): string {
/** @param array<int,string> $families @param array<string,string> $payloads @param array<int,array<string,string>> $diagnostics
* @return array{state:'embedded',css:string}
* | array{state:'preserved',reason:string,observed_bytes:int,url:string,aggregate_bytes?:int}
*/
private static function embed_font_sources( string $css, array $families, array &$payloads, int &$payload_bytes, array &$diagnostics ): array {
if ( ! preg_match_all( '/@font-face\s*\{([^{}]*)\}/is', $css, $faces ) ) {
$diagnostics[] = self::diagnostic( 'stylesheet_font_faces_missing' );
return '';
$diagnostics[] = self::diagnostic_with_detail(
'stylesheet_font_faces_missing',
array(
'observed_bytes' => strlen( $css ),
'limit_bytes' => self::CSS_LIMIT,
)
);
return array(
'state' => 'preserved',
'reason' => 'stylesheet_font_faces_missing',
'observed_bytes' => strlen( $css ),
'url' => '',
);
}
$embedded = array();
$embedded = array();
$current_source_url = '';
foreach ( $faces[0] as $index => $face ) {
$current_source_url = '';
if ( ! preg_match( '/font-family\s*:\s*(["\']?)([^;"\']+)\1\s*;/i', $faces[1][ $index ], $family ) || ! in_array( trim( $family[2] ), $families, true ) ) {
continue;
}
if ( str_contains( $face, '<' ) || ! preg_match_all( '/url\(\s*(["\']?)([^"\'\s\)]+)\1\s*\)/i', $face, $urls ) ) {
$diagnostics[] = self::diagnostic( 'untrusted_font_url' );
return '';
return array(
'state' => 'preserved',
'reason' => 'untrusted_font_url',
'observed_bytes' => 0,
'url' => $current_source_url,
);
}
$rewritten = $face;
foreach ( array_unique( $urls[2] ) as $url ) {
$parts = wp_parse_url( $url );
$path = is_array( $parts ) ? strtolower( (string) ( $parts['path'] ?? '' ) ) : '';
$current_source_url = $url;
$parts = wp_parse_url( $url );
$path = is_array( $parts ) ? strtolower( (string) ( $parts['path'] ?? '' ) ) : '';
if ( ! is_array( $parts ) || 'https' !== strtolower( (string) ( $parts['scheme'] ?? '' ) ) || 'fonts.gstatic.com' !== strtolower( (string) ( $parts['host'] ?? '' ) ) || ( isset( $parts['port'] ) && 443 !== (int) $parts['port'] ) || isset( $parts['user'] ) || isset( $parts['pass'] ) || ! preg_match( '/\.woff2?$/', $path ) ) {
$diagnostics[] = self::diagnostic( 'untrusted_font_url' );
return '';
return array(
'state' => 'preserved',
'reason' => 'untrusted_font_url',
'observed_bytes' => 0,
'url' => $url,
);
}
if ( ! isset( $payloads[ $url ] ) ) {
$response = self::request( $url, self::FONT_LIMIT );
$payload = is_wp_error( $response ) ? '' : (string) wp_remote_retrieve_body( $response );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) || '' === $payload || strlen( $payload ) > self::FONT_LIMIT ) {
$diagnostics[] = self::diagnostic( 'font_payload_fetch_failed' );
return '';
$observed = strlen( $payload );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) || '' === $payload || $observed > self::FONT_LIMIT ) {
$diagnostics[] = self::diagnostic_with_detail(
'font_payload_fetch_failed',
array(
'url' => $url,
'observed_bytes' => $observed,
'limit_bytes' => self::FONT_LIMIT,
)
);
return array(
'state' => 'preserved',
'reason' => 'font_payload_fetch_failed',
'observed_bytes' => $observed,
'url' => $url,
);
}
if ( self::TOTAL_FONT_LIMIT < $payload_bytes + strlen( $payload ) ) {
$diagnostics[] = self::diagnostic( 'font_payload_total_too_large' );
return '';
if ( self::TOTAL_FONT_LIMIT < $payload_bytes + $observed ) {
$diagnostics[] = self::diagnostic_with_detail(
'google_fonts_payloads_partial_preserved',
array(
'url' => $url,
'observed_bytes' => $observed,
'aggregate_bytes' => $payload_bytes,
'limit_bytes' => self::TOTAL_FONT_LIMIT,
)
);
return array(
'state' => 'preserved',
'reason' => 'google_fonts_payloads_partial_preserved',
'observed_bytes' => $observed,
'aggregate_bytes' => $payload_bytes,
'url' => $url,
);
}
$payloads[ $url ] = $payload;
$payload_bytes += strlen( $payload );
$payload_bytes += $observed;
}
$mime = str_ends_with( $path, '.woff2' ) ? 'font/woff2' : 'font/woff';
$rewritten = str_replace( $url, 'data:' . $mime . ';base64,' . base64_encode( $payloads[ $url ] ), $rewritten ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Encodes a fetched font asset for a CSS data URL.
Expand All @@ -621,7 +762,10 @@ private static function embed_font_sources( string $css, array $families, array
if ( empty( $embedded ) ) {
$diagnostics[] = self::diagnostic( 'matching_font_faces_missing' );
}
return implode( "\n", $embedded );
return array(
'state' => 'embedded',
'css' => implode( "\n", $embedded ),
);
}

private static function request( string $url, int $limit ) {
Expand Down Expand Up @@ -828,4 +972,14 @@ private static function diagnostic( string $reason ): array {
'reason' => $reason,
);
}

/** @return array{type:string,source:string,reason:string,details:array<string,mixed>} */
private static function diagnostic_with_detail( string $reason, array $details ): array {
return array(
'type' => 'font_materialization_failed',
'source' => 'static-site-importer/font-materializer',
'reason' => $reason,
'details' => $details,
);
}
}
1 change: 1 addition & 0 deletions test-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
{ "path": "tests/smoke-validation-runtime-diagnostics.php", "environment": "standalone-php" },
{ "path": "tests/smoke-visual-repair-css.php", "environment": "standalone-php" },
{ "path": "tests/smoke-webfont-producer-consumer.php", "environment": "standalone-php" },
{ "path": "tests/smoke-google-fonts-cap-fallback.php", "environment": "standalone-php" },
{ "path": "tests/smoke-website-artifact-import-input.php", "environment": "standalone-php" },
{ "path": "tests/smoke-wordpress-site-plan-materializer.php", "environment": "standalone-php" },
{ "path": "tests/smoke-artifact-run-primitives.php", "environment": "standalone-php" },
Expand Down
Loading
Loading