Skip to content
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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;
}
Expand Down
78 changes: 78 additions & 0 deletions tests/phpunit/tests/formatting/excerptRemoveBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,82 @@ public function test_excerpt_infinite_loop() {
$query->the_post();
$this->assertEmpty( do_blocks( '<!-- wp:core/fake /-->' ) );
}

/**
* 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 = '<!-- wp:paragraph {"metadata":{"blockVisibility":false}} -->
<p>hidden</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph --><p>visible</p><!-- /wp:paragraph -->';

$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 = '<!-- wp:group {"metadata":{"blockVisibility":false}} -->
<div class="wp-block-group">
<!-- wp:paragraph --><p>hidden inside group</p><!-- /wp:paragraph -->
</div>
<!-- /wp:group -->
<!-- wp:paragraph --><p>visible</p><!-- /wp:paragraph -->';

$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 = '<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph {"metadata":{"blockVisibility":false}} --><p>hidden inner</p><!-- /wp:paragraph -->
<!-- wp:paragraph --><p>visible inner</p><!-- /wp:paragraph -->
</div>
<!-- /wp:group -->';

$output = excerpt_remove_blocks( $content );

$this->assertStringNotContainsString( 'hidden inner', $output );
$this->assertStringContainsString( 'visible inner', $output );
}

/**
* Tests that a block hidden only on a specific viewport is kept in the
* excerpt. Viewport visibility only affects the rendered display via CSS,
* so it must not strip the block's text from the excerpt.
*
* @ticket 65456
*/
public function test_excerpt_remove_blocks_keeps_viewport_hidden_block() {
$content = '<!-- wp:paragraph {"metadata":{"blockVisibility":{"viewport":{"desktop":false}}}} -->
<p>Hello World</p>
<!-- /wp:paragraph -->';

$output = excerpt_remove_blocks( $content );

$this->assertStringContainsString( 'Hello World', $output );
}
}
Loading