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