Make WordPress Core

Ticket #8071: 8071.4.diff

File 8071.4.diff, 7.9 KB (added by wonderboymusic, 9 years ago)
  • src/wp-includes/class-wp-comment-query.php

     
    7171        public $comments;
    7272
    7373        /**
     74         * The amount of comments for the current query.
     75         *
     76         * @since 4.4.0
     77         * @access public
     78         * @var int
     79         */
     80        public $comment_count = 0;
     81
     82        /**
     83         * The amount of found comments for the current query.
     84         *
     85         * If limit clause was not used, equals $comment_count.
     86         *
     87         * @since 4.4.0
     88         * @access public
     89         * @var int
     90         */
     91        public $found_comments = 0;
     92
     93        /**
     94         * The amount of pages.
     95         *
     96         * @since 4.4.0
     97         * @access public
     98         * @var int
     99         */
     100        public $max_num_pages = 0;
     101
     102        /**
    74103         * Make private/protected methods readable for backwards compatibility.
    75104         *
    76105         * @since 4.0.0
     
    197226                        'user_id' => '',
    198227                        'search' => '',
    199228                        'count' => false,
     229                        'no_found_rows' => false,
    200230                        'meta_key' => '',
    201231                        'meta_value' => '',
    202232                        'meta_query' => '',
     
    208238                }
    209239        }
    210240
     241        public function init() {
     242                $this->comment_count = 0;
     243                $this->found_comments = 0;
     244                $this->max_num_pages = 0;
     245        }
     246
    211247        /**
    212248         * Parse arguments passed to the comment query with default query parameters.
    213249         *
     
    222258                        $query = $this->query_vars;
    223259                }
    224260
     261                $this->init();
    225262                $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
    226263                do_action_ref_array( 'parse_comment_query', array( &$this ) );
    227264        }
     
    281318                        $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
    282319                }
    283320
     321                $number = absint( $this->query_vars['number'] );
     322                $offset = absint( $this->query_vars['offset'] );
     323
     324                if ( ! empty( $number ) ) {
     325                        if ( $offset ) {
     326                                $limits = 'LIMIT ' . $offset . ',' . $number;
     327                        } else {
     328                                $limits = 'LIMIT ' . $number;
     329                        }
     330                } else {
     331                        $limits = '';
     332                }
     333
    284334                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    285335                $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
    286336                $last_changed = wp_cache_get( 'last_changed', 'comment' );
     
    292342
    293343                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
    294344                        $this->comments = $cache;
     345                        $this->comment_count = count( $this->comments );
     346                        $this->set_found_comments( $limits );
    295347                        return $this->comments;
    296348                }
    297349
     
    452504                        $orderby = "$wpdb->comments.comment_date_gmt $order";
    453505                }
    454506
    455                 $number = absint( $this->query_vars['number'] );
    456                 $offset = absint( $this->query_vars['offset'] );
    457 
    458                 if ( ! empty( $number ) ) {
    459                         if ( $offset ) {
    460                                 $limits = 'LIMIT ' . $offset . ',' . $number;
    461                         } else {
    462                                 $limits = 'LIMIT ' . $number;
    463                         }
    464                 } else {
    465                         $limits = '';
    466                 }
    467 
    468507                if ( $this->query_vars['count'] ) {
    469508                        $fields = 'COUNT(*)';
    470509                } else {
     
    671710                        $orderby = "ORDER BY $orderby";
    672711                }
    673712
    674                 $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
     713                if ( isset( $this->query_vars['no_found_rows'] ) ) {
     714                        $this->query_vars['no_found_rows'] = (bool) $this->query_vars['no_found_rows'];
     715                } else {
     716                        $this->query_vars['no_found_rows'] = false;
     717                }
    675718
     719                $found_rows = '';
     720                if ( ! $this->query_vars['no_found_rows'] && ! empty( $limits ) ) {
     721                        $found_rows = 'SQL_CALC_FOUND_ROWS';
     722                }
     723
     724                $this->request = "SELECT $found_rows $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
     725
    676726                if ( $this->query_vars['count'] ) {
    677727                        return $wpdb->get_var( $this->request );
    678728                }
     
    679729
    680730                if ( 'ids' == $this->query_vars['fields'] ) {
    681731                        $this->comments = $wpdb->get_col( $this->request );
     732                        $this->comment_count = count( $this->comments );
     733                        $this->set_found_comments( $limits );
    682734                        return array_map( 'intval', $this->comments );
    683735                }
    684736
     
    702754                }
    703755
    704756                $this->comments = $comments;
     757                $this->comment_count = count( $this->comments );
     758                $this->set_found_comments( $limits );
    705759                return $this->comments;
    706760        }
    707761
     
    814868                        return 'DESC';
    815869                }
    816870        }
     871
     872        protected function set_found_comments( $limits ) {
     873                global $wpdb;
     874
     875                // Bail if comments is an empty array.
     876                if ( $this->query_vars['no_found_rows'] || empty( $this->comments ) ) {
     877                        return;
     878                }
     879
     880                if ( ! empty( $limits ) ) {
     881                        /**
     882                         * Filter the query to run for retrieving the found comments.
     883                         *
     884                         * @since 4.4.0
     885                         *
     886                         * @param string           $found_comments The query to run to find the found comments.
     887                         * @param WP_Comment_Query $this           The WP_Comment_Query instance.
     888                         */
     889                        $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
     890                        $this->found_comments = $wpdb->get_var( $found_comments_query );
     891                } else {
     892                        $this->found_comments = count( $this->comments );
     893                }
     894
     895                /**
     896                 * Filter the number of found comments for the query.
     897                 *
     898                 * @since 4.4.0
     899                 *
     900                 * @param int              $found_comments The number of comments found.
     901                 * @param WP_Comment_Query $this           The WP_Comment_Query instance.
     902                 */
     903                $this->found_comments = apply_filters( 'found_comments', $this->found_comments, $this );
     904
     905                if ( ! empty( $limits ) ) {
     906                        $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
     907                }
     908        }
    817909}
  • src/wp-includes/comment-template.php

     
    12081208
    12091209        $comment_args = array(
    12101210                'order'   => 'ASC',
     1211                'fields'  => 'ids',
    12111212                'orderby' => 'comment_date_gmt',
    12121213                'status'  => 'approve',
    12131214                'post_id' => $post->ID,
     
    12191220                $comment_args['include_unapproved'] = array( $comment_author_email );
    12201221        }
    12211222
     1223        $per_page = (int) get_query_var( 'comments_per_page' );
     1224        if ( 0 === $per_page ) {
     1225                $per_page = (int) get_option( 'comments_per_page' );
     1226        }
     1227
     1228        $threshold = $per_page ? $per_page * 2 : 100;
     1229        /**
     1230         * Filter the threshold at which to force paging
     1231         *
     1232         * @since 4.4.0
     1233         *
     1234         * @param int     $threshold Number of comments on a post before forcing paging.
     1235         * @param WP_Post $post      The WP_Post instance.
     1236         */
     1237        $paging_threshold = apply_filters( 'force_comment_paging_threshold', $threshold, $post );
     1238
     1239        if ( (int) get_option( 'page_comments' ) || $post->comment_count > $paging_threshold ) {
     1240                add_filter( 'pre_option_page_comments', '__return_true' );
     1241
     1242                if ( $per_page ) {
     1243                        $comment_args['number'] = $per_page;
     1244                } else {
     1245                        $comment_args['number'] = $paging_threshold / 2;
     1246                }
     1247                $page = (int) get_query_var( 'cpage' );
     1248                if ( $page ) {
     1249                        $comment_args['offset'] = ( $page - 1 ) * $per_page;
     1250                }
     1251        }
     1252
     1253        $threaded = get_option( 'thread_comments' );
     1254        if ( $threaded ) {
     1255                $comment_args['parent'] = 0;
     1256        }
     1257       
     1258        $comment_query = new WP_Comment_Query( $comment_args );
     1259        $comment_ids = $comment_query->comments;
     1260        $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
     1261
     1262        unset(
     1263                $comment_args['number'],
     1264                $comment_args['offset'],
     1265                $comment_args['parent']
     1266        );
     1267
     1268        if ( $comment_query->max_num_pages > 1 && $threaded ) {
     1269                $threaded_ids = array();
     1270                $args = $comment_args;
     1271
     1272                do {
     1273                        $threaded_ids = array_merge( $threaded_ids, $comment_ids );
     1274                        $args['parent__in'] = $comment_ids;
     1275                        $comment_ids = get_comments( $args );
     1276                } while ( count( $comment_ids ) );
     1277
     1278                $comment_ids = $threaded_ids;
     1279        }
     1280
     1281        $comment_args['comment__in'] = $comment_ids;
     1282        $comment_args['orderby'] = 'comment__in';
     1283        unset(
     1284                $comment_args['order'],
     1285                $comment_args['fields']
     1286        );
    12221287        $comments = get_comments( $comment_args );
    12231288
    12241289        /**
     
    18491914        if ( null === $r['reverse_top_level'] )
    18501915                $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
    18511916
     1917        if ( null === $comments && 1 < $wp_query->max_num_comment_pages ) {
     1918                $r['page'] = 1;
     1919        }
     1920
    18521921        if ( empty( $r['walker'] ) ) {
    18531922                $walker = new Walker_Comment;
    18541923        } else {