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. Built from https://develop.svn.wordpress.org/trunk@53036 git-svn-id: http://core.svn.wordpress.org/trunk@52625 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
6a97b83d01
commit
1ad5807bb0
|
@ -383,21 +383,6 @@ function get_comment_count( $post_id = 0 ) {
|
|||
|
||||
$post_id = (int) $post_id;
|
||||
|
||||
$where = '';
|
||||
if ( $post_id > 0 ) {
|
||||
$where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id );
|
||||
}
|
||||
|
||||
$totals = (array) $wpdb->get_results(
|
||||
"
|
||||
SELECT comment_approved, COUNT( * ) AS total
|
||||
FROM {$wpdb->comments}
|
||||
{$where}
|
||||
GROUP BY comment_approved
|
||||
",
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$comment_count = array(
|
||||
'approved' => 0,
|
||||
'awaiting_moderation' => 0,
|
||||
|
@ -408,32 +393,27 @@ function get_comment_count( $post_id = 0 ) {
|
|||
'all' => 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'];
|
||||
break;
|
||||
case '1':
|
||||
$comment_count['approved'] = $row['total'];
|
||||
$comment_count['total_comments'] += $row['total'];
|
||||
$comment_count['all'] += $row['total'];
|
||||
break;
|
||||
case '0':
|
||||
$comment_count['awaiting_moderation'] = $row['total'];
|
||||
$comment_count['total_comments'] += $row['total'];
|
||||
$comment_count['all'] += $row['total'];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
$args = array(
|
||||
'count' => true,
|
||||
'update_comment_meta_cache' => false,
|
||||
);
|
||||
if ( $post_id > 0 ) {
|
||||
$args['post_id'] = $post_id;
|
||||
}
|
||||
$mapping = array(
|
||||
'approved' => 'approve',
|
||||
'awaiting_moderation' => 'hold',
|
||||
'spam' => 'spam',
|
||||
'trash' => 'trash',
|
||||
'post-trashed' => 'post-trashed',
|
||||
);
|
||||
$comment_count = array();
|
||||
foreach ( $mapping as $key => $value ) {
|
||||
$comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) );
|
||||
}
|
||||
|
||||
$comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation'];
|
||||
$comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
|
||||
|
||||
return array_map( 'intval', $comment_count );
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.0-alpha-53035';
|
||||
$wp_version = '6.0-alpha-53036';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue