Make WordPress Core


Ignore:
Timestamp:
03/31/2022 09:07:02 AM (5 years ago)
Author:
spacedmonkey
Message:

Comments: Improve performance of the wp_count_comments function.

Improve performance of the wp_count_comments function by replacing a complex query with multiple calls to the get_comments function. Passing the count parameter to the get_comments function results in a simple
count query that returns quickly. Using get_comments also means that query is cached and run through filters.

Props FolioVision, markjaquith, nacin, ryan, coffee2code, wonderboymusic, ComputerGuru, jb510, SergeyBiryukov, Znuff, Rahe, uday17035, spacedmonkey, peterwilsoncc.
Fixes #19901.

File:
1 edited

Legend:

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

    r52707 r53036  
    384384        $post_id = (int) $post_id;
    385385
    386         $where = '';
    387         if ( $post_id > 0 ) {
    388                 $where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id );
    389         }
    390 
    391         $totals = (array) $wpdb->get_results(
    392                 "
    393                 SELECT comment_approved, COUNT( * ) AS total
    394                 FROM {$wpdb->comments}
    395                 {$where}
    396                 GROUP BY comment_approved
    397         ",
    398                 ARRAY_A
    399         );
    400 
    401386        $comment_count = array(
    402387                'approved'            => 0,
    … …  
    409394        );
    410395
    411         foreach ( $totals as $row ) {
    412                 switch ( $row['comment_approved'] ) {
    413                         case 'trash':
    414                                 $comment_count['trash'] = $row['total'];
    415                                 break;
    416                         case 'post-trashed':
    417                                 $comment_count['post-trashed'] = $row['total'];
    418                                 break;
    419                         case 'spam':
    420                                 $comment_count['spam']            = $row['total'];
    421                                 $comment_count['total_comments'] += $row['total'];
    422                                 break;
    423                         case '1':
    424                                 $comment_count['approved']        = $row['total'];
    425                                 $comment_count['total_comments'] += $row['total'];
    426                                 $comment_count['all']            += $row['total'];
    427                                 break;
    428                         case '0':
    429                                 $comment_count['awaiting_moderation'] = $row['total'];
    430                                 $comment_count['total_comments']     += $row['total'];
    431                                 $comment_count['all']                += $row['total'];
    432                                 break;
    433                         default:
    434                                 break;
    435                 }
    436         }
     396        $args = array(
     397                'count'                     => true,
     398                'update_comment_meta_cache' => false,
     399        );
     400        if ( $post_id > 0 ) {
     401                $args['post_id'] = $post_id;
     402        }
     403        $mapping       = array(
     404                'approved'            => 'approve',
     405                'awaiting_moderation' => 'hold',
     406                'spam'                => 'spam',
     407                'trash'               => 'trash',
     408                'post-trashed'        => 'post-trashed',
     409        );
     410        $comment_count = array();
     411        foreach ( $mapping as $key => $value ) {
     412                $comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) );
     413        }
     414
     415        $comment_count['all']            = $comment_count['approved'] + $comment_count['awaiting_moderation'];
     416        $comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
    437417
    438418        return array_map( 'intval', $comment_count );
Note: See TracChangeset for help on using the changeset viewer.