Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fonts API] Automatically enqueue user-selected global fonts #50529

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adds user-select fonts enqueuer.
  • Loading branch information
hellofromtonya committed May 10, 2023
commit ab24c0af740174798e40397f5764bff4dec42e85
102 changes: 102 additions & 0 deletions lib/experimental/fonts-api/class-wp-fonts-resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* WP_Fonts_Resolver class.
*
* @package WordPress
* @subpackage Fonts API
* @since X.X.X
*/

if ( class_exists( 'WP_Fonts_Resolver' ) ) {
return;
}

/**
* The Fonts API Resolver abstracts the processing of different data sources
* (such as theme.json and global styles) for font interactions with the API.
*
* This class is for internal core usage and is not supposed to be used by
* extenders (plugins and/or themes).
*
* @access private
*/
class WP_Fonts_Resolver {
/**
* Defines the key structure in global styles to the fontFamily
* user-selected font.
*
* @since X.X.X
*
* @var string[][]
*/
protected static $global_styles_font_family_structure = array(
array( 'elements', 'link', 'typography', 'fontFamily' ),
array( 'elements', 'heading', 'typography', 'fontFamily' ),
array( 'elements', 'caption', 'typography', 'fontFamily' ),
array( 'elements', 'button', 'typography', 'fontFamily' ),
array( 'typography', 'fontFamily' ),
);

/**
* Enqueues user-selected fonts via global styles.
*
* @since X.X.X
*
* @return array User selected font-families when exists, else empty array.
*/
public static function enqueue_user_selected_fonts() {
$global_styles = wp_get_global_styles();
$user_selected_fonts = static::get_user_selected_fonts( $global_styles );
if ( empty( $user_selected_fonts ) ) {
return array();
}

wp_enqueue_fonts( $user_selected_fonts );
return $user_selected_fonts;
}

/**
* Gets the user-selected font-family handles.
*
* @since X.X.X
*
* @param array $global_styles Global styles potentially containing user-selected fonts.
* @return array User-selected font-families.
*/
private static function get_user_selected_fonts( array $global_styles ) {
$font_families = array();

foreach ( static::$global_styles_font_family_structure as $path ) {
$style_value = _wp_array_get( $global_styles, $path, '' );

$font_family = static::get_value_from_style( $style_value );
if ( '' !== $font_family ) {
$font_families[] = $font_family;
}
}

return array_unique( $font_families );
}

/**
* Get the value (i.e. preset slug) from the given style value.
*
* @since X.X.X
*
* @param string $style The style to parse.
* @param string $preset_type Optional. The type to parse. Default 'font-family'.
* @return string Preset slug.
*/
private static function get_value_from_style( $style, $preset_type = 'font-family' ) {
if ( '' === $style ) {
return '';
}

$starting_pattern = "var:preset|{$preset_type}|";
if ( ! str_starts_with( $style, $starting_pattern ) ) {
return '';
}

return substr( $style, strlen( $starting_pattern ) );
}
}
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/fonts-api/register-fonts-from-theme-json.php';
require __DIR__ . '/experimental/fonts-api/class-wp-fonts.php';
require __DIR__ . '/experimental/fonts-api/class-wp-fonts-provider-local.php';
require __DIR__ . '/experimental/fonts-api/class-wp-fonts-resolver.php';
require __DIR__ . '/experimental/fonts-api/fonts-api.php';

// BC Layer files, which will not be backported to WP Core.
require __DIR__ . '/experimental/fonts-api/bc-layer/class-gutenberg-fonts-api-bc-layer.php';
require __DIR__ . '/experimental/fonts-api/bc-layer/webfonts-deprecations.php';
Expand Down
256 changes: 256 additions & 0 deletions phpunit/fonts-api/wpFontsResolver/enqueueUserSelectedFonts-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php
/**
* WP_Fonts_Resolver::enqueue_user_selected_fonts() tests.
*
* @package WordPress
* @subpackage Fonts API
*/

require_once __DIR__ . '/../wp-fonts-testcase.php';

/**
* @group fontsapi
* @covers WP_Fonts_Resolver::enqueue_user_selected_fonts
*/
class Tests_Fonts_WpFontsResolver_EnqueueUserSelectedFonts extends WP_Fonts_TestCase {
const FONTS_THEME = 'fonts-block-theme';
/**
* Administrator ID.
*
* @var int
*/
private static $administrator_id = 0;

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

parent::set_up_before_class();

self::$administrator_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_email' => 'administrator@example.com',
)
);
}

/**
* @dataProvider data_should_not_enqueue_when_no_user_selected_fonts
*
* @param array $styles Optional. Test styles. Default empty array.
*/
public function test_should_not_enqueue_when_no_user_selected_fonts( $styles = array() ) {
$this->set_up_global_styles( $styles );

$mock = $this->set_up_mock( 'enqueue' );
$mock->expects( $this->never() )
->method( 'enqueue' );

$expected = array();
$this->assertSame( $expected, WP_Fonts_Resolver::enqueue_user_selected_fonts() );
}

