Make WordPress Core

Ticket #8071: 8071.2.diff

File 8071.2.diff, 3.6 KB (added by wonderboymusic, 10 years ago)
  • src/wp-includes/comment-functions.php

     
    761761function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
    762762        global $wp_query;
    763763
     764        if ( null === $comments && !empty($wp_query->comment_pages_count) )
     765                return $wp_query->comment_pages_count;
     766
    764767        if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
    765768                return $wp_query->max_num_comment_pages;
    766769
  • src/wp-includes/comment-template.php

     
    11171117        }
    11181118}
    11191119
     1120function wp_comments_per_page() {
     1121        $per_page = (int) get_query_var( 'comments_per_page' );
     1122        if ( 0 === $per_page ) {
     1123                $per_page = (int) get_option( 'comments_per_page' );
     1124        }
     1125        return $per_page;
     1126}
     1127
     1128function wp_comment_pages_count( $total, $top_level ) {
     1129        if ( ! get_option( 'page_comments' ) ) {
     1130                return 1;
     1131        }
     1132
     1133        $per_page = wp_comments_per_page();
     1134        if ( 0 === $per_page ) {
     1135                return 1;
     1136        }
     1137
     1138        $threaded = get_option( 'thread_comments' );
     1139
     1140        if ( $threaded ) {
     1141                $count = ceil( $top_level / $per_page );
     1142        } else {
     1143                $count = ceil( $total / $per_page );
     1144        }
     1145
     1146        return $count;
     1147}
     1148
     1149function wp_threaded_comment_ids( $comment_ids, $comment_args ) {
     1150        $threaded_ids = array();
     1151        $args = $comment_args;
     1152        unset( $args['parent'] );
     1153
     1154        do {
     1155                $threaded_ids = array_merge( $threaded_ids, $comment_ids );
     1156                $args['parent__in'] = $comment_ids;
     1157                $comment_ids = get_comments( $args );
     1158        } while ( count( $comment_ids ) );
     1159
     1160        return $threaded_ids;
     1161}
     1162
    11201163/**
    11211164 * Load the comment template specified in $file.
    11221165 *
     
    11861229
    11871230        $comment_args = array(
    11881231                'order'   => 'ASC',
     1232                'fields'  => 'ids',
    11891233                'orderby' => 'comment_date_gmt',
    11901234                'status'  => 'approve',
    11911235                'post_id' => $post->ID,
     
    11971241                $comment_args['include_unapproved'] = array( $comment_author_email );
    11981242        }
    11991243
     1244        $top_level = 0;
     1245        $threaded = get_option( 'thread_comments' );
     1246        if ( $threaded ) {
     1247                $comment_args['parent'] = 0;
     1248                // array of all top-level comment IDs for the post
     1249                $comment_ids = get_comments( $comment_args );
     1250                $top_level = count( $comment_ids );
     1251        } else {
     1252                // array of all comment IDs for the post
     1253                $comment_ids = get_comments( $comment_args );
     1254        }
     1255
     1256        $pages = wp_comment_pages_count( $post->comment_count, $top_level );
     1257        $wp_query->comment_pages_count = $pages;
     1258        $wp_query->comment_pages_page = 1;
     1259
     1260        if ( $pages > 1 ) {
     1261                $per_page = wp_comments_per_page();
     1262                $page = (int) get_query_var( 'cpage' );
     1263                if ( 0 === $page ) {
     1264                        $page = 1;
     1265                }
     1266                $wp_query->comment_pages_page = $page;
     1267
     1268                $comment_ids = array_slice( $comment_ids, ( $page - 1 ) * $per_page, $per_page );
     1269                if ( $threaded ) {
     1270                        $comment_ids = wp_threaded_comment_ids( $comment_ids, $comment_args );
     1271                }
     1272
     1273                $comment_args['comment__in'] = $comment_ids;
     1274                $comment_args['orderby'] = 'comment__in';
     1275                unset( $comment_args['order'] );
     1276        }
     1277
     1278        unset( $comment_args['parent'], $comment_args['fields'] );
    12001279        $comments = get_comments( $comment_args );
    12011280
    12021281        /**
     
    18181897        if ( null === $r['reverse_top_level'] )
    18191898                $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
    18201899
     1900        if ( null === $comments && 1 < $wp_query->comment_pages_count ) {
     1901                $r['page'] = 1;
     1902        }
     1903
    18211904        if ( empty( $r['walker'] ) ) {
    18221905                $walker = new Walker_Comment;
    18231906        } else {