Editor: Fix enqueueing additional styles in wp_enqueue_block_style() to print only when blocks render.

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 commit is a backport from Gutenberg that fixes the issue by printing the styles when a block renders.

Follow-up to [52069].

Props poena, aristath, Mamaduka.
Fixes #54787.
Built from https://develop.svn.wordpress.org/trunk@52556


git-svn-id: http://core.svn.wordpress.org/trunk@52146 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2022-01-11 16:00:01 +00:00
parent fcdf1ce242
commit 2a686addf0
2 changed files with 26 additions and 2 deletions

View File

@ -1254,7 +1254,31 @@ function wp_enqueue_block_style( $block_name, $args ) {
$hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
if ( wp_should_load_separate_core_block_assets() ) {
$hook = "render_block_$block_name";
/**
* Callback function to register and enqueue styles.
*
* @param string $content The block content.
* @param array $block The full block, including name and attributes.
* @return string Block content.
*/
$callback_separate = static function( $content, $block ) use ( $block_name, $callback ) {
if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
return $callback( $content );
}
return $content;
};
/*
* The filter's callback here is an anonymous function because
* using a named function in this case is not possible.
*
* The function cannot be unhooked, however, users are still able
* to dequeue the stylesheets registered/enqueued by the callback
* which is why in this case, using an anonymous function
* was deemed acceptable.
*/
add_filter( 'render_block', $callback_separate, 10, 2 );
return;
}
/*

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-52555';
$wp_version = '6.0-alpha-52556';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.