2013-08-09 16:11:15 +00:00
< ? php
2015-11-06 09:22:19 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-08-09 16:11:15 +00:00
/**
2015-11-03 13:53:50 +00:00
* Comments
2013-08-09 16:11:15 +00:00
*
2015-11-03 13:31:20 +00:00
* Handle comments ( reviews and order notes ) .
2013-08-09 16:11:15 +00:00
*
2015-02-04 15:59:42 +00:00
* @ class WC_Comments
2015-01-28 17:09:37 +00:00
* @ version 2.3 . 0
* @ package WooCommerce / Classes / Products
* @ category Class
* @ author WooThemes
2013-08-09 16:11:15 +00:00
*/
class WC_Comments {
/**
2015-02-04 15:59:42 +00:00
* Hook in methods .
2013-08-09 16:11:15 +00:00
*/
2014-05-28 13:52:50 +00:00
public static function init () {
2013-08-09 16:11:15 +00:00
// Rating posts
2014-05-28 13:52:50 +00:00
add_filter ( 'preprocess_comment' , array ( __CLASS__ , 'check_comment_rating' ), 0 );
add_action ( 'comment_post' , array ( __CLASS__ , 'add_comment_rating' ), 1 );
2015-05-13 14:47:29 +00:00
add_action ( 'comment_moderation_recipients' , array ( __CLASS__ , 'comment_moderation_recipients' ), 10 , 2 );
2013-08-09 16:11:15 +00:00
2015-02-04 15:59:42 +00:00
// Clear transients
2014-07-03 11:01:51 +00:00
add_action ( 'wp_update_comment_count' , array ( __CLASS__ , 'clear_transients' ) );
2013-08-09 16:11:15 +00:00
// Secure order notes
2013-11-10 19:01:12 +00:00
add_filter ( 'comments_clauses' , array ( __CLASS__ , 'exclude_order_comments' ), 10 , 1 );
2014-05-28 13:52:50 +00:00
add_action ( 'comment_feed_join' , array ( __CLASS__ , 'exclude_order_comments_from_feed_join' ) );
add_action ( 'comment_feed_where' , array ( __CLASS__ , 'exclude_order_comments_from_feed_where' ) );
2014-07-30 20:23:41 +00:00
2014-08-12 15:13:29 +00:00
// Secure webhook comments
2014-07-30 20:23:41 +00:00
add_filter ( 'comments_clauses' , array ( __CLASS__ , 'exclude_webhook_comments' ), 10 , 1 );
add_action ( 'comment_feed_join' , array ( __CLASS__ , 'exclude_webhook_comments_from_feed_join' ) );
add_action ( 'comment_feed_where' , array ( __CLASS__ , 'exclude_webhook_comments_from_feed_where' ) );
2014-08-12 15:13:29 +00:00
// Count comments
add_filter ( 'wp_count_comments' , array ( __CLASS__ , 'wp_count_comments' ), 10 , 2 );
2015-01-20 05:26:33 +00:00
2015-02-04 15:59:42 +00:00
// Support avatars for `review` comment type
2015-01-20 05:26:33 +00:00
add_filter ( 'get_avatar_comment_types' , array ( __CLASS__ , 'add_avatar_for_review_comment_type' ) );
2015-10-07 19:42:26 +00:00
// Review of verified purchase
add_action ( 'comment_post' , array ( __CLASS__ , 'add_comment_purchase_verification' ) );
2013-08-09 16:11:15 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Exclude order comments from queries and RSS .
2013-08-09 16:11:15 +00:00
*
2015-11-03 13:31:20 +00:00
* This code should exclude shop_order comments from queries . Some queries ( like the recent comments widget on the dashboard ) are hardcoded .
* and are not filtered , however , the code current_user_can ( 'read_post' , $comment -> comment_post_ID ) should keep them safe since only admin and .
2013-08-09 16:11:15 +00:00
* shop managers can view orders anyway .
*
2013-11-04 06:34:21 +00:00
* The frontend view order pages get around this filter by using remove_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_order_comments' ), 10 , 1 );
2015-02-04 15:59:42 +00:00
* @ param array $clauses
2013-08-09 16:11:15 +00:00
* @ return array
*/
2013-11-04 06:34:21 +00:00
public static function exclude_order_comments ( $clauses ) {
2014-06-08 20:33:11 +00:00
global $wpdb , $typenow ;
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( is_admin () && in_array ( $typenow , wc_get_order_types () ) && current_user_can ( 'manage_woocommerce' ) ) {
2013-08-09 16:11:15 +00:00
return $clauses ; // Don't hide when viewing orders in admin
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( ! $clauses [ 'join' ] ) {
2013-08-09 16:11:15 +00:00
$clauses [ 'join' ] = '' ;
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( ! strstr ( $clauses [ 'join' ], " JOIN $wpdb->posts " ) ) {
2013-08-09 16:11:15 +00:00
$clauses [ 'join' ] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts .ID " ;
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
if ( $clauses [ 'where' ] ) {
2013-08-09 16:11:15 +00:00
$clauses [ 'where' ] .= ' AND ' ;
2014-08-12 15:07:23 +00:00
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:05:09 +00:00
$clauses [ 'where' ] .= " $wpdb->posts .post_type NOT IN (' " . implode ( " ',' " , wc_get_order_types () ) . " ') " ;
2013-08-09 16:11:15 +00:00
return $clauses ;
}
/**
2015-11-03 13:31:20 +00:00
* Exclude order comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ param string $join
2013-08-09 16:11:15 +00:00
* @ return string
*/
2014-05-28 13:52:50 +00:00
public static function exclude_order_comments_from_feed_join ( $join ) {
2013-08-09 16:11:15 +00:00
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( ! strstr ( $join , $wpdb -> posts ) ) {
$join = " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
return $join ;
2013-08-09 16:11:15 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Exclude order comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ param string $where
2013-08-09 16:11:15 +00:00
* @ return string
*/
2014-05-28 13:52:50 +00:00
public static function exclude_order_comments_from_feed_where ( $where ) {
2013-08-09 16:11:15 +00:00
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( $where ) {
$where .= ' AND ' ;
}
2013-08-09 16:11:15 +00:00
2014-08-12 15:05:09 +00:00
$where .= " $wpdb->posts .post_type NOT IN (' " . implode ( " ',' " , wc_get_order_types () ) . " ') " ;
2013-08-09 16:11:15 +00:00
2014-08-12 15:07:23 +00:00
return $where ;
2013-08-09 16:11:15 +00:00
}
2014-07-30 20:23:41 +00:00
/**
2015-11-03 13:31:20 +00:00
* Exclude webhook comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ since 2.2
* @ param array $clauses
2014-07-30 20:23:41 +00:00
* @ return array
*/
public static function exclude_webhook_comments ( $clauses ) {
global $wpdb ;
if ( ! $clauses [ 'join' ] ) {
$clauses [ 'join' ] = '' ;
}
if ( ! strstr ( $clauses [ 'join' ], " JOIN $wpdb->posts " ) ) {
$clauses [ 'join' ] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts .ID " ;
}
if ( $clauses [ 'where' ] ) {
$clauses [ 'where' ] .= ' AND ' ;
}
$clauses [ 'where' ] .= " $wpdb->posts .post_type <> 'shop_webhook' " ;
return $clauses ;
}
/**
2015-11-03 13:31:20 +00:00
* Exclude webhook comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ since 2.2
* @ param string $join
2014-07-30 20:23:41 +00:00
* @ return string
*/
public static function exclude_webhook_comments_from_feed_join ( $join ) {
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( ! strstr ( $join , $wpdb -> posts ) ) {
2014-07-30 20:23:41 +00:00
$join = " LEFT JOIN $wpdb->posts ON $wpdb->comments .comment_post_ID = $wpdb->posts .ID " ;
2014-08-12 15:07:23 +00:00
}
2014-07-30 20:23:41 +00:00
return $join ;
}
/**
2015-11-03 13:31:20 +00:00
* Exclude webhook comments from queries and RSS .
2015-02-04 15:59:42 +00:00
* @ since 2.1
* @ param string $where
2014-07-30 20:23:41 +00:00
* @ return string
*/
public static function exclude_webhook_comments_from_feed_where ( $where ) {
global $wpdb ;
2014-08-12 15:07:23 +00:00
if ( $where ) {
2014-07-30 20:23:41 +00:00
$where .= ' AND ' ;
2014-08-12 15:07:23 +00:00
}
2014-07-30 20:23:41 +00:00
$where .= " $wpdb->posts .post_type <> 'shop_webhook' " ;
return $where ;
}
2013-08-09 16:11:15 +00:00
/**
* Validate the comment ratings .
2015-02-04 15:59:42 +00:00
* @ param array $comment_data
2013-08-09 16:11:15 +00:00
* @ return array
*/
2014-05-28 13:52:50 +00:00
public static function check_comment_rating ( $comment_data ) {
2013-08-09 16:11:15 +00:00
// If posting a comment (not trackback etc) and not logged in
2015-06-03 19:57:36 +00:00
if (
! is_admin ()
&& 'product' === get_post_type ( $_POST [ 'comment_post_ID' ] )
&& empty ( $_POST [ 'rating' ] )
&& '' === $comment_data [ 'comment_type' ]
&& 'yes' === get_option ( 'woocommerce_enable_review_rating' )
&& 'yes' === get_option ( 'woocommerce_review_rating_required' )
) {
2013-08-09 16:11:15 +00:00
wp_die ( __ ( 'Please rate the product.' , 'woocommerce' ) );
exit ;
}
2015-01-28 17:09:37 +00:00
2013-08-09 16:11:15 +00:00
return $comment_data ;
}
/**
* Rating field for comments .
2015-01-28 17:09:37 +00:00
* @ param int $comment_id
2013-08-09 16:11:15 +00:00
*/
2014-05-28 13:52:50 +00:00
public static function add_comment_rating ( $comment_id ) {
2015-02-11 23:06:18 +00:00
if ( isset ( $_POST [ 'rating' ] ) && 'product' === get_post_type ( $_POST [ 'comment_post_ID' ] ) ) {
2014-07-03 11:01:51 +00:00
if ( ! $_POST [ 'rating' ] || $_POST [ 'rating' ] > 5 || $_POST [ 'rating' ] < 0 ) {
2013-08-09 16:11:15 +00:00
return ;
2014-07-03 11:01:51 +00:00
}
2013-08-09 16:11:15 +00:00
add_comment_meta ( $comment_id , 'rating' , ( int ) esc_attr ( $_POST [ 'rating' ] ), true );
}
}
2015-05-13 14:47:29 +00:00
/**
* Modify recipient of review email .
* @ return array
*/
public static function comment_moderation_recipients ( $emails , $comment_id ) {
$comment = get_comment ( $comment_id );
if ( $comment && 'product' === get_post_type ( $comment -> comment_post_ID ) ) {
$emails = array ( get_option ( 'admin_email' ) );
}
return $emails ;
}
2013-08-09 16:11:15 +00:00
/**
* Clear transients for a review .
2015-01-28 17:09:37 +00:00
* @ param int $post_id
2013-08-09 16:11:15 +00:00
*/
2014-07-03 11:01:51 +00:00
public static function clear_transients ( $post_id ) {
2014-09-17 08:03:10 +00:00
$post_id = absint ( $post_id );
2015-01-28 17:09:37 +00:00
$transient_version = WC_Cache_Helper :: get_transient_version ( 'product' );
delete_transient ( 'wc_average_rating_' . $post_id . $transient_version );
delete_transient ( 'wc_rating_count_' . $post_id . $transient_version );
2015-02-11 22:55:16 +00:00
delete_transient ( 'wc_review_count_' . $post_id . $transient_version );
2013-08-09 16:11:15 +00:00
}
2014-08-12 15:13:29 +00:00
/**
2015-11-03 13:31:20 +00:00
* Remove order notes from wp_count_comments () .
2015-02-04 15:59:42 +00:00
* @ since 2.2
* @ param object $stats
* @ param int $post_id
2014-08-12 15:13:29 +00:00
* @ return object
*/
public static function wp_count_comments ( $stats , $post_id ) {
global $wpdb ;
if ( 0 === $post_id ) {
$count = wp_cache_get ( 'comments-0' , 'counts' );
if ( false !== $count ) {
return $count ;
}
$count = $wpdb -> get_results ( " SELECT comment_approved, COUNT( * ) AS num_comments FROM { $wpdb -> comments } WHERE comment_type != 'order_note' GROUP BY comment_approved " , ARRAY_A );
$total = 0 ;
$approved = array ( '0' => 'moderated' , '1' => 'approved' , 'spam' => 'spam' , 'trash' => 'trash' , 'post-trashed' => 'post-trashed' );
foreach ( ( array ) $count as $row ) {
// Don't count post-trashed toward totals
if ( 'post-trashed' != $row [ 'comment_approved' ] && 'trash' != $row [ 'comment_approved' ] ) {
$total += $row [ 'num_comments' ];
}
if ( isset ( $approved [ $row [ 'comment_approved' ] ] ) ) {
$stats [ $approved [ $row [ 'comment_approved' ] ] ] = $row [ 'num_comments' ];
}
}
$stats [ 'total_comments' ] = $total ;
foreach ( $approved as $key ) {
if ( empty ( $stats [ $key ] ) ) {
$stats [ $key ] = 0 ;
}
}
$stats = ( object ) $stats ;
wp_cache_set ( 'comments-0' , $stats , 'counts' );
}
return $stats ;
}
2015-01-20 05:26:33 +00:00
/**
2015-11-03 13:31:20 +00:00
* Make sure WP displays avatars for comments with the `review` type .
2015-02-04 15:59:42 +00:00
* @ since 2.3
* @ param array $comment_types
2015-01-20 05:26:33 +00:00
* @ return array
*/
public static function add_avatar_for_review_comment_type ( $comment_types ) {
return array_merge ( $comment_types , array ( 'review' ) );
}
2015-10-07 19:42:26 +00:00
/**
* Determine if a review is from a verified owner at submission .
* @ param int $comment_id
2015-10-09 16:21:40 +00:00
* @ return bool
2015-10-07 19:42:26 +00:00
*/
public static function add_comment_purchase_verification ( $comment_id ) {
2015-10-09 16:21:40 +00:00
$comment = get_comment ( $comment_id );
$verified = false ;
2015-10-07 19:42:26 +00:00
if ( 'product' === get_post_type ( $comment -> comment_post_ID ) ) {
$verified = wc_customer_bought_product ( $comment -> comment_author_email , $comment -> user_id , $comment -> comment_post_ID );
add_comment_meta ( $comment_id , 'verified' , ( int ) $verified , true );
}
2015-10-09 16:21:40 +00:00
return $verified ;
2015-10-07 19:42:26 +00:00
}
2013-08-09 16:11:15 +00:00
}
2014-05-28 13:52:50 +00:00
WC_Comments :: init ();