-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enqueue webfonts listed in theme.json
- Loading branch information
Showing
4 changed files
with
150 additions
and
8 deletions.
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
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
56 changes: 56 additions & 0 deletions
56
lib/experimental/enqueue-webfonts-listed-in-theme-json.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,56 @@ | ||
<?php | ||
|
||
function gutenberg_is_externally_registered_webfont( $webfont ) { | ||
return isset( $webfont['origin'] ) && 'gutenberg_wp_webfonts_api' === $webfont['origin']; | ||
} | ||
|
||
function gutenberg_enqueue_webfonts_listed_in_theme_json() { | ||
$theme_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings(); | ||
|
||
// Bail out early if there are no settings for webfonts. | ||
if ( empty( $theme_settings['typography'] ) || empty( $theme_settings['typography']['fontFamilies'] ) ) { | ||
return; | ||
} | ||
|
||
// Look for fontFamilies. | ||
foreach ( $theme_settings['typography']['fontFamilies'] as $font_families ) { | ||
foreach ( $font_families as $font_family ) { | ||
// Skip dynamically included font families. We only want to enqueue explicitly added fonts. | ||
if ( gutenberg_is_externally_registered_webfont( $font_family ) ) { | ||
continue; | ||
} | ||
|
||
// If no font faces defined. | ||
if ( ! isset( $font_family['fontFaces'] ) ) { | ||
// And the font family is registered. | ||
if ( ! wp_webfonts()->is_font_family_registered( $font_family['fontFamily'] ) ) { | ||
continue; | ||
} | ||
|
||
// Enqueue the entire family. | ||
wp_webfonts()->enqueue_webfont( $font_family ); | ||
continue; | ||
} | ||
|
||
// Loop through all the font faces, enqueueing each one of them. | ||
foreach ( $font_family['fontFaces'] as $font_face ) { | ||
// Skip dynamically included font faces. We only want to enqueue the font faces listed in theme.json. | ||
if ( gutenberg_is_externally_registered_webfont( $font_face ) ) { | ||
continue; | ||
} | ||
|
||
wp_webfonts()->enqueue_webfont( $font_family, $font_face ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
add_filter( 'wp_loaded', 'gutenberg_enqueue_webfonts_listed_in_theme_json' ); | ||
|
||
// No need to run this -- opening the admin interface enqueues all the webfonts. | ||
add_action( | ||
'admin_init', | ||
function() { | ||
remove_filter( 'wp_loaded', 'gutenberg_enqueue_webfonts_listed_in_theme_json' ); | ||
} | ||
); |
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