mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Block Hooks: Persist information about ignoredHookedBlocks
in anchor block metadata
#5712
Closed
ockham
wants to merge
19
commits into
WordPress:trunk
from
ockham:update/inject-block-hooks-into-modified-layouts
Closed
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c74cdb2
Extract get_hooked_block_markup function
ockham 552c66e
Set ignoredHookedBlocks metadata
ockham 498dcf2
Rewrite a bit
ockham 450c05f
Rewrite a bit more
ockham bef2301
Add ticket PHPDoc
ockham 061cbd6
Inject hooked blocks into modified templates and parts
ockham acbd7f2
Relax test assertion a bit
ockham 9e22a87
Get tests to pass
ockham ca6b474
Fix test
ockham 3ad8dce
Relax tests
ockham df1fc75
Add some assertions for ignoredHookedBlocks
ockham 9339ea2
Rename test
ockham 5289f74
Add PHPDoc
ockham 7dc3dfa
Tabs vs spaces
ockham dc35659
Missing .php file extension :facepalm:
ockham ef73917
Add test case
ockham 41c2f1c
Coding Standards
ockham f3dbb7a
Revert "Inject hooked blocks into modified templates and parts"
ockham f4379e1
Mark get_hooked_block_markup() as private
ockham 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 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
This file contains 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 |
---|---|---|
|
@@ -757,6 +757,35 @@ function get_hooked_blocks() { | |
return $hooked_blocks; | ||
} | ||
|
||
/** | ||
* Conditionally returns the markup for a given hooked block type. | ||
* | ||
* Accepts two arguments: A reference to an anchor block, and the name of a hooked block type. | ||
* If the anchor block has already been processed, and the given hooked block type is in the list | ||
* of ignored hooked blocks, an empty string is returned. | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with other similar utilities, should it be marked as private, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in f4379e1. |
||
* @since 6.5.0 | ||
* | ||
* @param array $anchor_block The anchor block. Passed by reference. | ||
* @param string $hooked_block_type The name of the hooked block type. | ||
* @return string The markup for the given hooked block type, or an empty string if the block is ignored. | ||
*/ | ||
function get_hooked_block_markup( &$anchor_block, $hooked_block_type ) { | ||
if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { | ||
$anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); | ||
} | ||
|
||
if ( in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { | ||
return ''; | ||
} | ||
|
||
// The following is only needed for the REST API endpoint. | ||
// However, its presence does not affect the frontend. | ||
$anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; | ||
|
||
return get_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
} | ||
|
||
/** | ||
* Returns a function that injects the theme attribute into, and hooked blocks before, a given block. | ||
* | ||
|
@@ -813,7 +842,7 @@ function make_before_block_visitor( $hooked_blocks, $context ) { | |
*/ | ||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); | ||
foreach ( $hooked_block_types as $hooked_block_type ) { | ||
$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
$markup .= get_hooked_block_markup( $parent_block, $hooked_block_type ); | ||
} | ||
} | ||
|
||
|
@@ -826,7 +855,7 @@ function make_before_block_visitor( $hooked_blocks, $context ) { | |
/** This filter is documented in wp-includes/blocks.php */ | ||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); | ||
foreach ( $hooked_block_types as $hooked_block_type ) { | ||
$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
$markup .= get_hooked_block_markup( $block, $hooked_block_type ); | ||
} | ||
|
||
return $markup; | ||
|
@@ -874,7 +903,7 @@ function make_after_block_visitor( $hooked_blocks, $context ) { | |
/** This filter is documented in wp-includes/blocks.php */ | ||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); | ||
foreach ( $hooked_block_types as $hooked_block_type ) { | ||
$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
$markup .= get_hooked_block_markup( $block, $hooked_block_type ); | ||
} | ||
|
||
if ( $parent_block && ! $next ) { | ||
|
@@ -888,7 +917,7 @@ function make_after_block_visitor( $hooked_blocks, $context ) { | |
/** This filter is documented in wp-includes/blocks.php */ | ||
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); | ||
foreach ( $hooked_block_types as $hooked_block_type ) { | ||
$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
$markup .= get_hooked_block_markup( $parent_block, $hooked_block_type ); | ||
} | ||
} | ||
|
||
|
This file contains 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,69 @@ | ||
<?php | ||
/** | ||
* Tests for the get_hooked_block_markup function. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @group blocks | ||
* @group block-hooks | ||
*/ | ||
class Tests_Blocks_GetHookedBlockMarkup extends WP_UnitTestCase | ||
{ | ||
/** | ||
* @ticket 59646 | ||
* | ||
* @covers ::get_hooked_block_markup | ||
*/ | ||
public function test_get_hooked_block_markup_adds_metadata() { | ||
$anchor_block = array( | ||
'blockName' => 'tests/anchor-block', | ||
); | ||
|
||
$actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' ); | ||
$this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); | ||
$this->assertSame( '<!-- wp:tests/hooked-block /-->', $actual ); | ||
} | ||
|
||
/** | ||
* @ticket 59646 | ||
* | ||
* @covers ::get_hooked_block_markup | ||
*/ | ||
public function test_get_hooked_block_markup_if_block_is_already_hooked() { | ||
$anchor_block = array( | ||
'blockName' => 'tests/anchor-block', | ||
'attrs' => array( | ||
'metadata' => array( | ||
'ignoredHookedBlocks' => array( 'tests/hooked-block' ), | ||
), | ||
) | ||
); | ||
|
||
$actual = get_hooked_block_markup( $anchor_block, 'tests/hooked-block' ); | ||
$this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); | ||
$this->assertSame( '', $actual ); | ||
} | ||
|
||
/** | ||
* @ticket 59646 | ||
* | ||
* @covers ::get_hooked_block_markup | ||
*/ | ||
public function test_get_hooked_block_markup_adds_to_ignored_hooked_blocks() { | ||
$anchor_block = array( | ||
'blockName' => 'tests/anchor-block', | ||
'attrs' => array( | ||
'metadata' => array( | ||
'ignoredHookedBlocks' => array( 'tests/hooked-block' ), | ||
), | ||
) | ||
); | ||
|
||
$actual = get_hooked_block_markup( $anchor_block, 'tests/other-hooked-block' ); | ||
$this->assertSame( array( 'tests/hooked-block', 'tests/other-hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ); | ||
$this->assertSame( '<!-- wp:tests/other-hooked-block /-->', $actual ); | ||
} | ||
} |
This file contains 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
This file contains 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
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.
If we leave out the change in this file, we could land the rest at any point. So the question is whether we can think of any potential issue with making the edited templates hookable?
Do we have user-created patterns covered?
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.
We can give it some smoke testing, plus maybe some more unit test coverage (e.g. in
tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php
) for increased confidence.I haven't checked yet! I'm assuming that yes (since AFAIK their only "entry point" is the Patterns Registry class), but it'll be good to verify 👍
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 think that leaves only Synced Blocks (aka Reusable Blocks), which are loaded through code similar to: