Skip to content

Conversation

@aristath
Copy link
Member

@aristath aristath commented Jan 11, 2022

Description

Issue reported in https://make.wordpress.org/core/2021/12/15/using-multiple-stylesheets-per-block/#comment-42320

In a block theme, additional block styles registered using the wp_enqueue_block_style function should only get printed when the block exists on a page. However, they currently always get rendered.

This PR fixes the issue so that these styles will only get printed when a block gets rendered.

How has this been tested?

Using 5.0-RC, with the twentytwentytwo theme:
Added this in the theme's functions.php file:

add_action( 'after_setup_theme', function() {
	wp_enqueue_block_style( 'core/gallery', array(
		'handle' => 'my-theme-gallery',
		'src'    => get_theme_file_uri( 'assets/blocks/gallery.css' ),
		'path'   => get_theme_file_path( 'assets/blocks/gallery.css' ),
	) );
} );

Then, I added a new assets/blocks/gallery.css file in the theme with these contents:

img {
	padding: 10px;
	background: #000;
	color: #fff;
}

Tested and confirmed that the styles only get added when the block exists.
Then switched to the twentytwentyone theme (or any other non-block theme), and confirmed that the styles get added regardless of whether the block exists on a page or not (conditional stylesheets loading should only work for block themes)

This should be backported to 5.9 👍

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).
  • I've updated related schemas if appropriate.

@aristath aristath added [Type] Bug An existing feature does not function as intended Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Jan 11, 2022
@aristath aristath requested a review from a team January 11, 2022 08:01
@aristath aristath requested review from Mamaduka and gziolo January 11, 2022 08:02
@Mamaduka
Copy link
Member

@aristath, is there a track ticket for this issue? We have to manually backport these changes since they're not part of the packages.

@aristath
Copy link
Member Author

There's no trac ticket for that, it was simply reported on https://make.wordpress.org/core/2021/12/15/using-multiple-stylesheets-per-block/#comment-42320

@aristath aristath mentioned this pull request Jan 11, 2022
8 tasks
@aristath aristath requested a review from carolinan January 11, 2022 08:59
@aristath aristath force-pushed the fix/wp_enqueue_block_style branch from 21fe2f5 to 4485c89 Compare January 11, 2022 09:28
@aristath
Copy link
Member Author

@Mamaduka do you want me to create a ticket on track for this issue? Or have you already done that?

@Mamaduka
Copy link
Member

@aristath, core ticket, and the patch would be great to ship fix with RC2.

@aristath
Copy link
Member Author

Trac ticket & patch: https://core.trac.wordpress.org/ticket/54790

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

The fix works as expected. Thank you, Ari.

P.S. I had to test using WP 5.8.x since plugin functions aren't loading in 5.9 RC, and used img { border: 5px solid red; } to make file existance immediately visible.

@aristath aristath merged commit 22edce2 into trunk Jan 11, 2022
@aristath aristath deleted the fix/wp_enqueue_block_style branch January 11, 2022 11:52
@github-actions github-actions bot added this to the Gutenberg 12.4 milestone Jan 11, 2022
@noisysocks
Copy link
Member

I see that these changes were already made to Core (https://core.trac.wordpress.org/changeset/52559) so just noting here to my future self that I only need to merge this into wp/5.9 for posterity and not do any actual backporting 👍

@noisysocks noisysocks removed the Backport to WP 6.9 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jan 17, 2022
noisysocks pushed a commit that referenced this pull request Jan 17, 2022
* Fix enqueueing additional styles for blocks only when rendered

* phpcs fix

* backport changes from core
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants