This repository was archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
Blockbase: Register Fonts via Webfonts API instead of theme.json #6794
Open
jffng
wants to merge
4
commits into
trunk
Choose a base branch
from
try/blockbase-conditionally-load-webfonts
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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
Next
Next commit
Register custom fonts conditionally.
- Loading branch information
commit 31bce23fb35a3d1fa42873a32407c99f711a3419
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,11 +6,6 @@ | |
| // Font Migration | ||
| require get_template_directory() . '/inc/fonts/custom-font-migration.php'; | ||
|
|
||
| add_action( 'init', 'enqueue_global_styles_fonts', 100 ); | ||
| add_action( 'admin_init', 'enqueue_fse_font_styles' ); | ||
| add_filter( 'pre_render_block', 'enqueue_block_fonts', 10, 2 ); | ||
| add_filter( 'jetpack_google_fonts_list', 'blockbase_filter_jetpack_google_fonts_list' ); | ||
|
|
||
| $blockbase_enqueued_font_slugs = array(); | ||
|
|
||
| /** | ||
|
|
@@ -119,8 +114,7 @@ function extract_font_slug_from_setting( $setting ) { | |
| */ | ||
| function collect_fonts_from_blockbase() { | ||
| $fonts = array(); | ||
| $parent_theme_json_data = json_decode( file_get_contents( get_template_directory() . '/theme.json' ), true ); | ||
| $font_families = $parent_theme_json_data['settings']['typography']['fontFamilies']; | ||
| $font_families = json_decode( file_get_contents( get_template_directory() . '/inc/fonts/fontFamilies.json' ), true )['fontFamilies']; | ||
|
|
||
| foreach ( $font_families as $font ) { | ||
| // Only pick it up if we're claiming it as ours to manage | ||
|
|
@@ -205,12 +199,37 @@ function enqueue_block_fonts( $content, $parsed_block ) { | |
| return $content; | ||
| } | ||
|
|
||
| /** | ||
| * Jetpack may attempt to register fonts for the Google Font Provider. | ||
| * If that happens on a child theme then ONLY Jetpack fonts are registered. | ||
| * This 'filter' filters out all of the fonts Jetpack should register | ||
| * so that we depend exclusively on those provided by Blockbase. | ||
| */ | ||
| function blockbase_filter_jetpack_google_fonts_list( $list_to_filter ) { | ||
| return array(); | ||
| function blockbase_register_fonts() { | ||
| if ( ! function_exists( 'wp_register_webfonts' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $font_families = collect_fonts_from_blockbase(); | ||
| foreach ( $font_families as $font_family ) { | ||
| wp_register_webfonts( | ||
| array( | ||
| array( | ||
| // 'name' key will eventually be supported: https://github.com/WordPress/gutenberg/issues/46398, | ||
| // in cases where we want the font's display name to differ from the 'font-family' key. | ||
| 'font-family' => $font_family['name'], | ||
| 'font-weight' => '100 900', | ||
| 'font-style' => 'normal', | ||
| 'font-display' => 'fallback', | ||
| 'provider' => $font_family['provider'], | ||
| ), | ||
| array( | ||
| 'font-family' => $font_family['name'], | ||
| 'font-weight' => '100 900', | ||
| 'font-style' => 'italic', | ||
| 'font-display' => 'fallback', | ||
| 'provider' => $font_family['provider'], | ||
| ), | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| add_action( 'init', 'enqueue_global_styles_fonts', 100 ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note I just moved all action and filter registrations to the bottom since it seems to be more of a convention to do this way. |
||
| add_action( 'admin_init', 'enqueue_fse_font_styles' ); | ||
| add_filter( 'pre_render_block', 'enqueue_block_fonts', 10, 2 ); | ||
| add_action( 'after_setup_theme', 'blockbase_register_fonts' ); | ||
This file contains hidden or 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,195 @@ | ||
| { | ||
| "fontFamilies": [ | ||
| { | ||
| "fontFamily": "Arvo", | ||
| "slug": "arvo", | ||
| "name": "Arvo", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Bodoni Moda', serif", | ||
| "slug": "bodoni-moda", | ||
| "name": "Bodoni Moda", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Cabin, sans-serif", | ||
| "slug": "cabin", | ||
| "name": "Cabin", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Chivo, sans-serif", | ||
| "slug": "chivo", | ||
| "name": "Chivo", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Courier Prime', serif", | ||
| "slug": "courier-prime", | ||
| "name": "Courier Prime", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'DM Sans', sans-serif", | ||
| "slug": "dm-sans", | ||
| "name": "DM Sans", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Domine, serif", | ||
| "slug": "domine", | ||
| "name": "Domine", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'EB Garamond', serif", | ||
| "slug": "eb-garamond", | ||
| "name": "EB Garamond", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Fira Sans', sans-serif", | ||
| "slug": "fira-sans", | ||
| "name": "Fira Sans", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Helvetica Neue','Helvetica', 'Arial', sans-serif", | ||
| "slug": "helvetica-neue", | ||
| "name": "Helvetica Neue" | ||
| }, | ||
| { | ||
| "fontFamily": "'IBM Plex Mono', monospace", | ||
| "slug": "ibm-plex-mono", | ||
| "name": "IBM Plex Mono", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Inter, sans-serif", | ||
| "slug": "inter", | ||
| "name": "Inter", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Josefin Sans', sans-serif", | ||
| "slug": "josefin-sans", | ||
| "name": "Josefin Sans", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Libre Baskerville', serif", | ||
| "slug": "libre-baskerville", | ||
| "name": "Libre Baskerville", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Libre Franklin', sans-serif", | ||
| "slug": "libre-franklin", | ||
| "name": "Libre Franklin", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Lora, serif", | ||
| "slug": "lora", | ||
| "name": "Lora", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Merriweather, serif", | ||
| "slug": "merriweather", | ||
| "name": "Merriweather", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Montserrat, sans-serif", | ||
| "slug": "montserrat", | ||
| "name": "Montserrat", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Nunito, sans-serif", | ||
| "slug": "nunito", | ||
| "name": "Nunito", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Open Sans', sans-serif", | ||
| "slug": "open-sans", | ||
| "name": "Open Sans", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Overpass, sans-serif", | ||
| "slug": "overpass", | ||
| "name": "Overpass", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Playfair Display', serif", | ||
| "slug": "playfair-display", | ||
| "name": "Playfair Display", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Poppins, sans-serif", | ||
| "slug": "poppins", | ||
| "name": "Poppins", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Raleway, sans-serif", | ||
| "slug": "raleway", | ||
| "name": "Raleway", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Red Hat Display', sans-serif", | ||
| "slug": "red-hat-display", | ||
| "name": "Red Hat Display", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Roboto, sans-serif", | ||
| "slug": "roboto", | ||
| "name": "Roboto", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Roboto Slab', sans-serif", | ||
| "slug": "roboto-slab", | ||
| "name": "Roboto Slab", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "Rubik, sans-serif", | ||
| "slug": "rubik", | ||
| "name": "Rubik", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Source Sans Pro', sans-serif", | ||
| "slug": "source-sans-pro", | ||
| "name": "Source Sans Pro", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Source Serif Pro', sans-serif", | ||
| "slug": "source-serif-pro", | ||
| "name": "Source Serif Pro", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Space Mono', sans-serif", | ||
| "slug": "space-mono", | ||
| "name": "Space Mono", | ||
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Work Sans', sans-serif", | ||
| "slug": "work-sans", | ||
| "name": "Work Sans", | ||
| "provider": "blockbase-fonts" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
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.
FYI using
wp_register_webfontswith this array signature is fine today, we'll just need to migrate to new format once the new API stabilizes, as this exact format got deprecated.More in WordPress/gutenberg#43492
Uh oh!
There was an error while loading. Please reload this page.
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.
Gotcha. If this approach is acceptable, we can open a follow-up PR to change the format and merge it after the new API lands in Gutenberg 14.9.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yep exactly. We'll also need to migrate Jetpack Automattic/jetpack#28054
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.
More info here: Automattic/jetpack#28063