Skip to content
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

Post Template Block: Set block context via filter #50313

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,18 @@ function render_block_core_post_template( $attributes, $content, $block ) {
// This ensures that for the inner instances of the Post Template block, we do not render any block supports.
$block_instance['blockName'] = 'core/null';

$post_id = get_the_ID();
$post_type = get_post_type();
$filter_block_context = static function( $context ) use ( $post_id, $post_type ) {
$context['postType'] = $post_type;
$context['postId'] = $post_id;
return $context;
};
add_filter( 'render_block_context', $filter_block_context );
// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
// `render_callback` and ensure that no wrapper markup is included.
$block_content = (
new WP_Block(
$block_instance,
array(
'postType' => get_post_type(),
'postId' => get_the_ID(),
)
)
)->render( array( 'dynamic' => false ) );
$block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) );
remove_filter( 'render_block_context', $filter_block_context );

// Wrap the render inner blocks in a `li` element with the appropriate post classes.
$post_classes = implode( ' ', get_post_class( 'wp-block-post' ) );
Expand Down
73 changes: 73 additions & 0 deletions phpunit/blocks/render-post-template-test.php
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 )
);
}
}