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

Fix query loop bugs by correctly relying on the main query and removing problematic workaround #49904

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Add test coverage.
felixarntz committed Aug 28, 2023
commit 67f582a35f9208fef3fae8bd8e58b13ff64628b1
86 changes: 86 additions & 0 deletions phpunit/blocks/render-post-template-test.php
Original file line number Diff line number Diff line change
@@ -70,4 +70,90 @@ public function test_rendering_post_template() {
str_replace( array( "\n", "\t" ), '', $markup )
);
}

/**
* Tests that the `core/post-template` block triggers the main query loop when rendering within a corresponding
* `core/query` block.
*/
public function test_rendering_post_template_with_main_query_loop() {
global $wp_query, $wp_the_query;

// Query block with post template block.
$content = '<!-- wp:query {"query":{"inherit":true}} -->';
$content .= '<!-- wp:post-template {"align":"wide"} -->';
$content .= '<!-- wp:post-title /--><!-- wp:test/in-the-loop-logger /-->';
$content .= '<!-- /wp:post-template -->';
$content .= '<!-- /wp:query -->';

$expected = '<ul class="alignwide wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow wp-block-query-is-layout-flow">';
$expected .= '<li class="wp-block-post post-' . self::$post->ID . ' post type-post status-publish format-standard hentry category-uncategorized">';
$expected .= '<h2 class="wp-block-post-title">' . self::$post->post_title . '</h2>';
$expected .= '</li>';
$expected .= '</ul>';

// Set main query to single post.
$wp_query = new WP_Query( array( 'p' => self::$post->ID ) );
$wp_the_query = $wp_query;

// Register test block to log `in_the_loop()` results.
$in_the_loop_logs = array();
register_block_type(
'test/in-the-loop-logger',
array(
'render_callback' => static function() use ( &$in_the_loop_logs ) {
$in_the_loop_logs[] = in_the_loop();
return '';
},
)
);

$output = do_blocks( $content );
$this->assertSame( $expected, $output, 'Unexpected parsed blocks content' );
$this->assertSame( array( true ), $in_the_loop_logs, 'Unexpected in_the_loop() result' );
}

/**
* Tests that the `core/post-template` block does not tamper with the main query loop when rendering within a post
* as the main query loop has already been started. In this case, the main query object needs to be cloned to
* prevent an infinite loop.
*/
public function test_rendering_post_template_with_main_query_loop_already_started() {
global $wp_query, $wp_the_query;

// Query block with post template block.
$content = '<!-- wp:query {"query":{"inherit":true}} -->';
$content .= '<!-- wp:post-template {"align":"wide"} -->';
$content .= '<!-- wp:post-title /-->';
$content .= '<!-- /wp:post-template -->';
$content .= '<!-- /wp:query -->';

$expected = '<ul class="alignwide wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow wp-block-query-is-layout-flow">';
$expected .= '<li class="wp-block-post post-' . self::$post->ID . ' post type-post status-publish format-standard hentry category-uncategorized">';
$expected .= '<h2 class="wp-block-post-title">' . self::$post->post_title . '</h2>';
$expected .= '</li>';
$expected .= '</ul>';

// Update the post's content to have a query block for the same query as the main query.
wp_update_post(
array(
'ID' => self::$post->ID,
'post_content' => $content,
'post_content_filtered' => $content,
)
);

// Set main query to single post.
$wp_query = new WP_Query( array( 'p' => self::$post->ID ) );
$wp_the_query = $wp_query;

// Get post content within main query loop.
$output = '';
while ( $wp_query->have_posts() ) {
$wp_query->the_post();

$output = get_echo( 'the_content' );
}

$this->assertSame( $expected, $output, 'Unexpected parsed post content' );
}
}