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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 2 years ago admin 2 years ago blocks 5 years ago cli 2 years ago customizer 3 years ago data-stores 2 years ago emails 2 years ago export 2 years ago gateways 2 years ago import 2 years ago integrations 2 years ago interfaces 3 years ago legacy 2 years ago libraries 3 years ago log-handlers 2 years ago payment-tokens 5 years ago queue 4 years ago react-admin 2 years ago rest-api 2 years ago shipping 2 years ago shortcodes 2 years ago theme-support 2 years ago tracks 2 years ago traits 5 years ago walkers 5 years ago wccom-site 2 years ago widgets 3 years ago class-wc-ajax.php 2 years ago class-wc-api.php 4 years ago class-wc-auth.php 4 years ago class-wc-autoloader.php 5 years ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 3 years ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 years ago class-wc-cart-totals.php 2 years ago class-wc-cart.php 2 years ago class-wc-checkout.php 2 years ago class-wc-cli.php 3 years ago class-wc-comments.php 3 years ago class-wc-countries.php 2 years ago class-wc-coupon.php 2 years ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 4 years ago class-wc-customer.php 2 years 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 8 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 2 years ago class-wc-download-handler.php 3 years ago class-wc-emails.php 2 years ago class-wc-embed.php 5 years ago class-wc-form-handler.php 2 years ago class-wc-frontend-scripts.php 2 years ago class-wc-geo-ip.php 4 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 3 years ago class-wc-https.php 2 years ago class-wc-install.php 2 years ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 2 years ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 3 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 4 years ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 years ago class-wc-order-item-shipping.php 4 years ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 4 years ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 2 years ago class-wc-order.php 2 years ago class-wc-payment-gateways.php 2 years ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 3 years ago class-wc-post-types.php 3 years ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 4 years ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 2 years ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 5 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 2 years ago class-wc-product-variable.php 3 years ago class-wc-product-variation.php 4 years ago class-wc-query.php 3 years ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 3 years ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 3 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 2 years ago class-wc-shipping-rate.php 3 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 2 years ago class-wc-structured-data.php 3 years ago class-wc-tax.php 2 years ago class-wc-template-loader.php 2 years ago class-wc-tracker.php 2 years ago class-wc-validation.php 3 years ago class-wc-webhook.php 2 years ago class-woocommerce.php 3 months ago wc-account-functions.php 2 years ago wc-attribute-functions.php 3 years ago wc-cart-functions.php 2 years ago wc-conditional-functions.php 2 years ago wc-core-functions.php 2 years ago wc-coupon-functions.php 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 2 years ago wc-notice-functions.php 2 years ago wc-order-functions.php 2 years ago wc-order-item-functions.php 3 years ago wc-page-functions.php 2 years ago wc-product-functions.php 2 years ago wc-rest-functions.php 3 years ago wc-stock-functions.php 3 years ago wc-template-functions.php 2 years ago wc-template-hooks.php 5 years ago wc-term-functions.php 3 years ago wc-update-functions.php 2 years ago wc-user-functions.php 2 years ago wc-webhook-functions.php 4 years ago wc-widget-functions.php 5 years ago
class-wc-comments.php
519 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 * Hook in methods.
22 */
23 public static function init() {
24 // Rating posts.
25 add_filter( 'comments_open', array( __CLASS__, 'comments_open' ), 10, 2 );
26 add_filter( 'preprocess_comment', array( __CLASS__, 'check_comment_rating' ), 0 );
27 add_action( 'comment_post', array( __CLASS__, 'add_comment_rating' ), 1 );
28 add_action( 'comment_moderation_recipients', array( __CLASS__, 'comment_moderation_recipients' ), 10, 2 );
29
30 // Clear transients.
31 add_action( 'wp_update_comment_count', array( __CLASS__, 'clear_transients' ) );
32
33 // Secure order notes.
34 add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
35 add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
36
37 // Secure webhook comments.
38 add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
39 add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_webhook_comments_from_feed_where' ) );
40
41 // Exclude product reviews.
42 add_filter( 'comments_clauses', array( ReviewsUtil::class, 'comments_clauses_without_product_reviews' ), 10, 1 );
43
44 // Count comments.
45 add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 10, 2 );
46
47 // Delete comments count cache whenever there is a new comment or a comment status changes.
48 add_action( 'wp_insert_comment', array( __CLASS__, 'delete_comments_count_cache' ) );
49 add_action( 'wp_set_comment_status', array( __CLASS__, 'delete_comments_count_cache' ) );
50
51 // Support avatars for `review` comment type.
52 add_filter( 'get_avatar_comment_types', array( __CLASS__, 'add_avatar_for_review_comment_type' ) );
53
54 // Add Product Reviews filter for `review` comment type.
55 add_filter( 'admin_comment_types_dropdown', array( __CLASS__, 'add_review_comment_filter' ) );
56
57 // Review of verified purchase.
58 add_action( 'comment_post', array( __CLASS__, 'add_comment_purchase_verification' ) );
59
60 // Set comment type.
61 add_action( 'preprocess_comment', array( __CLASS__, 'update_comment_type' ), 1 );
62
63 // Validate product reviews if requires verified owners.
64 add_action( 'pre_comment_on_post', array( __CLASS__, 'validate_product_review_verified_owners' ) );
65 }
66
67 /**
68 * See if comments are open.
69 *
70 * @since 3.1.0
71 * @param bool $open Whether the current post is open for comments.
72 * @param int $post_id Post ID.
73 * @return bool
74 */
75 public static function comments_open( $open, $post_id ) {
76 if ( 'product' === get_post_type( $post_id ) && ! post_type_supports( 'product', 'comments' ) ) {
77 $open = false;
78 }
79 return $open;
80 }
81
82 /**
83 * Exclude order comments from queries and RSS.
84 *
85 * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
86 * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and.
87 * shop managers can view orders anyway.
88 *
89 * The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
90 *
91 * @param array $clauses A compacted array of comment query clauses.
92 * @return array
93 */
94 public static function exclude_order_comments( $clauses ) {
95 $clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " comment_type != 'order_note' ";
96 return $clauses;
97 }
98
99 /**
100 * Exclude order comments from feed.
101 *
102 * @deprecated 3.1
103 * @param mixed $join Deprecated.
104 */
105 public static function exclude_order_comments_from_feed_join( $join ) {
106 wc_deprecated_function( 'WC_Comments::exclude_order_comments_from_feed_join', '3.1' );
107 }
108
109 /**
110 * Exclude order comments from queries and RSS.
111 *
112 * @param string $where The WHERE clause of the query.
113 * @return string
114 */
115 public static function exclude_order_comments_from_feed_where( $where ) {
116 return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'order_note' ";
117 }
118
119 /**
120 * Exclude webhook comments from queries and RSS.
121 *
122 * @since 2.2
123 * @param array $clauses A compacted array of comment query clauses.
124 * @return array
125 */
126 public static function exclude_webhook_comments( $clauses ) {
127 $clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
128 return $clauses;
129 }
130
131 /**
132 * Exclude webhooks comments from feed.
133 *
134 * @deprecated 3.1
135 * @param mixed $join Deprecated.
136 */
137 public static function exclude_webhook_comments_from_feed_join( $join ) {
138 wc_deprecated_function( 'WC_Comments::exclude_webhook_comments_from_feed_join', '3.1' );
139 }
140
141 /**
142 * Exclude webhook comments from queries and RSS.
143 *
144 * @since 2.1
145 * @param string $where The WHERE clause of the query.
146 * @return string
147 */
148 public static function exclude_webhook_comments_from_feed_where( $where ) {
149 return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
150 }
151
152 /**
153 * Validate the comment ratings.
154 *
155 * @param array $comment_data Comment data.
156 * @return array
157 */
158 public static function check_comment_rating( $comment_data ) {
159 // If posting a comment (not trackback etc) and not logged in.
160 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.
161 wp_die( esc_html__( 'Please rate the product.', 'woocommerce' ) );
162 exit;
163 }
164 return $comment_data;
165 }
166
167 /**
168 * Rating field for comments.
169 *
170 * @param int $comment_id Comment ID.
171 */
172 public static function add_comment_rating( $comment_id ) {
173 if ( isset( $_POST['rating'], $_POST['comment_post_ID'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
174 if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
175 return;
176 }
177 add_comment_meta( $comment_id, 'rating', intval( $_POST['rating'] ), true ); // WPCS: input var ok, CSRF ok.
178
179 $post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // WPCS: input var ok, CSRF ok.
180 if ( $post_id ) {
181 self::clear_transients( $post_id );
182 }
183 }
184 }
185
186 /**
187 * Modify recipient of review email.
188 *
189 * @param array $emails Emails.
190 * @param int $comment_id Comment ID.
191 * @return array
192 */
193 public static function comment_moderation_recipients( $emails, $comment_id ) {
194 $comment = get_comment( $comment_id );
195
196 if ( $comment && 'product' === get_post_type( $comment->comment_post_ID ) ) {
197 $emails = array( get_option( 'admin_email' ) );
198 }
199
200 return $emails;
201 }
202
203 /**
204 * Ensure product average rating and review count is kept up to date.
205 *
206 * @param int $post_id Post ID.
207 */
208 public static function clear_transients( $post_id ) {
209 if ( 'product' === get_post_type( $post_id ) ) {
210 $product = wc_get_product( $post_id );
211 $product->set_rating_counts( self::get_rating_counts_for_product( $product ) );
212 $product->set_average_rating( self::get_average_rating_for_product( $product ) );
213 $product->set_review_count( self::get_review_count_for_product( $product ) );
214 $product->save();
215 }
216 }
217
218 /**
219 * Delete comments count cache whenever there is
220 * new comment or the status of a comment changes. Cache
221 * will be regenerated next time WC_Comments::wp_count_comments()
222 * is called.
223 */
224 public static function delete_comments_count_cache() {
225 delete_transient( 'wc_count_comments' );
226 }
227
228 /**
229 * Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().
230 *
231 * @since 2.2
232 * @param object $stats Comment stats.
233 * @param int $post_id Post ID.
234 * @return object
235 */
236 public static function wp_count_comments( $stats, $post_id ) {
237 global $wpdb;
238
239 if ( 0 === $post_id ) {
240 $stats = get_transient( 'wc_count_comments' );
241
242 if ( ! $stats ) {
243 $stats = array(
244 'total_comments' => 0,
245 'all' => 0,
246 );
247
248 $count = $wpdb->get_results(
249 "
250 SELECT comment_approved, COUNT(*) AS num_comments
251 FROM {$wpdb->comments}
252 LEFT JOIN {$wpdb->posts} ON comment_post_ID = {$wpdb->posts}.ID
253 WHERE comment_type NOT IN ('action_log', 'order_note', 'webhook_delivery') AND {$wpdb->posts}.post_type NOT IN ('product')
254 GROUP BY comment_approved
255 ",
256 ARRAY_A
257 );
258
259 $approved = array(
260 '0' => 'moderated',
261 '1' => 'approved',
262 'spam' => 'spam',
263 'trash' => 'trash',
264 'post-trashed' => 'post-trashed',
265 );
266
267 foreach ( (array) $count as $row ) {
268 // Don't count post-trashed toward totals.
269 if ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash', 'spam' ), true ) ) {
270 $stats['all'] += $row['num_comments'];
271 $stats['total_comments'] += $row['num_comments'];
272 } elseif ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash' ), true ) ) {
273 $stats['total_comments'] += $row['num_comments'];
274 }
275 if ( isset( $approved[ $row['comment_approved'] ] ) ) {
276 $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
277 }
278 }
279
280 foreach ( $approved as $key ) {
281 if ( empty( $stats[ $key ] ) ) {
282 $stats[ $key ] = 0;
283 }
284 }
285
286 $stats = (object) $stats;
287 set_transient( 'wc_count_comments', $stats );
288 }
289 }
290
291 return $stats;
292 }
293
294 /**
295 * Make sure WP displays avatars for comments with the `review` type.
296 *
297 * @since 2.3
298 * @param array $comment_types Comment types.
299 * @return array
300 */
301 public static function add_avatar_for_review_comment_type( $comment_types ) {
302 return array_merge( $comment_types, array( 'review' ) );
303 }
304
305 /**
306 * Add Product Reviews filter for `review` comment type.
307 *
308 * @since 6.0.0
309 *
310 * @param array $comment_types Array of comment type labels keyed by their name.
311 *
312 * @return array
313 */
314 public static function add_review_comment_filter( array $comment_types ): array {
315 $comment_types['review'] = __( 'Product Reviews', 'woocommerce' );
316 return $comment_types;
317 }
318
319 /**
320 * Determine if a review is from a verified owner at submission.
321 *
322 * @param int $comment_id Comment ID.
323 * @return bool
324 */
325 public static function add_comment_purchase_verification( $comment_id ) {
326 $comment = get_comment( $comment_id );
327 $verified = false;
328 if ( 'product' === get_post_type( $comment->comment_post_ID ) ) {
329 $verified = wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $comment->comment_post_ID );
330 add_comment_meta( $comment_id, 'verified', (int) $verified, true );
331 }
332 return $verified;
333 }
334
335 /**
336 * Get product rating for a product. Please note this is not cached.
337 *
338 * @since 3.0.0
339 * @param WC_Product $product Product instance.
340 * @return float
341 */
342 public static function get_average_rating_for_product( &$product ) {
343 global $wpdb;
344
345 $count = $product->get_rating_count();
346
347 if ( $count ) {
348 $ratings = $wpdb->get_var(
349 $wpdb->prepare(
350 "
351 SELECT SUM(meta_value) FROM $wpdb->commentmeta
352 LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
353 WHERE meta_key = 'rating'
354 AND comment_post_ID = %d
355 AND comment_approved = '1'
356 AND meta_value > 0
357 ",
358 $product->get_id()
359 )
360 );
361 $average = number_format( $ratings / $count, 2, '.', '' );
362 } else {
363 $average = 0;
364 }
365
366 return $average;
367 }
368
369 /**
370 * Utility function for getting review counts for multiple products in one query. This is not cached.
371 *
372 * @since 5.0.0
373 *
374 * @param array $product_ids Array of product IDs.
375 *
376 * @return array
377 */
378 public static function get_review_counts_for_product_ids( $product_ids ) {
379 global $wpdb;
380
381 if ( empty( $product_ids ) ) {
382 return array();
383 }
384
385 $product_id_string_placeholder = substr( str_repeat( ',%s', count( $product_ids ) ), 1 );
386
387 $review_counts = $wpdb->get_results(
388 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Ignored for allowing interpolation in IN query.
389 $wpdb->prepare(
390 "
391 SELECT comment_post_ID as product_id, COUNT( comment_post_ID ) as review_count
392 FROM $wpdb->comments
393 WHERE
394 comment_parent = 0
395 AND comment_post_ID IN ( $product_id_string_placeholder )
396 AND comment_approved = '1'
397 AND comment_type in ( 'review', '', 'comment' )
398 GROUP BY product_id
399 ",
400 $product_ids
401 ),
402 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared.
403 ARRAY_A
404 );
405
406 // Convert to key value pairs.
407 $counts = array_replace( array_fill_keys( $product_ids, 0 ), array_column( $review_counts, 'review_count', 'product_id' ) );
408
409 return $counts;
410 }
411
412 /**
413 * Get product review count for a product (not replies). Please note this is not cached.
414 *
415 * @since 3.0.0
416 * @param WC_Product $product Product instance.
417 * @return int
418 */
419 public static function get_review_count_for_product( &$product ) {
420 $counts = self::get_review_counts_for_product_ids( array( $product->get_id() ) );
421
422 return $counts[ $product->get_id() ];
423 }
424
425 /**
426 * Get product rating count for a product. Please note this is not cached.
427 *
428 * @since 3.0.0
429 * @param WC_Product $product Product instance.
430 * @return int[]
431 */
432 public static function get_rating_counts_for_product( &$product ) {
433 global $wpdb;
434
435 $counts = array();
436 $raw_counts = $wpdb->get_results(
437 $wpdb->prepare(
438 "
439 SELECT meta_value, COUNT( * ) as meta_value_count FROM $wpdb->commentmeta
440 LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
441 WHERE meta_key = 'rating'
442 AND comment_post_ID = %d
443 AND comment_approved = '1'
444 AND meta_value > 0
445 GROUP BY meta_value
446 ",
447 $product->get_id()
448 )
449 );
450
451 foreach ( $raw_counts as $count ) {
452 $counts[ $count->meta_value ] = absint( $count->meta_value_count ); // WPCS: slow query ok.
453 }
454
455 return $counts;
456 }
457
458 /**
459 * Update comment type of product reviews.
460 *
461 * @since 3.5.0
462 * @param array $comment_data Comment data.
463 * @return array
464 */
465 public static function update_comment_type( $comment_data ) {
466 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.
467 $comment_data['comment_type'] = 'review';
468 }
469
470 return $comment_data;
471 }
472
473 /**
474 * Validate product reviews if requires a verified owner.
475 *
476 * @param int $comment_post_id Post ID.
477 */
478 public static function validate_product_review_verified_owners( $comment_post_id ) {
479 // Only validate if option is enabled.
480 if ( 'yes' !== get_option( 'woocommerce_review_rating_verification_required' ) ) {
481 return;
482 }
483
484 // Validate only products.
485 if ( 'product' !== get_post_type( $comment_post_id ) ) {
486 return;
487 }
488
489 // Skip if is a verified owner.
490 if ( wc_customer_bought_product( '', get_current_user_id(), $comment_post_id ) ) {
491 return;
492 }
493
494 wp_die(
495 esc_html__( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ),
496 esc_html__( 'Reviews can only be left by "verified owners"', 'woocommerce' ),
497 array(
498 'code' => 403,
499 )
500 );
501 }
502
503 /**
504 * Determines if a comment is of the default type.
505 *
506 * Prior to WordPress 5.5, '' was the default comment type.
507 * As of 5.5, the default type is 'comment'.
508 *
509 * @since 4.3.0
510 * @param string $comment_type Comment type.
511 * @return bool
512 */
513 private static function is_default_comment_type( $comment_type ) {
514 return ( '' === $comment_type || 'comment' === $comment_type );
515 }
516 }
517
518 WC_Comments::init();
519