-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webfonts API: expose enqueueing method instead of directly enqueueing fonts on registration #39327
Changes from all commits
86ece5f
b084fd0
a9bf4b6
91dbeba
ab36072
53ab9d1
0888132
6a0cf04
59ef049
e0a573d
391b0b0
2df17bf
7a12067
5ab6da7
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 |
---|---|---|
|
@@ -5,6 +5,16 @@ | |
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Generates a font id based on the provided font object. | ||
* | ||
* @param array $font The font object. | ||
* @return string | ||
*/ | ||
function gutenberg_generate_font_id( $font ) { | ||
return sanitize_title( "{$font['font-family']}-{$font['font-weight']}-{$font['font-style']}-{$font['provider']}" ); | ||
} | ||
|
||
/** | ||
* Register webfonts defined in theme.json. | ||
*/ | ||
|
@@ -31,19 +41,6 @@ function gutenberg_register_webfonts_from_theme_json() { | |
$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 ); | ||
|
@@ -58,7 +55,8 @@ function gutenberg_register_webfonts_from_theme_json() { | |
} | ||
} | ||
foreach ( $webfonts as $webfont ) { | ||
wp_webfonts()->register_font( $webfont ); | ||
$id = isset( $webfont['id'] ) ? $webfont['id'] : gutenberg_generate_font_id( $webfont ); | ||
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. The sole reason we're doing this ternary is that it might break themes already using the Webfont API and registering webfonts, but without specifying the font id. The thing is, the Webfont API hasn't been made public yet, so we can do whatever we want with the signature... Including breaking changes such as this one. "Why are we making the
|
||
wp_register_webfont( $id, $webfont ); | ||
} | ||
} | ||
|
||
|
@@ -69,8 +67,8 @@ function gutenberg_register_webfonts_from_theme_json() { | |
* | ||
* @return array The global styles with missing fonts data. | ||
*/ | ||
function gutenberg_add_registered_webfonts_to_theme_json( $data ) { | ||
$font_families_registered = wp_webfonts()->get_fonts(); | ||
function gutenberg_add_webfonts_to_theme_json( $data ) { | ||
$webfonts = wp_webfonts()->get_all_webfonts(); | ||
$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']; | ||
|
@@ -101,7 +99,7 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { | |
|
||
// Diff the arrays to find the missing fonts. | ||
$to_add = array_diff( | ||
$get_families( $font_families_registered ), | ||
$get_families( $webfonts ), | ||
$get_families( $font_families_from_theme ) | ||
); | ||
|
||
|
@@ -125,7 +123,7 @@ function gutenberg_add_registered_webfonts_to_theme_json( $data ) { | |
// Add missing fonts. | ||
foreach ( $to_add as $family ) { | ||
$font_face = array(); | ||
foreach ( $font_families_registered as $font_family ) { | ||
foreach ( $webfonts as $font_family ) { | ||
if ( $family !== $font_family['font-family'] ) { | ||
continue; | ||
} | ||
|
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 that using the WP internal
_doing_it_wrong
here would be better. Plugins and themes do all kinds of weird things and we generally don't want a users' site to WSOD on account of it._doing_it_wrong
has the virtue of throwing actual errors whenWP_DEBUG
is defined, which is typical for developers, who will get the more aggressive treatment.I see several other uses of
trigger_error
in here and would replace all of them.