Improve pagination internals in WP_Comment_Query.

`WP_Comment_Query` will now report the total number of comments matching the
query params (`comments_found`), as well as the total number of pages required
to display these comments (`max_num_pages`). Because `SQL_CALC_FOUND_ROWS`
queries can introduce a lot of overhead in some cases, we disable the feature
by default. Pass `no_found_rows=false` to `WP_Comment_Query` to enable the
count. (We use the negative parameter name 'no_found_rows' for parity with
`WP_Query`.)

Props wonderboymusic, boonebgorges.
See #8071.

git-svn-id: https://develop.svn.wordpress.org/trunk@34544 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-09-25 14:34:20 +00:00
parent 03515ca82d
commit 8e955c4805
2 changed files with 92 additions and 2 deletions

View File

@@ -1873,4 +1873,51 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEquals( array( $c2, $c3 ), $ids->comments );
}
/**
* @ticket 8071
*/
public function test_no_found_rows_should_default_to_true() {
$comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
$q = new WP_Comment_Query( array(
'post_id' => $this->post_id,
'number' => 2,
) );
$this->assertEquals( 0, $q->found_comments );
$this->assertEquals( 0, $q->max_num_pages );
}
/**
* @ticket 8071
*/
public function test_should_respect_no_found_rows_true() {
$comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
$q = new WP_Comment_Query( array(
'post_id' => $this->post_id,
'number' => 2,
'no_found_rows' => true,
) );
$this->assertEquals( 0, $q->found_comments );
$this->assertEquals( 0, $q->max_num_pages );
}
/**
* @ticket 8071
*/
public function test_should_respect_no_found_rows_false() {
$comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
$q = new WP_Comment_Query( array(
'post_id' => $this->post_id,
'number' => 2,
'no_found_rows' => false,
) );
$this->assertEquals( 3, $q->found_comments );
$this->assertEquals( 2, $q->max_num_pages );
}
}