Skip to content

Commit 5eecf16

Browse files
committed
Editor: Always hide blocks with visibility set to false.
After a block stopped supporting visibility, a block that had been set to hide everywhere would show up again on the front end. The check is now reordered, so a block set to hide everywhere stays hidden, whether or not the block still supports visibility. Props masteradhoc, ramonopoly, tusharaddweb wildworks. Fixes #65389. git-svn-id: https://develop.svn.wordpress.org/trunk@62586 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bbfb4d5 commit 5eecf16

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/wp-includes/block-supports/block-visibility.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@
2020
function wp_render_block_visibility_support( $block_content, $block ) {
2121
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
2222

23-
if ( ! $block_type || ! block_has_support( $block_type, 'visibility', true ) ) {
23+
if ( ! $block_type ) {
2424
return $block_content;
2525
}
2626

2727
$block_visibility = $block['attrs']['metadata']['blockVisibility'] ?? null;
2828

29+
// Hide the block whenever the value is boolean false, regardless of the
30+
// block's current visibility support. This prevents blocks that previously
31+
// supported visibility from unintentionally appearing on the front end
32+
// after their support was disabled.
2933
if ( false === $block_visibility ) {
3034
return '';
3135
}
3236

37+
if ( ! block_has_support( $block_type, 'visibility', true ) ) {
38+
return $block_content;
39+
}
40+
3341
if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) {
3442
$viewport_config = $block_visibility['viewport'] ?? null;
3543

tests/phpunit/tests/block-supports/block-visibility.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ public function test_block_visibility_support_hides_block_when_visibility_false(
8080
}
8181

8282
/**
83-
* Tests that block visibility support renders block normally when visibility is false
84-
* but blockVisibility support is not opted in.
83+
* Tests that block visibility support hides the block when visibility is false
84+
* even when blockVisibility support is not opted in.
8585
*
8686
* @ticket 64061
87+
* @ticket 65389
8788
*/
88-
public function test_block_visibility_support_shows_block_when_support_not_opted_in(): void {
89+
public function test_block_visibility_support_hides_block_when_visibility_false_even_without_support(): void {
8990
$this->register_visibility_block_with_support(
9091
'test/visibility-block',
9192
array( 'visibility' => false )
@@ -103,7 +104,7 @@ public function test_block_visibility_support_shows_block_when_support_not_opted
103104

104105
$result = wp_render_block_visibility_support( $block_content, $block );
105106

106-
$this->assertSame( $block_content, $result, 'Block content should remain unchanged when blockVisibility support is not opted in.' );
107+
$this->assertSame( '', $result, 'Block content should be empty when blockVisibility is false, even without visibility support.' );
107108
}
108109

109110
/**

0 commit comments

Comments
 (0)