-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Webfonts: scan content for font families and enqueue them (Attempt #2) #39593
Closed
zaguiini
wants to merge
2
commits into
try/register-and-enqueue-webfonts-by-family
from
try/scan-content-for-webfonts-by-family
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,32 @@ | ||
<?php | ||
/** | ||
* Webfonts API: scan and enqueue webfonts used in blocks. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
if ( ! function_exists( 'gutenberg_enqueue_webfonts_used_in_block' ) ) { | ||
/** | ||
* Looks for font families in the attributes and enqueue them. | ||
* | ||
* @param string $content The block content. | ||
* @param array $parsed_block The parsed block attributes. | ||
* | ||
* @return array | ||
*/ | ||
function gutenberg_enqueue_webfonts_used_in_block( $content, $parsed_block ) { | ||
if ( isset( $parsed_block['attrs']['fontFamily'] ) ) { | ||
wp_webfonts()->enqueue_webfont( $parsed_block['attrs']['fontFamily'] ); | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* We are already enqueueing all registered fonts by default when loading the block editor, | ||
* so we only need to scan for webfonts when browsing as a guest. | ||
*/ | ||
if ( ! is_admin() ) { | ||
add_filter( 'pre_render_block', 'gutenberg_enqueue_webfonts_used_in_block', 10, 2 ); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
lib/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php
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,83 @@ | ||
<?php | ||
/** | ||
* Webfonts API: scan and enqueue webfonts used in Global Styles. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
if ( ! function_exists( 'gutenberg_enqueue_webfonts_used_in_global_styles' ) ) { | ||
/** | ||
* Extract the font family slug from a settings object. | ||
* | ||
* @param object $setting The setting object. | ||
* | ||
* @return string|void | ||
*/ | ||
function gutenberg_extract_font_slug_from_setting( $setting ) { | ||
if ( isset( $setting['typography'] ) && isset( $setting['typography']['fontFamily'] ) ) { | ||
$font_family = $setting['typography']['fontFamily']; | ||
|
||
// Full string: var(--wp--preset--font-family--slug). | ||
// We do not care about the origin of the font, only its slug. | ||
preg_match( '/font-family--(?P<slug>.+)\)$/', $font_family, $matches ); | ||
|
||
if ( isset( $matches['slug'] ) ) { | ||
return $matches['slug']; | ||
} | ||
|
||
// Full string: var:preset|font-family|slug | ||
// We do not care about the origin of the font, only its slug. | ||
preg_match( '/font-family\|(?P<slug>.+)$/', $font_family, $matches ); | ||
|
||
if ( isset( $matches['slug'] ) ) { | ||
return $matches['slug']; | ||
} | ||
|
||
return $font_family; | ||
} | ||
} | ||
|
||
/** | ||
* Looks for font families in the global styles and enqueue them. | ||
*/ | ||
function gutenberg_enqueue_webfonts_used_in_global_styles() { | ||
$global_styles = gutenberg_get_global_styles(); | ||
|
||
// Scan block presets looking for webfonts... | ||
if ( isset( $global_styles['blocks'] ) ) { | ||
foreach ( $global_styles['blocks'] as $setting ) { | ||
$font_slug = gutenberg_extract_font_slug_from_setting( $setting ); | ||
|
||
if ( $font_slug ) { | ||
wp_webfonts()->enqueue_webfont( $font_slug ); | ||
} | ||
} | ||
} | ||
|
||
// Scan HTML element presets looking for webfonts... | ||
if ( isset( $global_styles['elements'] ) ) { | ||
foreach ( $global_styles['elements'] as $setting ) { | ||
$font_slug = gutenberg_extract_font_slug_from_setting( $setting ); | ||
|
||
if ( $font_slug ) { | ||
wp_webfonts()->enqueue_webfont( $font_slug ); | ||
} | ||
} | ||
} | ||
|
||
// Check if a global typography setting was defined. | ||
$font_slug = gutenberg_extract_font_slug_from_setting( $global_styles ); | ||
|
||
if ( $font_slug ) { | ||
wp_webfonts()->enqueue_webfont( $font_slug ); | ||
} | ||
} | ||
|
||
/** | ||
* We are already enqueueing all registered fonts by default when loading the block editor, | ||
* so we only need to scan for webfonts when browsing as a guest. | ||
*/ | ||
if ( ! is_admin() ) { | ||
add_action( 'wp_loaded', 'gutenberg_enqueue_webfonts_used_in_global_styles' ); | ||
} | ||
} |
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
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 really wish we did not have to worry about this. Maybe we can make
enqueue_webfont
a noop when opening the page as admin?But still, all the code that looks for webfonts in blocks/global styles would run, and that's what I'd like to avoid. Is there a better way? Are we okay with exposing this implementation detail? Are there better options?