diff --git a/backport-changelog/7.1/12053.md b/backport-changelog/7.1/12053.md new file mode 100644 index 00000000000000..0a87da86d9781f --- /dev/null +++ b/backport-changelog/7.1/12053.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/12053 + +* https://github.com/WordPress/gutenberg/pull/78780 diff --git a/lib/block-supports/block-visibility.php b/lib/block-supports/block-visibility.php index 6bbce6b15cfb15..2bb194bce5a8cb 100644 --- a/lib/block-supports/block-visibility.php +++ b/lib/block-supports/block-visibility.php @@ -15,16 +15,24 @@ function gutenberg_render_block_visibility_support( $block_content, $block ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - if ( ! $block_type || ! block_has_support( $block_type, 'visibility', true ) ) { + if ( ! $block_type ) { return $block_content; } $block_visibility = $block['attrs']['metadata']['blockVisibility'] ?? null; + // 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_visibility ) { return ''; } + if ( ! block_has_support( $block_type, 'visibility', true ) ) { + return $block_content; + } + if ( is_array( $block_visibility ) && ! empty( $block_visibility ) ) { // Get viewport configuration from nested structure. $viewport_config = $block_visibility['viewport'] ?? null; diff --git a/phpunit/block-supports/block-visibility-test.php b/phpunit/block-supports/block-visibility-test.php index ad20004a87de90..4f930a8f64a73b 100644 --- a/phpunit/block-supports/block-visibility-test.php +++ b/phpunit/block-supports/block-visibility-test.php @@ -93,7 +93,7 @@ public function test_block_visibility_support_hides_block_when_visibility_false( $this->assertSame( '', $result, 'Block content should be empty when blockVisibility is false and support is opted in.' ); } - public function test_block_visibility_support_shows_block_when_support_not_opted_in() { + public function test_block_visibility_support_hides_block_when_visibility_false_even_without_support() { $this->register_visibility_block_with_support( 'test/visibility-block', array( 'visibility' => false ) @@ -111,7 +111,7 @@ public function test_block_visibility_support_shows_block_when_support_not_opted $result = gutenberg_render_block_visibility_support( $block_content, $block ); - $this->assertSame( $block_content, $result, 'Block content should remain unchanged when blockVisibility support is not opted in.' ); + $this->assertSame( '', $result, 'Block content should be empty when blockVisibility is false, even without visibility support.' ); } public function test_block_visibility_support_no_visibility_attribute() {