Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Anonymous functions as callbacks to filter/action is prohibited in Core.

To resolve, an overall architectural strategy is needed to provide the means to unhook callbacks and remove the anonymous functions.

However, as this pattern already exists in this file and it's too late in 5.9 to introduce a new architectural strategy to remove the need for these lambdas, this pattern is consistent. Plus the static keyword is for performance.

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;
}

/*
Expand Down