Skip to content

Commit f0dc55b

Browse files
committed
Block visibility: Add block visibility support.
Adds block support to control block visibility. This support will prevent blocks from being rendered on the frontend, i.e., server-side. Props wildworks, mukesh27, rollybueno. Fixes #64061. git-svn-id: https://develop.svn.wordpress.org/trunk@61014 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2077aa2 commit f0dc55b

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Block visibility block support flag.
4+
*
5+
* @package WordPress
6+
* @since 6.9.0
7+
*/
8+
9+
/**
10+
* Render nothing if the block is hidden.
11+
*
12+
* @since 6.9.0
13+
* @access private
14+
*
15+
* @param string $block_content Rendered block content.
16+
* @param array $block Block object.
17+
* @return string Filtered block content.
18+
*/
19+
function wp_render_block_visibility_support( $block_content, $block ) {
20+
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
21+
22+
if ( ! $block_type || ! block_has_support( $block_type, 'blockVisibility', true ) ) {
23+
return $block_content;
24+
}
25+
26+
if ( isset( $block['attrs']['metadata']['blockVisibility'] ) && false === $block['attrs']['metadata']['blockVisibility'] ) {
27+
return '';
28+
}
29+
30+
return $block_content;
31+
}
32+
33+
add_filter( 'render_block', 'wp_render_block_visibility_support', 10, 2 );

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@
393393
require ABSPATH . WPINC . '/block-supports/background.php';
394394
require ABSPATH . WPINC . '/block-supports/block-style-variations.php';
395395
require ABSPATH . WPINC . '/block-supports/aria-label.php';
396+
require ABSPATH . WPINC . '/block-supports/block-visibility.php';
396397
require ABSPATH . WPINC . '/style-engine.php';
397398
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php';
398399
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-css-declarations.php';
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Test the block visibility block support.
4+
*
5+
* @package WordPress
6+
* @subpackage Block Supports
7+
* @since 6.9.0
8+
*
9+
* @group block-supports
10+
*
11+
* @covers ::wp_render_block_visibility_support
12+
*/
13+
class Tests_Block_Supports_Block_Visibility extends WP_UnitTestCase {
14+
/**
15+
* @var string|null
16+
*/
17+
private $test_block_name;
18+
19+
public function set_up() {
20+
parent::set_up();
21+
$this->test_block_name = null;
22+
}
23+
24+
public function tear_down() {
25+
unregister_block_type( $this->test_block_name );
26+
$this->test_block_name = null;
27+
parent::tear_down();
28+
}
29+
30+
/**
31+
* Registers a new block for testing block visibility support.
32+
*
33+
* @param string $block_name Name for the test block.
34+
* @param array $supports Array defining block support configuration.
35+
*
36+
* @return WP_Block_Type The block type for the newly registered test block.
37+
*/
38+
private function register_visibility_block_with_support( $block_name, $supports = array() ) {
39+
$this->test_block_name = $block_name;
40+
register_block_type(
41+
$this->test_block_name,
42+
array(
43+
'api_version' => 3,
44+
'attributes' => array(
45+
'metadata' => array(
46+
'type' => 'object',
47+
),
48+
),
49+
'supports' => $supports,
50+
)
51+
);
52+
$registry = WP_Block_Type_Registry::get_instance();
53+
54+
return $registry->get_registered( $this->test_block_name );
55+
}
56+
57+
/**
58+
* Tests that block visibility support renders empty string when block is hidden
59+
* and blockVisibility support is opted in.
60+
*
61+
* @ticket 64061
62+
*/
63+
public function test_block_visibility_support_hides_block_when_visibility_false() {
64+
$block_type = $this->register_visibility_block_with_support(
65+
'test/visibility-block',
66+
array( 'blockVisibility' => true )
67+
);
68+
69+
$block_content = '<p>This is a test block.</p>';
70+
$block = array(
71+
'blockName' => 'test/visibility-block',
72+
'attrs' => array(
73+
'metadata' => array(
74+
'blockVisibility' => false,
75+
),
76+
),
77+
);
78+
79+
$result = wp_render_block_visibility_support( $block_content, $block );
80+
81+
$this->assertSame( '', $result, 'Block content should be empty when blockVisibility is false and support is opted in.' );
82+
}
83+
84+
/**
85+
* Tests that block visibility support renders block normally when visibility is false
86+
* but blockVisibility support is not opted in.
87+
*
88+
* @ticket 64061
89+
*/
90+
public function test_block_visibility_support_shows_block_when_support_not_opted_in() {
91+
$block_type = $this->register_visibility_block_with_support(
92+
'test/visibility-block',
93+
array( 'blockVisibility' => false )
94+
);
95+
96+
$block_content = '<p>This is a test block.</p>';
97+
$block = array(
98+
'blockName' => 'test/visibility-block',
99+
'attrs' => array(
100+
'metadata' => array(
101+
'blockVisibility' => false,
102+
),
103+
),
104+
);
105+
106+
$result = wp_render_block_visibility_support( $block_content, $block );
107+
108+
$this->assertSame( $block_content, $result, 'Block content should remain unchanged when blockVisibility support is not opted in.' );
109+
}
110+
}

0 commit comments

Comments
 (0)