Comments: wp_count_comments()
can use get_comment_count()
internally to makes its DB query, provided that get_comment_count()
returns more properties.
Adds/updates unit tests. There were zero (0) unit tests for `wp_count_comments()`. Fixes #19903. git-svn-id: https://develop.svn.wordpress.org/trunk@33822 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
af101dec19
commit
3028a096be
@ -363,17 +363,25 @@ function get_comment_count( $post_id = 0 ) {
|
||||
", ARRAY_A);
|
||||
|
||||
$comment_count = array(
|
||||
"approved" => 0,
|
||||
"awaiting_moderation" => 0,
|
||||
"spam" => 0,
|
||||
"total_comments" => 0
|
||||
'approved' => 0,
|
||||
'awaiting_moderation' => 0,
|
||||
'spam' => 0,
|
||||
'trash' => 0,
|
||||
'post-trashed' => 0,
|
||||
'total_comments' => 0,
|
||||
);
|
||||
|
||||
foreach ( $totals as $row ) {
|
||||
switch ( $row['comment_approved'] ) {
|
||||
case 'trash':
|
||||
$comment_count['trash'] = $row['total'];
|
||||
break;
|
||||
case 'post-trashed':
|
||||
$comment_count['post-trashed'] = $row['total'];
|
||||
break;
|
||||
case 'spam':
|
||||
$comment_count['spam'] = $row['total'];
|
||||
$comment_count["total_comments"] += $row['total'];
|
||||
$comment_count['total_comments'] += $row['total'];
|
||||
break;
|
||||
case '1':
|
||||
$comment_count['approved'] = $row['total'];
|
||||
@ -921,14 +929,10 @@ function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_age
|
||||
*
|
||||
* @since 2.5.0
|
||||
*
|
||||
* @global wpdb $wpdb
|
||||
*
|
||||
* @param int $post_id Optional. Post ID.
|
||||
* @return object|array Comment stats.
|
||||
*/
|
||||
function wp_count_comments( $post_id = 0 ) {
|
||||
global $wpdb;
|
||||
|
||||
$post_id = (int) $post_id;
|
||||
|
||||
/**
|
||||
@ -939,41 +943,24 @@ function wp_count_comments( $post_id = 0 ) {
|
||||
* @param array $count An empty array.
|
||||
* @param int $post_id The post ID.
|
||||
*/
|
||||
$stats = apply_filters( 'wp_count_comments', array(), $post_id );
|
||||
if ( !empty($stats) )
|
||||
return $stats;
|
||||
$filtered = apply_filters( 'wp_count_comments', array(), $post_id );
|
||||
if ( ! empty( $filtered ) ) {
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
$count = wp_cache_get("comments-{$post_id}", 'counts');
|
||||
|
||||
if ( false !== $count )
|
||||
$count = wp_cache_get( "comments-{$post_id}", 'counts' );
|
||||
if ( false !== $count ) {
|
||||
return $count;
|
||||
|
||||
$where = '';
|
||||
if ( $post_id > 0 )
|
||||
$where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
|
||||
|
||||
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
|
||||
|
||||
$total = 0;
|
||||
$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
|
||||
foreach ( (array) $count as $row ) {
|
||||
// Don't count post-trashed toward totals
|
||||
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
|
||||
$total += $row['num_comments'];
|
||||
if ( isset( $approved[$row['comment_approved']] ) )
|
||||
$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
|
||||
}
|
||||
|
||||
$stats['total_comments'] = $total;
|
||||
foreach ( $approved as $key ) {
|
||||
if ( empty($stats[$key]) )
|
||||
$stats[$key] = 0;
|
||||
}
|
||||
$stats = get_comment_count( $post_id );
|
||||
$stats['moderated'] = $stats['awaiting_moderation'];
|
||||
unset( $stats['awaiting_moderation'] );
|
||||
|
||||
$stats = (object) $stats;
|
||||
wp_cache_set("comments-{$post_id}", $stats, 'counts');
|
||||
$stats_object = (object) $stats;
|
||||
wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
|
||||
|
||||
return $stats;
|
||||
return $stats_object;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,6 +8,8 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
|
||||
$this->assertEquals( 0, $count['approved'] );
|
||||
$this->assertEquals( 0, $count['awaiting_moderation'] );
|
||||
$this->assertEquals( 0, $count['spam'] );
|
||||
$this->assertEquals( 0, $count['trash'] );
|
||||
$this->assertEquals( 0, $count['post-trashed'] );
|
||||
$this->assertEquals( 0, $count['total_comments'] );
|
||||
}
|
||||
|
||||
@ -21,6 +23,8 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
|
||||
$this->assertEquals( 1, $count['approved'] );
|
||||
$this->assertEquals( 0, $count['awaiting_moderation'] );
|
||||
$this->assertEquals( 0, $count['spam'] );
|
||||
$this->assertEquals( 0, $count['trash'] );
|
||||
$this->assertEquals( 0, $count['post-trashed'] );
|
||||
$this->assertEquals( 1, $count['total_comments'] );
|
||||
}
|
||||
|
||||
@ -34,6 +38,8 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
|
||||
$this->assertEquals( 0, $count['approved'] );
|
||||
$this->assertEquals( 1, $count['awaiting_moderation'] );
|
||||
$this->assertEquals( 0, $count['spam'] );
|
||||
$this->assertEquals( 0, $count['trash'] );
|
||||
$this->assertEquals( 0, $count['post-trashed'] );
|
||||
$this->assertEquals( 1, $count['total_comments'] );
|
||||
}
|
||||
|
||||
@ -47,6 +53,8 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
|
||||
$this->assertEquals( 0, $count['approved'] );
|
||||
$this->assertEquals( 0, $count['awaiting_moderation'] );
|
||||
$this->assertEquals( 1, $count['spam'] );
|
||||
$this->assertEquals( 0, $count['trash'] );
|
||||
$this->assertEquals( 0, $count['post-trashed'] );
|
||||
$this->assertEquals( 1, $count['total_comments'] );
|
||||
}
|
||||
|
||||
@ -60,6 +68,23 @@ class Tests_Get_Comment_Count extends WP_UnitTestCase {
|
||||
$this->assertEquals( 0, $count['approved'] );
|
||||
$this->assertEquals( 0, $count['awaiting_moderation'] );
|
||||
$this->assertEquals( 0, $count['spam'] );
|
||||
$this->assertEquals( 1, $count['trash'] );
|
||||
$this->assertEquals( 0, $count['post-trashed'] );
|
||||
$this->assertEquals( 0, $count['total_comments'] );
|
||||
}
|
||||
|
||||
public function test_get_comment_count_post_trashed() {
|
||||
$this->factory->comment->create( array(
|
||||
'comment_approved' => 'post-trashed'
|
||||
) );
|
||||
|
||||
$count = get_comment_count();
|
||||
|
||||
$this->assertEquals( 0, $count['approved'] );
|
||||
$this->assertEquals( 0, $count['awaiting_moderation'] );
|
||||
$this->assertEquals( 0, $count['spam'] );
|
||||
$this->assertEquals( 0, $count['trash'] );
|
||||
$this->assertEquals( 1, $count['post-trashed'] );
|
||||
$this->assertEquals( 0, $count['total_comments'] );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user