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

Load block support styles in the head for block themes #2323

Closed
wants to merge 1 commit into from
Closed

Load block support styles in the head for block themes #2323

wants to merge 1 commit into from

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Feb 16, 2022

Part of https://core.trac.wordpress.org/ticket/55148
Backports WordPress/gutenberg#38750

The dynamic block styles for layout and elements should be loaded in the <head> for block themes. While that should also be the case for classic themes, the current mechanism we use (render_block) does not allow us to do that, hence, this PR doesn't change anything for them and will be loaded the <body>.

How to test

Block theme in WordPress 5.9:

  • Use TwentyTwentyTwo and load the front end.
  • Verify the embedded styles for elements (.wp-elements-UUID ...) and layout (.wp-container-UUUID ...) are loaded in the <head>.

Classic theme in WordPress 5.9:

  • Use TwentyTwenty and load the front end.
  • Verify the embedded styles for elements (.wp-elements-UUID ...) and layout (.wp-container-UUUID ...) are loaded in the <body>.

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Committing right now.

}
add_action(
$action_hook_name,
function () use ( $style ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
function () use ( $style ) {
static function () use ( $style ) {

Tiny performance boost where PHP creates the embedded function once as a static instead of it being created each time the parent function is invoked.

*
* @param string $style String containing the CSS styles to be added.
*/
function wp_enqueue_block_support( $style ) {
Copy link
Contributor

@hellofromtonya hellofromtonya Feb 16, 2022

Choose a reason for hiding this comment

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

  1. The function name does not fully identify what happens when this function is invoked. Its purpose is to enqueue styles rather than "support". How about wp_enqueue_inline_block_styles ? @oandregal what do you think?

  2. Let's move this function to wp-includes/script-loader.php where like functions exist.

  3. Nitpick: @since annonation is needed (though can be added by core committer in prepping the commit.

Copy link
Member Author

@oandregal oandregal Feb 16, 2022

Choose a reason for hiding this comment

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

  1. Naming. I'd like to retain the block_support if possible, perhaps wp_enqueue_block_support_styles?
  2. Moving: I'm fine with that place if folks are opinionated about that. I also wanted to share more of the rationale as to why blocks.php sounds like a good place as well: this is where other functions related to blocks live, such as wp_enqueue_block_style or wp_migrate_old_typography_shape that deal with block things (styles, block supports, etc).
  3. Sorry I've missed the @since tag.

* because the wp_head action (and wp_enqueue_scripts)
* happens before the render_block.
*
* See https://core.trac.wordpress.org/ticket/53494.
Copy link
Member

Choose a reason for hiding this comment

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

This needs @since annotation

}
add_action(
$action_hook_name,
function () use ( $style ) {
Copy link
Member

Choose a reason for hiding this comment

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

How can someone unhook this function if desired?

Copy link
Contributor

Choose a reason for hiding this comment

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

This same problem exists elsewhere too. Here's the ticket for that work https://core.trac.wordpress.org/ticket/54028.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's also preexisting to this PR, note that this focused on fixing a bug with block themes (and moved this code around for convenience). I didn't have the context on why an anonymous function was used, so I didn't want to mix things. That can be updated separately.

* because the wp_head action (and wp_enqueue_scripts)
* happens before the render_block.
*
* See https://core.trac.wordpress.org/ticket/53494.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* See https://core.trac.wordpress.org/ticket/53494.
* @link https://core.trac.wordpress.org/ticket/53494.

@oandregal
Copy link
Member Author

@swissspidy @hellofromtonya I see this had already been approved&commited at https://core.trac.wordpress.org/changeset/52741

Is there anyone with commit access that can do those changes directly? I also can prepare a follow-up if it's convenient for people.

@oandregal oandregal closed this Feb 16, 2022
@oandregal oandregal deleted the fix/block-styles-head branch February 16, 2022 18:22
@hellofromtonya
Copy link
Contributor

@jorgefilipecosta is preparing a follow-up commit https://wordpress.slack.com/archives/C02RQBWTW/p1645035406929929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants