From 2a686addf02c4cac0fac0ff7b949ff7ffbcbf5f8 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Tue, 11 Jan 2022 16:00:01 +0000 Subject: [PATCH] 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 --- wp-includes/blocks.php | 26 +++++++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 0f34aac1df..d6d73bdfe8 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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; } /* diff --git a/wp-includes/version.php b/wp-includes/version.php index 36220f08e2..2443ce9388 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.