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 level settings: remove WP filters in Gutenberg #49598

Merged
merged 1 commit into from
Apr 6, 2023

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Apr 5, 2023

What?

Remove the corresponding WP filters in the plugin for _gutenberg_add_block_level_presets_class so that we don't double up on block level settings styles when the plugin is active.

Why?

#42124 introduced two hooks for _gutenberg_add_block_level_presets_class to print block level settings stylesheets to the page.

These changes were backported to Core in WordPress/wordpress-develop#4013.

Now, when running the plugin in WordPress 6.3-alpha-55628 the style block is output twice.

How?

Removing the WP filters:

remove_filter( 'render_block', '_wp_add_block_level_presets_class', 10, 2 );
remove_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10, 2 );

Testing Instructions

Here is some test HTML :

<!-- wp:group {"settings":{"blocks":{"core/button":{"color":{"palette":{"custom":[{"slug":"button-red","color":"red","name":"button red"},{"slug":"button-blue","color":"blue","name":"button blue"}]}}},"core/paragraph":{"color":{"palette":{"custom":[{"slug":"paragraph-red","color":"red","name":"paragraph red"},{"slug":"paragraph-blue","color":"blue","name":"paragraph blue"}]}}}},"color":{"palette":{"custom":[{"slug":"global-aquamarine","color":"aquamarine","name":"Global aquamarine"},{"slug":"global-pink","color":"pink","name":"Global Pink"}]}}}} -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>Leaf paragraph of inner group block.</p>
<!-- /wp:paragraph -->

<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"backgroundColor":"button-blue"} -->
<div class="wp-block-button"><a class="wp-block-button__link has-button-blue-background-color has-background wp-element-button">blue</a></div>
<!-- /wp:button -->

<!-- wp:button {"backgroundColor":"button-red"} -->
<div class="wp-block-button"><a class="wp-block-button__link has-button-red-background-color has-background wp-element-button">red</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->

<!-- wp:paragraph {"backgroundColor":"paragraph-blue"} -->
<p class="has-paragraph-blue-background-color has-background">blue</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"paragraph-red"} -->
<p class="has-paragraph-red-background-color has-background">red</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

And don't forget the theme.json with the block-level CSS settings.

Example theme.json
{
	"$schema": "https://schemas.wp.org/trunk/theme.json",
	"version": 2,
	"settings": {
		"appearanceTools": true,
		"layout": {
			"contentSize": "840px",
			"wideSize": "1100px"
		},
		"color": {
			"palette": [
				{
					"slug": "global-aquamarine",
					"color": "aquamarine",
					"name": "Global aquamarine"
				},
				{
					"slug": "global-pink",
					"color": "pink",
					"name": "Global Pink"
				}
			]
		},
		"blocks": {
			"core/button": {
				"color": {
					"palette": {
						"custom": [
							{
								"slug": "button-red",
								"color": "red",
								"name": "button red"
							},
							{
								"slug": "button-blue",
								"color": "blue",
								"name": "button blue"
							}
						]
					}
				}
			},
			"core/paragraph": {
				"color": {
					"palette": {
						"custom": [
							{
								"slug": "paragraph-red",
								"color": "red",
								"name": "paragraph red"
							},
							{
								"slug": "paragraph-blue",
								"color": "blue",
								"name": "paragraph blue"
							}
						]
					}
				}
			}
		}
	},
	"styles": {
		"color": {
			"background": "var(--wp--preset--color--global-aquamarine)",
			"text": "var(--wp--preset--color--global-pink)"
		}
	},
	"patterns": [
		"short-text-surrounded-by-round-images",
		"partner-logos"
	]
}

Check that the resulting CSS is not output twice.

Before After
Screenshot 2023-04-05 at 2 42 03 pm Screenshot 2023-04-05 at 2 42 15 pm

…we don't double up on styles when the plugin is active.
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Block] Group Affects the Group Block (and row, stack and grid variants) Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Apr 5, 2023
@ramonjd ramonjd self-assigned this Apr 5, 2023
@ramonjd ramonjd requested a review from spacedmonkey as a code owner April 5, 2023 04:47
@github-actions
Copy link

github-actions bot commented Apr 5, 2023

Flaky tests detected in eae4665.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4615028665
📝 Reported issues:

Copy link
Contributor

@andrewserong andrewserong 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 tidying this up @ramonjd!

✅ Confirmed that on trunk the style tag and its contents is output twice
✅ With this PR applied, the style tag is only output once

Unrelated to this PR, it looks like this block support's feature is only supported on the site frontend and not within the post or site editors? At any rate, there's no difference in behaviour to the editor with this PR so that's a little off topic for now 😄

LGTM! ✨

@ramonjd
Copy link
Member Author

ramonjd commented Apr 6, 2023

Unrelated to this PR, it looks like this block support's feature is only supported on the site frontend and not within the post or site editors?

I noticed that too and assumed that it was just implemented to be a theme.json API for now. Maybe @jorgefilipecosta can confirm.

Thanks a lot for testing!!

@ramonjd ramonjd merged commit 6ec3184 into trunk Apr 6, 2023
@ramonjd ramonjd deleted the update/remove-wp-block-level-preset-style-filters branch April 6, 2023 05:57
@github-actions github-actions bot added this to the Gutenberg 15.6 milestone Apr 6, 2023
@ramonjd ramonjd added the Needs PHP backport Needs PHP backport to Core label Jun 5, 2023
@ramonjd ramonjd removed the Needs PHP backport Needs PHP backport to Core label Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Group Affects the Group Block (and row, stack and grid variants) Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants