From b570228aaa95a859be8d3b99df573bbf2ce7570b Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 11 Jan 2022 09:48:29 +0200 Subject: [PATCH 1/3] Fix enqueueing additional styles for blocks only when rendered --- lib/blocks.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index eae7c0abde892..b80d9fc8d0364 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -639,10 +639,41 @@ 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; } - // Enqueue assets in the frontend. + /* + * 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( $hook, $callback ); // Enqueue assets in the editor. From bf22f4dddf96fbc36a6fabb4c5bcebfcad4750c2 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 11 Jan 2022 10:09:53 +0200 Subject: [PATCH 2/3] phpcs fix --- lib/blocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/blocks.php b/lib/blocks.php index b80d9fc8d0364..f25f6b46c656b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -652,6 +652,7 @@ function wp_enqueue_block_style( $block_name, $args ) { } return $content; }; + /* * The filter's callback here is an anonymous function because * using a named function in this case is not possible. From ec6ded84b097ca4bfa91dbbf0c940e1e954f15cf Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 11 Jan 2022 12:36:43 +0200 Subject: [PATCH 3/3] backport changes from core --- lib/blocks.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index f25f6b46c656b..0094ef32230b9 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -605,10 +605,9 @@ function wp_enqueue_block_style( $block_name, $args ) { * @param string $content When the callback is used for the render_block filter, * the content needs to be returned so the function parameter * is to ensure the content exists. - * - * @return string + * @return string Block content. */ - $callback = function( $content ) use ( $args ) { + $callback = static function( $content ) use ( $args ) { // Register the stylesheet. if ( ! empty( $args['src'] ) ) { wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );