WC_Comments::wp_count_comments()public staticWC 2.2

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

Метод класса: WC_Comments{}

Хуков нет.

Возвращает

Объект.

Использование

$result = WC_Comments::wp_count_comments( $stats, $post_id );
$stats(объект) (обязательный)
Comment stats.
$post_id(int) (обязательный)
Post ID.

Список изменений

С версии 2.2 Введена.

Код WC_Comments::wp_count_comments() WC 8.7.0

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;
}