WordPress.org

Make WordPress Core

Changeset 34544


Ignore:
Timestamp:
09/25/2015 02:34:20 PM (4 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r34542 r34544  
    9797
    9898    /**
     99     * The amount of found comments for the current query.
     100     *
     101     * @since 4.4.0
     102     * @access public
     103     * @var int
     104     */
     105    public $found_comments = 0;
     106
     107    /**
     108     * The number of pages.
     109     *
     110     * @since 4.4.0
     111     * @access public
     112     * @var int
     113     */
     114    public $max_num_pages = 0;
     115
     116    /**
    99117     * Make private/protected methods readable for backwards compatibility.
    100118     *
     
    120138     * @since 4.2.0
    121139     * @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
    122      * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache` was added.
     140     * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache` and `$no_found_rows` were added.
    123141     * @access public
    124142     *
     
    149167     *     @type int          $offset              Number of comments to offset the query. Used to build LIMIT clause.
    150168     *                                             Default 0.
     169     *     @type bool         $no_found_rows       Whether to disable the `SQL_CALC_FOUND_ROWS` query.
     170     *                                             Default: true.
    151171     *     @type string|array $orderby             Comment status or array of statuses. To use 'meta_value' or
    152172     *                                             'meta_value_num', `$meta_key` must also be defined. To sort by
     
    204224            'number' => '',
    205225            'offset' => '',
     226            'no_found_rows' => true,
    206227            'orderby' => '',
    207228            'order' => 'DESC',
     
    331352        $comment_ids = array_map( 'intval', $comment_ids );
    332353
     354        $this->comment_count = count( $this->comments );
     355
     356        if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
     357            /**
     358             * Filter the query used to retrieve found comment count.
     359             *
     360             * @since 4.4.0
     361             *
     362             * @param string           $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
     363             * @param WP_Comment_Query $comment_query        The `WP_Comment_Query` instance.
     364             */
     365            $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
     366            $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
     367
     368            $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
     369        }
     370
    333371        if ( 'ids' == $this->query_vars['fields'] ) {
    334372            $this->comments = $comment_ids;
     
    739777        }
    740778
    741         $this->sql_clauses['select']  = "SELECT $fields";
     779        $found_rows = '';
     780        if ( ! $this->query_vars['no_found_rows'] ) {
     781            $found_rows = 'SQL_CALC_FOUND_ROWS';
     782        }
     783
     784        $this->sql_clauses['select']  = "SELECT $found_rows $fields";
    742785        $this->sql_clauses['from']    = "FROM $wpdb->comments $join";
    743786        $this->sql_clauses['groupby'] = $groupby;
  • trunk/tests/phpunit/tests/comment/query.php

    r34310 r34544  
    18741874
    18751875    }
     1876
     1877    /**
     1878     * @ticket 8071
     1879     */
     1880    public function test_no_found_rows_should_default_to_true() {
     1881        $comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
     1882
     1883        $q = new WP_Comment_Query( array(
     1884            'post_id' => $this->post_id,
     1885            'number' => 2,
     1886        ) );
     1887
     1888        $this->assertEquals( 0, $q->found_comments );
     1889        $this->assertEquals( 0, $q->max_num_pages );
     1890    }
     1891
     1892    /**
     1893     * @ticket 8071
     1894     */
     1895    public function test_should_respect_no_found_rows_true() {
     1896        $comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
     1897
     1898        $q = new WP_Comment_Query( array(
     1899            'post_id' => $this->post_id,
     1900            'number' => 2,
     1901            'no_found_rows' => true,
     1902        ) );
     1903
     1904        $this->assertEquals( 0, $q->found_comments );
     1905        $this->assertEquals( 0, $q->max_num_pages );
     1906    }
     1907
     1908    /**
     1909     * @ticket 8071
     1910     */
     1911    public function test_should_respect_no_found_rows_false() {
     1912        $comments = $this->factory->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
     1913
     1914        $q = new WP_Comment_Query( array(
     1915            'post_id' => $this->post_id,
     1916            'number' => 2,
     1917            'no_found_rows' => false,
     1918        ) );
     1919
     1920        $this->assertEquals( 3, $q->found_comments );
     1921        $this->assertEquals( 2, $q->max_num_pages );
     1922    }
    18761923}
Note: See TracChangeset for help on using the changeset viewer.