Skip to content

Conversation

@tellthemachines
Copy link
Contributor

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.

Copy link
Contributor

@costdev costdev left a 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.

Comment on lines 2179 to 2181
// 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.
Copy link
Contributor

@costdev costdev Jan 30, 2023

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved here 0768aa8.

Copy link
Member

@oandregal oandregal left a 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.json and under styles.blocks.core/quote add 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 🚢

@tellthemachines
Copy link
Contributor Author

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.

@tellthemachines
Copy link
Contributor Author

tellthemachines commented Jan 31, 2023

I removed the spaces between classnames from the output string as they're not meant to be there, but have no idea why the Parse error: syntax error, unexpected ')' in /var/www/tests/phpunit/tests/theme/wpThemeJson.php on line 3620 - the tests are now passing for me locally 😕

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.

@tellthemachines tellthemachines self-assigned this Jan 31, 2023
hellofromtonya and others added 2 commits January 31, 2023 13:28
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();
Copy link
Contributor

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?

https://3v4l.org/kHpHT

  • = 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.

Suggested change
$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'] );
}

Copy link
Contributor

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 ] ) ) {
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved here 994c202.

Comment on lines 2176 to 2177
function( $split_feature_selector ) use ( $style_variation_selector ) {
return trim( $style_variation_selector ) . trim( $split_feature_selector );
Copy link
Contributor

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 the foreach. It does not change without the loop. By moving it outside of the loop, trimming happens once instead of on each iteration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved here 994c202.

Copy link
Contributor

@hellofromtonya hellofromtonya left a 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.

@hellofromtonya
Copy link
Contributor

@tellthemachines Collectively there are some changes made in the PR for:

  • Adding tests
  • Multiline comment formatting
  • Micro-optimizations
  • Guarding to prevent PHP errors

The PR looks ready to me for commit. What do you think? Are the changes okay?

Copy link
Contributor Author

@tellthemachines tellthemachines left a 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 ✅

@hellofromtonya
Copy link
Contributor

Thanks @tellthemachines! Prepping commit now.

@hellofromtonya
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants