Skip to content

Commit f3209f6

Browse files
committed
Introduce enqueue_empty_block_content_assets filter
1 parent 0e5a3f2 commit f3209f6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/wp-includes/class-wp-block.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,16 @@ public function render( $options = array() ) {
632632
* In all these cases, adding scripts and styles will be a waste since they will not be used on the page.
633633
*/
634634
$processor = new WP_HTML_Tag_Processor( $block_content );
635-
if ( $processor->next_tag() ) {
635+
636+
/**
637+
* Filters whether to enqueue assets for a block which has no rendered content.
638+
*
639+
* @since 6.9.0
640+
*
641+
* @param bool $enqueue Whether to enqueue assets.
642+
* @param string $block_name Block name.
643+
*/
644+
if ( (bool) apply_filters( 'enqueue_empty_block_content_assets', $processor->next_tag(), $this->name ) ) {
636645
if ( ( ! empty( $this->block_type->script_handles ) ) ) {
637646
foreach ( $this->block_type->script_handles as $script_handle ) {
638647
wp_enqueue_script( $script_handle );

src/wp-includes/script-loader.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,8 @@ static function ( $html, $block ) use ( $block_name, $style_properties ) {
27392739
if ( $block['blockName'] === $block_name ) {
27402740
// If the block didn't render any tags, then do not enqueue any styles. Rendering just an HTML comment is also excluded.
27412741
$processor = new WP_HTML_Tag_Processor( $html );
2742-
if ( $processor->next_tag() ) {
2742+
/** This filter is documented in src/wp-includes/class-wp-block.php */
2743+
if ( (bool) apply_filters( 'enqueue_empty_block_content_assets', $processor->next_tag(), $block_name ) ) {
27432744
wp_enqueue_style( $style_properties['style_handle'] );
27442745
}
27452746
}
@@ -3295,11 +3296,12 @@ function wp_enqueue_block_style( $block_name, $args ) {
32953296
* is to ensure the content exists.
32963297
* @return string Block content.
32973298
*/
3298-
$callback = static function ( $content ) use ( $args ) {
3299+
$callback = static function ( $content ) use ( $block_name, $args ) {
32993300

33003301
// If the block didn't render any tags, then do not enqueue any styles. Rendering just an HTML comment is also excluded.
33013302
$processor = new WP_HTML_Tag_Processor( $content );
3302-
if ( ! $processor->next_tag() ) {
3303+
/** This filter is documented in src/wp-includes/class-wp-block.php */
3304+
if ( ! (bool) apply_filters( 'enqueue_empty_block_content_assets', $processor->next_tag(), $block_name ) ) {
33033305
return $content;
33043306
}
33053307

0 commit comments

Comments
 (0)