-
Notifications
You must be signed in to change notification settings - Fork 360
Move Blockbase Google Fonts to Locally Hosted ( DO NOT MERGE ) #6010
Changes from 30 commits
8acfd57
50e202c
9617c6a
5d9c457
6bfefa6
7fb833e
63b57d5
04d0626
f4428e1
c6506b4
807395a
df62e8f
49cc34a
23c5c6d
bd20f82
1620884
caeed7c
8983f7c
926032e
dad8f8d
37183e9
09a060d
9f2df6e
d0b8e79
0f1a15d
1d3a5cb
f435f45
4d2dbb9
ab22d3a
1782be8
1d08e2e
d445c89
a6bdb08
7728c2c
99a68ee
d0ba696
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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. | ||
|
@@ -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( | ||
|
@@ -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. | ||
|
@@ -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', | ||
|
@@ -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' ) ) { | ||
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' ); | ||
zaguiini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* 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', | ||
), | ||
) | ||
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. 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 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. (I'm wondering if we should do something similar with the Jetpack Google Fonts module, as well 🤔) 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.
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
I agree 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. 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... 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.
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. I haven't verified anything yet, but I would've assumed a smaller range would optimize the font family requests? 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.
That's fine by me! 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. |
||
); | ||
} | ||
|
||
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' ); |
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.
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.
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.
Good catch 👍
Drafting a PR now
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.
@creativecoder 959-gh-Automattic/wpcomsh