From 59a0d5a0c654356469ecfea27b6cc1e4d21fc405 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 17 Oct 2024 10:59:47 -0300 Subject: [PATCH 01/10] Load fonts from style variations --- src/wp-admin/includes/admin-filters.php | 1 + src/wp-includes/block-editor.php | 1 + src/wp-includes/fonts.php | 16 ++++++++++ .../fonts/class-wp-font-face-resolver.php | 32 +++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/wp-admin/includes/admin-filters.php b/src/wp-admin/includes/admin-filters.php index b5adb946cf0d3..587fbc25e9e3c 100644 --- a/src/wp-admin/includes/admin-filters.php +++ b/src/wp-admin/includes/admin-filters.php @@ -172,3 +172,4 @@ // Font management. add_action( 'admin_print_styles', 'wp_print_font_faces', 50 ); +add_action( 'admin_print_styles', 'wp_print_font_faces_from_style_variations', 50 ); diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index be8a8f901fe5c..bdca0d2c08528 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -366,6 +366,7 @@ function _wp_get_iframed_editor_assets() { ob_start(); wp_print_styles(); wp_print_font_faces(); + wp_print_font_faces_from_style_variations(); $styles = ob_get_clean(); if ( $has_emoji_styles ) { diff --git a/src/wp-includes/fonts.php b/src/wp-includes/fonts.php index 4e286b71dc7f2..70e0e74d1887a 100644 --- a/src/wp-includes/fonts.php +++ b/src/wp-includes/fonts.php @@ -54,6 +54,22 @@ function wp_print_font_faces( $fonts = array() ) { $wp_font_face->generate_and_print( $fonts ); } +/** + * Generates and prints font-face styles defined the the theme style variations. + * + * @since 6.7.0 + * + */ +function wp_print_font_faces_from_style_variations() { + $fonts = WP_Font_Face_Resolver::get_fonts_from_style_variations(); + + if ( empty( $fonts ) ) { + return; + } + + wp_print_font_faces( $fonts ); +} + /** * Registers a new font collection in the font library. * diff --git a/src/wp-includes/fonts/class-wp-font-face-resolver.php b/src/wp-includes/fonts/class-wp-font-face-resolver.php index 12245af556b58..1a7ab449d87cf 100644 --- a/src/wp-includes/fonts/class-wp-font-face-resolver.php +++ b/src/wp-includes/fonts/class-wp-font-face-resolver.php @@ -36,6 +36,38 @@ public static function get_fonts_from_theme_json() { return static::parse_settings( $settings ); } + /** + * Gets fonts defined in style variations. + * + * @since 6.7.0 + * + * @return array Returns an array of font-families. + */ + public static function get_fonts_from_style_variations() { + $variations = WP_Theme_JSON_Resolver::get_style_variations(); + $fonts = array(); + + if ( empty( $variations ) ) { + return $fonts; + } + + foreach ( $variations as $variation ) { + if ( ! empty( $variation['settings']['typography']['fontFamilies']['theme'] ) ) { + $fonts = array_merge( $fonts, $variation['settings']['typography']['fontFamilies']['theme'] ); + } + } + + $settings = array( + 'typography' => array( + 'fontFamilies' => array( + 'theme' => $fonts, + ), + ), + ); + + return static::parse_settings( $settings ); + } + /** * Parse theme.json settings to extract font definitions with variations grouped by font-family. * From aa955ad6a68789986e824904bc0e10a068f74364 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 17 Oct 2024 12:01:46 -0300 Subject: [PATCH 02/10] lint php --- src/wp-includes/fonts.php | 2 +- src/wp-includes/fonts/class-wp-font-face-resolver.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/fonts.php b/src/wp-includes/fonts.php index 70e0e74d1887a..17db0396089f8 100644 --- a/src/wp-includes/fonts.php +++ b/src/wp-includes/fonts.php @@ -58,7 +58,7 @@ function wp_print_font_faces( $fonts = array() ) { * Generates and prints font-face styles defined the the theme style variations. * * @since 6.7.0 - * + * */ function wp_print_font_faces_from_style_variations() { $fonts = WP_Font_Face_Resolver::get_fonts_from_style_variations(); diff --git a/src/wp-includes/fonts/class-wp-font-face-resolver.php b/src/wp-includes/fonts/class-wp-font-face-resolver.php index 1a7ab449d87cf..153008b53aec0 100644 --- a/src/wp-includes/fonts/class-wp-font-face-resolver.php +++ b/src/wp-includes/fonts/class-wp-font-face-resolver.php @@ -45,7 +45,7 @@ public static function get_fonts_from_theme_json() { */ public static function get_fonts_from_style_variations() { $variations = WP_Theme_JSON_Resolver::get_style_variations(); - $fonts = array(); + $fonts = array(); if ( empty( $variations ) ) { return $fonts; @@ -56,7 +56,7 @@ public static function get_fonts_from_style_variations() { $fonts = array_merge( $fonts, $variation['settings']['typography']['fontFamilies']['theme'] ); } } - + $settings = array( 'typography' => array( 'fontFamilies' => array( From 0a612013b51de21ed2d0a0fee488c6ee535e2226 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 17 Oct 2024 14:00:12 -0300 Subject: [PATCH 03/10] adding tests for get_fonts_from_style_variations --- .../font-face/wp-font-face-tests-dataset.php | 80 +++++++++++++++++++ .../getFontsFromStyleVariations.php | 55 +++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php diff --git a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php index 1432c8589f5cc..b0631b9dd3b49 100644 --- a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php +++ b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php @@ -403,4 +403,84 @@ public static function get_custom_font_families( $key = '' ) { return $data; } + + public static function get_custom_style_variations( $key = '' ) { + static $data = null; + + $uri = get_stylesheet_directory_uri() . '/assets/fonts/'; + $expected_font_families = array( + array( + array( + 'src' => array( + '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Regular.woff2', + ), + 'font-family' => 'DM Sans', + 'font-stretch' => 'normal', + 'font-style' => 'normal', + 'font-weight' => '400', + ), + array( + 'src' => array( + '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Bold.woff2', + ), + 'font-family' => 'DM Sans', + 'font-stretch' => 'normal', + 'font-style' => 'normal', + 'font-weight' => '700', + ), + ), + array( + array( + 'src' => array( + '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/open-sans/OpenSans-VariableFont_wdth,wght.ttf', + ), + 'font-family' => 'Open Sans', + 'font-stretch' => 'normal', + 'font-style' => 'normal', + 'font-weight' => '400', + ), + array( + 'src' => array( + '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/open-sans/OpenSans-Italic-VariableFont_wdth,wght.ttf', + ), + 'font-family' => 'Open Sans', + 'font-stretch' => 'normal', + 'font-style' => 'italic', + 'font-weight' => '400', + ), + ), + array( + array( + 'src' => array( + '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Medium.woff2', + ), + 'font-family' => 'DM Sans', + 'font-stretch' => 'normal', + 'font-style' => 'normal', + 'font-weight' => '500', + ), + array( + 'src' => array( + '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Medium-Italic.woff2', + ), + 'font-family' => 'DM Sans', + 'font-stretch' => 'normal', + 'font-style' => 'italic', + 'font-weight' => '500', + ), + ), + ); + + if ( null === $data ) { + $data = array( + 'expected' => $expected_font_families, + ); + } + + if ( isset( $data[ $key ] ) ) { + return $data[ $key ]; + } + + return $data; + } } diff --git a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php new file mode 100644 index 0000000000000..2244c7026c006 --- /dev/null +++ b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php @@ -0,0 +1,55 @@ +assertIsArray( $fonts, 'Should return an array data type' ); + $this->assertEmpty( $fonts, 'Should return an empty array' ); + } + + public function test_should_return_all_fonts_from_all_style_variations() { + switch_theme( static::FONTS_THEME ); + + $actual = WP_Font_Face_Resolver::get_fonts_from_style_variations(); + $expected = self::get_custom_style_variations( 'expected' ); + + $this->assertSame( $expected, $actual, 'All the fonts from the theme variations should be returned.' ); + } + + public function test_should_replace_src_file_placeholder() { + switch_theme( static::FONTS_THEME ); + + $fonts = WP_Font_Face_Resolver::get_fonts_from_style_variations(); + + // check that the there is no theme relative url in the src list. + foreach ( $fonts as $family ) { + foreach ( $family as $font ) { + foreach ( $font['src'] as $src ) { + $this->assertStringNotContainsString( 'file:./', $src, 'Font src should not contain the "file:./" placeholder' ); + } + } + } + } +} From f3ce968a597296ae70c53be6a774b537d1618335 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 17 Oct 2024 14:43:23 -0300 Subject: [PATCH 04/10] add tests for wp_print_font_faces_from_style_variations --- .../font-face/wp-font-face-tests-dataset.php | 12 ++++- .../wpPrintFontFacesFromStyleVariations.php | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php diff --git a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php index b0631b9dd3b49..cd774811214b3 100644 --- a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php +++ b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php @@ -471,9 +471,19 @@ public static function get_custom_style_variations( $key = '' ) { ), ); + $expected_styles = << $expected_font_families, + 'expected' => $expected_font_families, + 'expected_styles' => $expected_styles, ); } diff --git a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php new file mode 100644 index 0000000000000..03240b5d75fc5 --- /dev/null +++ b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php @@ -0,0 +1,45 @@ +expectOutputString( '' ); + wp_print_font_faces_from_style_variations(); + } + + public function test_should_print_fonts_in_style_variations() { + switch_theme( static::FONTS_THEME ); + + $expected = $this->get_custom_style_variations( 'expected_styles' ); + $expected_output = $this->get_expected_styles_output( $expected ); + + $this->expectOutputString( $expected_output ); + wp_print_font_faces_from_style_variations(); + } + + private function get_expected_styles_output( $styles ) { + $style_element = "\n"; + return sprintf( $style_element, $styles ); + } +} From 6a70c04c02a9d7cc7cd3398ed53e0dd73de4848b Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 21 Oct 2024 09:11:50 +1100 Subject: [PATCH 05/10] Use dynamic path to account for different systems. --- .../fonts/font-face/wp-font-face-tests-dataset.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php index cd774811214b3..d410acb7c4124 100644 --- a/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php +++ b/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php @@ -407,12 +407,13 @@ public static function get_custom_font_families( $key = '' ) { public static function get_custom_style_variations( $key = '' ) { static $data = null; + $path = get_stylesheet_directory() . '/assets/fonts/'; $uri = get_stylesheet_directory_uri() . '/assets/fonts/'; $expected_font_families = array( array( array( 'src' => array( - '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Regular.woff2', + "{$path}dm-sans/DMSans-Regular.woff2", ), 'font-family' => 'DM Sans', 'font-stretch' => 'normal', @@ -421,7 +422,7 @@ public static function get_custom_style_variations( $key = '' ) { ), array( 'src' => array( - '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Bold.woff2', + "{$path}dm-sans/DMSans-Bold.woff2", ), 'font-family' => 'DM Sans', 'font-stretch' => 'normal', @@ -432,7 +433,7 @@ public static function get_custom_style_variations( $key = '' ) { array( array( 'src' => array( - '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/open-sans/OpenSans-VariableFont_wdth,wght.ttf', + "{$path}open-sans/OpenSans-VariableFont_wdth,wght.ttf", ), 'font-family' => 'Open Sans', 'font-stretch' => 'normal', @@ -441,7 +442,7 @@ public static function get_custom_style_variations( $key = '' ) { ), array( 'src' => array( - '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/open-sans/OpenSans-Italic-VariableFont_wdth,wght.ttf', + "{$path}open-sans/OpenSans-Italic-VariableFont_wdth,wght.ttf", ), 'font-family' => 'Open Sans', 'font-stretch' => 'normal', @@ -452,7 +453,7 @@ public static function get_custom_style_variations( $key = '' ) { array( array( 'src' => array( - '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Medium.woff2', + "{$path}dm-sans/DMSans-Medium.woff2", ), 'font-family' => 'DM Sans', 'font-stretch' => 'normal', @@ -461,7 +462,7 @@ public static function get_custom_style_variations( $key = '' ) { ), array( 'src' => array( - '/var/www/tests/phpunit/data/themedir1/fonts-block-theme/assets/fonts/dm-sans/DMSans-Medium-Italic.woff2', + "{$path}dm-sans/DMSans-Medium-Italic.woff2", ), 'font-family' => 'DM Sans', 'font-stretch' => 'normal', From b8bd2fd6fb5b058ea1d3ee9312aa09d1783d040e Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 21 Oct 2024 09:17:19 +1100 Subject: [PATCH 06/10] Replace ID with class per earlier commit. --- .../fonts/font-face/wpPrintFontFacesFromStyleVariations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php index 03240b5d75fc5..1888bdff65c37 100644 --- a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php @@ -39,7 +39,7 @@ public function test_should_print_fonts_in_style_variations() { } private function get_expected_styles_output( $styles ) { - $style_element = "\n"; + $style_element = "\n"; return sprintf( $style_element, $styles ); } } From d749a9d93fbbe4d14bd0d3cdc2d8c73845b6c356 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 21 Oct 2024 09:19:14 +1100 Subject: [PATCH 07/10] Set up general before specific per CS. --- .../fonts/font-face/wpPrintFontFacesFromStyleVariations.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php index 1888bdff65c37..dc2f8167f2be1 100644 --- a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php @@ -16,9 +16,8 @@ class Tests_Fonts_WpPrintFontFacesFromStyleVariations extends WP_Font_Face_UnitT const FONTS_THEME = 'fonts-block-theme'; public static function set_up_before_class() { - self::$requires_switch_theme_fixtures = true; - parent::set_up_before_class(); + self::$requires_switch_theme_fixtures = true; } public function test_should_not_print_when_no_fonts() { From 24a77bcb4c841ac8a068851f323970d27b2e0171 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 21 Oct 2024 09:24:52 +1100 Subject: [PATCH 08/10] Add docblocks and ticket references. --- .../getFontsFromStyleVariations.php | 15 +++++++++++++++ .../wpPrintFontFacesFromStyleVariations.php | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php index 2244c7026c006..c829205836a14 100644 --- a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php @@ -21,6 +21,11 @@ public static function set_up_before_class() { parent::set_up_before_class(); } + /** + * Ensure that an empty array is returned when the theme has no style variations. + * + * @ticket 62231 + */ public function test_should_return_empty_array_when_theme_has_no_style_variations() { switch_theme( 'block-theme' ); @@ -29,6 +34,11 @@ public function test_should_return_empty_array_when_theme_has_no_style_variation $this->assertEmpty( $fonts, 'Should return an empty array' ); } + /** + * Ensure that all variations are loaded from a theme. + * + * @ticket 62231 + */ public function test_should_return_all_fonts_from_all_style_variations() { switch_theme( static::FONTS_THEME ); @@ -38,6 +48,11 @@ public function test_should_return_all_fonts_from_all_style_variations() { $this->assertSame( $expected, $actual, 'All the fonts from the theme variations should be returned.' ); } + /** + * Ensure that file:./ is replaced in the src list. + * + * @ticket 62231 + */ public function test_should_replace_src_file_placeholder() { switch_theme( static::FONTS_THEME ); diff --git a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php index dc2f8167f2be1..a59ba882a4e86 100644 --- a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php @@ -20,6 +20,11 @@ public static function set_up_before_class() { self::$requires_switch_theme_fixtures = true; } + /** + * Ensure that no fonts are printed when the theme has no fonts. + * + * @ticket 62231 + */ public function test_should_not_print_when_no_fonts() { switch_theme( 'block-theme' ); @@ -27,6 +32,11 @@ public function test_should_not_print_when_no_fonts() { wp_print_font_faces_from_style_variations(); } + /** + * Ensure that all fonts are printed from the theme style variations. + * + * @ticket 62231 + */ public function test_should_print_fonts_in_style_variations() { switch_theme( static::FONTS_THEME ); From 725c506dd43260d88a2b80fe6aa0c31bcf185692 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 21 Oct 2024 09:25:56 +1100 Subject: [PATCH 09/10] Sentence case for comment. --- .../wpFontFaceResolver/getFontsFromStyleVariations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php index c829205836a14..51f382062efba 100644 --- a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php @@ -58,7 +58,7 @@ public function test_should_replace_src_file_placeholder() { $fonts = WP_Font_Face_Resolver::get_fonts_from_style_variations(); - // check that the there is no theme relative url in the src list. + // Check that the there is no theme relative url in the src list. foreach ( $fonts as $family ) { foreach ( $family as $font ) { foreach ( $font['src'] as $src ) { From 32c353453e3b54646e25cecff9486eee335c9420 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 21 Oct 2024 09:37:10 +1100 Subject: [PATCH 10/10] Include file basename in message for uniqueness. --- .../wpFontFaceResolver/getFontsFromStyleVariations.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php index 51f382062efba..0087dac087292 100644 --- a/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromStyleVariations.php @@ -62,7 +62,8 @@ public function test_should_replace_src_file_placeholder() { foreach ( $fonts as $family ) { foreach ( $family as $font ) { foreach ( $font['src'] as $src ) { - $this->assertStringNotContainsString( 'file:./', $src, 'Font src should not contain the "file:./" placeholder' ); + $src_basename = basename( $src ); + $this->assertStringNotContainsString( 'file:./', $src, "Font $src_basename should not contain the 'file:./' placeholder" ); } } }