Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5fc7dfa
Copy files from core patch
aristath Nov 11, 2021
6078f5e
load the webfonts API
aristath Nov 11, 2021
8b855a7
add missing textdomain
aristath Nov 11, 2021
c3115f2
CS & path fixes
aristath Nov 11, 2021
4f359b4
Should be an instance of WP_Webfonts_Controller
aristath Nov 11, 2021
2739d60
Register webfonts from theme.json (#35625)
aristath Nov 11, 2021
9fdc963
Update theme.json implementation
aristath Nov 11, 2021
93e8f37
Switches test fixtures back to native.
hellofromtonya Nov 12, 2021
df31267
remove unused vars
aristath Nov 12, 2021
bb45144
minify fontface css for local & bugfix (missing ;)
aristath Nov 12, 2021
070ff83
require some xtra files
aristath Nov 12, 2021
ab87605
Now prints minified styles
aristath Nov 12, 2021
0e0cb47
style does not include type
aristath Nov 12, 2021
3f08abb
Split function & add some caching
aristath Nov 19, 2021
5fc6322
function was renamed to _wp_to_kebab_case
aristath Nov 19, 2021
01135e3
create function to add missing webfonts to global styles
aristath Nov 19, 2021
87c279c
Add registered webfonts to the font-family picker
aristath Nov 22, 2021
a3ceb6f
Testing for notices is not yet implemented in Gutenberg
aristath Nov 22, 2021
3341803
Move hook to wp_loaded - fixes some failing tests
aristath Nov 22, 2021
9c8d7ac
Add webfonts global styles
aristath Nov 22, 2021
c2bbba5
Add wp_block_styles_preset_vars filter
aristath Nov 22, 2021
9b65a90
simplify implementation
aristath Nov 22, 2021
9416b07
rename function
aristath Nov 22, 2021
944ba98
Use empty instead of isset
aristath Nov 22, 2021
2c458ac
combine & simplify
aristath Nov 22, 2021
22671d5
Update phpunit/webfonts-api/class-wp-webfonts-schema-validator-test.php
aristath Nov 23, 2021
5a83bc4
Revert "Testing for notices is not yet implemented in Gutenberg"
aristath Nov 23, 2021
4861efd
use expectException
aristath Nov 23, 2021
f5e8ce9
should be strict
aristath Nov 23, 2021
5907d76
Can't check for error messages
aristath Nov 23, 2021
db3c8a1
Remove the theme_json_data filter
aristath Nov 24, 2021
cb32b30
revert strict
aristath Nov 24, 2021
56dcf19
pluck out the google-fonts provider.
aristath Nov 24, 2021
71e9a30
resolve rebase error
aristath Dec 2, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ public static function get_theme_data( $deprecated = array() ) {
if ( null === self::$theme ) {
$theme_json_data = self::read_json_file( self::get_file_path_from_theme( 'theme.json' ) );
$theme_json_data = self::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
self::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );

// Add webfonts data.
$theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data );

self::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );

if ( wp_get_theme()->parent() ) {
// Get parent theme.json.
Expand Down
128 changes: 128 additions & 0 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,133 @@ function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $
return ! ! preg_match( '/^var\(--wp-[a-zA-Z0-9\-]+\)$/', trim( $parts[1] ) );
}

/**
* Register webfonts defined in theme.json.
*/
function gutenberg_register_webfonts_from_theme_json() {
// Get settings from theme.json.
$theme_settings = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_settings();

// Bail out early if there are no settings for webfonts.
if ( empty( $theme_settings['typography'] ) || empty( $theme_settings['typography']['fontFamilies'] ) ) {
return;
}

$webfonts = array();

// Look for fontFamilies.
foreach ( $theme_settings['typography']['fontFamilies'] as $font_families ) {
foreach ( $font_families as $font_family ) {

// Skip if fontFace is not defined.
if ( empty( $font_family['fontFace'] ) ) {
continue;
}

$font_family['fontFace'] = (array) $font_family['fontFace'];

foreach ( $font_family['fontFace'] as $font_face ) {
// Check if webfonts have a "src" param, and if they do account for the use of "file:./".
if ( ! empty( $font_face['src'] ) ) {
$font_face['src'] = (array) $font_face['src'];

foreach ( $font_face['src'] as $src_key => $url ) {
// Tweak the URL to be relative to the theme root.
if ( 0 !== strpos( $url, 'file:./' ) ) {
continue;
}
$font_face['src'][ $src_key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
}
}

// Convert keys to kebab-case.
foreach ( $font_face as $property => $value ) {
$kebab_case = _wp_to_kebab_case( $property );
$font_face[ $kebab_case ] = $value;
if ( $kebab_case !== $property ) {
unset( $font_face[ $property ] );
}
}

$webfonts[] = $font_face;
}
}
}
wp_register_webfonts( $webfonts );
}

/**
* Add missing fonts data to the global styles.
*
* @param array $data The global styles.
*
* @return array The global styles with missing fonts data.
*/
function gutenberg_add_registered_webfonts_to_theme_json( $data ) {
$font_families_registered = wp_webfonts()->webfonts()->get_all_registered();
$font_families_from_theme = array();
if ( ! empty( $data['settings'] ) && ! empty( $data['settings']['typography'] ) && ! empty( $data['settings']['typography']['fontFamilies'] ) ) {
$font_families_from_theme = $data['settings']['typography']['fontFamilies'];
}

/**
* Helper to get an array of the font-families.
*
* @param array $families_data The font-families data.
*
* @return array The font-families array.
*/
$get_families = function( $families_data ) {
$families = array();
foreach ( $families_data as $family ) {
if ( isset( $family['font-family'] ) ) {
$families[] = $family['font-family'];
} elseif ( isset( $family['fontFamily'] ) ) {
$families[] = $family['fontFamily'];
}
}

// Micro-optimization: Use array_flip( array_flip( $array ) )
// instead of array_unique( $array ) because it's faster.
// The result is the same.
return array_flip( array_flip( $families ) );
};

// Diff the arrays to find the missing fonts.
$to_add = array_diff(
$get_families( $font_families_registered ),
$get_families( $font_families_from_theme )
);

// Bail out early if there are no missing fonts.
if ( empty( $to_add ) ) {
return $data;
}

// Make sure the path to settings.typography.fontFamilies.theme exists
// before adding missing fonts.
if ( empty( $data['settings'] ) ) {
$data['settings'] = array();
}
if ( empty( $data['settings']['typography'] ) ) {
$data['settings']['typography'] = array();
}
if ( empty( $data['settings']['typography']['fontFamilies'] ) ) {
$data['settings']['typography']['fontFamilies'] = array();
}

// Add missing fonts.
foreach ( $to_add as $family ) {
$data['settings']['typography']['fontFamilies'][] = array(
'fontFamily' => false !== strpos( $family, ' ' ) ? "'{$family}'" : $family,
'name' => $family,
'slug' => sanitize_title( $family ),
);
}

return $data;
}

// The else clause can be removed when plugin support requires WordPress 5.8.0+.
if ( function_exists( 'get_block_editor_settings' ) ) {
add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
Expand All @@ -297,6 +424,7 @@ function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $

add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' );
add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' );
add_action( 'wp_loaded', 'gutenberg_register_webfonts_from_theme_json' );

// kses actions&filters.
add_action( 'init', 'gutenberg_global_styles_kses_init' );
Expand Down
28 changes: 28 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,31 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-supports/spacing.php';
require __DIR__ . '/block-supports/dimensions.php';
require __DIR__ . '/block-supports/duotone.php';

// Webfonts API.
if ( ! function_exists( 'wp_webfonts' ) ) {

/** WordPress Webfonts Classes & Functions */
require_once __DIR__ . '/webfonts-api/class-wp-webfonts-schema-validator.php';
require_once __DIR__ . '/webfonts-api/class-wp-webfonts-registry.php';
require_once __DIR__ . '/webfonts-api/class-wp-webfonts-provider-registry.php';
require_once __DIR__ . '/webfonts-api/class-wp-webfonts-controller.php';
require_once __DIR__ . '/webfonts.php';

/**
* Add webfonts mime types.
*/
add_filter(
'mime_types',
function( $mime_types ) {
// Webfonts formats.
$mime_types['woff2'] = 'font/woff2';
$mime_types['woff'] = 'font/woff';
$mime_types['ttf'] = 'font/ttf';
$mime_types['eot'] = 'application/vnd.ms-fontobject';
$mime_types['otf'] = 'application/x-font-opentype';

return $mime_types;
}
);
}
Loading