/includes/class-wc-comments.php

https://gitlab.com/0072016/woocommerce · PHP · 314 lines · 160 code · 55 blank · 99 comment · 37 complexity · 35013369eddd69388d9b50a688994f56 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * Comments
  7. *
  8. * Handle comments (reviews and order notes).
  9. *
  10. * @class WC_Comments
  11. * @version 2.3.0
  12. * @package WooCommerce/Classes/Products
  13. * @category Class
  14. * @author WooThemes
  15. */
  16. class WC_Comments {
  17. /**
  18. * Hook in methods.
  19. */
  20. public static function init() {
  21. // Rating posts
  22. add_filter( 'preprocess_comment', array( __CLASS__, 'check_comment_rating' ), 0 );
  23. add_action( 'comment_post', array( __CLASS__, 'add_comment_rating' ), 1 );
  24. add_action( 'comment_moderation_recipients', array( __CLASS__, 'comment_moderation_recipients' ), 10, 2 );
  25. // Clear transients
  26. add_action( 'wp_update_comment_count', array( __CLASS__, 'clear_transients' ) );
  27. // Secure order notes
  28. add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
  29. add_action( 'comment_feed_join', array( __CLASS__, 'exclude_order_comments_from_feed_join' ) );
  30. add_action( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
  31. // Secure webhook comments
  32. add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
  33. add_action( 'comment_feed_join', array( __CLASS__, 'exclude_webhook_comments_from_feed_join' ) );
  34. add_action( 'comment_feed_where', array( __CLASS__, 'exclude_webhook_comments_from_feed_where' ) );
  35. // Count comments
  36. add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 10, 2 );
  37. // Support avatars for `review` comment type
  38. add_filter( 'get_avatar_comment_types', array( __CLASS__, 'add_avatar_for_review_comment_type' ) );
  39. // Review of verified purchase
  40. add_action( 'comment_post', array( __CLASS__, 'add_comment_purchase_verification' ) );
  41. }
  42. /**
  43. * Exclude order comments from queries and RSS.
  44. *
  45. * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
  46. * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and.
  47. * shop managers can view orders anyway.
  48. *
  49. * The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
  50. * @param array $clauses
  51. * @return array
  52. */
  53. public static function exclude_order_comments( $clauses ) {
  54. global $wpdb, $typenow;
  55. if ( is_admin() && in_array( $typenow, wc_get_order_types() ) && current_user_can( 'manage_woocommerce' ) ) {
  56. return $clauses; // Don't hide when viewing orders in admin
  57. }
  58. if ( ! $clauses['join'] ) {
  59. $clauses['join'] = '';
  60. }
  61. if ( ! strstr( $clauses['join'], "JOIN $wpdb->posts" ) ) {
  62. $clauses['join'] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts.ID ";
  63. }
  64. if ( $clauses['where'] ) {
  65. $clauses['where'] .= ' AND ';
  66. }
  67. $clauses['where'] .= " $wpdb->posts.post_type NOT IN ('" . implode( "','", wc_get_order_types() ) . "') ";
  68. return $clauses;
  69. }
  70. /**
  71. * Exclude order comments from queries and RSS.
  72. * @param string $join
  73. * @return string
  74. */
  75. public static function exclude_order_comments_from_feed_join( $join ) {
  76. global $wpdb;
  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. * Exclude order comments from queries and RSS.
  84. * @param string $where
  85. * @return string
  86. */
  87. public static function exclude_order_comments_from_feed_where( $where ) {
  88. global $wpdb;
  89. if ( $where ) {
  90. $where .= ' AND ';
  91. }
  92. $where .= " $wpdb->posts.post_type NOT IN ('" . implode( "','", wc_get_order_types() ) . "') ";
  93. return $where;
  94. }
  95. /**
  96. * Exclude webhook comments from queries and RSS.
  97. * @since 2.2
  98. * @param array $clauses
  99. * @return array
  100. */
  101. public static function exclude_webhook_comments( $clauses ) {
  102. global $wpdb;
  103. if ( ! $clauses['join'] ) {
  104. $clauses['join'] = '';
  105. }
  106. if ( ! strstr( $clauses['join'], "JOIN $wpdb->posts" ) ) {
  107. $clauses['join'] .= " LEFT JOIN $wpdb->posts ON comment_post_ID = $wpdb->posts.ID ";
  108. }
  109. if ( $clauses['where'] ) {
  110. $clauses['where'] .= ' AND ';
  111. }
  112. $clauses['where'] .= " $wpdb->posts.post_type <> 'shop_webhook' ";
  113. return $clauses;
  114. }
  115. /**
  116. * Exclude webhook comments from queries and RSS.
  117. * @since 2.2
  118. * @param string $join
  119. * @return string
  120. */
  121. public static function exclude_webhook_comments_from_feed_join( $join ) {
  122. global $wpdb;
  123. if ( ! strstr( $join, $wpdb->posts ) ) {
  124. $join = " LEFT JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID ";
  125. }
  126. return $join;
  127. }
  128. /**
  129. * Exclude webhook comments from queries and RSS.
  130. * @since 2.1
  131. * @param string $where
  132. * @return string
  133. */
  134. public static function exclude_webhook_comments_from_feed_where( $where ) {
  135. global $wpdb;
  136. if ( $where ) {
  137. $where .= ' AND ';
  138. }
  139. $where .= " $wpdb->posts.post_type <> 'shop_webhook' ";
  140. return $where;
  141. }
  142. /**
  143. * Validate the comment ratings.
  144. * @param array $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 (
  150. ! is_admin()
  151. && 'product' === get_post_type( $_POST['comment_post_ID'] )
  152. && empty( $_POST['rating'] )
  153. && '' === $comment_data['comment_type']
  154. && 'yes' === get_option( 'woocommerce_enable_review_rating' )
  155. && 'yes' === get_option( 'woocommerce_review_rating_required' )
  156. ) {
  157. wp_die( __( 'Please rate the product.', 'woocommerce' ) );
  158. exit;
  159. }
  160. return $comment_data;
  161. }
  162. /**
  163. * Rating field for comments.
  164. * @param int $comment_id
  165. */
  166. public static function add_comment_rating( $comment_id ) {
  167. if ( isset( $_POST['rating'] ) && 'product' === get_post_type( $_POST['comment_post_ID'] ) ) {
  168. if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) {
  169. return;
  170. }
  171. add_comment_meta( $comment_id, 'rating', (int) esc_attr( $_POST['rating'] ), true );
  172. }
  173. }
  174. /**
  175. * Modify recipient of review email.
  176. * @param array $emails
  177. * @param int $comment_id
  178. * @return array
  179. */
  180. public static function comment_moderation_recipients( $emails, $comment_id ) {
  181. $comment = get_comment( $comment_id );
  182. if ( $comment && 'product' === get_post_type( $comment->comment_post_ID ) ) {
  183. $emails = array( get_option( 'admin_email' ) );
  184. }
  185. return $emails;
  186. }
  187. /**
  188. * Ensure product average rating and review count is kept up to date.
  189. * @param int $post_id
  190. */
  191. public static function clear_transients( $post_id ) {
  192. delete_post_meta( $post_id, '_wc_average_rating' );
  193. delete_post_meta( $post_id, '_wc_rating_count' );
  194. delete_post_meta( $post_id, '_wc_review_count' );
  195. WC_Product::sync_average_rating( $post_id );
  196. }
  197. /**
  198. * Remove order notes from wp_count_comments().
  199. * @since 2.2
  200. * @param object $stats
  201. * @param int $post_id
  202. * @return object
  203. */
  204. public static function wp_count_comments( $stats, $post_id ) {
  205. global $wpdb;
  206. if ( 0 === $post_id ) {
  207. $count = wp_cache_get( 'comments-0', 'counts' );
  208. if ( false !== $count ) {
  209. return $count;
  210. }
  211. $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 );
  212. $total = 0;
  213. $approved = array( '0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed' );
  214. foreach ( (array) $count as $row ) {
  215. // Don't count post-trashed toward totals
  216. if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
  217. $total += $row['num_comments'];
  218. }
  219. if ( isset( $approved[ $row['comment_approved'] ] ) ) {
  220. $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
  221. }
  222. }
  223. $stats['total_comments'] = $total;
  224. $stats['all'] = $total;
  225. foreach ( $approved as $key ) {
  226. if ( empty( $stats[ $key ] ) ) {
  227. $stats[ $key ] = 0;
  228. }
  229. }
  230. $stats = (object) $stats;
  231. wp_cache_set( 'comments-0', $stats, 'counts' );
  232. }
  233. return $stats;
  234. }
  235. /**
  236. * Make sure WP displays avatars for comments with the `review` type.
  237. * @since 2.3
  238. * @param array $comment_types
  239. * @return array
  240. */
  241. public static function add_avatar_for_review_comment_type( $comment_types ) {
  242. return array_merge( $comment_types, array( 'review' ) );
  243. }
  244. /**
  245. * Determine if a review is from a verified owner at submission.
  246. * @param int $comment_id
  247. * @return bool
  248. */
  249. public static function add_comment_purchase_verification( $comment_id ) {
  250. $comment = get_comment( $comment_id );
  251. $verified = false;
  252. if ( 'product' === get_post_type( $comment->comment_post_ID ) ) {
  253. $verified = wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $comment->comment_post_ID );
  254. add_comment_meta( $comment_id, 'verified', (int) $verified, true );
  255. }
  256. return $verified;
  257. }
  258. }
  259. WC_Comments::init();