-
Notifications
You must be signed in to change notification settings - Fork 365
Blockbase: Register Fonts via Webfonts API instead of theme.json #6794
base: trunk
Are you sure you want to change the base?
Conversation
| } | ||
| } | ||
|
|
||
| add_action( 'init', 'enqueue_global_styles_fonts', 100 ); |
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.
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.
| /** | ||
| * Do not use the custom fonts provided by Blockbase if Jetpack fonts module is active | ||
| */ | ||
| if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'google-fonts' ) ) { |
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.
@simison is this a safe way to check?
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.
I think so! but let's @Automattic/jetpack-crew for 2nd opinion
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.
Clever doing module check instead of "is dotcom" check 👍
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.
Yes, it should work. The module will always be considered as "active" on WordPress.com simple, and is force-activated on WoA, so you'll end up using the module on both platforms at all times.
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.
I tested this PR in .com simple sites and the file didn't get loaded. 👍
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.
Is there any chance that the font module will become part of a standalone plugin at some point? If so, it might be better to do it like this:
| if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'google-fonts' ) ) { | |
| if ( ! class_exists( \Automattic\Jetpack\Modules::class ) || ! ( new \Automattic\Jetpack\Modules() )->is_active( 'google-fonts' ) ) { |
(feel free to put a use Automattic\Jetpack\Modules; at the top of the file instead of using a fully-qualified class name here, of course).
|
Ah I just realized this is going to break Blockbase fonts for sites that are not running Gutenberg which exposes the public webfonts API currently. |
|
@jffng I like this approach. I especially like that you've separate out the json - that seems more efficient. About Blockbase breaking for sites not running Gutenberg, I remember discussing our assumption was that Blockbase was assuming Gutenberg was active, but gracefully degrades when it isn't, or am I remembering that incorrectly? |
| 'font-display' => 'fallback', | ||
| 'provider' => $font_family['provider'], | ||
| ), | ||
| ) |
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_webfonts with 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
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.
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
That sounds right, by this assumption do you think it's acceptable to only have the system font available? Note that this would only be until the public Webfonts API lands in Core. |
|
@jffng I am comfortable with that being the assumption in order to get the best out of the theme. System fonts seems a good fallback on .org - .com won't have that problem though. @mikachan @ianstewart @matiasbenedetto @vcanales @madhusudhand @scruffian @MaggieCabrera any objections to this assumption? @jffng we would also need to test this with the Blockbase premium themes and merge it across to the premium version of blockbase. But that's a separate implementation. Let's get the main Blockbase implementation complete first. |
Sort of related to adding more safe web fonts in Gutenberg WordPress/gutenberg#39552 |
I like this approach, all sounds good to me 👍 |
|
@jeffikus were you able to test this, locally or on dotcom? |
| "name": "System Font" | ||
| }, | ||
| { | ||
| "fontFamily": "Arvo, serif", |
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.
Since these fonts were Arvo, serif previously here, will sites continue functioning if the font now is registered with just Arvo in global styles?
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.
Yes, since the slug is just Arvo and thus generated CSS variable that applies the font is the same.
| "provider": "blockbase-fonts" | ||
| }, | ||
| { | ||
| "fontFamily": "'Helvetica Neue','Helvetica', 'Arial', sans-serif", |
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.
Since these Helvetica+Arial fonts are from the OS, should you keep them here?
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.
Oh yeah, we should keep this in theme.json.
Done in 6bf79ff
I did test on .com simple sites — works fine (#6794 (comment)). Just a bit concerned if old sites continue working (https://github.com/Automattic/themes/pull/6794/files#r1057756452). |
While that's the assumption I also have this scenario feels different in that the additional fonts ARE currently available with Blockbase (or any of the children) without Gutenberg activated. So removing that functionality without waiting for an opportunity to refactor it seems like breakage rather than a degradation. |
|
@pbking that's a fair point. This approach would break fonts for Blockbase + children that are not running the plugin, until the refactor webfonts API lands in core. And we don't really know what the surface area of sites that really affects is.
Do you have any other ideas for how we might refactor this in the meantime? I do not know why #6777 did not work. |
|
Heads up:
For example: foreach ( $fonts_to_register as $font_family ) {
wp_register_fonts(
array(
$font_family => array(
array(
'font-family' => $font_family,
'font-weight' => '100 900',
'font-style' => 'normal',
'font-display' => 'fallback',
'provider' => 'jetpack-google-fonts',
),
array(
'font-family' => $font_family,
'font-weight' => '100 900',
'font-style' => 'italic',
'font-display' => 'fallback',
'provider' => 'jetpack-google-fonts',
),
),
)
);
}Also added notes to Jetpack's Google Fonts Provider Automattic/jetpack#28063 (comment). |
|
Amazing, thank you for the update @hellofromtonya! The plan is for this updated API to make it into 6.2, right? |
Yes, hopeful for it to land in WP 6.2 🤞 @jffng |
|
I would like to ship this change (or some version of it), but not until 6.2 lands. Until then #6831 should get wpcom where it needs to be. |
Changes proposed in this Pull Request:
This PR changes the way Blockbase registers fonts, from theme.json to use the Webfonts API directly instead.
This is done so that we can conditionally load them — only if Jetpack fonts are not active.
To test:
npm run deploy:push:blockbase, verify there are no duplicates and you see all the Jetpack fonts, e.g. with Zoologist:Related issue(s):
Solves #6765, alternative to #6777 which failed for reasons I still can't quite identify.
More reasoning why
Quoting @simison here for why I went with this approach instead: