Ticket #8071: 8071.4.diff
File 8071.4.diff, 7.9 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-comment-query.php
71 71 public $comments; 72 72 73 73 /** 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 /** 74 103 * Make private/protected methods readable for backwards compatibility. 75 104 * 76 105 * @since 4.0.0 … … 197 226 'user_id' => '', 198 227 'search' => '', 199 228 'count' => false, 229 'no_found_rows' => false, 200 230 'meta_key' => '', 201 231 'meta_value' => '', 202 232 'meta_query' => '', … … 208 238 } 209 239 } 210 240 241 public function init() { 242 $this->comment_count = 0; 243 $this->found_comments = 0; 244 $this->max_num_pages = 0; 245 } 246 211 247 /** 212 248 * Parse arguments passed to the comment query with default query parameters. 213 249 * … … 222 258 $query = $this->query_vars; 223 259 } 224 260 261 $this->init(); 225 262 $this->query_vars = wp_parse_args( $query, $this->query_var_defaults ); 226 263 do_action_ref_array( 'parse_comment_query', array( &$this ) ); 227 264 } … … 281 318 $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 282 319 } 283 320 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 284 334 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. 285 335 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 286 336 $last_changed = wp_cache_get( 'last_changed', 'comment' ); … … 292 342 293 343 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { 294 344 $this->comments = $cache; 345 $this->comment_count = count( $this->comments ); 346 $this->set_found_comments( $limits ); 295 347 return $this->comments; 296 348 } 297 349 … … 452 504 $orderby = "$wpdb->comments.comment_date_gmt $order"; 453 505 } 454 506 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 468 507 if ( $this->query_vars['count'] ) { 469 508 $fields = 'COUNT(*)'; 470 509 } else { … … 671 710 $orderby = "ORDER BY $orderby"; 672 711 } 673 712 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 } 675 718 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 676 726 if ( $this->query_vars['count'] ) { 677 727 return $wpdb->get_var( $this->request ); 678 728 } … … 679 729 680 730 if ( 'ids' == $this->query_vars['fields'] ) { 681 731 $this->comments = $wpdb->get_col( $this->request ); 732 $this->comment_count = count( $this->comments ); 733 $this->set_found_comments( $limits ); 682 734 return array_map( 'intval', $this->comments ); 683 735 } 684 736 … … 702 754 } 703 755 704 756 $this->comments = $comments; 757 $this->comment_count = count( $this->comments ); 758 $this->set_found_comments( $limits ); 705 759 return $this->comments; 706 760 } 707 761 … … 814 868 return 'DESC'; 815 869 } 816 870 } 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 } 817 909 } -
src/wp-includes/comment-template.php
1208 1208 1209 1209 $comment_args = array( 1210 1210 'order' => 'ASC', 1211 'fields' => 'ids', 1211 1212 'orderby' => 'comment_date_gmt', 1212 1213 'status' => 'approve', 1213 1214 'post_id' => $post->ID, … … 1219 1220 $comment_args['include_unapproved'] = array( $comment_author_email ); 1220 1221 } 1221 1222 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 ); 1222 1287 $comments = get_comments( $comment_args ); 1223 1288 1224 1289 /** … … 1849 1914 if ( null === $r['reverse_top_level'] ) 1850 1915 $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') ); 1851 1916 1917 if ( null === $comments && 1 < $wp_query->max_num_comment_pages ) { 1918 $r['page'] = 1; 1919 } 1920 1852 1921 if ( empty( $r['walker'] ) ) { 1853 1922 $walker = new Walker_Comment; 1854 1923 } else {