-
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
Layout Support: Replace incremental IDs with hashes #68210
base: trunk
Are you sure you want to change the base?
Layout Support: Replace incremental IDs with hashes #68210
Conversation
Flaky tests detected in eb456cb. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/13332123167
|
Added some potential reviewers for when this is ready. |
There are more places where incremental IDs are used. Example for layouts in child blocks. Should the strategy change there as well? gutenberg/lib/block-supports/layout.php Line 624 in 9c568ad
|
@gziolo Good catch! Yes, |
@westonruter, do you have any thoughts on this proposal? I remember you suggested switching to |
@Mamaduka I fully endorse this proposal! Long ago Gutenberg was using Related, the Embed Optimizer plugin in Performance Lab is also using |
lib/block-supports/layout.php
Outdated
* | ||
* @return string The generated unique ID for the array. | ||
*/ | ||
function gutenberg_unique_prefixed_id_from_array( array $data, string $prefix = '' ): string { |
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.
Nit (non-blocking): I think we could use a more generic name here, wp_unique_id_from_values
. Since the prefix is optional, we could also remove it.
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 see the name was changed to gutenberg_unique_id_from_values
. @DAreRodz, can you clarify your intentions regarding the prefix?
eb70891
to
eb456cb
Compare
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Setting this PR ready for review. The logic for generating layout styles is complex, so I've added some test cases instead of testing the feature extensively. More test cases are welcome. 🙂 |
@@ -501,7 +501,47 @@ public function data_layout_support_flag_renders_classnames_on_wrapper() { | |||
), | |||
), | |||
), | |||
'expected_output' => '<p class="wp-container-content-1">Some text.</p>', // The generated classname number assumes `wp_unique_prefixed_id( 'wp-container-content-' )` will not have run previously in this test. | |||
'expected_output' => '/<p class="wp-container-content-[a-f0-9]{8}">Some text.<\\/p>/', |
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.
md5 is predictable so it should stay the same between the test runs. What is the reasoning behind using pattern matching here? I expected the tests would ensure that these values don't accidentally change as long as params provided remain the same.
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.
No problem. I can change that. I wasn't sure if it would be a good idea to update the tests every time the passed parameters change, but that's not going to happen often, right? 😅
PS: I copied what is being done for the duotone classes (although, in that case, the IDs might not be predictable).
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 just updated the tests to check the generated hashes with their expected values.
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.
Cool, it seems like it's doable for the use cases in this PR. We could leave a note in the tests that the hash might change if the conditions provided get refined in the future.
In my opinion, this PR is shaping up nicely, and my recent comments are mostly stylistic. I don't think I have any major feedback left at this point. I would appreciate some additional review on how the hash is calculated by someone more familiar with layout processing. |
$fallback_gap_value, | ||
$block_spacing, | ||
), | ||
'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-' |
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.
Not a blocker: I have a general thought after looking at the generated CSS. The fact that the class name contains the name of the block might be no longer needed. It's currently a general-purpose class that groups similar styles. It might be interesting to explore in the follow-up PR whether the class name prefix can be simplified to the wp-container-block-is-layout-
. That would be also similar to the child layouts and the wp-container-content-
prefix.

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.
From my POV this here is a great fix that works as advertised that we should get into WordPress 6.8.
I'm about to start the RC for 20.4 which is the last release to make it into 6.8 so ideally I would have wanted to merge this before then. But since it is a bugfix we should be able to still get it in after today. So I'll hold merging and wait a few more days for additional feedback 👍
Fabian, feel free to land it if you confirmed it works correctly. We can discuss my feedback separately as the more I think about it, the less it's of an issue that editors encapsulates the styles under different class names. It doesn't impact visual aspects and developers can't target them anyway. |
Yeah I agree we should align the class name generation in JS to match. But since they are auto generated already and users cannot target them for me the two are separate and one doesn't need to block the other :) And yes in my testing this solves the issue it was intended to solve and I am not noticing any regressions on the sample content I have as testing data :) |
@fabiankaegy, feel free to merge this PR once it's ready. 😊 |
I think aligning class name generation between JS and PHP would be a lovely addition. @DAreRodz, @gziolo, do you have time to work on this? Considering the bug status of this PR, it should be safe to merge in release betas. @joemcgill, @desrosj, what do you think? |
I agree with Fabian that, although aligning JS class name generation would be a nice addition, it shouldn't block merging this PR. |
What are the remaining items here? Just aligning JS class name generation? Also, is the core backport PR up to date? @desrosj, @joemcgill, what do you think about shipping this fix in WP 6.8? |
What?
Changes how class names for layout containers are generated, replacing per-block, incrementally-generated IDs with hashes obtained from the layout styles definition.
Will fix #67308.
Why?
This currently affects the style of elements outside of router regions, even when navigating between pages generated with the same template, because layout class names depend on the number of blocks rendered using them (see #67826), which can vary.
How?
The PR adds a new function named
gutenberg_unique_id_from_values
, which basically generates a short hash from the passed array.This function is used to generate the layout container class, receiving all the data required to generate the style output. That guarantees hashes to be unique for each layout style combination.
Similar examples can be found in the WordPress codebase already:
Testing Instructions
div
container for these groups has a class name similar towp-container-core-group-is-layout-634f0b9d
(a different hash is OK).Screenshots or screencast
Screen.Recording.2024-12-20.at.13.45.58.mov
Screen.Recording.2024-12-20.at.13.48.13.mov