-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add block visibility support #10110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
t-hamano
wants to merge
3
commits into
WordPress:trunk
Choose a base branch
from
t-hamano:64061/block-visibility-support
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add block visibility support #10110
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Block visibility block support flag. | ||
* | ||
* @package WordPress | ||
* @since 6.9.0 | ||
*/ | ||
|
||
/** | ||
* Render nothing if the block is hidden. | ||
* | ||
* @since 6.9.0 | ||
* @access private | ||
* | ||
* @param string $block_content Rendered block content. | ||
* @param array $block Block object. | ||
* @return string Filtered block content. | ||
*/ | ||
function wp_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, 'blockVisibility', true ) ) { | ||
return $block_content; | ||
} | ||
|
||
if ( isset( $block['attrs']['metadata']['blockVisibility'] ) && false === $block['attrs']['metadata']['blockVisibility'] ) { | ||
return ''; | ||
} | ||
|
||
return $block_content; | ||
} | ||
|
||
add_filter( 'render_block', 'wp_render_block_visibility_support', 10, 2 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tests/phpunit/tests/block-supports/block-visibility.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* Test the block visibility block support. | ||
* | ||
* @package WordPress | ||
* @subpackage Block Supports | ||
* @since 6.9.0 | ||
* | ||
* @group block-supports | ||
* | ||
* @covers ::wp_render_block_visibility_support | ||
*/ | ||
class Tests_Block_Supports_Block_Visibility extends WP_UnitTestCase { | ||
/** | ||
* @var string|null | ||
*/ | ||
private $test_block_name; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
$this->test_block_name = null; | ||
} | ||
|
||
public function tear_down() { | ||
unregister_block_type( $this->test_block_name ); | ||
$this->test_block_name = null; | ||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Registers a new block for testing block visibility support. | ||
* | ||
* @param string $block_name Name for the test block. | ||
* @param array $supports Array defining block support configuration. | ||
* | ||
* @return WP_Block_Type The block type for the newly registered test block. | ||
*/ | ||
private function register_visibility_block_with_support( $block_name, $supports = array() ) { | ||
$this->test_block_name = $block_name; | ||
register_block_type( | ||
$this->test_block_name, | ||
array( | ||
'api_version' => 3, | ||
'attributes' => array( | ||
'metadata' => array( | ||
'type' => 'object', | ||
), | ||
), | ||
'supports' => $supports, | ||
) | ||
); | ||
$registry = WP_Block_Type_Registry::get_instance(); | ||
|
||
return $registry->get_registered( $this->test_block_name ); | ||
} | ||
|
||
/** | ||
* Tests that block visibility support renders empty string when block is hidden | ||
* and blockVisibility support is opted in. | ||
* | ||
* @ticket 64061 | ||
*/ | ||
public function test_block_visibility_support_hides_block_when_visibility_false() { | ||
$block_type = $this->register_visibility_block_with_support( | ||
'test/visibility-block', | ||
array( 'blockVisibility' => true ) | ||
); | ||
|
||
$block_content = '<p>This is a test block.</p>'; | ||
$block = array( | ||
'blockName' => 'test/visibility-block', | ||
'attrs' => array( | ||
'metadata' => array( | ||
'blockVisibility' => false, | ||
), | ||
), | ||
); | ||
|
||
$result = wp_render_block_visibility_support( $block_content, $block ); | ||
|
||
$this->assertSame( '', $result, 'Block content should be empty when blockVisibility is false and support is opted in.' ); | ||
} | ||
|
||
/** | ||
* Tests that block visibility support renders block normally when visibility is false | ||
* but blockVisibility support is not opted in. | ||
* | ||
* @ticket 64061 | ||
*/ | ||
public function test_block_visibility_support_shows_block_when_support_not_opted_in() { | ||
$block_type = $this->register_visibility_block_with_support( | ||
'test/visibility-block', | ||
array( 'blockVisibility' => false ) | ||
); | ||
|
||
$block_content = '<p>This is a test block.</p>'; | ||
$block = array( | ||
'blockName' => 'test/visibility-block', | ||
'attrs' => array( | ||
'metadata' => array( | ||
'blockVisibility' => false, | ||
), | ||
), | ||
); | ||
|
||
$result = wp_render_block_visibility_support( $block_content, $block ); | ||
|
||
$this->assertSame( $block_content, $result, 'Block content should remain unchanged when blockVisibility support is not opted in.' ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we should move these checks to the top. If the attribute is already set, we can return early and avoid registering the type unnecessarily. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the intention, but I think we should check the block support first. Otherwise, the block may return an empty string even though it doesn't support
blockVisibility
.