1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13 14 15
16 class {
17
18 19 20
21 public function __construct() {
22
23 add_filter( 'preprocess_comment', array( $this, 'check_comment_rating' ), 0 );
24 add_action( 'comment_post', array( $this, 'add_comment_rating' ), 1 );
25
26
27 add_action( 'wp_set_comment_status', array( $this, 'clear_transients' ) );
28 add_action( 'edit_comment', array( $this, 'clear_transients' ) );
29
30
31 add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
32 add_action( 'comment_feed_join', array( $this, 'exclude_order_comments_from_feed_join' ) );
33 add_action( 'comment_feed_where', array( $this, 'exclude_order_comments_from_feed_where' ) );
34 }
35
36 37 38 39 40 41 42 43 44 45 46 47
48 public static function ( $clauses ) {
49 global $wpdb, $typenow, $pagenow;
50
51 if ( is_admin() && $typenow == 'shop_order' && current_user_can( 'manage_woocommerce' ) )
52 return $clauses;
53
54 if ( ! $clauses['join'] )
55 $clauses['join'] = '';
56
57 if ( ! strstr( $clauses['join'], "JOIN $wpdb->posts" ) )
58 $clauses['join'] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts.ID ";
59
60 if ( $clauses['where'] )
61 $clauses['where'] .= ' AND ';
62
63 $clauses['where'] .= " $wpdb->posts.post_type NOT IN ('shop_order') ";
64
65 return $clauses;
66 }
67
68 69 70 71 72 73
74 public function ( $join ) {
75 global $wpdb;
76
77 if ( ! strstr( $join, $wpdb->posts ) )
78 $join = " LEFT JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID ";
79
80 return $join;
81 }
82
83 84 85 86 87 88
89 public function ( $where ) {
90 global $wpdb;
91
92 if ( $where )
93 $where .= ' AND ';
94
95 $where .= " $wpdb->posts.post_type NOT IN ('shop_order') ";
96
97 return $where;
98 }
99
100 101 102 103 104 105
106 public function ( $comment_data ) {
107
108 if ( isset( $_POST['rating'] ) && empty( $_POST['rating'] ) && $comment_data['comment_type'] === '' && get_option('woocommerce_review_rating_required') === 'yes' ) {
109 wp_die( __( 'Please rate the product.', 'woocommerce' ) );
110 exit;
111 }
112 return $comment_data;
113 }
114
115 116 117 118 119
120 public function ( $comment_id ) {
121 if ( isset( $_POST['rating'] ) ) {
122
123 if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 )
124 return;
125
126 add_comment_meta( $comment_id, 'rating', (int) esc_attr( $_POST['rating'] ), true );
127
128 $this->clear_transients( $comment_id );
129 }
130 }
131
132 133 134 135 136
137 public function clear_transients( $comment_id ) {
138 $comment = get_comment( $comment_id );
139
140 if ( ! empty( $comment->comment_post_ID ) ) {
141 delete_transient( 'wc_average_rating_' . absint( $comment->comment_post_ID ) );
142 delete_transient( 'wc_rating_count_' . absint( $comment->comment_post_ID ) );
143 }
144 }
145 }
146
147 new WC_Comments();
148