WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/25/2015 02:34:20 PM (6 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.

File:
1 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;
Note: See TracChangeset for help on using the changeset viewer.