-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
hellofromtonya
merged 5 commits into
trunk
from
try/automatic-enqueue-user-selected-global-fonts
May 22, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab24c0a
Adds user-select fonts enqueuer.
hellofromtonya a2ee32e
Auto queue before printing
hellofromtonya 4c8b071
Always set $handles to false when empty
hellofromtonya fe2bd0d
Use WP_Theme_JSON_Resolver_Gutenberg::get_user_data().
hellofromtonya f87724b
Adds print tests. Moves datasets to trait.
hellofromtonya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?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() { | ||
$user_selected_fonts = array(); | ||
$user_global_styles = WP_Theme_JSON_Resolver_Gutenberg::get_user_data()->get_raw_data(); | ||
if ( isset( $user_global_styles['styles'] ) ) { | ||
$user_selected_fonts = static::get_user_selected_fonts( $user_global_styles['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(--wp--preset--{$preset_type}--"; | ||
$ending_pattern = ')'; | ||
if ( ! str_starts_with( $style, $starting_pattern ) ) { | ||
return ''; | ||
} | ||
|
||
$offset = strlen( $starting_pattern ); | ||
$length = strpos( $style, $ending_pattern ) - $offset; | ||
return substr( $style, $offset, $length ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,13 @@ abstract class WP_Fonts_TestCase extends WP_UnitTestCase { | |
*/ | ||
protected $orig_theme_dir; | ||
|
||
/** | ||
* Administrator ID. | ||
* | ||
* @var int | ||
*/ | ||
protected static $administrator_id = 0; | ||
|
||
public static function set_up_before_class() { | ||
parent::set_up_before_class(); | ||
|
||
|
@@ -346,4 +353,39 @@ protected function get_handles_for_provider( array $fonts, $provider_id ) { | |
|
||
return $handles; | ||
} | ||
|
||
protected static function set_up_admin_user() { | ||
self::$administrator_id = self::factory()->user->create( | ||
array( | ||
'role' => 'administrator', | ||
'user_email' => '[email protected]', | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Sets up the global styles. | ||
* | ||
* @param array $styles User-selected styles structure. | ||
* @param array $theme Optional. Theme to switch to for the test. Default 'fonts-block-theme'. | ||
*/ | ||
protected function set_up_global_styles( array $styles, $theme = 'fonts-block-theme' ) { | ||
switch_theme( $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 ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've learned a bit more how the fonts API works. This is my suggestion for the way forward: #40363 (comment) The first step we should address is #50576 and we can revisit what this PR is trying to achieve after.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are too many threads! For clarity and transparency, #50576 shouldn't block this work.
I'd also like to bring attention to this thread #40363 (comment) (perhaps better comment there):