Make WordPress Core

Changeset 33822


Ignore:
Timestamp:
08/31/2015 06:50:12 PM (11 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-functions.php

    r33811 r33822  
    364364
    365365        $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,
    370372        );
    371373
    372374        foreach ( $totals as $row ) {
    373375                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;
    374382                        case 'spam':
    375383                                $comment_count['spam'] = $row['total'];
    376                                 $comment_count["total_comments"] += $row['total'];
     384                                $comment_count['total_comments'] += $row['total'];
    377385                                break;
    378386                        case '1':
     
    922930 * @since 2.5.0
    923931 *
    924  * @global wpdb $wpdb
    925  *
    926932 * @param int $post_id Optional. Post ID.
    927933 * @return object|array Comment stats.
    928934 */
    929935function wp_count_comments( $post_id = 0 ) {
    930         global $wpdb;
    931 
    932936        $post_id = (int) $post_id;
    933937
     
    940944         * @param int   $post_id The post ID.
    941945         */
    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 ) {
    949953                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;
    977964}
    978965
  • trunk/tests/phpunit/tests/comment/getCommentCount.php

    r33806 r33822  
    99                $this->assertEquals( 0, $count['awaiting_moderation'] );
    1010                $this->assertEquals( 0, $count['spam'] );
     11                $this->assertEquals( 0, $count['trash'] );
     12                $this->assertEquals( 0, $count['post-trashed'] );
    1113                $this->assertEquals( 0, $count['total_comments'] );
    1214        }
     
    2224                $this->assertEquals( 0, $count['awaiting_moderation'] );
    2325                $this->assertEquals( 0, $count['spam'] );
     26                $this->assertEquals( 0, $count['trash'] );
     27                $this->assertEquals( 0, $count['post-trashed'] );
    2428                $this->assertEquals( 1, $count['total_comments'] );
    2529        }
     
    3539                $this->assertEquals( 1, $count['awaiting_moderation'] );
    3640                $this->assertEquals( 0, $count['spam'] );
     41                $this->assertEquals( 0, $count['trash'] );
     42                $this->assertEquals( 0, $count['post-trashed'] );
    3743                $this->assertEquals( 1, $count['total_comments'] );
    3844        }
     
    4854                $this->assertEquals( 0, $count['awaiting_moderation'] );
    4955                $this->assertEquals( 1, $count['spam'] );
     56                $this->assertEquals( 0, $count['trash'] );
     57                $this->assertEquals( 0, $count['post-trashed'] );
    5058                $this->assertEquals( 1, $count['total_comments'] );
    5159        }
     
    6169                $this->assertEquals( 0, $count['awaiting_moderation'] );
    6270                $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'] );
    6388                $this->assertEquals( 0, $count['total_comments'] );
    6489        }
Note: See TracChangeset for help on using the changeset viewer.