WordPress.org

Make WordPress Core

Ticket #8071: 8071.7.diff

File 8071.7.diff, 8.5 KB (added by boonebgorges, 4 years ago)
  • src/wp-includes/canonical.php

    diff --git src/wp-includes/canonical.php src/wp-includes/canonical.php
    index a88b3a8..52850b4 100644
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    321321                                }
    322322                        }
    323323
    324                         if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) {
     324                        if ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) {
    325325                                $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
    326326                                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
    327327                        }
  • src/wp-includes/class-wp-comment-query.php

    diff --git src/wp-includes/class-wp-comment-query.php src/wp-includes/class-wp-comment-query.php
    index 2e978c4..4050e48 100644
    class WP_Comment_Query { 
    865865                $level = 0;
    866866                do {
    867867                        $parent_ids = $levels[ $level ];
     868                        if ( ! $parent_ids ) {
     869                                break;
     870                        }
     871
    868872                        $where = 'WHERE ' . implode( ' AND ', $where_clauses ) . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
    869873                        $comment_ids = $wpdb->get_col( "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']}" );
    870874
  • src/wp-includes/class-wp-comment.php

    diff --git src/wp-includes/class-wp-comment.php src/wp-includes/class-wp-comment.php
    index 955925b..7225539 100644
    final class WP_Comment { 
    227227         * @since 4.4.0
    228228         * @access public
    229229         *
     230         * @param string $format Return value format. 'tree' for a hierarchical tree, 'flat' for a flattened array.
     231         *                       Default 'tree'.
    230232         * @return array Array of `WP_Comment` objects.
    231233         */
    232         public function get_children() {
     234        public function get_children( $format = 'tree' ) {
    233235                if ( is_null( $this->children ) ) {
    234236                        $this->children = get_comments( array(
    235237                                'parent' => $this->comment_ID,
    final class WP_Comment { 
    237239                        ) );
    238240                }
    239241
    240                 return $this->children;
     242                if ( 'flat' === $format ) {
     243                        $children = array();
     244                        foreach ( $this->children as $child ) {
     245                                $children = array_merge( $children, array( $child ), $child->get_children( 'flat' ) );
     246                        }
     247                } else {
     248                        $children = $this->children;
     249                }
     250
     251                return $children;
    241252        }
    242253
    243254        /**
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index d8c16a9..8495cf3 100644
    function comments_template( $file = '/comments.php', $separate_comments = false 
    12141214        $comment_author_url = esc_url($commenter['comment_author_url']);
    12151215
    12161216        $comment_args = array(
    1217                 'order'   => 'ASC',
    12181217                'orderby' => 'comment_date_gmt',
    12191218                'status'  => 'approve',
    12201219                'post_id' => $post->ID,
     1220                'hierarchical' => 'threaded',
     1221                'no_found_rows' => false,
    12211222                'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
    12221223        );
    12231224
    function comments_template( $file = '/comments.php', $separate_comments = false 
    12271228                $comment_args['include_unapproved'] = array( $comment_author_email );
    12281229        }
    12291230
    1230         $comments = get_comments( $comment_args );
     1231        $per_page = (int) get_query_var( 'comments_per_page' );
     1232        if ( 0 === $per_page ) {
     1233                $per_page = (int) get_option( 'comments_per_page' );
     1234        }
     1235
     1236        $paging_threshold = $per_page ? $per_page * 2 : 100;
     1237
     1238        /**
     1239         * Filter the threshold at which comment pagination is forced.
     1240         *
     1241         * Pages with large numbers of comments perform extremely poorly, so we force pagination when the comment
     1242         * count exceeds a certain threshold. Use this filter to change that threshold.
     1243         *
     1244         * @since 4.4.0
     1245         *
     1246         * @param int     $paging_threshold Number of comments a post must have before pagination is forced.
     1247         * @param WP_Post $post             The WP_Post instance.
     1248         */
     1249        $paging_threshold = apply_filters( 'force_comment_paging_threshold', $paging_threshold, $post );
     1250
     1251        $flip_comment_order = $trim_comments_on_page = false;
     1252        if ( (int) get_option( 'page_comments' ) || $post->comment_count > $paging_threshold ) {
     1253                // Force pagination to be turned on in theme comment navigation functions.
     1254                add_filter( 'pre_option_page_comments', '__return_true' );
     1255
     1256                if ( ! $per_page ) {
     1257                        $comment_args['number'] = $paging_threshold / 2;
     1258                }
     1259                $comment_args['number'] = $per_page;
     1260
     1261                /*
     1262                 * For legacy reasons, higher page numbers always mean more recent comments, regardless of sort order.
     1263                 * Since we don't have full pagination info until after the query, we use some tricks to get the
     1264                 * right comments for the current page.
     1265                 *
     1266                 * Abandon all hope, ye who enter here!
     1267                 */
     1268                $page = (int) get_query_var( 'cpage' );
     1269                if ( 'newest' === get_option( 'default_comments_page' ) ) {
     1270                        if ( $page ) {
     1271                                $comment_args['order'] = 'ASC';
     1272
     1273                                /*
     1274                                 * We don't have enough data (namely, the total number of comments) to calculate an
     1275                                 * exact offset. We'll fetch too many comments, and trim them as needed
     1276                                 * after the query.
     1277                                 */
     1278                                $offset = ( $page - 2 ) * $per_page;
     1279                                if ( 0 > $offset ) {
     1280                                        // `WP_Comment_Query` doesn't support negative offsets.
     1281                                        $comment_args['offset'] = 0;
     1282                                } else {
     1283                                        $comment_args['offset'] = $offset;
     1284                                }
     1285
     1286                                // Fetch double the number of comments we need.
     1287                                $comment_args['number'] += $per_page;
     1288                                $trim_comments_on_page = true;
     1289                        } else {
     1290                                $comment_args['order'] = 'DESC';
     1291                                $comment_args['offset'] = 0;
     1292                                $flip_comment_order = true;
     1293                        }
     1294                } else {
     1295                        $comment_args['order'] = 'ASC';
     1296                        if ( $page ) {
     1297                                $comment_args['offset'] = ( $page - 1 ) * $per_page;
     1298                        } else {
     1299                                $comment_args['offset'] = 0;
     1300                        }
     1301                }
     1302        }
     1303
     1304        $comment_query = new WP_Comment_Query( $comment_args );
     1305        $_comments = $comment_query->comments;
     1306
     1307        // Delightful pagination quirk #1: first page of results sometimes needs reordering.
     1308        if ( $flip_comment_order ) {
     1309                $_comments = array_reverse( $_comments );
     1310        }
     1311
     1312        // Delightful pagination quirk #2: reverse chronological order requires page shifting.
     1313        if ( $trim_comments_on_page ) {
     1314                // Correct the value of max_num_pages, which is wrong because we manipulated the per_page 'number'.
     1315                $comment_query->max_num_pages = ceil( $comment_query->found_comments / $per_page );
     1316
     1317                // Identify the number of comments that should appear on page 1.
     1318                $page_1_count = $comment_query->found_comments - ( ( $comment_query->max_num_pages - 1 ) * $per_page );
     1319
     1320                // Use that value to shift the matched comments.
     1321                if ( 1 === $page ) {
     1322                        $_comments = array_slice( $_comments, 0, $page_1_count );
     1323                } else {
     1324                        $_comments = array_slice( $_comments, $page_1_count, $per_page );
     1325                }
     1326        }
     1327
     1328        // Trees must be flattened before they're passed to the walker.
     1329        $comments_flat = array();
     1330        foreach ( $_comments as $_comment ) {
     1331                $comments_flat = array_merge( $comments_flat, array( $_comment ), $_comment->get_children( 'flat' ) );
     1332        }
    12311333
    12321334        /**
    12331335         * Filter the comments array.
    function comments_template( $file = '/comments.php', $separate_comments = false 
    12371339         * @param array $comments Array of comments supplied to the comments template.
    12381340         * @param int   $post_ID  Post ID.
    12391341         */
    1240         $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
     1342        $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
    12411343        $comments = &$wp_query->comments;
    12421344        $wp_query->comment_count = count($wp_query->comments);
     1345        $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
    12431346
    12441347        if ( $separate_comments ) {
    12451348                $wp_query->comments_by_type = separate_comments($comments);
    function wp_list_comments( $args = array(), $comments = null ) { 
    18251928                } else {
    18261929                        $_comments = $wp_query->comments;
    18271930                }
     1931
     1932                // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
     1933                if ( 1 < $wp_query->max_num_comment_pages ) {
     1934                        $r['page'] = 1;
     1935                }
    18281936        }
    18291937
    18301938        if ( '' === $r['per_page'] && get_option('page_comments') )
    function wp_list_comments( $args = array(), $comments = null ) { 
    18661974        }
    18671975
    18681976        $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r );
    1869         $wp_query->max_num_comment_pages = $walker->max_pages;
    18701977
    18711978        $in_comment_loop = false;
    18721979