Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve theme relative font faces uris in theme.json and style variations #7570

Closed
wants to merge 3 commits into from
Closed
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
31 changes: 30 additions & 1 deletion src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ public static function get_style_variations( $scope = 'theme' ) {
* as the value of `_link` object in REST API responses.
*
* @since 6.6.0
* @since 6.7.0 Resolve relative paths in block styles.
* @since 6.7.0 Added support for resolving block style and font face URIs.
*
* @param WP_Theme_JSON $theme_json A theme json instance.
* @return array An array of resolved paths.
Expand All @@ -867,6 +867,35 @@ public static function get_resolved_theme_uris( $theme_json ) {
*/
$placeholder = 'file:./';

// Add font URIs.
if ( ! empty( $theme_json_data['settings']['typography']['fontFamilies'] ) ) {
$font_families = array_merge(
$theme_json_data['settings']['typography']['fontFamilies']['theme'] ?? array(),
$theme_json_data['settings']['typography']['fontFamilies']['custom'] ?? array(),
$theme_json_data['settings']['typography']['fontFamilies']['default'] ?? array()
);
foreach ( $font_families as $font_family ) {
if ( ! empty( $font_family['fontFace'] ) ) {
foreach ( $font_family['fontFace'] as $font_face ) {
if ( ! empty( $font_face['src'] ) ) {
$sources = is_string( $font_face['src'] )
? array( $font_face['src'] )
: $font_face['src'];
foreach ( $sources as $source ) {
if ( str_starts_with( $source, $placeholder ) ) {
$resolved_theme_uris[] = array(
'name' => $source,
'href' => sanitize_url( get_theme_file_uri( str_replace( $placeholder, '', $source ) ) ),
'target' => "typography.fontFamilies.{$font_family['slug']}.fontFace.src",
);
}
}
}
}
}
}
}

// Top level styles.
$background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null;
if (
Expand Down
50 changes: 48 additions & 2 deletions tests/phpunit/tests/theme/wpThemeJsonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,44 @@ public function test_resolve_theme_file_uris() {
public function test_get_resolved_theme_uris() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'typography' => array(
'fontFamilies' => array(
array(
'fontFace' => array(
array(
'fontFamily' => 'Tocco',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => array(
'file:./assets/tocco-400-normal.woff2',
),
),
),
'fontFamily' => 'Tocco, system-ui',
'name' => 'Tocco',
'slug' => 'secondary',
),
array(
'fontFace' => array(
array(
'fontFamily' => '"Strozzapreti"',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => array(
'file:./assets/strozzapreti-400-normal.woff2',
),
),
),
'fontFamily' => '"Strozzapreti", cursive',
'name' => 'Strozzapreti',
'slug' => 'primary',
),
),
),
),
'styles' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'file:./assets/image.png',
Expand All @@ -1359,6 +1395,16 @@ public function test_get_resolved_theme_uris() {
);

$expected_data = array(
array(
'name' => 'file:./assets/tocco-400-normal.woff2',
'href' => 'https://example.org/wp-content/themes/example-theme/assets/tocco-400-normal.woff2',
'target' => 'typography.fontFamilies.secondary.fontFace.src',
),
array(
'name' => 'file:./assets/strozzapreti-400-normal.woff2',
'href' => 'https://example.org/wp-content/themes/example-theme/assets/strozzapreti-400-normal.woff2',
'target' => 'typography.fontFamilies.primary.fontFace.src',
),
array(
'name' => 'file:./assets/image.png',
'href' => 'https://example.org/wp-content/themes/example-theme/assets/image.png',
Expand Down
Loading