In comments_template(), don't run hierarchical queries if comment threading is disabled.

When hierarchical=true, `WP_Comment_Query` will always fetch comments according
to the comment hierarchy, even if 'thread_comments' is disabled for the site.
This can cause problems when comment threading is disabled after threaded
comments have been recorded on the site; comments will no longer be returned in
a strictly chronological order.

We address the issue by refraining from querying hierarchically when comment
threading is disabled.

Props jmdodd.
Fixes #35378.

git-svn-id: https://develop.svn.wordpress.org/trunk@36226 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-01-08 22:16:11 +00:00
parent 5304745be0
commit 8dbc62d267
2 changed files with 59 additions and 11 deletions

View File

@@ -689,4 +689,43 @@ class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
$commenter['comment_author_email'] = 'foo@example.com';
return $commenter;
}
/**
* @ticket 35378
*/
public function test_hierarchy_should_be_ignored_when_threading_is_disabled() {
$now = time();
$p = self::factory()->post->create();
$comment_1 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_content' => '1',
'comment_approved' => '1',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 100 ),
) );
$comment_2 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_content' => '2',
'comment_approved' => '1',
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 300 ),
) );
$comment_3 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_content' => '3',
'comment_approved' => '1',
'comment_parent' => $comment_1,
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 200 ),
) );
update_option( 'comment_order', 'asc' );
update_option( 'thread_comments', 0 );
$this->go_to( get_permalink( $p ) );
$found = get_echo( 'comments_template' );
// Find the found comments in the markup.
preg_match_all( '|id="comment-([0-9]+)|', $found, $matches );
$found_cids = array_map( 'intval', $matches[1] );
$this->assertSame( array( $comment_2, $comment_3, $comment_1 ), $found_cids );
}
}