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 fonts #50297

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 ) {
Copy link
Member

Choose a reason for hiding this comment

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

👋 What's the intention of this code? My reading is that we are using the var:preset|... syntax to detect whether the font is provided by the user or anyone else (theme, default). This doesn't work, as the theme can also use the var:... syntax.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I ran into this while fixing this issue #50499 See also core-editor slack https://wordpress.slack.com/archives/C02QB2JS7/p1683704524419649

Copy link
Contributor Author

@hellofromtonya hellofromtonya May 10, 2023

Choose a reason for hiding this comment

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

The intent of this code is to get all of the user "selected" fonts, meaning the fonts a user selects when using Site Editor > Styles > Typography UI.

Theme fonts defined in its theme.json are already automatically enqueued after registering all of its fonts.

Before this PR, plugins had to detect if a user selected any of their fonts and then the plugin had to enqueue those fonts. With this PR, the detection and enqueuing of their fonts is done within the API.

$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 ) );
}
}
24 changes: 11 additions & 13 deletions lib/experimental/fonts-api/fonts-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,17 @@ function wp_print_fonts( $handles = false ) {
return array();
}

// 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;
}
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.
$queue = $wp_fonts->queue;
$done = $wp_fonts->done;
$wp_fonts->done = array();
$wp_fonts->queue = $registered;
$handles = false;
}

_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
Expand Down
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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
Loading