From bc7ddbe36c244a97ddef85d44558bc07a28e62f5 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Tue, 29 Mar 2022 19:50:46 -0300 Subject: [PATCH 1/2] Add global styles webfont scanning action --- .../scan-webfonts-used-in-global-styles.php | 83 +++++++++++++++++++ lib/load.php | 1 + 2 files changed, 84 insertions(+) create mode 100644 lib/compat/wordpress-6.0/scan-webfonts-used-in-global-styles.php 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..ae256fc68c468f 100644 --- a/lib/load.php +++ b/lib/load.php @@ -102,6 +102,7 @@ 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/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'; From 0f3b94d36334abd6ba5a3965fe0138b31db81561 Mon Sep 17 00:00:00 2001 From: Luis Felipe Zaguini Date: Tue, 29 Mar 2022 19:51:03 -0300 Subject: [PATCH 2/2] Add blocks webfont scanning action --- .../scan-webfonts-used-in-blocks.php | 32 +++++++++++++++++++ lib/load.php | 1 + 2 files changed, 33 insertions(+) create mode 100644 lib/compat/wordpress-6.0/scan-webfonts-used-in-blocks.php 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/load.php b/lib/load.php index ae256fc68c468f..e3bba340c42b34 100644 --- a/lib/load.php +++ b/lib/load.php @@ -103,6 +103,7 @@ function gutenberg_is_experiment_enabled( $name ) { 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';