-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Backport: add theme json class changes to edit style variations #3941
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
Backport: add theme json class changes to edit style variations #3941
Conversation
costdev
left a comment
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.
Thanks for the PR @tellthemachines! I've just left a comment in this review regarding documentation standards for this file and future backports.
| // Merge new declarations with any that already exist for | ||
| // the feature selector. This may occur when multiple block | ||
| // support features use the same custom selector. |
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.
Multiline comments should be formatted as such:
/*
* Line 1
* Line 2
* ...
*/
Ref
🔢 Applies elsewhere in the PR.
However, I note that this file contains many multiline comments that don't adhere to the standards.
In lieu of a change in this PR that creates an inconsistency in this file, I strongly suggest a follow up PR to bring this file up to the documentation coding standards, and that all future code being backported meets the standard.
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.
Resolved here 0768aa8.
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've tested this PR by doing the following:
- Go to TwentyTwentyThree's
theme.jsonand understyles.blocks.core/quoteadd this:"variations": { "plain": { "color": { "background": "hotpink"} } }. - Then go to the editor and add a post that contains a quote block.
- Set the quote to use the "plain" style variation and publish.
- The expected result is that the background color is hotpink.
I went ahead and prepared tellthemachines#1 to add some unit test. It'd be great if it could be merged into this PR. Other than that and the comments brought up by @costdev , this is ready 🚢
|
Thanks for reviewing folks! @costdev it's probably easiest to do a follow-up PR to fix all the comments once this one is in; I'm happy to do it. @oandregal thanks for the test! I can't get it to pass locally but I'll merge it into this branch so we can see if it passes CI. |
Unit test for Backport/edit style variations
|
I removed the spaces between classnames from the output string as they're not meant to be there, but have no idea why the Edit: they now seem to be failing only on PHP versions 7.2 and below. Not sure why this would happen unless the actual issue is on a different line? That closing bracket it's complaining about seems to be in the right place. |
Co-authored-by: André <[email protected]>
Splits the tests into 2 separate tests. Adds a data provider for each. Adds more datasets.
| $schema_styles_blocks[ $block ] = $styles_non_top_level; | ||
| $schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements; | ||
| // Build the schema for each block style variation. | ||
| $style_variation_names = isset( $input['styles']['blocks'][ $block ]['variations'] ) ? array_keys( $input['styles']['blocks'][ $block ]['variations'] ) : array(); |
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 if the array structure exists, but the 'variations' are not an array? What would happen?
-
= PHP 8.0
Fatal error: Uncaught TypeError: array_keys(): Argument #1 ($array) must be of type array, string given
-
PHP 8.0:
Warning: array_keys() expects parameter 1 to be array, string given
To avoid these, also check if it is an array.
I'd also suggest switching to ! empty() instead of isset(). Why? To avoid doing array_keys() on an empty array.
| $style_variation_names = isset( $input['styles']['blocks'][ $block ]['variations'] ) ? array_keys( $input['styles']['blocks'][ $block ]['variations'] ) : array(); | |
| $style_variation_names = array(); | |
| if ( | |
| ! empty( $input['styles']['blocks'][ $block ]['variations'] ) && | |
| is_array( $input['styles']['blocks'][ $block ]['variations'] ) | |
| ) { | |
| $style_variation_names = array_keys( $input['styles']['blocks'][ $block ]['variations'] ); | |
| } |
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.
Resolved here 86ac232.
| // If the block has feature selectors, generate the declarations for them within the current style variation. | ||
| if ( ! empty( $block_metadata['features'] ) ) { | ||
| foreach ( $block_metadata['features'] as $feature_name => $feature_selector ) { | ||
| if ( ! empty( $style_variation_node[ $feature_name ] ) ) { |
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.
Unwrap the inner code by inverting this check. Why? Shifts the code to the left to make it more readable, as more levels of indentation can reduce readability.
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.
Resolved here 994c202.
| function( $split_feature_selector ) use ( $style_variation_selector ) { | ||
| return trim( $style_variation_selector ) . trim( $split_feature_selector ); |
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.
A few things here for micro-optimizations:
-
Use
static function. That way the callback is created once rather than on each iteration. -
Move the
trim( $style_variation_selector )outside of theforeach. It does not change without the loop. By moving it outside of the loop, trimming happens once instead of on each iteration.
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.
Resolved here 994c202.
hellofromtonya
left a comment
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 PR LGTM 👍 It has test coverage, some micro-optimizations, and meets Core's coding standards.
Will wait for @tellthemachines to confirm the changes are okay before marking for commit.
|
@tellthemachines Collectively there are some changes made in the PR for:
The PR looks ready to me for |
tellthemachines
left a comment
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.
Thanks for the improvements and detailed explanations @hellofromtonya ! The code looks good to me and tests are passing ✅
|
Thanks @tellthemachines! Prepping commit now. |
|
Committed via https://core.trac.wordpress.org/changeset/55172. |
Trac ticket: https://core.trac.wordpress.org/ticket/57583
Backports changes from WordPress/gutenberg#46343
These changes can't really be tested without the corresponding package updates, but I checked the Site Editor and Global Styles sidebar and at least nothing seems to be breaking 😅
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.