abstracts
6 years ago
admin
6 years ago
api
6 years ago
blocks
7 years ago
cli
7 years ago
customizer
8 years ago
data-stores
6 years ago
emails
7 years ago
export
7 years ago
gateways
7 years ago
import
7 years ago
interfaces
7 years ago
legacy
7 years ago
libraries
6 years ago
log-handlers
7 years ago
payment-tokens
8 years ago
queue
7 years ago
shipping
7 years ago
shortcodes
6 years ago
theme-support
7 years ago
tracks
6 years ago
walkers
8 years ago
widgets
6 years ago
class-wc-ajax.php
6 years ago
class-wc-api.php
7 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
7 years ago
class-wc-cart-fees.php
8 years ago
class-wc-cart-session.php
7 years ago
class-wc-cart-totals.php
7 years ago
class-wc-cart.php
7 years ago
class-wc-checkout.php
7 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
7 years ago
class-wc-countries.php
6 years ago
class-wc-coupon.php
7 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
8 years ago
class-wc-customer.php
7 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
7 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
7 years ago
class-wc-download-handler.php
6 years ago
class-wc-emails.php
7 years ago
class-wc-embed.php
8 years ago
class-wc-form-handler.php
6 years ago
class-wc-frontend-scripts.php
7 years ago
class-wc-geo-ip.php
8 years ago
class-wc-geolite-integration.php
7 years ago
class-wc-geolocation.php
7 years ago
class-wc-https.php
8 years ago
class-wc-install.php
6 years ago
class-wc-integrations.php
8 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
8 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
7 years ago
class-wc-order-item-shipping.php
7 years ago
class-wc-order-item-tax.php
8 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
7 years ago
class-wc-payment-gateways.php
7 years ago
class-wc-payment-tokens.php
7 years ago
class-wc-post-data.php
7 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
8 years ago
class-wc-product-variable.php
7 years ago
class-wc-product-variation.php
7 years ago
class-wc-query.php
7 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-session-handler.php
7 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
7 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
7 years ago
class-wc-shortcodes.php
7 years ago
class-wc-structured-data.php
6 years ago
class-wc-tax.php
7 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
7 years ago
class-woocommerce.php
6 years ago
wc-account-functions.php
7 years ago
wc-attribute-functions.php
7 years ago
wc-cart-functions.php
7 years ago
wc-conditional-functions.php
6 years ago
wc-core-functions.php
7 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
7 years ago
wc-order-functions.php
7 years ago
wc-order-item-functions.php
7 years ago
wc-page-functions.php
7 years ago
wc-product-functions.php
7 years ago
wc-rest-functions.php
7 years ago
wc-stock-functions.php
7 years ago
wc-template-functions.php
7 years ago
wc-template-hooks.php
7 years ago
wc-term-functions.php
7 years ago
wc-update-functions.php
7 years ago
wc-user-functions.php
7 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 ('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 |