WC_Comments::wp_count_comments
Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().
Method of the class: WC_Comments{}
No Hooks.
Returns
Object.
Usage
$result = WC_Comments::wp_count_comments( $stats, $post_id );
- $stats(array|object) (required)
- Comment stats.
- $post_id(int) (required)
- Post ID.
Changelog
| Since 2.2 | Introduced. |
WC_Comments::wp_count_comments() WC Comments::wp count comments code WC 10.3.5
public static function wp_count_comments( $stats, $post_id ) {
if ( 0 !== $post_id || ! empty( $stats ) ) {
// If $stats isn't empty, another plugin may have already made changes to the values that we can't account for, so we don't attempt to modify it.
return $stats;
}
$comment_counts = array();
// WordPress is inconsistent in the names it uses for approved/unapproved comment statuses, so we need to remap the names.
$stat_key_to_comment_query_status_mapping = array(
'approved' => 'approve',
'moderated' => 'hold',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);
$comment_query_status_to_comment_status_mapping = array(
'approve' => 'approved',
'hold' => 'unapproved',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);
$args = array(
'count' => true,
'update_comment_meta_cache' => false,
'orderby' => 'none',
);
foreach ( $stat_key_to_comment_query_status_mapping as $stat_key => $query_status ) {
// For simplicity, the cache key is by the comment status returned by wp_get_comment_status() and used by wp_transition_comment_status().
$cache_key = 'wc_count_comments_' . $comment_query_status_to_comment_status_mapping[ $query_status ];
$count = wp_cache_get( $cache_key, self::COMMENT_COUNT_CACHE_GROUP );
if ( false === $count ) {
$count = (int) get_comments( array_merge( $args, array( 'status' => $query_status ) ) );
wp_cache_set( $cache_key, $count, self::COMMENT_COUNT_CACHE_GROUP, 3 * DAY_IN_SECONDS );
}
$comment_counts[ $stat_key ] = (int) $count;
}
$comment_counts['all'] = $comment_counts['approved'] + $comment_counts['moderated'];
$comment_counts['total_comments'] = $comment_counts['all'] + $comment_counts['spam'];
return (object) $comment_counts;
}