Changeset 33822
- Timestamp:
- 08/31/2015 06:50:12 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment-functions.php
r33811 r33822 364 364 365 365 $comment_count = array( 366 "approved" => 0, 367 "awaiting_moderation" => 0, 368 "spam" => 0, 369 "total_comments" => 0 366 'approved' => 0, 367 'awaiting_moderation' => 0, 368 'spam' => 0, 369 'trash' => 0, 370 'post-trashed' => 0, 371 'total_comments' => 0, 370 372 ); 371 373 372 374 foreach ( $totals as $row ) { 373 375 switch ( $row['comment_approved'] ) { 376 case 'trash': 377 $comment_count['trash'] = $row['total']; 378 break; 379 case 'post-trashed': 380 $comment_count['post-trashed'] = $row['total']; 381 break; 374 382 case 'spam': 375 383 $comment_count['spam'] = $row['total']; 376 $comment_count[ "total_comments"] += $row['total'];384 $comment_count['total_comments'] += $row['total']; 377 385 break; 378 386 case '1': … … 922 930 * @since 2.5.0 923 931 * 924 * @global wpdb $wpdb925 *926 932 * @param int $post_id Optional. Post ID. 927 933 * @return object|array Comment stats. 928 934 */ 929 935 function wp_count_comments( $post_id = 0 ) { 930 global $wpdb;931 932 936 $post_id = (int) $post_id; 933 937 … … 940 944 * @param int $post_id The post ID. 941 945 */ 942 $ stats= apply_filters( 'wp_count_comments', array(), $post_id );943 if ( ! empty($stats) )944 return $ stats;945 946 $count = wp_cache_get("comments-{$post_id}", 'counts'); 947 948 if ( false !== $count ) 946 $filtered = apply_filters( 'wp_count_comments', array(), $post_id ); 947 if ( ! empty( $filtered ) ) { 948 return $filtered; 949 } 950 951 $count = wp_cache_get( "comments-{$post_id}", 'counts' ); 952 if ( false !== $count ) { 949 953 return $count; 950 951 $where = ''; 952 if ( $post_id > 0 ) 953 $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id ); 954 955 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A ); 956 957 $total = 0; 958 $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed'); 959 foreach ( (array) $count as $row ) { 960 // Don't count post-trashed toward totals 961 if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) 962 $total += $row['num_comments']; 963 if ( isset( $approved[$row['comment_approved']] ) ) 964 $stats[$approved[$row['comment_approved']]] = $row['num_comments']; 965 } 966 967 $stats['total_comments'] = $total; 968 foreach ( $approved as $key ) { 969 if ( empty($stats[$key]) ) 970 $stats[$key] = 0; 971 } 972 973 $stats = (object) $stats; 974 wp_cache_set("comments-{$post_id}", $stats, 'counts'); 975 976 return $stats; 954 } 955 956 $stats = get_comment_count( $post_id ); 957 $stats['moderated'] = $stats['awaiting_moderation']; 958 unset( $stats['awaiting_moderation'] ); 959 960 $stats_object = (object) $stats; 961 wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' ); 962 963 return $stats_object; 977 964 } 978 965 -
trunk/tests/phpunit/tests/comment/getCommentCount.php
r33806 r33822 9 9 $this->assertEquals( 0, $count['awaiting_moderation'] ); 10 10 $this->assertEquals( 0, $count['spam'] ); 11 $this->assertEquals( 0, $count['trash'] ); 12 $this->assertEquals( 0, $count['post-trashed'] ); 11 13 $this->assertEquals( 0, $count['total_comments'] ); 12 14 } … … 22 24 $this->assertEquals( 0, $count['awaiting_moderation'] ); 23 25 $this->assertEquals( 0, $count['spam'] ); 26 $this->assertEquals( 0, $count['trash'] ); 27 $this->assertEquals( 0, $count['post-trashed'] ); 24 28 $this->assertEquals( 1, $count['total_comments'] ); 25 29 } … … 35 39 $this->assertEquals( 1, $count['awaiting_moderation'] ); 36 40 $this->assertEquals( 0, $count['spam'] ); 41 $this->assertEquals( 0, $count['trash'] ); 42 $this->assertEquals( 0, $count['post-trashed'] ); 37 43 $this->assertEquals( 1, $count['total_comments'] ); 38 44 } … … 48 54 $this->assertEquals( 0, $count['awaiting_moderation'] ); 49 55 $this->assertEquals( 1, $count['spam'] ); 56 $this->assertEquals( 0, $count['trash'] ); 57 $this->assertEquals( 0, $count['post-trashed'] ); 50 58 $this->assertEquals( 1, $count['total_comments'] ); 51 59 } … … 61 69 $this->assertEquals( 0, $count['awaiting_moderation'] ); 62 70 $this->assertEquals( 0, $count['spam'] ); 71 $this->assertEquals( 1, $count['trash'] ); 72 $this->assertEquals( 0, $count['post-trashed'] ); 73 $this->assertEquals( 0, $count['total_comments'] ); 74 } 75 76 public function test_get_comment_count_post_trashed() { 77 $this->factory->comment->create( array( 78 'comment_approved' => 'post-trashed' 79 ) ); 80 81 $count = get_comment_count(); 82 83 $this->assertEquals( 0, $count['approved'] ); 84 $this->assertEquals( 0, $count['awaiting_moderation'] ); 85 $this->assertEquals( 0, $count['spam'] ); 86 $this->assertEquals( 0, $count['trash'] ); 87 $this->assertEquals( 1, $count['post-trashed'] ); 63 88 $this->assertEquals( 0, $count['total_comments'] ); 64 89 }
Note: See TracChangeset
for help on using the changeset viewer.