From 9f56ce822e4af4f04d148a2e1cf4072164947082 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Sun, 28 Nov 2021 21:10:08 +0000 Subject: [PATCH] Script Loader: Enqueue block stylesheet only when the corresponding block is used. Before this change, block stylesheets were enqueued although the corresponding blocks were not used in the current post, even if the `should_load_separate_core_block_assets` filter was set to `true`. Props shimotomoki, devutpol, audrasjb. Fixes #54457. git-svn-id: https://develop.svn.wordpress.org/trunk@52262 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/script-loader.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 972b4497a6..7ffa2d5fde 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2459,10 +2459,14 @@ function enqueue_block_styles_assets() { if ( wp_should_load_separate_core_block_assets() ) { add_filter( 'render_block', - function( $html ) use ( $style_properties ) { - wp_enqueue_style( $style_properties['style_handle'] ); + function( $html, $block ) use ( $block_name, $style_properties ) { + if ( $block['blockName'] === $block_name ) { + wp_enqueue_style( $style_properties['style_handle'] ); + } return $html; - } + }, + 10, + 2 ); } else { wp_enqueue_style( $style_properties['style_handle'] );