From 7355fadfddd305d0f9f8a2d4965345fda5aa7c3c Mon Sep 17 00:00:00 2001 From: Dan Wyman Date: Thu, 30 May 2019 12:45:47 -0700 Subject: [PATCH] Tracker: fix review count. Fixes #23848 --- includes/class-wc-tracker.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/includes/class-wc-tracker.php b/includes/class-wc-tracker.php index 48effc67ae6..4bde61e6ba3 100644 --- a/includes/class-wc-tracker.php +++ b/includes/class-wc-tracker.php @@ -349,8 +349,14 @@ class WC_Tracker { */ private static function get_review_counts() { global $wpdb; - $review_count = array(); - $counts = $wpdb->get_results( + $review_count = array( 'total' => 0 ); + $status_map = array( + '0' => 'pending', + '1' => 'approved', + 'trash' => 'trash', + 'spam' => 'spam', + ); + $counts = $wpdb->get_results( " SELECT comment_approved, COUNT(*) AS num_reviews FROM {$wpdb->comments} @@ -359,15 +365,19 @@ class WC_Tracker { ", ARRAY_A ); - if ( $counts ) { - foreach ( $counts as $count ) { - if ( 1 === $count['comment_approved'] ) { - $review_count['approved'] = $count['num_reviews']; - } else { - $review_count['pending'] = $count['num_reviews']; - } - } + + if ( ! $counts ) { + return $review_count; } + + foreach ( $counts as $count ) { + $status = $count['comment_approved']; + if ( array_key_exists( $status, $status_map ) ) { + $review_count[ $status_map[ $status ] ] = $count['num_reviews']; + } + $review_count['total'] += $count['num_reviews']; + } + return $review_count; }