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

Webfonts: scan content for font families and enqueue them (Attempt #2) #39593

Closed
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
32 changes: 32 additions & 0 deletions lib/compat/wordpress-6.0/scan-webfonts-used-in-blocks.php
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() ) {
Copy link
Member Author

@zaguiini zaguiini Mar 29, 2022

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?

add_filter( 'pre_render_block', 'gutenberg_enqueue_webfonts_used_in_block', 10, 2 );
}
}
83 changes: 83 additions & 0 deletions lib/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php
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' );
}
}
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.0/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.0/block-template.php';
require __DIR__ . '/compat/wordpress-6.0/register-webfonts-from-theme-json.php';
require __DIR__ . '/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php';
require __DIR__ . '/compat/wordpress-6.0/scan-webfonts-used-in-blocks.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-theme-json-resolver-gutenberg.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider.php';
Expand Down