Introduce paged
argument to WP_Comment_Query
.
Using `paged` with `number` allows developers to request paginated comment results without having to do a manual offset calculation. Props AdamWills. Fixes #38268. git-svn-id: https://develop.svn.wordpress.org/trunk@41287 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
7157569a72
commit
04b5965866
@ -140,6 +140,7 @@ class WP_Comment_Query {
|
||||
* `$hierarchical`, and `$update_comment_post_cache` were added.
|
||||
* @since 4.5.0 Introduced the `$author_url` argument.
|
||||
* @since 4.6.0 Introduced the `$cache_domain` argument.
|
||||
* @since 4.9.0 Introduced the `$paged` argument.
|
||||
*
|
||||
* @param string|array $query {
|
||||
* Optional. Array or query string of comment query parameters. Default empty.
|
||||
@ -170,6 +171,8 @@ class WP_Comment_Query {
|
||||
* See WP_Meta_Query. Default empty.
|
||||
* @type int $number Maximum number of comments to retrieve.
|
||||
* Default empty (no limit).
|
||||
* @type int $paged When used with $number, defines the page of results to return.
|
||||
* When used with $offset, $offset takes precedence. Default 1.
|
||||
* @type int $offset Number of comments to offset the query. Used to build
|
||||
* LIMIT clause. Default 0.
|
||||
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query.
|
||||
@ -263,6 +266,7 @@ class WP_Comment_Query {
|
||||
'no_found_rows' => true,
|
||||
'orderby' => '',
|
||||
'order' => 'DESC',
|
||||
'paged' => 1,
|
||||
'parent' => '',
|
||||
'parent__in' => '',
|
||||
'parent__not_in' => '',
|
||||
@ -628,12 +632,13 @@ class WP_Comment_Query {
|
||||
|
||||
$number = absint( $this->query_vars['number'] );
|
||||
$offset = absint( $this->query_vars['offset'] );
|
||||
$paged = absint( $this->query_vars['paged'] );
|
||||
|
||||
if ( ! empty( $number ) ) {
|
||||
if ( $offset ) {
|
||||
$limits = 'LIMIT ' . $offset . ',' . $number;
|
||||
} else {
|
||||
$limits = 'LIMIT ' . $number;
|
||||
$limits = 'LIMIT ' . ( $number * ( $paged - 1 ) ) . ',' . $number;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1561,6 +1561,80 @@ class Tests_Comment_Query extends WP_UnitTestCase {
|
||||
$this->assertEquals( 2, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 38268
|
||||
*/
|
||||
public function test_paged() {
|
||||
$now = time();
|
||||
|
||||
$c1 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 50 ),
|
||||
) );
|
||||
$c2 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ),
|
||||
) );
|
||||
$c3 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ),
|
||||
) );
|
||||
$c4 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ),
|
||||
) );
|
||||
|
||||
$query = new WP_Comment_Query();
|
||||
$found = $query->query( array(
|
||||
'paged' => 2,
|
||||
'number' => 2,
|
||||
'orderby' => 'comment_date_gmt',
|
||||
'order' => 'DESC',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$expected = array( $c2, $c1 );
|
||||
$this->assertSame( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 38268
|
||||
*/
|
||||
public function test_offset_should_take_precedence_over_paged() {
|
||||
$now = time();
|
||||
|
||||
$c1 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 50 ),
|
||||
) );
|
||||
$c2 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 40 ),
|
||||
) );
|
||||
$c3 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ),
|
||||
) );
|
||||
$c4 = self::factory()->comment->create( array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ),
|
||||
) );
|
||||
|
||||
$query = new WP_Comment_Query();
|
||||
$found = $query->query( array(
|
||||
'paged' => 2,
|
||||
'offset' => 1,
|
||||
'number' => 2,
|
||||
'orderby' => 'comment_date_gmt',
|
||||
'order' => 'DESC',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$expected = array( $c3, $c2 );
|
||||
|
||||
$this->assertSame( $expected, $found );
|
||||
}
|
||||
|
||||
public function test_post_type_single_value() {
|
||||
register_post_type( 'post-type-1' );
|
||||
register_post_type( 'post-type-2' );
|
||||
|
Loading…
Reference in New Issue
Block a user