Skip to content

Commit

Permalink
Allow dynamic blocks to create loops (#9182)
Browse files Browse the repository at this point in the history
Stores and restores the global post object around dynamic block callbacks. This allows dynamic blocks to create new `WP_Query` instances and set up its post data, without changing the currently edited post.

Fixes #7427, fixes #8035.
  • Loading branch information
obenland authored Aug 21, 2018
1 parent 1d92884 commit ba5dc99
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,23 @@ function gutenberg_render_block( $block ) {
* Parses dynamic blocks out of `post_content` and re-renders them.
*
* @since 0.1.0
* @global WP_Post $post The post to edit.
*
* @param string $content Post content.
* @return string Updated post content.
*/
function do_blocks( $content ) {
global $post;

$rendered_content = '';
$dynamic_block_pattern = get_dynamic_blocks_regex();

/*
* Back up global post, to restore after render callback.
* Allows callbacks to run new WP_Query instances without breaking the global post.
*/
$global_post = $post;

while ( preg_match( $dynamic_block_pattern, $content, $block_match, PREG_OFFSET_CAPTURE ) ) {
$opening_tag = $block_match[0][0];
$offset = $block_match[0][1];
Expand Down Expand Up @@ -215,6 +224,9 @@ function do_blocks( $content ) {

// Replace dynamic block with server-rendered output.
$rendered_content .= $block_type->render( $attributes, $inner_content );

// Restore global $post.
$post = $global_post;
}

// Append remaining unmatched content.
Expand Down
55 changes: 55 additions & 0 deletions phpunit/class-dynamic-blocks-render-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ function render_dummy_block_numeric() {
return 10;
}

/**
* Dummy block rendering function, creating a new WP_Query instance.
*
* @return string Block output.
*/
function render_dummy_block_wp_query() {
$content = '';
$recent = new WP_Query( array(
'numberposts' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true,
) );

while ( $recent->have_posts() ) {
$recent->the_post();

$content .= get_the_title();
}

wp_reset_postdata();

return $content;
}

/**
* Tear down.
*/
Expand Down Expand Up @@ -87,6 +114,34 @@ function test_dynamic_block_rendering() {
);
}

/**
* Tests that do_blocks() maintains the global $post variable when dynamic
* blocks create new WP_Query instances in their callbacks.
*
* @covers ::do_blocks
*/
function test_global_post_persistence() {
global $post;

register_block_type(
'core/dummy',
array(
'render_callback' => array(
$this,
'render_dummy_block_wp_query',
),
)
);

$posts = self::factory()->post->create_many( 5 );
$post = get_post( end( $posts ) );

$global_post = $post;
do_blocks( '<!-- wp:core/dummy /-->' );

$this->assertEquals( $global_post, $post );
}

/**
* Test dynamic blocks return string value from render, even if render
* callback does not.
Expand Down

0 comments on commit ba5dc99

Please sign in to comment.