-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Post Template Block: Set block context via filter (#50313)
* Post Template Block: Set block context via filter In the Post Template block's render callback, use the `render_block_context` filter to set `postId` and `postType` context, rather than passing that context directly to the `WP_Block` constructor. This approach arguably follows our established patterns with regard to handling block context better. Notably, with the previous approach, we were only setting block context for the Post Template block itself. In this PR, we extend it to apply to all child blocks, including ones that are dynamically inserted, e.g. via the `render_block` filter. This is relevant for Auto-inserting blocks (see #50103). This follows the precedent of the Comment Template block, see #50279. Furthermore, add some test coverage to guard against duplicated block-supports class names, which was an issue in a previous iteration of this PR.
- Loading branch information
Showing
2 changed files
with
83 additions
and
9 deletions.
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Tests for the Post Template block rendering. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* @since 6.0.0 | ||
* | ||
* @group blocks | ||
*/ | ||
class Tests_Blocks_RenderPostTemplateBlock extends WP_UnitTestCase { | ||
|
||
private static $post; | ||
private static $other_post; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
|
||
self::$post = self::factory()->post->create_and_get( | ||
array( | ||
'post_type' => 'post', | ||
'post_status' => 'publish', | ||
'post_name' => 'metaldog', | ||
'post_title' => 'Metal Dog', | ||
'post_content' => 'Metal Dog content', | ||
'post_excerpt' => 'Metal Dog', | ||
) | ||
); | ||
|
||
self::$other_post = self::factory()->post->create_and_get( | ||
array( | ||
'post_type' => 'post', | ||
'post_status' => 'publish', | ||
'post_name' => 'ceilingcat', | ||
'post_title' => 'Ceiling Cat', | ||
'post_content' => 'Ceiling Cat content', | ||
'post_excerpt' => 'Ceiling Cat', | ||
) | ||
); | ||
} | ||
|
||
public function test_rendering_post_template() { | ||
$parsed_blocks = parse_blocks( | ||
'<!-- wp:post-template --><!-- wp:post-title /--><!-- wp:post-excerpt /--><!-- /wp:post-template -->' | ||
); | ||
$block = new WP_Block( $parsed_blocks[0] ); | ||
$markup = $block->render(); | ||
|
||
$post_id = self::$post->ID; | ||
$other_post_id = self::$other_post->ID; | ||
|
||
$expected = <<<END | ||
<ul class="wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow"> | ||
<li class="wp-block-post post-$other_post_id post type-post status-publish format-standard hentry category-uncategorized"> | ||
<h2 class="wp-block-post-title">Ceiling Cat</h2> | ||
<div class="wp-block-post-excerpt"> | ||
<p class="wp-block-post-excerpt__excerpt">Ceiling Cat </p> | ||
</div> | ||
</li> | ||
<li class="wp-block-post post-$post_id post type-post status-publish format-standard hentry category-uncategorized"> | ||
<h2 class="wp-block-post-title">Metal Dog</h2> | ||
<div class="wp-block-post-excerpt"> | ||
<p class="wp-block-post-excerpt__excerpt">Metal Dog </p> | ||
</div> | ||
</li> | ||
</ul> | ||
END; | ||
$this->assertSame( | ||
str_replace( array( "\n", "\t" ), '', $expected ), | ||
str_replace( array( "\n", "\t" ), '', $markup ) | ||
); | ||
} | ||
} |
31ddc1f
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.
Flaky tests detected in 31ddc1f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5121935385
📝 Reported issues:
/test/e2e/specs/site-editor/style-book.spec.js