Changeset 34561 for trunk/src/wp-includes/comment-template.php
- Timestamp:
- 09/25/2015 08:39:18 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment-template.php
r34525 r34561 684 684 $args = wp_parse_args( $args, $defaults ); 685 685 686 if ( '' === $args['per_page'] && get_option('page_comments'))686 if ( '' === $args['per_page'] ) 687 687 $args['per_page'] = get_option('comments_per_page'); 688 688 … … 1215 1215 1216 1216 $comment_args = array( 1217 'order' => 'ASC',1218 1217 'orderby' => 'comment_date_gmt', 1219 1218 'status' => 'approve', 1220 1219 'post_id' => $post->ID, 1220 'hierarchical' => 'threaded', 1221 'no_found_rows' => false, 1221 1222 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance. 1222 1223 ); … … 1228 1229 } 1229 1230 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 $flip_comment_order = $trim_comments_on_page = false; 1237 if ( $post->comment_count > $per_page ) { 1238 $comment_args['number'] = $per_page; 1239 1240 /* 1241 * For legacy reasons, higher page numbers always mean more recent comments, regardless of sort order. 1242 * Since we don't have full pagination info until after the query, we use some tricks to get the 1243 * right comments for the current page. 1244 * 1245 * Abandon all hope, ye who enter here! 1246 */ 1247 $page = (int) get_query_var( 'cpage' ); 1248 if ( 'newest' === get_option( 'default_comments_page' ) ) { 1249 if ( $page ) { 1250 $comment_args['order'] = 'ASC'; 1251 1252 /* 1253 * We don't have enough data (namely, the total number of comments) to calculate an 1254 * exact offset. We'll fetch too many comments, and trim them as needed 1255 * after the query. 1256 */ 1257 $offset = ( $page - 2 ) * $per_page; 1258 if ( 0 > $offset ) { 1259 // `WP_Comment_Query` doesn't support negative offsets. 1260 $comment_args['offset'] = 0; 1261 } else { 1262 $comment_args['offset'] = $offset; 1263 } 1264 1265 // Fetch double the number of comments we need. 1266 $comment_args['number'] += $per_page; 1267 $trim_comments_on_page = true; 1268 } else { 1269 $comment_args['order'] = 'DESC'; 1270 $comment_args['offset'] = 0; 1271 $flip_comment_order = true; 1272 } 1273 } else { 1274 $comment_args['order'] = 'ASC'; 1275 if ( $page ) { 1276 $comment_args['offset'] = ( $page - 1 ) * $per_page; 1277 } else { 1278 $comment_args['offset'] = 0; 1279 } 1280 } 1281 } 1282 1283 $comment_query = new WP_Comment_Query( $comment_args ); 1284 $_comments = $comment_query->comments; 1285 1286 // Delightful pagination quirk #1: first page of results sometimes needs reordering. 1287 if ( $flip_comment_order ) { 1288 $_comments = array_reverse( $_comments ); 1289 } 1290 1291 // Delightful pagination quirk #2: reverse chronological order requires page shifting. 1292 if ( $trim_comments_on_page ) { 1293 // Correct the value of max_num_pages, which is wrong because we manipulated the per_page 'number'. 1294 $comment_query->max_num_pages = ceil( $comment_query->found_comments / $per_page ); 1295 1296 // Identify the number of comments that should appear on page 1. 1297 $page_1_count = $comment_query->found_comments - ( ( $comment_query->max_num_pages - 1 ) * $per_page ); 1298 1299 // Use that value to shift the matched comments. 1300 if ( 1 === $page ) { 1301 $_comments = array_slice( $_comments, 0, $page_1_count ); 1302 } else { 1303 $_comments = array_slice( $_comments, $page_1_count, $per_page ); 1304 } 1305 } 1306 1307 // Trees must be flattened before they're passed to the walker. 1308 $comments_flat = array(); 1309 foreach ( $_comments as $_comment ) { 1310 $comments_flat = array_merge( $comments_flat, array( $_comment ), $_comment->get_children( 'flat' ) ); 1311 } 1231 1312 1232 1313 /** … … 1238 1319 * @param int $post_ID Post ID. 1239 1320 */ 1240 $wp_query->comments = apply_filters( 'comments_array', $comments , $post->ID );1321 $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID ); 1241 1322 $comments = &$wp_query->comments; 1242 1323 $wp_query->comment_count = count($wp_query->comments); 1324 $wp_query->max_num_comment_pages = $comment_query->max_num_pages; 1243 1325 1244 1326 if ( $separate_comments ) { … … 1250 1332 1251 1333 $overridden_cpage = false; 1252 if ( '' == get_query_var('cpage') && get_option('page_comments')) {1334 if ( '' == get_query_var('cpage') ) { 1253 1335 set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 ); 1254 1336 $overridden_cpage = true; … … 1826 1908 $_comments = $wp_query->comments; 1827 1909 } 1828 } 1829 1830 if ( '' === $r['per_page'] && get_option('page_comments') ) 1910 1911 // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother. 1912 if ( 1 < $wp_query->max_num_comment_pages ) { 1913 $r['page'] = 1; 1914 } 1915 } 1916 1917 if ( '' === $r['per_page'] ) 1831 1918 $r['per_page'] = get_query_var('comments_per_page'); 1832 1919 … … 1867 1954 1868 1955 $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r ); 1869 $wp_query->max_num_comment_pages = $walker->max_pages;1870 1956 1871 1957 $in_comment_loop = false;
Note: See TracChangeset
for help on using the changeset viewer.