WC_Comments::wp_count_comments()public staticWC 2.2

Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().

Method of the class: WC_Comments{}

No Hooks.

Return

Object.

Usage

$result = WC_Comments::wp_count_comments( $stats, $post_id );
$stats(object) (required)
Comment stats.
$post_id(int) (required)
Post ID.

Changelog

Since 2.2 Introduced.

WC_Comments::wp_count_comments() code WC 9.4.2

public static function wp_count_comments( $stats, $post_id ) {
	global $wpdb;

	if ( 0 === $post_id ) {
		$stats = get_transient( 'wc_count_comments' );

		if ( ! $stats ) {
			$stats = array(
				'total_comments' => 0,
				'all'            => 0,
			);

			$count = $wpdb->get_results(
				"
				SELECT comment_approved, COUNT(*) AS num_comments
				FROM {$wpdb->comments}
				LEFT JOIN {$wpdb->posts} ON comment_post_ID = {$wpdb->posts}.ID
				WHERE comment_type NOT IN ('action_log', 'order_note', 'webhook_delivery') AND {$wpdb->posts}.post_type NOT IN ('product')
				GROUP BY comment_approved
				",
				ARRAY_A
			);

			$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 ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash', 'spam' ), true ) ) {
					$stats['all']            += $row['num_comments'];
					$stats['total_comments'] += $row['num_comments'];
				} elseif ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash' ), true ) ) {
					$stats['total_comments'] += $row['num_comments'];
				}
				if ( isset( $approved[ $row['comment_approved'] ] ) ) {
					$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
				}
			}

			foreach ( $approved as $key ) {
				if ( empty( $stats[ $key ] ) ) {
					$stats[ $key ] = 0;
				}
			}

			$stats = (object) $stats;
			set_transient( 'wc_count_comments', $stats );
		}
	}

	return $stats;
}