/**
* Data provider.
*
* @return array
*/
public function data_should_not_enqueue_when_no_user_selected_fonts() {
return array(
'no user-selected styles' => array(),
'invalid element' => array(
array(
'elements' => array(
'invalid' => array(
'typography' => array(
'fontFamily' => 'var:preset|font-family|font1',
'fontStyle' => 'normal',
'fontWeight' => '400',
),
),
),
),
),
);
}

/**
* @dataProvider data_should_enqueue_when_user_selected_fonts
*
* @param array $styles Test styles.
* @param array $expected Expected results.
*/
public function test_should_enqueue_when_user_selected_fonts( $styles, $expected ) {
$mock = $this->set_up_mock( 'enqueue' );
$mock->expects( $this->once() )
->method( 'enqueue' )
->with(
$this->identicalTo( $expected )
);

$this->set_up_global_styles( $styles );

$this->assertSameSets( $expected, WP_Fonts_Resolver::enqueue_user_selected_fonts() );
}

/**
* Data provider.
*
* @return array
*/
public function data_should_enqueue_when_user_selected_fonts() {
$fonts = array(
'font1-400-normal' => array(
'fontFamily' => 'var:preset|font-family|font1',
'fontStyle' => 'normal',
'fontWeight' => '400',
),
'font1-400-italic' => array(
'fontFamily' => 'var:preset|font-family|font1',
'fontStyle' => 'italic',
'fontWeight' => '400',
),
'font2-600-normal' => array(
'fontFamily' => 'var:preset|font-family|font2',
'fontStyle' => 'normal',
'fontWeight' => '600',
),
'font2-600-italic' => array(
'fontFamily' => 'var:preset|font-family|font2',
'fontStyle' => 'italic',
'fontWeight' => '600',
),
'font3-900-normal' => array(
'fontFamily' => 'var:preset|font-family|font3',
'fontStyle' => 'normal',
'fontWeight' => '900',
),
);

return array(
'link' => array(
'styles' => array(
'elements' => array(
'link' => array(
'typography' => $fonts['font1-400-italic'],
),
),
),
'expected' => array( 'font1' ),
),
'heading' => array(
'styles' => array(
'elements' => array(
'heading' => array(
'typography' => $fonts['font2-600-italic'],
),
),
),
'expected' => array( 'font2' ),
),
'caption' => array(
'styles' => array(
'elements' => array(
'caption' => array(
'typography' => $fonts['font2-600-normal'],
),
),
),
'expected' => array( 'font2' ),
),
'button' => array(
'styles' => array(
'elements' => array(
'button' => array(
'typography' => $fonts['font1-400-normal'],
),
),
),
'expected' => array( 'font1' ),
),
'text' => array(
'styles' => array(
'typography' => $fonts['font1-400-normal'],
),
'expected' => array( 'font1' ),
),
'all elements' => array(
'styles' => array(
'elements' => array(
'link' => array(
'typography' => $fonts['font1-400-italic'],
),
'heading' => array(
'typography' => $fonts['font2-600-italic'],
),
'caption' => array(
'typography' => $fonts['font2-600-normal'],
),
'button' => array(
'typography' => $fonts['font1-400-normal'],
),
),
),
'expected' => array( 'font1', 'font2' ),
),
'all elements and text' => array(
'styles' => array(
'elements' => array(
'link' => array(
'typography' => $fonts['font1-400-italic'],
),
'heading' => array(
'typography' => $fonts['font2-600-italic'],
),
'caption' => array(
'typography' => $fonts['font3-900-normal'],
),
'button' => array(
'typography' => $fonts['font1-400-normal'],
),
),
'typography' => $fonts['font1-400-normal'],
),
'expected' => array( 'font1', 'font2', 'font3' ),
),
'with invalid element' => array(
'styles' => array(
'elements' => array(
'button' => array(
'typography' => $fonts['font1-400-normal'],
),
'invalid' => array(
'typography' => $fonts['font3-900-normal'],
),
),
),
'expected' => array( 'font1' ),
),
);
}

/**
* Sets up the global styles.
*
* @param array $styles User-selected styles structure.
*/
private function set_up_global_styles( array $styles ) {
switch_theme( static::FONTS_THEME );

if ( empty( $styles ) ) {
return;
}

// Make sure there is data from the user origin.
wp_set_current_user( self::$administrator_id );
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true );
$config = json_decode( $user_cpt['post_content'], true );

// Add the test styles.
$config['styles'] = $styles;

// Update the global styles and settings post.
$user_cpt['post_content'] = wp_json_encode( $config );
wp_update_post( $user_cpt, true, false );
}
}