diff --git a/lib/compat/wordpress-6.0/scan-webfonts-used-in-blocks.php b/lib/compat/wordpress-6.0/scan-webfonts-used-in-blocks.php new file mode 100644 index 00000000000000..29826ba5f484dd --- /dev/null +++ b/lib/compat/wordpress-6.0/scan-webfonts-used-in-blocks.php @@ -0,0 +1,32 @@ +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 ); + } +} diff --git a/lib/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php b/lib/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php new file mode 100644 index 00000000000000..bc3f43ad525321 --- /dev/null +++ b/lib/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php @@ -0,0 +1,83 @@ +.+)\)$/', $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.+)$/', $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' ); + } +} diff --git a/lib/load.php b/lib/load.php index c6619708df3df4..e3bba340c42b34 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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';