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