WordPress at Your Fingertips
function is not described

WC_Comments::wp_count_comments() public WC 2.2

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

{} It's a 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.

Code of WC_Comments::wp_count_comments() WC 5.7.1

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}
				WHERE comment_type NOT IN ('action_log', 'order_note', 'webhook_delivery')
				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;
}