Skip to content

Commit

Permalink
Registered fonts for Site Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed May 22, 2023
1 parent ffc592a commit 84091a2
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 19 deletions.
21 changes: 13 additions & 8 deletions lib/experimental/fonts-api/fonts-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,34 +189,39 @@ function wp_register_font_provider( $name, $classname ) {
* An empty array if none were processed.
*/
function wp_print_fonts( $handles = false ) {
global $pagenow;

$wp_fonts = wp_fonts();
$registered = $wp_fonts->get_registered_font_families();
$in_iframed_editor = true === $handles;
$is_site_editor = $in_iframed_editor && 'site-editor.php' === $pagenow;

// Nothing to print, as no fonts are registered.
if ( empty( $registered ) ) {
return array();
}

if ( empty( $handles ) ) {
// Automatically enqueue all user-selected fonts.
WP_Fonts_Resolver::enqueue_user_selected_fonts();
$handles = false;
} elseif ( $in_iframed_editor ) {
// Print all registered fonts for the iframed editor.
// Print all registered fonts for the Site Editor.
if ( $is_site_editor ) {
$queue = $wp_fonts->queue;
$done = $wp_fonts->done;
$wp_fonts->done = array();
$wp_fonts->queue = $registered;
$handles = false;
}

// Automatically enqueue all user-selected fonts.
if ( empty( $handles ) || ( $in_iframed_editor && ! $is_site_editor ) ) {
WP_Fonts_Resolver::enqueue_user_selected_fonts();
$handles = false;
}

_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

$printed = $wp_fonts->do_items( $handles );

// Reset the API.
if ( $in_iframed_editor ) {
// Restore the API state.
if ( $is_site_editor ) {
$wp_fonts->done = $done;
$wp_fonts->queue = $queue;
}
Expand Down
114 changes: 103 additions & 11 deletions phpunit/fonts-api/wpPrintFonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
* @covers ::wp_print_fonts
*/
class Tests_Fonts_WpPrintFonts extends WP_Fonts_TestCase {
private static $pagenow = '';

public static function set_up_before_class() {
self::$requires_switch_theme_fixtures = true;

parent::set_up_before_class();

static::set_up_admin_user();
self::$pagenow = $GLOBALS['pagenow'];
}

public function tear_down() {
$GLOBALS['pagenow'] = self::$pagenow;
parent::tear_down();
}

public function test_should_return_empty_array_when_no_fonts_registered() {
Expand Down Expand Up @@ -113,13 +120,21 @@ public function test_should_print_handles_when_not_enqueued( $setup, $expected_d
}

/**
* @dataProvider data_should_print_all_registered_fonts_for_iframed_editor
* @dataProvider data_should_print_registered_fonts_for_site_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 );
public function test_should_print_registered_fonts_for_site_editor( $fonts, $expected ) {
// Set up the Site Editor.
global $pagenow;
$pagenow = 'site-editor.php';
set_current_screen( 'site-editor' );

$setup = array(
'registered' => $fonts,
);
$this->setup_integrated_deps( $setup, wp_fonts(), false );

$this->expectOutputString( $expected['output'] );
$actual_done = wp_print_fonts( true );
Expand All @@ -131,7 +146,7 @@ public function test_should_print_all_registered_fonts_for_iframed_editor( $font
*
* @return array
*/
public function data_should_print_all_registered_fonts_for_iframed_editor() {
public function data_should_print_registered_fonts_for_site_editor() {
$local_fonts = $this->get_registered_local_fonts();
$font_faces = $this->get_registered_fonts_css();

Expand All @@ -141,7 +156,7 @@ public function data_should_print_all_registered_fonts_for_iframed_editor() {
'expected' => array(
'done' => array( 'merriweather', 'merriweather-200-900-normal' ),
'output' => sprintf(
"<style id='wp-fonts-local' type='text/css'>\n%s\n</style>\n",
"<style id='wp-fonts-local'>\n%s\n</style>\n",
$font_faces['merriweather-200-900-normal']
),
),
Expand All @@ -151,7 +166,7 @@ public function data_should_print_all_registered_fonts_for_iframed_editor() {
'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",
"<style id='wp-fonts-local'>\n%s%s\n</style>\n",
$font_faces['Source Serif Pro-300-normal'],
$font_faces['Source Serif Pro-900-italic']
),
Expand All @@ -168,7 +183,80 @@ public function data_should_print_all_registered_fonts_for_iframed_editor() {
'Source Serif Pro-900-italic',
),
'output' => sprintf(
"<style id='wp-fonts-local' type='text/css'>\n%s%s%s\n</style>\n",
"<style id='wp-fonts-local'>\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']
),
),
),
);
}

/**
* @dataProvider data_should_print_enqueued_fonts_for_non_site_editor
*
* @param string $fonts Fonts to enqueue.
* @param array $expected Expected results.
*/
public function test_should_print_enqueued_fonts_for_non_site_editor( $fonts, $expected ) {
global $pagenow;
$pagenow = 'post.php';
set_current_screen( 'edit-post' );

$setup = array(
'registered' => $this->get_registered_local_fonts(),
'enqueued' => $fonts,
);
$this->setup_integrated_deps( $setup, wp_fonts() );

$this->expectOutputString( $expected['output'] );
$actual_done = wp_print_fonts( true );
$this->assertSameSets( $expected['done'], $actual_done, 'All enqueued font-family handles should be returned' );
}

/**
* Data provider.
*
* @return array
*/
public function data_should_print_enqueued_fonts_for_non_site_editor() {
$font_faces = $this->get_registered_fonts_css();

return array(
'Merriweather with 1 variation' => array(
'fonts' => array( 'merriweather' ),
'expected' => array(
'done' => array( 'merriweather', 'merriweather-200-900-normal' ),
'output' => sprintf(
"<style id='wp-fonts-local'>\n%s\n</style>\n",
$font_faces['merriweather-200-900-normal']
),
),
),
'Source Serif Pro with 2 variations' => array(
'fonts' => array( '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'>\n%s%s\n</style>\n",
$font_faces['Source Serif Pro-300-normal'],
$font_faces['Source Serif Pro-900-italic']
),
),
),
'all fonts' => array(
'fonts' => array( 'merriweather', 'source-serif-pro' ),
'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'>\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']
Expand Down Expand Up @@ -212,11 +300,15 @@ public function test_should_print_user_selected_fonts( $global_styles, $expected
* @param bool $enqueue Whether to enqueue. Default true.
*/
private function setup_integrated_deps( array $setup, $wp_fonts, $enqueue = true ) {
foreach ( $setup['provider'] as $provider ) {
$wp_fonts->register_provider( $provider['id'], $provider['class'] );
if ( isset( $setup['provider'] ) ) {
foreach ( $setup['provider'] as $provider ) {
$wp_fonts->register_provider( $provider['id'], $provider['class'] );
}
}
foreach ( $setup['registered'] as $handle => $variations ) {
$this->setup_register( $handle, $variations, $wp_fonts );
if ( isset( $setup['registered'] ) ) {
foreach ( $setup['registered'] as $handle => $variations ) {
$this->setup_register( $handle, $variations, $wp_fonts );
}
}

if ( $enqueue ) {
Expand Down

0 comments on commit 84091a2

Please sign in to comment.