Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Conversation

@jffng
Copy link
Contributor

@jffng jffng commented Dec 21, 2022

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:

  • Checkout this PR, activate Blockbase locally (no other plugins active), ensure custom fonts load as expected in the editors and front-end:

Screenshot 2022-12-21 at 11 39 16 AM

  • Checkout this version of blockbase onto a wpcom simple site (you can use npm run deploy:push:blockbase, verify there are no duplicates and you see all the Jetpack fonts, e.g. with Zoologist:

Screenshot 2022-12-21 at 11 52 36 AM

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:

Although I think we should still just disable Blockbase fonts entirely when Jetpack fonts module is active instead of doing this kind of "merge both sources" stuff, as Jetpack fonts:

  • works with all themes and experience is hence more consistent especially considering theme switches,
  • is a single source that's easier to keep up to date. Blockbase seems to already have outdated fonts list. Current either/or setup is way too complex, and this PR is actually making it even more complicated (merged lists) although it does fix an issue.
  • Jetpack fonts are now loaded from WP.com instead of Google

}
}

add_action( 'init', 'enqueue_global_styles_fonts', 100 );
Copy link
Contributor Author

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' ) ) {
Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Member

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 👍

Copy link
Member

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.

Copy link
Member

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. 👍

Copy link

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:

Suggested change
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).

@jffng jffng requested review from a team and simison December 21, 2022 19:55
@jffng
Copy link
Contributor Author

jffng commented Dec 21, 2022

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.

@jeffikus
Copy link
Contributor

@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'],
),
)
Copy link
Member

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

Copy link
Contributor Author

@jffng jffng Dec 21, 2022

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.

Copy link
Member

@simison simison Dec 22, 2022

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

Copy link
Member

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

@jffng
Copy link
Contributor Author

jffng commented Dec 21, 2022

@jeffikus

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?

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.

@jeffikus
Copy link
Contributor

@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.

@simison
Copy link
Member

simison commented Dec 22, 2022

do you think it's acceptable to only have the system font available?

Sort of related to adding more safe web fonts in Gutenberg WordPress/gutenberg#39552

@mikachan
Copy link
Member

any objections to this assumption?

I like this approach, all sounds good to me 👍

@jffng
Copy link
Contributor Author

jffng commented Dec 22, 2022

@jeffikus were you able to test this, locally or on dotcom?

"name": "System Font"
},
{
"fontFamily": "Arvo, serif",
Copy link
Member

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?

Copy link
Contributor Author

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",
Copy link
Member

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?

Copy link
Contributor Author

@jffng jffng Jan 9, 2023

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

@simison
Copy link
Member

simison commented Dec 27, 2022

were you able to test this, locally or on dotcom?

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).

@pbking
Copy link
Contributor

pbking commented Jan 5, 2023

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?

That sounds right, by this assumption do you think it's acceptable to only have the system font available?

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.

@jffng
Copy link
Contributor Author

jffng commented Jan 9, 2023

@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.

without waiting for an opportunity to refactor it

Do you have any other ideas for how we might refactor this in the meantime? I do not know why #6777 did not work.

@pbking
Copy link
Contributor

pbking commented Jan 11, 2023

@jffng the only alternative I can think of that doesn't negatively impact non-plugin-users is to add the fonts to Blockbase. I have opened a change to do that here: #6820

@hellofromtonya
Copy link

Heads up:
The Fonts API architectural work is now complete.

  • The API renaming work merged today into Gutenberg trunk. For example, wp_register_webfonts() is now wp_register_fonts().

  • Registering fonts through a theme's theme.json file is NOT impacted as the APIs automatically ingest, process, register, and enqueue defined fonts.

  • Directly registering fonts via wp_register_fonts(): the data structure to register changed:

    • Variations are grouped to their font-family
    • The font-family is the key for this group of variations

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).

@jffng
Copy link
Contributor Author

jffng commented Jan 19, 2023

Amazing, thank you for the update @hellofromtonya! The plan is for this updated API to make it into 6.2, right?

@hellofromtonya
Copy link

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

@pbking
Copy link
Contributor

pbking commented Jan 23, 2023

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.

@pbking pbking changed the title Blockbase: defer to Jetpack fonts Blockbase: Register Fonts via Webfonts API instead of theme.json Mar 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants