-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Font face resolver: print font faces from font families defined in all theme.json origins #6161
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
Font face resolver: print font faces from font families defined in all theme.json origins #6161
Conversation
…are defined in more than one origin (default, theme, custom).
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Co-authored-by: Tonya Mork <[email protected]>
tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromThemeJson.php
Show resolved
Hide resolved
tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromThemeJson.php
Outdated
Show resolved
Hide resolved
Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Tonya Mork <[email protected]>
tests/phpunit/tests/fonts/font-face/wpFontFaceResolver/getFontsFromThemeJson.php
Outdated
Show resolved
Hide resolved
Co-authored-by: Tonya Mork <[email protected]>
| $data = $theme_json_data->get_data(); | ||
|
|
||
| // Add font families to the custom origin of theme json. | ||
| $data['settings']['typography']['fontFamilies']['custom'] = $this->get_custom_font_families( 'input' ); |
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.
Whoopsie, the function does not understand $this.
How to fix? Move this function callback to be a method instead.
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.
What about this way?fea3908
| $add_custom_fonts = function ( $theme_json_data ) { | ||
| $data = $theme_json_data->get_data(); | ||
| // Add font families to the custom origin of theme json. | ||
| $data['settings']['typography']['fontFamilies']['custom'] = $this->get_custom_font_families( 'input' ); |
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.
Whoopsie, a function within a method does not understand $this.
How to fix? Move this function callback to be a method instead.
Sorry, I didn't notice it when I suggested moving to a variable.
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.
This commit fixes it fea3908
|
Thanks for the fix here, @matiasbenedetto and @hellofromtonya! Test ReportEnvironment
Steps to Test Patch
Actual Results
Supplemental ArtifactsFont displayed comparison before/after patch: From view source during testing: Before Patch `@font-face` Declarations -- missing theme fonts ❌<style id="wp-fonts-local">
@font-face {
font-family: Inter;
font-style: normal;
font-weight: 300 900;
font-display: fallback;
src: url('http://wp-src.test/wp-content/themes/twentytwentyfour/assets/fonts/inter/Inter-VariableFont_slnt,wght.woff2') format('woff2');
font-stretch: normal;
}
@font-face {
font-family: Cardo;
font-style: italic;
font-weight: 400;
font-display: fallback;
src: url('http://wp-src.test/wp-content/fonts/wlpxgwjKBV1pqhv93IE73W5OcCk.woff2') format('woff2');
}
</style>After Patch `@font-face` Declarations -- including theme fonts ✅<style id="wp-fonts-local">
@font-face {
font-family: Inter;
font-style: normal;
font-weight: 300 900;
font-display: fallback;
src: url('http://wp-src.test/wp-content/themes/twentytwentyfour/assets/fonts/inter/Inter-VariableFont_slnt,wght.woff2') format('woff2');
font-stretch: normal;
}
@font-face {
font-family: Cardo;
font-style: normal;
font-weight: 400;
font-display: fallback;
src: url('http://wp-src.test/wp-content/themes/twentytwentyfour/assets/fonts/cardo/cardo_normal_400.woff2') format('woff2');
}
@font-face {
font-family: Cardo;
font-style: italic;
font-weight: 400;
font-display: fallback;
src: url('http://wp-src.test/wp-content/themes/twentytwentyfour/assets/fonts/cardo/cardo_italic_400.woff2') format('woff2');
}
@font-face {
font-family: Cardo;
font-style: normal;
font-weight: 700;
font-display: fallback;
src: url('http://wp-src.test/wp-content/themes/twentytwentyfour/assets/fonts/cardo/cardo_normal_700.woff2') format('woff2');
}
@font-face {
font-family: Cardo;
font-style: italic;
font-weight: 400;
font-display: fallback;
src: url('http://wp-src.test/wp-content/fonts/wlpxgwjKBV1pqhv93IE73W5OcCk.woff2') format('woff2');
}
</style> |
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.
Using an indexed array looks good to me 👍
Note 1: Maybe, in the future, we could add some checking to avoid printing repeated font variants (same weight, style, character set, etc). I'm not completely sure that's needed, though, because the browser handles that. Anyways, that's out of the scope of this PR.
Yep, this is the only concern. We may print duplicate face-faces definitions that are defined by different origins and it has a chance to increase the response size a lot.
|
Thanks, everyone, for the help here. @youknowriad this seems ready to go into the beta. Could you take a look? |

What?
Font face resolver: If a font family name is repeated across theme.json origins (default, theme, custom), only the font faces from one origin are rendered, so the site lacks the other font faces added.
This problem was reported originally here: WordPress/gutenberg#58764
Why?
To print the missing font faces when a font family is defined in more than one place.
How ?
The
parse_settingsmethod of theWP_Font_Face_Resolverclass uses an associative array to list all the font families that must be printed on the page. This PR replaces it with an indexed array because the associative array prevents having more than one font family with the same name, causing the bug that this PR fixes and seems unnecessary.Testing instruction
<style id="wp-fonts-local"></style>tag.Notes
Note 1: Maybe, in the future, we could add some checking to avoid printing repeated font variants (same weight, style, character set, etc). I'm not completely sure that's needed, though, because the browser handles that. Anyways, that's out of the scope of this PR.
Note 2: this issue isn't related strictly to the Font Library. The font library made it visible because of the use of custom apart from theme theme.json data origin, and because of the existing UI is easy to test this using the Font Library.
Note 3: This is an alternative approach to WordPress/gutenberg#59119
Trac ticket: https://core.trac.wordpress.org/ticket/60605#ticket