From 0af2d5667a21921993449372e619b8411b401119 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 1 May 2023 16:13:04 -0500 Subject: [PATCH 1/7] Relocate from 6.2 script-loader --- lib/compat/wordpress-6.2/script-loader.php | 27 ++++---------------- lib/experimental/fonts-api/fonts-api.php | 29 ++++++++++++++++++---- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/compat/wordpress-6.2/script-loader.php b/lib/compat/wordpress-6.2/script-loader.php index 149a6a18e14507..fa21bb531f7522 100644 --- a/lib/compat/wordpress-6.2/script-loader.php +++ b/lib/compat/wordpress-6.2/script-loader.php @@ -129,28 +129,11 @@ function gutenberg_resolve_assets_override() { $scripts = ob_get_clean(); - /* - * Generate font @font-face styles for the site editor iframe. - * Use the registered font families for printing. - */ - if ( class_exists( 'WP_Fonts' ) ) { - $wp_fonts = wp_fonts(); - $registered = $wp_fonts->get_registered_font_families(); - if ( ! empty( $registered ) ) { - $queue = $wp_fonts->queue; - $done = $wp_fonts->done; - - $wp_fonts->done = array(); - $wp_fonts->queue = $registered; - - ob_start(); - $wp_fonts->do_items(); - $styles .= ob_get_clean(); - - // Reset the Web Fonts API. - $wp_fonts->done = $done; - $wp_fonts->queue = $queue; - } + // Generate font @font-face styles. + if ( function_exists( 'wp_print_fonts' ) ) { + ob_start(); + wp_print_fonts( false, true ); + $styles .= ob_get_clean(); } return array( diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 85d0f94ccc96fc..f440a8656c5010 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -176,13 +176,15 @@ function wp_register_font_provider( $name, $classname ) { * * @since X.X.X * - * @param string|string[]|false $handles Optional. Items to be processed: queue (false), - * single item (string), or multiple items (array of strings). - * Default false. + * @param string|string[]|false $handles Optional. Items to be processed: queue (false), + * single item (string), or multiple items (array of strings). + * Default false. + * @param bool $for_iframed_editor Optional. Whether the fonts are for iframed editor. + * Default false. * @return array|string[] Array of font handles that have been processed. * An empty array if none were processed. */ - function wp_print_fonts( $handles = false ) { + function wp_print_fonts( $handles = false, $for_iframed_editor = false ) { $wp_fonts = wp_fonts(); $registered = $wp_fonts->get_registered_font_families(); @@ -197,7 +199,24 @@ function wp_print_fonts( $handles = false ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); - return $wp_fonts->do_items( $handles ); + // Print all registered fonts for the iframed editor. + if ( $for_iframed_editor ) { + $queue = $wp_fonts->queue; + $done = $wp_fonts->done; + + $wp_fonts->done = array(); + $wp_fonts->queue = $registered; + } + + $printed_fonts = $wp_fonts->do_items( $handles ); + + // Reset after printing. + if ( $for_iframed_editor ) { + $wp_fonts->done = $done; + $wp_fonts->queue = $queue; + } + + return $printed_fonts; } } From 6147c9bc16205fe14d93ab1c5962965f5ddf9064 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 1 May 2023 16:14:08 -0500 Subject: [PATCH 2/7] Add flag to 6.3-script-loader call --- lib/compat/wordpress-6.3/script-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/script-loader.php b/lib/compat/wordpress-6.3/script-loader.php index c735f3b8a792a7..d271330e632b8f 100644 --- a/lib/compat/wordpress-6.3/script-loader.php +++ b/lib/compat/wordpress-6.3/script-loader.php @@ -56,7 +56,7 @@ function _gutenberg_get_iframed_editor_assets() { ob_start(); wp_print_styles(); - wp_print_fonts(); + wp_print_fonts( false, true ); $styles = ob_get_clean(); ob_start(); From 6dd2c358f4aaeea8df700cac5e733158d94494b8 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 2 May 2023 09:59:14 -0500 Subject: [PATCH 3/7] Add tests --- phpunit/fonts-api/wpPrintFonts-test.php | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/phpunit/fonts-api/wpPrintFonts-test.php b/phpunit/fonts-api/wpPrintFonts-test.php index d0108eec14faff..940203e9059820 100644 --- a/phpunit/fonts-api/wpPrintFonts-test.php +++ b/phpunit/fonts-api/wpPrintFonts-test.php @@ -123,4 +123,70 @@ private function setup_integrated_deps( array $setup, $wp_fonts, $enqueue = true $wp_fonts->enqueue( $setup['enqueued'] ); } } + + /** + * @dataProvider data_should_print_all_registered_fonts_for_iframed_editor + * + * @param string $fonts Fonts to register. + * @param array $expected Expected results. + */ + public function test_should_print_all_registered_fonts_for_iframed_editor( $fonts, $expected ) { + wp_register_fonts( $fonts ); + + $this->expectOutputString( $expected['output'] ); + $actual_done = wp_print_fonts( false, true ); + $this->assertSameSets( $expected['done'], $actual_done, 'All registered font-family handles should be returned' ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_print_all_registered_fonts_for_iframed_editor() { + $local_fonts = $this->get_registered_local_fonts(); + $font_faces = $this->get_registered_fonts_css(); + + return array( + 'Merriweather with 1 variation' => array( + 'fonts' => array( 'merriweather' => $local_fonts['merriweather'] ), + 'expected' => array( + 'done' => array( 'merriweather', 'merriweather-200-900-normal' ), + 'output' => sprintf( + "\n", + $font_faces['merriweather-200-900-normal'] + ), + ), + ), + 'Source Serif Pro with 2 variations' => array( + 'fonts' => array( 'Source Serif Pro' => $local_fonts['Source Serif Pro'] ), + 'expected' => array( + 'done' => array( 'source-serif-pro', 'Source Serif Pro-300-normal', 'Source Serif Pro-900-italic' ), + 'output' => sprintf( + "\n", + $font_faces['Source Serif Pro-300-normal'], + $font_faces['Source Serif Pro-900-italic'] + ), + ), + ), + 'all fonts' => array( + 'fonts' => $local_fonts, + 'expected' => array( + 'done' => array( + 'merriweather', + 'merriweather-200-900-normal', + 'source-serif-pro', + 'Source Serif Pro-300-normal', + 'Source Serif Pro-900-italic', + ), + 'output' => sprintf( + "\n", + $font_faces['merriweather-200-900-normal'], + $font_faces['Source Serif Pro-300-normal'], + $font_faces['Source Serif Pro-900-italic'] + ), + ), + ), + ); + } } From a64362c5cf4899050fc59dec21428b5830d14ad1 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 2 May 2023 10:01:48 -0500 Subject: [PATCH 4/7] Assign registered to $handles. This simplifies the code as it removes the need to modify and then reset the `done` and `queue` properties. --- lib/experimental/fonts-api/fonts-api.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index f440a8656c5010..afa72a65b0af3a 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -201,22 +201,10 @@ function wp_print_fonts( $handles = false, $for_iframed_editor = false ) { // Print all registered fonts for the iframed editor. if ( $for_iframed_editor ) { - $queue = $wp_fonts->queue; - $done = $wp_fonts->done; - - $wp_fonts->done = array(); - $wp_fonts->queue = $registered; - } - - $printed_fonts = $wp_fonts->do_items( $handles ); - - // Reset after printing. - if ( $for_iframed_editor ) { - $wp_fonts->done = $done; - $wp_fonts->queue = $queue; + $handles = $registered; } - return $printed_fonts; + return $wp_fonts->do_items( $handles ); } } From d95eb1ceb3c8317b8fe298f2e7386f5be16183bd Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 2 May 2023 10:52:27 -0500 Subject: [PATCH 5/7] Set $handles to true --- lib/compat/wordpress-6.2/script-loader.php | 2 +- lib/compat/wordpress-6.3/script-loader.php | 2 +- lib/experimental/fonts-api/fonts-api.php | 13 ++++++------- phpunit/fonts-api/wpPrintFonts-test.php | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/compat/wordpress-6.2/script-loader.php b/lib/compat/wordpress-6.2/script-loader.php index fa21bb531f7522..142cbf4e1477b3 100644 --- a/lib/compat/wordpress-6.2/script-loader.php +++ b/lib/compat/wordpress-6.2/script-loader.php @@ -132,7 +132,7 @@ function gutenberg_resolve_assets_override() { // Generate font @font-face styles. if ( function_exists( 'wp_print_fonts' ) ) { ob_start(); - wp_print_fonts( false, true ); + wp_print_fonts( true ); $styles .= ob_get_clean(); } diff --git a/lib/compat/wordpress-6.3/script-loader.php b/lib/compat/wordpress-6.3/script-loader.php index d271330e632b8f..8b00e10d09b660 100644 --- a/lib/compat/wordpress-6.3/script-loader.php +++ b/lib/compat/wordpress-6.3/script-loader.php @@ -56,7 +56,7 @@ function _gutenberg_get_iframed_editor_assets() { ob_start(); wp_print_styles(); - wp_print_fonts( false, true ); + wp_print_fonts( true ); $styles = ob_get_clean(); ob_start(); diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index afa72a65b0af3a..84f931cf53b9b8 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -176,15 +176,14 @@ function wp_register_font_provider( $name, $classname ) { * * @since X.X.X * - * @param string|string[]|false $handles Optional. Items to be processed: queue (false), - * single item (string), or multiple items (array of strings). - * Default false. - * @param bool $for_iframed_editor Optional. Whether the fonts are for iframed editor. - * Default false. + * @param string|string[]|bool $handles Optional. Items to be processed: queue (false), + * for iframed editor assets (true), single item (string), + * or multiple items (array of strings). + * Default false. * @return array|string[] Array of font handles that have been processed. * An empty array if none were processed. */ - function wp_print_fonts( $handles = false, $for_iframed_editor = false ) { + function wp_print_fonts( $handles = false ) { $wp_fonts = wp_fonts(); $registered = $wp_fonts->get_registered_font_families(); @@ -200,7 +199,7 @@ function wp_print_fonts( $handles = false, $for_iframed_editor = false ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); // Print all registered fonts for the iframed editor. - if ( $for_iframed_editor ) { + if ( true === $handles ) { $handles = $registered; } diff --git a/phpunit/fonts-api/wpPrintFonts-test.php b/phpunit/fonts-api/wpPrintFonts-test.php index 940203e9059820..cb500aaa0430f4 100644 --- a/phpunit/fonts-api/wpPrintFonts-test.php +++ b/phpunit/fonts-api/wpPrintFonts-test.php @@ -134,7 +134,7 @@ public function test_should_print_all_registered_fonts_for_iframed_editor( $font wp_register_fonts( $fonts ); $this->expectOutputString( $expected['output'] ); - $actual_done = wp_print_fonts( false, true ); + $actual_done = wp_print_fonts( true ); $this->assertSameSets( $expected['done'], $actual_done, 'All registered font-family handles should be returned' ); } From fd7ce3332255f340ca0bfc6f9fee207224ccb1fa Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 3 May 2023 09:26:37 -0500 Subject: [PATCH 6/7] Combine ifs into if/elseif + micro-optimization Combines the empty() and true checks into a if/elseif structure. For a tiny micro-optimization, wraps these checks within a not equal to `false`. Why? When using the default (i.e. on the front-end and with classic themes): * Skips running `empty()`. * Skips reassigning `false` to `$handle`. These skips gain a tiny tiny performance boost. --- lib/experimental/fonts-api/fonts-api.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 84f931cf53b9b8..3c24d04ee11867 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -192,17 +192,19 @@ function wp_print_fonts( $handles = false ) { return array(); } - if ( empty( $handles ) ) { - $handles = false; + // Skip this reassignment decision-making when using the default of `false`. + if ( false !== $handles ) { + // When `true`, print all registered fonts for the iframed editor. + if ( true === $handles ) { + $handles = $registered; + } elseif ( empty( $handles ) ) { + // When falsey, assign `false` to print enqueued fonts. + $handles = false; + } } _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); - // Print all registered fonts for the iframed editor. - if ( true === $handles ) { - $handles = $registered; - } - return $wp_fonts->do_items( $handles ); } } From dfd8657483d976df5829ed96adfc75a5175460e1 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 8 May 2023 14:08:45 -0500 Subject: [PATCH 7/7] Restore overwriting queue and done. Printing `$handles` does not work (for some reason that needs to be investigated further). Overwriting the queue and done properties does print and has been working since 6.2 alpha. This commit restores the overwriting properties approach. --- lib/experimental/fonts-api/fonts-api.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/experimental/fonts-api/fonts-api.php b/lib/experimental/fonts-api/fonts-api.php index 3c24d04ee11867..1d04f3aad2cca4 100644 --- a/lib/experimental/fonts-api/fonts-api.php +++ b/lib/experimental/fonts-api/fonts-api.php @@ -184,8 +184,9 @@ function wp_register_font_provider( $name, $classname ) { * An empty array if none were processed. */ function wp_print_fonts( $handles = false ) { - $wp_fonts = wp_fonts(); - $registered = $wp_fonts->get_registered_font_families(); + $wp_fonts = wp_fonts(); + $registered = $wp_fonts->get_registered_font_families(); + $in_iframed_editor = true === $handles; // Nothing to print, as no fonts are registered. if ( empty( $registered ) ) { @@ -195,8 +196,12 @@ function wp_print_fonts( $handles = false ) { // Skip this reassignment decision-making when using the default of `false`. if ( false !== $handles ) { // When `true`, print all registered fonts for the iframed editor. - if ( true === $handles ) { - $handles = $registered; + if ( $in_iframed_editor ) { + $queue = $wp_fonts->queue; + $done = $wp_fonts->done; + $wp_fonts->done = array(); + $wp_fonts->queue = $registered; + $handles = false; } elseif ( empty( $handles ) ) { // When falsey, assign `false` to print enqueued fonts. $handles = false; @@ -205,7 +210,15 @@ function wp_print_fonts( $handles = false ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); - return $wp_fonts->do_items( $handles ); + $printed = $wp_fonts->do_items( $handles ); + + // Reset the API. + if ( $in_iframed_editor ) { + $wp_fonts->done = $done; + $wp_fonts->queue = $queue; + } + + return $printed; } }