diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 6a6418d966457..eeb2a9ae5c3e5 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2238,6 +2238,14 @@ function excerpt_remove_blocks( $content ) { $output = ''; foreach ( $blocks as $block ) { + // Hide the block whenever the value is boolean false, regardless of the + // block's current visibility support. This prevents blocks that previously + // supported visibility from unintentionally appearing on the front end + // after their support was disabled. + if ( false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) ) { + continue; + } + if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { if ( ! empty( $block['innerBlocks'] ) ) { if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) { @@ -2299,6 +2307,14 @@ function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { $output = ''; foreach ( $parsed_block['innerBlocks'] as $inner_block ) { + // Hide the block whenever the value is boolean false, regardless of the + // block's current visibility support. This prevents blocks that previously + // supported visibility from unintentionally appearing on the front end + // after their support was disabled. + if ( false === ( $inner_block['attrs']['metadata']['blockVisibility'] ?? null ) ) { + continue; + } + if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) { continue; } diff --git a/tests/phpunit/tests/formatting/excerptRemoveBlocks.php b/tests/phpunit/tests/formatting/excerptRemoveBlocks.php index 2097c35bbf5b8..ae4ea76378f62 100644 --- a/tests/phpunit/tests/formatting/excerptRemoveBlocks.php +++ b/tests/phpunit/tests/formatting/excerptRemoveBlocks.php @@ -129,4 +129,82 @@ public function test_excerpt_infinite_loop() { $query->the_post(); $this->assertEmpty( do_blocks( '' ) ); } + + /** + * Tests that a top-level block hidden via the visibility block support + * is removed from the excerpt. + * + * @ticket 65456 + */ + public function test_excerpt_remove_blocks_skips_hidden_block() { + $content = ' +
hidden
+ +visible
'; + + $output = excerpt_remove_blocks( $content ); + + $this->assertStringNotContainsString( 'hidden', $output ); + $this->assertStringContainsString( 'visible', $output ); + } + + /** + * Tests that a hidden wrapper block (group/columns/column) is removed + * from the excerpt, including its inner blocks. + * + * @ticket 65456 + * + * @covers ::_excerpt_render_inner_blocks + */ + public function test_excerpt_remove_blocks_skips_hidden_wrapper_block() { + $content = ' +hidden inside group
+visible
'; + + $output = excerpt_remove_blocks( $content ); + + $this->assertStringNotContainsString( 'hidden inside group', $output ); + $this->assertStringContainsString( 'visible', $output ); + } + + /** + * Tests that a hidden block nested inside a visible wrapper is removed. + * + * @ticket 65456 + * + * @covers ::_excerpt_render_inner_blocks + */ + public function test_excerpt_remove_blocks_skips_hidden_inner_block() { + $content = ' +hidden inner
+visible inner
+Hello World
+'; + + $output = excerpt_remove_blocks( $content ); + + $this->assertStringContainsString( 'Hello World', $output ); + } }