diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 3a3c207cf92ee..cc73242f70fb6 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -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' ) ); diff --git a/phpunit/blocks/render-post-template-test.php b/phpunit/blocks/render-post-template-test.php new file mode 100644 index 0000000000000..13b5623cdd5dd --- /dev/null +++ b/phpunit/blocks/render-post-template-test.php @@ -0,0 +1,73 @@ +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( + '' + ); + $block = new WP_Block( $parsed_blocks[0] ); + $markup = $block->render(); + + $post_id = self::$post->ID; + $other_post_id = self::$other_post->ID; + + $expected = << +
  • +

    Ceiling Cat

    +
    +

    Ceiling Cat

    +
    +
  • +
  • +

    Metal Dog

    +
    +

    Metal Dog

    +
    +
  • + +END; + $this->assertSame( + str_replace( array( "\n", "\t" ), '', $expected ), + str_replace( array( "\n", "\t" ), '', $markup ) + ); + } +}