Skip to content
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

Block Styles: Ensure unique classname generation for variations #7200

Conversation

aaronrobertshaw
Copy link

Trac ticket: https://core.trac.wordpress.org/ticket/61877

This PR backports the PHP changes from WordPress/gutenberg#64511.

What?

Simplifies block style variation class name generation to ensure unique class.

Why?

Avoids potential for non-unique class names and conflicting styles when exact copies of a block are inserted via a repeated pattern.

Further background and context can be found over on WordPress/gutenberg#64511.

How?

Replace the hashing of block attributes in the block style variation classnames with a call to wp_unique_id.

Testing Instructions

Confirm unique classnames on frontend

  1. Create a pattern
  2. Add a button with the Outline block styles to the pattern
  3. Save the pattern and edit a post
  4. Insert the above pattern 3 times in the post
  5. Save and view the post on the frontend
  6. Inspect a button and search the markup for is-style-outline-. You should find CSS with three styles matching that pattern however they should now all be unique.

Ensure no regressions for block style variations on the frontend

  1. Add some custom block style variations to your theme. See #57908 for detailed examples or the snippet below for a quick option.
  2. Add suitable content to a page, making sure there is enough that nested applications of block style variations can occur
  3. In the site editor, apply the custom block style variations to various content, including in a nested fashion
  4. Save and confirm the correct styling on the frontend
Example block style variation theme.json partial (place under `/styles`)
{
	"$schema": "https://schemas.wp.org/trunk/theme.json",
	"version": 3,
	"title": "Section A",
	"slug": "section-a",
	"blockTypes": [ "core/group" ],
	"styles": {
		"color": {
			"background": "slategrey",
			"text": "snow"
		},
		"blocks": {
			"core/group": {
				"color": {
					"background": "darkslategrey",
					"text": "whitesmoke"
				}
			}
		}
	}
}

Screenshots or screencast

Simple unique classnames for exact copies of blocks

Screenshot 2024-08-14 at 9 27 11 PM

Block Style Variations still working

Screen.Recording.2024-08-14.at.9.24.28.PM.mp4

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

github-actions bot commented Aug 15, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aaronrobertshaw, martinkrcho, mukesh27, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Unit tests going failed. That needs to fix.

1) Tests_Block_Supports_WpCreateBlockStyleVariationInstanceName::test_block_style_variation_instance_name_generation
Unexpected deprecation notice for wp_create_block_style_variation_instance_name.
Function wp_create_block_style_variation_instance_name is deprecated since version 6.7.0! Use wp_unique_id instead.
Failed asserting that an array is empty.

/var/www/tests/phpunit/includes/abstract-testcase.php:643
/var/www/tests/phpunit/includes/abstract-testcase.php:655
/var/www/vendor/yoast/phpunit-polyfills/src/TestCases/TestCasePHPUnitGte8.php:82
phpvfscomposer:///var/www/vendor/phpunit/phpunit/phpunit:97
/var/www/vendor/bin/phpunit:118

@aaronrobertshaw aaronrobertshaw force-pushed the fix/non-unique-variation-classnames-for-duplicate-blocks branch from 59afe95 to b851a6b Compare August 28, 2024 04:33
@aaronrobertshaw
Copy link
Author

Thanks for flagging that @mukeshpanchal27 👍

I wasn't too sure on whether the test should be removed or just flagged as expecting the deprecation. I went with flagging the expected deprecation so it can be removed once the deprecated function is.

Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Relocation sale and a question.

src/wp-includes/block-supports/block-style-variations.php Outdated Show resolved Hide resolved
src/wp-includes/block-supports/block-style-variations.php Outdated Show resolved Hide resolved
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
Copy link
Member

@ramonjd ramonjd left a comment

Choose a reason for hiding this comment

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

✅ Unique ids appear for style variations inside multiple patterns on the frontend
✅ Unique style variations rules are being generated for global styles on the frontend, e.g., :root :where(.wp-block-group.is-style-section-a--17)

@ramonjd
Copy link
Member

ramonjd commented Aug 29, 2024

Committed in r58951 / 0a9dcb4

@ramonjd ramonjd closed this Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants