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

Move Blockbase Google Fonts to Locally Hosted ( DO NOT MERGE ) #6010

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8acfd57
WIP migrating Blockbase Customizer font settings
creativecoder May 9, 2022
50e202c
Fix error
creativecoder May 11, 2022
9617c6a
Revert "Fix error"
creativecoder May 11, 2022
5d9c457
Revert "WIP migrating Blockbase Customizer font settings"
creativecoder May 11, 2022
6bfefa6
[WIP] take 2
creativecoder May 11, 2022
7fb833e
Attempt to unset legacy data and append transformed data
jeyip May 11, 2022
63b57d5
More testing
creativecoder May 11, 2022
04d0626
Fix fontFamily typo
jeyip May 11, 2022
f4428e1
Ensure that the action only runs once
jeyip May 11, 2022
c6506b4
WIP x3
creativecoder May 12, 2022
807395a
Migrating Customizer settings to Global Styles
creativecoder May 13, 2022
df62e8f
Keep legacy font used at migration point working on the site
creativecoder May 13, 2022
49cc34a
Add customizer font deprecation notice when site is migrated
jeyip May 16, 2022
23c5c6d
Modify deprecation message
jeyip May 16, 2022
bd20f82
Load Google Fonts from Global Styles without Jetpack
creativecoder May 16, 2022
1620884
Disable Jetpack Google Fonts
creativecoder May 16, 2022
caeed7c
Add fonts from block settings
creativecoder May 17, 2022
8983f7c
Remove Jetpack requirement
creativecoder May 17, 2022
926032e
Add all fonts to theme.json
creativecoder May 17, 2022
dad8f8d
Remove unused code
creativecoder May 17, 2022
37183e9
Add docs hyperlink to deprecation notice
jeyip May 17, 2022
09a060d
Updates for using composer
creativecoder May 17, 2022
9f2df6e
Remove deep link to global styles panel from customizer
jeyip May 17, 2022
d0b8e79
Open styles panel in Site editor link
creativecoder May 17, 2022
0f1a15d
Unhook wpcomsh Google Fonts module activation
creativecoder May 20, 2022
1d3a5cb
Enqueue all fonts for editor
jeyip May 23, 2022
f435f45
Implement blockbase custom fonts with the core webfonts API
jeyip May 24, 2022
4d2dbb9
Use font-display swap for font registration
jeyip May 25, 2022
ab22d3a
Use theme mods instead of site options for migration
jeyip May 25, 2022
1782be8
Remove unnecessary inline comment
jeyip May 26, 2022
1d08e2e
Update comment and Gutenberg version
creativecoder May 26, 2022
d445c89
Move setting migration code to an earlier hook to prevent a request w…
creativecoder May 26, 2022
a6bdb08
Automatically enqueue default blockbase google fonts
jeyip May 31, 2022
7728c2c
Move google fonts registration into inc directory
jeyip Jun 1, 2022
99a68ee
Remove unnecessary customizer fonts files
jeyip Jun 1, 2022
d0ba696
Update meraki heading-font references
jeyip Jun 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ twentynineteen/
theme-dev-utils/
theme-dev-utils
vendor/
# Don't ignore blockbase/vendor, which has package code needed by the theme
!blockbase/vendor
*.DS_Store
*.zip
*.rej
Expand Down
9 changes: 9 additions & 0 deletions blockbase/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "automattic/blockbase",
"description": "A universal WordPress theme that bridges classic and block-based themes",
"type": "project",
"require": {
"automattic/jetpack-google-fonts-provider": "^0.3.0"
},
"license": "GPL-2.0-or-later"
}
65 changes: 65 additions & 0 deletions blockbase/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

165 changes: 104 additions & 61 deletions blockbase/functions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php

// Load Jetpack packages included with theme (Jetpack plugin is not required).
// Run `composer update --no-dev` to update packages and commit the changes.
require_once 'vendor/autoload.php';

if ( ! function_exists( 'blockbase_support' ) ) :
function blockbase_support() {
// Alignwide and alignfull classes in the block editor.
Expand Down Expand Up @@ -63,13 +68,6 @@ function( $settings ) {
* Enqueue scripts and styles.
*/
function blockbase_editor_styles() {
// Enqueue editor styles.
add_editor_style(
array(
blockbase_fonts_url(),
)
);

// Add the child theme CSS if it exists.
if ( file_exists( get_stylesheet_directory() . '/assets/theme.css' ) ) {
add_editor_style(
Expand All @@ -84,8 +82,6 @@ function blockbase_editor_styles() {
* Enqueue scripts and styles.
*/
function blockbase_scripts() {
// Enqueue Google fonts
wp_enqueue_style( 'blockbase-fonts', blockbase_fonts_url(), array(), null );
wp_enqueue_style( 'blockbase-ponyfill', get_template_directory_uri() . '/assets/ponyfill.css', array(), wp_get_theme()->get( 'Version' ) );

// Add the child theme CSS if it exists.
Expand All @@ -95,67 +91,18 @@ function blockbase_scripts() {
}
add_action( 'wp_enqueue_scripts', 'blockbase_scripts' );

/**
* Add Google webfonts
*
* @return $fonts_url
*/

function blockbase_fonts_url() {
if ( ! class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
return '';
}

$theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_settings();
if ( empty( $theme_data ) || empty( $theme_data['typography'] ) || empty( $theme_data['typography']['fontFamilies'] ) ) {
return '';
}

$font_families = [];
if ( ! empty( $theme_data['typography']['fontFamilies']['custom'] ) ) {
foreach( $theme_data['typography']['fontFamilies']['custom'] as $font ) {
if ( ! empty( $font['google'] ) ) {
$font_families[] = $font['google'];
}
}

// NOTE: This should be removed once Gutenberg 12.1 lands stably in all environments
} else if ( ! empty( $theme_data['typography']['fontFamilies']['user'] ) ) {
foreach( $theme_data['typography']['fontFamilies']['user'] as $font ) {
if ( ! empty( $font['google'] ) ) {
$font_families[] = $font['google'];
}
}
// End Gutenberg < 12.1 compatibility patch

} else {
if ( ! empty( $theme_data['typography']['fontFamilies']['theme'] ) ) {
foreach( $theme_data['typography']['fontFamilies']['theme'] as $font ) {
if ( ! empty( $font['google'] ) ) {
$font_families[] = $font['google'];
}
}
}
}

if ( empty( $font_families ) ) {
return '';
}

// Make a single request for the theme or user fonts.
return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', array_unique( $font_families ) ) . '&display=swap' );
}

/**
* Customize Global Styles
*/
if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
require get_template_directory() . '/inc/customizer/wp-customize-colors.php';
require get_template_directory() . '/inc/customizer/wp-customize-color-palettes.php';
require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';
require get_template_directory() . '/inc/social-navigation.php';
}

// Font settings migration and deprecation message. Fonts now set in Global Styles.
require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';

// Force menus to reload
add_action(
'customize_controls_enqueue_scripts',
Expand Down Expand Up @@ -187,3 +134,99 @@ function blockbase_core_navigation_render_fallback() {
if ( file_exists( get_stylesheet_directory() . '/inc/block-patterns.php' ) ) {
require_once get_stylesheet_directory() . '/inc/block-patterns.php';
}


// ---------- Custom Fonts ---------- //
const BLOCKBASE_GOOGLE_FONTS_LIST = array(
'Arvo',
'Bodoni Moda',
'Cabin',
'Chivo',
'Courier Prime',
'DM Sans',
'Domine',
'EB Garamond',
'Fira Sans',
'IBM Plex Sans',
'IBM Plex Mono',
'Inter',
'Josefin Sans',
'Jost',
'Libre Baskerville',
'Libre Franklin',
'Literata',
'Lora',
'Merriweather',
'Montserrat',
'Newsreader',
'Nunito',
'Open Sans',
'Overpass',
'Playfair Display',
'Poppins',
'Raleway',
'Red Hat Display',
'Roboto',
'Roboto Slab',
'Rubik',
'Source Sans Pro',
'Source Serif Pro',
'Space Mono',
'Texturina',
'Work Sans',
);

/**
* Register a curated selection of Google Fonts.
*
* @return void
*/
function blockbase_register_google_fonts() {
// Use jetpack's implementation of custom google fonts if it is already active
if ( method_exists( 'Jetpack', 'is_module_active' ) && 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.

Would be a good idea to check when wpcomsh is auto-activating the Jetpack Google Fonts module, and move that to fire before this function, to make sure this check is working as expected. Otherwise this might run adding fonts, then the Jetpack module gets activated afterward, and we'd have duplicate fonts for a request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

Drafting a PR now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@creativecoder 959-gh-Automattic/wpcomsh

return;
}

if ( ! function_exists( 'wp_register_webfont_provider' ) || ! function_exists( 'wp_register_webfonts' ) ) {
return;
}

wp_register_webfont_provider( 'blockbase-google-fonts', '\Automattic\Jetpack\Fonts\Google_Fonts_Provider' );

/**
* Curated list of Google Fonts.
*
* @module google-fonts
*
* @since 10.8
*
* @param array $fonts_to_register Array of Google Font names to register.
*/
$fonts_to_register = apply_filters( 'blockbase_google_fonts_list', BLOCKBASE_GOOGLE_FONTS_LIST );

foreach ( $fonts_to_register as $font_family ) {
wp_register_webfonts(
array(
array(
'font-family' => $font_family,
'font-weight' => '100 900',
'font-style' => 'normal',
'font-display' => 'swap',
'provider' => 'blockbase-google-fonts',
),
array(
'font-family' => $font_family,
'font-weight' => '100 900',
'font-style' => 'italic',
'font-display' => 'swap',
'provider' => 'blockbase-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.

I think we actually have the opportunity to be more selective here with the font weights by including only those needed by the themes, reducing the number of font-face rules that are output on the page.

What do you think of modifying BLOCKBASE_GOOGLE_FONTS_LIST to include the google information from GlobalStylesFontsCustomizer->$fonts, and load only the weights specified for each font?

Copy link
Contributor Author

@creativecoder creativecoder May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm wondering if we should do something similar with the Jetpack Google Fonts module, as well 🤔)

Copy link
Contributor

@jeyip jeyip May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of modifying BLOCKBASE_GOOGLE_FONTS_LIST to include the google information from GlobalStylesFontsCustomizer->$fonts, and load only the weights specified for each font?

I think this is fair, but would we mind doing it in a follow-up, seeing as how this is the same behavior that's already implemented in production for the Jetpack Google Fonts Module? That way we can tackle it in Blockbase
and Jetpack at the same time within the scope of another issue. Happy to create a ticket for us if you're okay with it. 👍

(I'm wondering if we should do something similar with the Jetpack Google Fonts module, as well 🤔)

I agree

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell the above is including only a single weight per-font family. I'm not sure I see how more selective it can be. Or perhaps I'm reading it wrong...

Copy link
Contributor

@jeyip jeyip May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell the above is including only a single weight per-font family. I'm not sure I see how more selective it can be. Or perhaps I'm reading it wrong...

Blockbase prescribes different font weight ranges for each font family, which is different from the 100 to 900 range we specify for all fonts with the aforementioned code.

Screen Shot 2022-05-26 at 8 53 30 AM

I haven't verified anything yet, but I would've assumed a smaller range would optimize the font family requests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fair, but would we mind doing it in a follow-up, seeing as how this is the same behavior that's already implemented in production for the Jetpack Google Fonts Module?

That's fine by me!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);
}

add_filter( 'wp_resource_hints', '\Automattic\Jetpack\Fonts\Utils::font_source_resource_hint', 10, 2 );
add_filter( 'pre_render_block', '\Automattic\Jetpack\Fonts\Introspectors\Blocks::enqueue_block_fonts', 10, 2 );
add_action( 'init', '\Automattic\Jetpack\Fonts\Introspectors\Global_Styles::enqueue_global_styles_fonts' );
}
add_action( 'after_setup_theme', 'blockbase_register_google_fonts' );
Loading