Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/wp-includes/block-supports/custom-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ function wp_render_custom_css_support_styles( $parsed_block ) {

if ( ! empty( $processed_css ) ) {
/*
* Register and add inline style for block custom CSS.
* The style depends on global-styles to ensure custom CSS loads after
* and can override global styles.
* Track which class names have already had their CSS enqueued to prevent
* duplicate styles when the same block is rendered multiple times inside
* a Query Loop (render_block_data fires once per loop iteration).
Comment on lines +60 to +62

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Track which class names have already had their CSS enqueued to prevent
* duplicate styles when the same block is rendered multiple times inside
* a Query Loop (render_block_data fires once per loop iteration).
* Skip CSS that has already been added. Blocks with identical attributes
* share the same class name and processed CSS via wp_unique_id_from_values(),
* so the same style would otherwise be enqueued more than once (e.g. inside
* a Query Loop or when blocks share identical custom CSS).

Since this issue is not limited to the Query Loop block, I recommend updating this comments to be more accurate.

For example, create content like the following:

<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"css":"background: red;"}} -->
<p class="has-custom-css">Hello World</p>
<!-- /wp:paragraph -->

This PR will eliminate the duplicate CSS.

Before:

<style id="wp-block-custom-css-inline-css">
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
:root :where(.wp-custom-css-38db0572){background: red;}
</style>

After:

```html
<style id="wp-block-custom-css-inline-css">
:root :where(.wp-custom-css-38db0572){background: red;}
</style>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good call, although I suggest {@see wp_unique_id_from_values()} to link the reference.

*/
wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) );
wp_add_inline_style( 'wp-block-custom-css', $processed_css );
$handle = 'wp-block-custom-css';
if ( ! wp_style_is( $handle, 'registered' ) ) {
wp_register_style( $handle, false, array( 'global-styles' ) );
}
$after_styles = wp_styles()->get_data( $handle, 'after' );
if ( ! is_array( $after_styles ) ) {
$after_styles = array();
}
if ( ! in_array( $processed_css, $after_styles, true ) ) {
wp_add_inline_style( $handle, $processed_css );
}
}

return $parsed_block;
Expand Down
Loading