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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-comments.php
woocommerce / includes Last commit date
abstracts 1 year ago admin 1 year ago blocks 1 year ago cli 1 year ago customizer 1 year ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 1 year ago libraries 1 year ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year ago theme-support 2 years ago tracks 1 year ago traits 5 years ago walkers 5 years ago wccom-site 1 year ago widgets 1 year ago class-wc-ajax.php 1 year ago class-wc-auth.php 1 year ago class-wc-autoloader.php 1 year ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 year ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 1 year ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 1 year ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 year ago class-wc-checkout.php 1 year ago class-wc-cli.php 1 year ago class-wc-comments.php 1 year ago class-wc-countries.php 1 year ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 1 year ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 year ago class-wc-emails.php 1 year ago class-wc-embed.php 1 year ago class-wc-form-handler.php 1 year ago class-wc-frontend-scripts.php 1 year ago class-wc-geo-ip.php 1 year ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 year ago class-wc-https.php 2 years ago class-wc-install.php 1 year ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 1 year ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 2 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 year ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 year ago class-wc-order-item-shipping.php 1 year ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 1 year ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 year ago class-wc-payment-gateways.php 1 year ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 year ago class-wc-post-types.php 1 year ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 1 year ago class-wc-product-grouped.php 1 year ago class-wc-product-query.php 1 year ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 year ago class-wc-product-variation.php 1 year ago class-wc-query.php 1 year ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 1 year ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 1 year ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 year ago class-wc-tax.php 2 years ago class-wc-template-loader.php 1 year ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 year ago class-woocommerce.php 1 year ago wc-account-functions.php 1 year ago wc-attribute-functions.php 1 year ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 year ago wc-conditional-functions.php 1 year ago wc-core-functions.php 1 year ago wc-coupon-functions.php 1 year ago wc-deprecated-functions.php 1 year ago wc-formatting-functions.php 1 year ago wc-notice-functions.php 1 year ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 1 year ago wc-page-functions.php 1 year ago wc-product-functions.php 1 year ago wc-rest-functions.php 1 year ago wc-stock-functions.php 1 year ago wc-template-functions.php 1 year ago wc-template-hooks.php 1 year ago wc-term-functions.php 1 year ago wc-update-functions.php 1 year ago wc-user-functions.php 1 year ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
class-wc-comments.php
663 lines
1 <?php
2 /**
3 * Comments
4 *
5 * Handle comments (reviews and order notes).
6 *
7 * @package WooCommerce\Classes\Products
8 * @version 2.3.0
9 */
10
11 use Automattic\WooCommerce\Internal\Admin\ProductReviews\ReviewsUtil;
12
13 defined( 'ABSPATH' ) || exit;
14
15 /**
16 * Comments class.
17 */
18 class WC_Comments {
19
20 /**
21 * The cache group to use for comment counts.
22 *
23 * @var string
24 */
25 private const COMMENT_COUNT_CACHE_GROUP = 'wc_comment_counts';
26
27 /**
28 * The cache key to use for pending product reviews counts.
29 *
30 * @var string
31 */
32 private const PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY = 'woocommerce_product_reviews_pending_count';
33
34 /**
35 * Hook in methods.
36 */
37 public static function init() {
38 // Rating posts.
39 add_filter( 'comments_open', array( __CLASS__, 'comments_open' ), 10, 2 );
40 add_filter( 'preprocess_comment', array( __CLASS__, 'check_comment_rating' ), 0 );
41 add_action( 'comment_post', array( __CLASS__, 'add_comment_rating' ), 1 );
42 add_action( 'comment_moderation_recipients', array( __CLASS__, 'comment_moderation_recipients' ), 10, 2 );
43
44 // Clear transients.
45 add_action( 'wp_update_comment_count', array( __CLASS__, 'clear_transients' ) );
46
47 // Secure order notes.
48 add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
49 add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
50
51 // Secure webhook comments.
52 add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
53 add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_webhook_comments_from_feed_where' ) );
54
55 // Secure potential remaining Action Logs.
56 add_filter( 'comments_clauses', array( __CLASS__, 'exclude_action_log_comments' ), 10, 2 );
57 add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_action_log_comments_from_feed_where' ) );
58
59 // Exclude product reviews from general comments.
60 add_filter( 'comments_clauses', array( ReviewsUtil::class, 'comments_clauses_without_product_reviews' ), 10, 2 );
61
62 // Count comments.
63 add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 10, 2 );
64
65 // Delete comments count cache whenever there is a new comment or a comment status changes.
66 add_action( 'wp_insert_comment', array( __CLASS__, 'increment_comments_count_cache_on_wp_insert_comment' ), 10, 2 );
67 add_action( 'transition_comment_status', array( __CLASS__, 'update_comments_count_cache_on_comment_status_change' ), 10, 3 );
68
69 // Count product reviews that pending moderation.
70 add_action( 'wp_insert_comment', array( __CLASS__, 'maybe_bump_products_reviews_pending_moderation_counter' ), 10, 2 );
71 add_action( 'transition_comment_status', array( __CLASS__, 'maybe_adjust_products_reviews_pending_moderation_counter' ), 10, 3 );
72
73 // Support avatars for `review` comment type.
74 add_filter( 'get_avatar_comment_types', array( __CLASS__, 'add_avatar_for_review_comment_type' ) );
75
76 // Add Product Reviews filter for `review` comment type.
77 add_filter( 'admin_comment_types_dropdown', array( __CLASS__, 'add_review_comment_filter' ) );
78
79 // Review of verified purchase.
80 add_action( 'comment_post', array( __CLASS__, 'add_comment_purchase_verification' ) );
81
82 // Set comment type.
83 add_action( 'preprocess_comment', array( __CLASS__, 'update_comment_type' ), 1 );
84
85 // Validate product reviews if requires verified owners.
86 add_action( 'pre_comment_on_post', array( __CLASS__, 'validate_product_review_verified_owners' ) );
87 }
88
89 /**
90 * See if comments are open.
91 *
92 * @since 3.1.0
93 * @param bool $open Whether the current post is open for comments.
94 * @param int $post_id Post ID.
95 * @return bool
96 */
97 public static function comments_open( $open, $post_id ) {
98 if ( 'product' === get_post_type( $post_id ) && ! post_type_supports( 'product', 'comments' ) ) {
99 $open = false;
100 }
101 return $open;
102 }
103
104 /**
105 * Exclude order comments from queries and RSS.
106 *
107 * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
108 * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and.
109 * shop managers can view orders anyway.
110 *
111 * The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
112 *
113 * @param array $clauses A compacted array of comment query clauses.
114 * @return array
115 */
116 public static function exclude_order_comments( $clauses ) {
117 $clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " comment_type != 'order_note' ";
118 return $clauses;
119 }
120
121 /**
122 * Exclude order comments from feed.
123 *
124 * @deprecated 3.1
125 * @param mixed $join Deprecated.
126 */
127 public static function exclude_order_comments_from_feed_join( $join ) {
128 wc_deprecated_function( 'WC_Comments::exclude_order_comments_from_feed_join', '3.1' );
129 }
130
131 /**
132 * Exclude order comments from queries and RSS.
133 *
134 * @param string $where The WHERE clause of the query.
135 * @return string
136 */
137 public static function exclude_order_comments_from_feed_where( $where ) {
138 return $where . ( trim( $where ) ? ' AND ' : '' ) . " comment_type != 'order_note' ";
139 }
140
141 /**
142 * Exclude webhook comments from queries and RSS.
143 *
144 * @since 2.2
145 * @param array $clauses A compacted array of comment query clauses.
146 * @return array
147 */
148 public static function exclude_webhook_comments( $clauses ) {
149 $clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
150 return $clauses;
151 }
152
153 /**
154 * Exclude webhooks comments from feed.
155 *
156 * @deprecated 3.1
157 * @param mixed $join Deprecated.
158 */
159 public static function exclude_webhook_comments_from_feed_join( $join ) {
160 wc_deprecated_function( 'WC_Comments::exclude_webhook_comments_from_feed_join', '3.1' );
161 }
162
163 /**
164 * Exclude action_log comments from queries and RSS.
165 *
166 * @since 2.1
167 * @param string $where The WHERE clause of the query.
168 * @return string
169 */
170 public static function exclude_action_log_comments_from_feed_where( $where ) {
171 return $where . ( trim( $where ) ? ' AND ' : '' ) . " comment_type != 'action_log' ";
172 }
173
174 /**
175 * Exclude action_log comments from queries.
176 *
177 * @param array $clauses A compacted array of comment query clauses.
178 * @param WP_Comment_Query $comment_query The WP_Comment_Query being filtered.
179 *
180 * @return array
181 * @since 9.9
182 */
183 public static function exclude_action_log_comments( $clauses, $comment_query ) {
184 if ( 'action_log' !== $comment_query->query_vars['type'] ) {
185 $clauses['where'] .= ( trim( $clauses['where'] ) ? ' AND ' : '' ) . " comment_type != 'action_log' ";
186 }
187
188 return $clauses;
189 }
190
191 /**
192 * Validate the comment ratings.
193 *
194 * @param array $comment_data Comment data.
195 * @return array
196 */
197 public static function check_comment_rating( $comment_data ) {
198 // If posting a comment (not trackback etc) and not logged in.
199 if ( ! is_admin() && isset( $_POST['comment_post_ID'], $_POST['rating'], $comment_data['comment_type'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) && empty( $_POST['rating'] ) && self::is_default_comment_type( $comment_data['comment_type'] ) && wc_review_ratings_enabled() && wc_review_ratings_required() ) { // WPCS: input var ok, CSRF ok.
200 wp_die( esc_html__( 'Please rate the product.', 'woocommerce' ) );
201 exit;
202 }
203 return $comment_data;
204 }
205
206 /**
207 * Rating field for comments.
208 *
209 * @param int $comment_id Comment ID.
210 */
211 public static function add_comment_rating( $comment_id ) {
212 if ( isset( $_POST['rating'], $_POST['comment_post_ID'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
213 if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
214 return;
215 }
216 add_comment_meta( $comment_id, 'rating', intval( $_POST['rating'] ), true ); // WPCS: input var ok, CSRF ok.
217
218 $post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // WPCS: input var ok, CSRF ok.
219 if ( $post_id ) {
220 self::clear_transients( $post_id );
221 }
222 }
223 }
224
225 /**
226 * Modify recipient of review email.
227 *
228 * @param array $emails Emails.
229 * @param int $comment_id Comment ID.
230 * @return array
231 */
232 public static function comment_moderation_recipients( $emails, $comment_id ) {
233 $comment = get_comment( $comment_id );
234
235 if ( $comment && 'product' === get_post_type( $comment->comment_post_ID ) ) {
236 $emails = array( get_option( 'admin_email' ) );
237 }
238
239 return $emails;
240 }
241
242 /**
243 * Ensure product average rating and review count is kept up to date.
244 *
245 * @param int $post_id Post ID.
246 */
247 public static function clear_transients( $post_id ) {
248 if ( 'product' === get_post_type( $post_id ) ) {
249 $product = wc_get_product( $post_id );
250 $product->set_rating_counts( self::get_rating_counts_for_product( $product ) );
251 $product->set_average_rating( self::get_average_rating_for_product( $product ) );
252 $product->set_review_count( self::get_review_count_for_product( $product ) );
253 $product->save();
254 }
255 }
256
257 /**
258 * Callback for 'wp_insert_comment' to delete the comment count cache if the comment is included in the count.
259 *
260 * @param int $comment_id The comment ID.
261 * @param WP_Comment $comment Comment object.
262 *
263 * @return void
264 */
265 public static function increment_comments_count_cache_on_wp_insert_comment( $comment_id, $comment ) {
266 if ( ! self::is_comment_excluded_from_wp_comment_counts( $comment ) ) {
267 $comment_status = wp_get_comment_status( $comment );
268 if ( false !== $comment_status ) {
269 wp_cache_incr( 'wc_count_comments_' . $comment_status, 1, self::COMMENT_COUNT_CACHE_GROUP );
270 }
271 }
272 }
273
274 /**
275 * Callback for 'comment_status_change' to delete the comment count cache if the comment is included in the count.
276 *
277 * @param int|string $new_status The new comment status.
278 * @param int|string $old_status The old comment status.
279 * @param WP_Comment $comment Comment object.
280 *
281 * @return void
282 */
283 public static function update_comments_count_cache_on_comment_status_change( $new_status, $old_status, $comment ) {
284 if ( ! self::is_comment_excluded_from_wp_comment_counts( $comment ) ) {
285 wp_cache_incr( 'wc_count_comments_' . $new_status, 1, self::COMMENT_COUNT_CACHE_GROUP );
286 wp_cache_decr( 'wc_count_comments_' . $old_status, 1, self::COMMENT_COUNT_CACHE_GROUP );
287 }
288 }
289
290 /**
291 * Determines whether the given comment should be included in the core WP comment counts that are displayed in the
292 * WordPress admin.
293 *
294 * @param WP_Comment $comment Comment object.
295 *
296 * @return bool
297 */
298 private static function is_comment_excluded_from_wp_comment_counts( $comment ) {
299 return in_array( $comment->comment_type, array( 'action_log', 'order_note', 'webhook_delivery' ), true )
300 || get_post_type( $comment->comment_post_ID ) === 'product';
301 }
302
303 /**
304 * Delete comments count cache whenever there is
305 * new comment or the status of a comment changes. Cache
306 * will be regenerated next time WC_Comments::wp_count_comments()
307 * is called.
308 */
309 public static function delete_comments_count_cache() {
310 $comment_status_keys = array(
311 'wc_count_comments_approved',
312 'wc_count_comments_unapproved',
313 'wc_count_comments_spam',
314 'wc_count_comments_trash',
315 'wc_count_comments_post-trashed',
316 );
317 wp_cache_delete_multiple( $comment_status_keys, self::COMMENT_COUNT_CACHE_GROUP );
318 }
319
320 /**
321 * Fetches (and populates if needed) the counter.
322 *
323 * @return int
324 */
325 public static function get_products_reviews_pending_moderation_counter(): int {
326 $count = wp_cache_get( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, self::COMMENT_COUNT_CACHE_GROUP );
327 if ( false === $count ) {
328 $count = (int) get_comments(
329 array(
330 'type__in' => array( 'review', 'comment' ),
331 'status' => '0',
332 'post_type' => 'product',
333 'count' => true,
334 )
335 );
336 wp_cache_set( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, $count, self::COMMENT_COUNT_CACHE_GROUP, DAY_IN_SECONDS );
337 }
338
339 return $count;
340 }
341
342 /**
343 * Handles `wp_insert_comment` hook processing and actualizes the counter.
344 *
345 * @param int $comment_id Comment ID.
346 * @param \WP_Comment $comment Comment object.
347 * @return void
348 */
349 public static function maybe_bump_products_reviews_pending_moderation_counter( $comment_id, $comment ): void {
350 $needs_bump = '0' === $comment->comment_approved;
351 if ( $needs_bump && in_array( $comment->comment_type, array( 'review', 'comment', '' ), true ) ) {
352 $is_product = 'product' === get_post_type( $comment->comment_post_ID );
353 if ( $is_product ) {
354 wp_cache_incr( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, 1, self::COMMENT_COUNT_CACHE_GROUP );
355 }
356 }
357 }
358
359 /**
360 * Handles `transition_comment_status` hook processing and actualizes the counter.
361 *
362 * @param int|string $new_status New status.
363 * @param int|string $old_status Old status.
364 * @param \WP_Comment $comment Comment object.
365 * @return void
366 */
367 public static function maybe_adjust_products_reviews_pending_moderation_counter( $new_status, $old_status, $comment ): void {
368 $needs_adjustments = 'unapproved' === $new_status || 'unapproved' === $old_status;
369 if ( $needs_adjustments && in_array( $comment->comment_type, array( 'review', 'comment', '' ), true ) ) {
370 $is_product = 'product' === get_post_type( $comment->comment_post_ID );
371 if ( $is_product ) {
372 if ( '0' === $comment->comment_approved ) {
373 wp_cache_incr( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, 1, self::COMMENT_COUNT_CACHE_GROUP );
374 } else {
375 wp_cache_decr( self::PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY, 1, self::COMMENT_COUNT_CACHE_GROUP );
376 }
377 }
378 }
379 }
380
381 /**
382 * Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().
383 *
384 * @param array|object $stats Comment stats.
385 * @param int $post_id Post ID.
386 *
387 * @return object
388 * @since 2.2
389 */
390 public static function wp_count_comments( $stats, $post_id ) {
391 if ( 0 !== $post_id || ! empty( $stats ) ) {
392 // 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.
393 return $stats;
394 }
395
396 $comment_counts = array();
397
398 // WordPress is inconsistent in the names it uses for approved/unapproved comment statuses, so we need to remap the names.
399 $stat_key_to_comment_query_status_mapping = array(
400 'approved' => 'approve',
401 'moderated' => 'hold',
402 'spam' => 'spam',
403 'trash' => 'trash',
404 'post-trashed' => 'post-trashed',
405 );
406
407 $comment_query_status_to_comment_status_mapping = array(
408 'approve' => 'approved',
409 'hold' => 'unapproved',
410 'spam' => 'spam',
411 'trash' => 'trash',
412 'post-trashed' => 'post-trashed',
413 );
414
415 $args = array(
416 'count' => true,
417 'update_comment_meta_cache' => false,
418 'orderby' => 'none',
419 );
420
421 foreach ( $stat_key_to_comment_query_status_mapping as $stat_key => $query_status ) {
422 // For simplicity, the cache key is by the comment status returned by wp_get_comment_status() and used by wp_transition_comment_status().
423 $cache_key = 'wc_count_comments_' . $comment_query_status_to_comment_status_mapping[ $query_status ];
424 $count = wp_cache_get( $cache_key, self::COMMENT_COUNT_CACHE_GROUP );
425 if ( false === $count ) {
426 $count = (int) get_comments( array_merge( $args, array( 'status' => $query_status ) ) );
427 wp_cache_set( $cache_key, $count, self::COMMENT_COUNT_CACHE_GROUP, 3 * DAY_IN_SECONDS );
428 }
429 $comment_counts[ $stat_key ] = (int) $count;
430 }
431
432 $comment_counts['all'] = $comment_counts['approved'] + $comment_counts['moderated'];
433 $comment_counts['total_comments'] = $comment_counts['all'] + $comment_counts['spam'];
434
435 return (object) $comment_counts;
436 }
437
438 /**
439 * Make sure WP displays avatars for comments with the `review` type.
440 *
441 * @since 2.3
442 * @param array $comment_types Comment types.
443 * @return array
444 */
445 public static function add_avatar_for_review_comment_type( $comment_types ) {
446 return array_merge( $comment_types, array( 'review' ) );
447 }
448
449 /**
450 * Add Product Reviews filter for `review` comment type.
451 *
452 * @since 6.0.0
453 *
454 * @param array $comment_types Array of comment type labels keyed by their name.
455 *
456 * @return array
457 */
458 public static function add_review_comment_filter( array $comment_types ): array {
459 $comment_types['review'] = __( 'Product Reviews', 'woocommerce' );
460 return $comment_types;
461 }
462
463 /**
464 * Determine if a review is from a verified owner at submission.
465 *
466 * @param int $comment_id Comment ID.
467 * @return bool
468 */
469 public static function add_comment_purchase_verification( $comment_id ) {
470 $comment = get_comment( $comment_id );
471 $verified = false;
472 if ( 'product' === get_post_type( $comment->comment_post_ID ) ) {
473 $verified = wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $comment->comment_post_ID );
474 add_comment_meta( $comment_id, 'verified', (int) $verified, true );
475 }
476 return $verified;
477 }
478
479 /**
480 * Get product rating for a product. Please note this is not cached.
481 *
482 * @since 3.0.0
483 * @param WC_Product $product Product instance.
484 * @return float
485 */
486 public static function get_average_rating_for_product( &$product ) {
487 global $wpdb;
488
489 $count = $product->get_rating_count();
490
491 if ( $count ) {
492 $ratings = $wpdb->get_var(
493 $wpdb->prepare(
494 "
495 SELECT SUM(meta_value) FROM $wpdb->commentmeta
496 LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
497 WHERE meta_key = 'rating'
498 AND comment_post_ID = %d
499 AND comment_approved = '1'
500 AND meta_value > 0
501 ",
502 $product->get_id()
503 )
504 );
505 $average = number_format( $ratings / $count, 2, '.', '' );
506 } else {
507 $average = 0;
508 }
509
510 return $average;
511 }
512
513 /**
514 * Utility function for getting review counts for multiple products in one query. This is not cached.
515 *
516 * @since 5.0.0
517 *
518 * @param array $product_ids Array of product IDs.
519 *
520 * @return array
521 */
522 public static function get_review_counts_for_product_ids( $product_ids ) {
523 global $wpdb;
524
525 if ( empty( $product_ids ) ) {
526 return array();
527 }
528
529 $product_id_string_placeholder = substr( str_repeat( ',%s', count( $product_ids ) ), 1 );
530
531 $review_counts = $wpdb->get_results(
532 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Ignored for allowing interpolation in IN query.
533 $wpdb->prepare(
534 "
535 SELECT comment_post_ID as product_id, COUNT( comment_post_ID ) as review_count
536 FROM $wpdb->comments
537 WHERE
538 comment_parent = 0
539 AND comment_post_ID IN ( $product_id_string_placeholder )
540 AND comment_approved = '1'
541 AND comment_type in ( 'review', '', 'comment' )
542 GROUP BY product_id
543 ",
544 $product_ids
545 ),
546 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared.
547 ARRAY_A
548 );
549
550 // Convert to key value pairs.
551 $counts = array_replace( array_fill_keys( $product_ids, 0 ), array_column( $review_counts, 'review_count', 'product_id' ) );
552
553 return $counts;
554 }
555
556 /**
557 * Get product review count for a product (not replies). Please note this is not cached.
558 *
559 * @since 3.0.0
560 * @param WC_Product $product Product instance.
561 * @return int
562 */
563 public static function get_review_count_for_product( &$product ) {
564 $counts = self::get_review_counts_for_product_ids( array( $product->get_id() ) );
565
566 return $counts[ $product->get_id() ];
567 }
568
569 /**
570 * Get product rating count for a product. Please note this is not cached.
571 *
572 * @since 3.0.0
573 * @param WC_Product $product Product instance.
574 * @return int[]
575 */
576 public static function get_rating_counts_for_product( &$product ) {
577 global $wpdb;
578
579 $counts = array();
580 $raw_counts = $wpdb->get_results(
581 $wpdb->prepare(
582 "
583 SELECT meta_value, COUNT( * ) as meta_value_count FROM $wpdb->commentmeta
584 LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
585 WHERE meta_key = 'rating'
586 AND comment_post_ID = %d
587 AND comment_approved = '1'
588 AND meta_value > 0
589 GROUP BY meta_value
590 ",
591 $product->get_id()
592 )
593 );
594
595 foreach ( $raw_counts as $count ) {
596 $counts[ $count->meta_value ] = absint( $count->meta_value_count ); // WPCS: slow query ok.
597 }
598
599 return $counts;
600 }
601
602 /**
603 * Update comment type of product reviews.
604 *
605 * @since 3.5.0
606 * @param array $comment_data Comment data.
607 * @return array
608 */
609 public static function update_comment_type( $comment_data ) {
610 if ( ! is_admin() && isset( $_POST['comment_post_ID'], $comment_data['comment_type'] ) && self::is_default_comment_type( $comment_data['comment_type'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
611 $comment_data['comment_type'] = 'review';
612 }
613
614 return $comment_data;
615 }
616
617 /**
618 * Validate product reviews if requires a verified owner.
619 *
620 * @param int $comment_post_id Post ID.
621 */
622 public static function validate_product_review_verified_owners( $comment_post_id ) {
623 // Only validate if option is enabled.
624 if ( 'yes' !== get_option( 'woocommerce_review_rating_verification_required' ) ) {
625 return;
626 }
627
628 // Validate only products.
629 if ( 'product' !== get_post_type( $comment_post_id ) ) {
630 return;
631 }
632
633 // Skip if is a verified owner.
634 if ( wc_customer_bought_product( '', get_current_user_id(), $comment_post_id ) ) {
635 return;
636 }
637
638 wp_die(
639 esc_html__( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ),
640 esc_html__( 'Reviews can only be left by "verified owners"', 'woocommerce' ),
641 array(
642 'code' => 403,
643 )
644 );
645 }
646
647 /**
648 * Determines if a comment is of the default type.
649 *
650 * Prior to WordPress 5.5, '' was the default comment type.
651 * As of 5.5, the default type is 'comment'.
652 *
653 * @since 4.3.0
654 * @param string $comment_type Comment type.
655 * @return bool
656 */
657 private static function is_default_comment_type( $comment_type ) {
658 return ( '' === $comment_type || 'comment' === $comment_type );
659 }
660 }
661
662 WC_Comments::init();
663