Skip to content

Commit

Permalink
[Fonts API] Relocate which fonts to print into wp_print_fonts() (#50151)
Browse files Browse the repository at this point in the history
Moves decision-making logic for iframed editor from the compat script-loader files into the Fonts API.
  • Loading branch information
hellofromtonya authored May 8, 2023
1 parent 05ad081 commit fa1d3b0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 31 deletions.
27 changes: 5 additions & 22 deletions lib/compat/wordpress-6.2/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( true );
$styles .= ob_get_clean();
}

return array(
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.3/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function _gutenberg_get_iframed_editor_assets() {

ob_start();
wp_print_styles();
wp_print_fonts();
wp_print_fonts( true );
$styles = ob_get_clean();

ob_start();
Expand Down
37 changes: 29 additions & 8 deletions lib/experimental/fonts-api/fonts-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,49 @@ 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[]|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 ) {
$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 ) ) {
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 ( $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;
}
}

_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;
}
}

Expand Down
66 changes: 66 additions & 0 deletions phpunit/fonts-api/wpPrintFonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( 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(
"<style id='wp-fonts-local' type='text/css'>\n%s\n</style>\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(
"<style id='wp-fonts-local' type='text/css'>\n%s%s\n</style>\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(
"<style id='wp-fonts-local' type='text/css'>\n%s%s%s\n</style>\n",
$font_faces['merriweather-200-900-normal'],
$font_faces['Source Serif Pro-300-normal'],
$font_faces['Source Serif Pro-900-italic']
),
),
),
);
}
}

1 comment on commit fa1d3b0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in fa1d3b0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4919156662
📝 Reported issues:

Please sign in to comment.