-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add additional sanitization for settings.typography.fontFamilies #53273
Conversation
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ lib/class-wp-theme-json-gutenberg.php ❔ phpunit/class-wp-theme-json-test.php |
I am not sure if this is a great approach to add all of these properties to the VALID_SETTINGS array. There is another public method of |
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.
Hi! Thanks for working on this @jffng. :)
I tested this and it is working as expected.
LGTM
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.
For those testing following the instructions on #52798 remember you need to create a new WP_Theme_JSON_Gutenberg
object instead of WP_Theme_JSON
because this change is not in core yet.
Nice one. Would you be able to provide a test case to make sure both the constructor and There are a couple of examples in the existing test suite
|
@oandregal When writing unit tests, I realized a bug about the current state of this PR — the sanitization removes any I think this is because the Any ideas for how to handle this? |
Ah, I see, it removes all keys that are not One approach would be to expand the logic in the {
"...": {
"fontFamilies": {
"0": { "name": "...", "slug": "...", "...": "..." },
"1": { "name": "...", "slug": "...", "...": "..." },
"2": { "name": "...", "slug": "...", "...": "..." }
}
}
} We do something similar for blocks, when we create the schema based on the blocks registered. The schema for the font family under the A second approach that comes to mind would be to use a special sign as key to signal that "any key is allowed as child". For example, make the {
"...": {
"fontFamilies": {
"*": { "name": "...", "slug": "...", "...": "..." }
}
}
} and update Finally, another approach would be to leverage the info already present in this other schema. Does this help? I don't feel strongly about any approach, other than making sure the way we do it can be reused for any other preset. |
Thank you for the ideas! I implemented your first suggestion here: ba8f4de — it seems to be working well, the unit tests are now passing. I also added a test to ensure the typography settings are sanitized correctly: 260d7cf EDIT |
I guess those tests were implemented by @anton-vlasenko or @hellofromtonya maybe they can help with the issues. |
While tying to fix the tests, I realized the shape of With the existing fonts API,
or
With the soon-to-be introduced Font Library, the input is expected to look like this:
Is there much benefit to change the sanitization function to allow all of these cases? It feels overly specific and prone to error. Alternatively we can add sanitization directly to the new Font Library class. |
That's a good discovery, glad you were looking into this.
I reckon I don't know every detail of font family management, though I understand we need to consolidate into only one shape. |
I excluded the |
Currently looking into why the Font Face tests are failing. |
@jffng Yes, these tests are still relevant and will remain. They are for Font Face, which replaces the Fonts API. It works with Font Library. |
Why are the Font Face tests failing? When public static function get_fonts_from_theme_json() {
$settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_settings();
// Bail out early if there are no font settings.
if ( empty( $settings['typography'] ) || empty( $settings['typography']['fontFamilies'] ) ) {
return array();
} The tests fail because it expects to receive the fonts from theme.json, but no fonts are returned (an empty array is returned). Why is Here's the test theme's |
Test ReportEnv:
Testing Instructions
Expected behavior: The Fonts option should be there. Test Results❌ The Fonts selector is not present. @jffng after applying the changes in this PR, the Fonts option and dropdown is no longer present in Site Editor > Styles > Typography > Headings UI. |
b0c6e6e
to
8067f83
Compare
I updated this PR so all the tests are now passing and the above bug reports should no longer be happening, it should be ready for another review. |
This works as advertised, and local PHP unit tests pass 👍🏻 Test ReportIncludes test from @hellofromtonya's report above, as well as the direct sanitation check from #52798. Steps to Test
Environment
Actual Results
|
As this PR hasn't been merged yet and the latest version of Gutenberg is due to be released today, I'm going to remove the I'm going to add the |
87a6834
to
1169e19
Compare
Flaky tests detected in 0eea111. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6383699639
|
The Font Face tests are failing because they are using Core's Font Face files which do not use Seems Font Face files in Gutenberg need to be modified to use Note: Once this PR is merged into Core, the tests will pass again. But the problem will resurface anytime there's a change in one of the files here in this repo that already exists in Core. |
PR #54990 should resolve the failing tests. @ironprogrammer can you test to verify please? |
0eea111
to
2493483
Compare
Thanks everyone for the input, it helped inform a cleaner solution at #56447. |
What?
This PR adds an additional level of sanitization for theme.json's
settings.typography.fontFamilies
.Why?
Addresses #52798
How?
By adding additional properties to the valid settings of the theme.json class.
Testing Instructions
See #52798.
Testing Instructions for Keyboard
Screenshots or screencast