From 502bf3d061070fdc252357310b81f03d8a9d16df Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 15 Oct 2024 15:01:37 -0300 Subject: [PATCH 1/3] Resolve theme relative font faces uris in theme.json and style variations --- .../class-wp-theme-json-resolver.php | 31 ++++++++++++- .../tests/theme/wpThemeJsonResolver.php | 46 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index d63a84353cd46..9c4987719ed7f 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -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. @@ -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 ( diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 4fb3784f1a41b..5eded85d85c76 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -1332,6 +1332,42 @@ public function test_get_resolved_theme_uris() { $theme_json = new WP_Theme_JSON( 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:./example/fonts/tocco/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:./example/fonts/strozzapreti/strozzapreti-400-normal.woff2', + ), + ), + ), + 'fontFamily' => '"Strozzapreti", cursive', + 'name' => 'Strozzapreti', + 'slug' => 'primary', + ), + ), + ), + ), 'styles' => array( 'background' => array( 'backgroundImage' => array( @@ -1359,6 +1395,16 @@ public function test_get_resolved_theme_uris() { ); $expected_data = array( + array( + 'name' => 'file:./example/fonts/tocco/tocco-400-normal.woff2', + 'href' => 'https://example.org/wp-content/themes/example-theme/example/fonts/tocco/tocco-400-normal.woff2', + 'target' => 'typography.fontFamilies.secondary.fontFace.src', + ), + array( + 'name' => 'file:./example/fonts/strozzapreti/strozzapreti-400-normal.woff2', + 'href' => 'https://example.org/wp-content/themes/example-theme/example/fonts/strozzapreti/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', From b0976e19da1645e8e15bfc327b4856189f132de9 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 15 Oct 2024 15:15:57 -0300 Subject: [PATCH 2/3] lint fix --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 5eded85d85c76..59c809a109390 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -1331,7 +1331,7 @@ 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, + 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'typography' => array( 'fontFamilies' => array( @@ -1368,7 +1368,7 @@ public function test_get_resolved_theme_uris() { ), ), ), - 'styles' => array( + 'styles' => array( 'background' => array( 'backgroundImage' => array( 'url' => 'file:./assets/image.png', From 9c4bdd6e1dfa48331c0b68270efc1301fbb9bac2 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 15 Oct 2024 16:09:46 -0300 Subject: [PATCH 3/3] fix tests urls --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 59c809a109390..cfe5f1ea28163 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -1342,7 +1342,7 @@ public function test_get_resolved_theme_uris() { 'fontStyle' => 'normal', 'fontWeight' => '400', 'src' => array( - 'file:./example/fonts/tocco/tocco-400-normal.woff2', + 'file:./assets/tocco-400-normal.woff2', ), ), ), @@ -1357,7 +1357,7 @@ public function test_get_resolved_theme_uris() { 'fontStyle' => 'normal', 'fontWeight' => '400', 'src' => array( - 'file:./example/fonts/strozzapreti/strozzapreti-400-normal.woff2', + 'file:./assets/strozzapreti-400-normal.woff2', ), ), ), @@ -1396,13 +1396,13 @@ public function test_get_resolved_theme_uris() { $expected_data = array( array( - 'name' => 'file:./example/fonts/tocco/tocco-400-normal.woff2', - 'href' => 'https://example.org/wp-content/themes/example-theme/example/fonts/tocco/tocco-400-normal.woff2', + '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:./example/fonts/strozzapreti/strozzapreti-400-normal.woff2', - 'href' => 'https://example.org/wp-content/themes/example-theme/example/fonts/strozzapreti/strozzapreti-400-normal.woff2', + '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(