class-wc-comments.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Comments class.
  13. */
  14. class WC_Comments {
  15. /**
  16. * Hook in methods.
  17. */
  18. public static function init() {
  19. // Rating posts.
  20. add_filter( 'comments_open', array( __CLASS__, 'comments_open' ), 10, 2 );
  21. add_filter( 'preprocess_comment', array( __CLASS__, 'check_comment_rating' ), 0 );
  22. add_action( 'comment_post', array( __CLASS__, 'add_comment_rating' ), 1 );
  23. add_action( 'comment_moderation_recipients', array( __CLASS__, 'comment_moderation_recipients' ), 10, 2 );
  24. // Clear transients.
  25. add_action( 'wp_update_comment_count', array( __CLASS__, 'clear_transients' ) );
  26. // Secure order notes.
  27. add_filter( 'comments_clauses', array( __CLASS__, 'exclude_order_comments' ), 10, 1 );
  28. add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_order_comments_from_feed_where' ) );
  29. // Secure webhook comments.
  30. add_filter( 'comments_clauses', array( __CLASS__, 'exclude_webhook_comments' ), 10, 1 );
  31. add_filter( 'comment_feed_where', array( __CLASS__, 'exclude_webhook_comments_from_feed_where' ) );
  32. // Count comments.
  33. add_filter( 'wp_count_comments', array( __CLASS__, 'wp_count_comments' ), 10, 2 );
  34. // Delete comments count cache whenever there is a new comment or a comment status changes.
  35. add_action( 'wp_insert_comment', array( __CLASS__, 'delete_comments_count_cache' ) );
  36. add_action( 'wp_set_comment_status', array( __CLASS__, 'delete_comments_count_cache' ) );
  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. * See if comments are open.
  44. *
  45. * @since 3.1.0
  46. * @param bool $open Whether the current post is open for comments.
  47. * @param int $post_id Post ID.
  48. * @return bool
  49. */
  50. public static function comments_open( $open, $post_id ) {
  51. if ( 'product' === get_post_type( $post_id ) && ! post_type_supports( 'product', 'comments' ) ) {
  52. $open = false;
  53. }
  54. return $open;
  55. }
  56. /**
  57. * Exclude order comments from queries and RSS.
  58. *
  59. * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded.
  60. * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and.
  61. * shop managers can view orders anyway.
  62. *
  63. * The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );
  64. *
  65. * @param array $clauses A compacted array of comment query clauses.
  66. * @return array
  67. */
  68. public static function exclude_order_comments( $clauses ) {
  69. $clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " comment_type != 'order_note' ";
  70. return $clauses;
  71. }
  72. /**
  73. * Exclude order comments from feed.
  74. *
  75. * @deprecated 3.1
  76. * @param mixed $join Deprecated.
  77. */
  78. public static function exclude_order_comments_from_feed_join( $join ) {
  79. wc_deprecated_function( 'WC_Comments::exclude_order_comments_from_feed_join', '3.1' );
  80. }
  81. /**
  82. * Exclude order comments from queries and RSS.
  83. *
  84. * @param string $where The WHERE clause of the query.
  85. * @return string
  86. */
  87. public static function exclude_order_comments_from_feed_where( $where ) {
  88. return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'order_note' ";
  89. }
  90. /**
  91. * Exclude webhook comments from queries and RSS.
  92. *
  93. * @since 2.2
  94. * @param array $clauses A compacted array of comment query clauses.
  95. * @return array
  96. */
  97. public static function exclude_webhook_comments( $clauses ) {
  98. $clauses['where'] .= ( $clauses['where'] ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
  99. return $clauses;
  100. }
  101. /**
  102. * Exclude webhooks comments from feed.
  103. *
  104. * @deprecated 3.1
  105. * @param mixed $join Deprecated.
  106. */
  107. public static function exclude_webhook_comments_from_feed_join( $join ) {
  108. wc_deprecated_function( 'WC_Comments::exclude_webhook_comments_from_feed_join', '3.1' );
  109. }
  110. /**
  111. * Exclude webhook comments from queries and RSS.
  112. *
  113. * @since 2.1
  114. * @param string $where The WHERE clause of the query.
  115. * @return string
  116. */
  117. public static function exclude_webhook_comments_from_feed_where( $where ) {
  118. return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'webhook_delivery' ";
  119. }
  120. /**
  121. * Validate the comment ratings.
  122. *
  123. * @param array $comment_data Comment data.
  124. * @return array
  125. */
  126. public static function check_comment_rating( $comment_data ) {
  127. // If posting a comment (not trackback etc) and not logged in.
  128. 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'] && 'yes' === get_option( 'woocommerce_enable_review_rating' ) && 'yes' === get_option( 'woocommerce_review_rating_required' ) ) { // WPCS: input var ok, CSRF ok.
  129. wp_die( esc_html__( 'Please rate the product.', 'woocommerce' ) );
  130. exit;
  131. }
  132. return $comment_data;
  133. }
  134. /**
  135. * Rating field for comments.
  136. *
  137. * @param int $comment_id Comment ID.
  138. */
  139. public static function add_comment_rating( $comment_id ) {
  140. if ( isset( $_POST['rating'], $_POST['comment_post_ID'] ) && 'product' === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
  141. if ( ! $_POST['rating'] || $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
  142. return;
  143. }
  144. add_comment_meta( $comment_id, 'rating', intval( $_POST['rating'] ), true ); // WPCS: input var ok, CSRF ok.
  145. $post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // WPCS: input var ok, CSRF ok.
  146. if ( $post_id ) {
  147. self::clear_transients( $post_id );
  148. }
  149. }
  150. }
  151. /**
  152. * Modify recipient of review email.
  153. *
  154. * @param array $emails Emails.
  155. * @param int $comment_id Comment ID.
  156. * @return array
  157. */
  158. public static function comment_moderation_recipients( $emails, $comment_id ) {
  159. $comment = get_comment( $comment_id );
  160. if ( $comment && 'product' === get_post_type( $comment->comment_post_ID ) ) {
  161. $emails = array( get_option( 'admin_email' ) );
  162. }
  163. return $emails;
  164. }
  165. /**
  166. * Ensure product average rating and review count is kept up to date.
  167. *
  168. * @param int $post_id Post ID.
  169. */
  170. public static function clear_transients( $post_id ) {
  171. if ( 'product' === get_post_type( $post_id ) ) {
  172. $product = wc_get_product( $post_id );
  173. self::get_rating_counts_for_product( $product );
  174. self::get_average_rating_for_product( $product );
  175. self::get_review_count_for_product( $product );
  176. }
  177. }
  178. /**
  179. * Delete comments count cache whenever there is
  180. * new comment or the status of a comment changes. Cache
  181. * will be regenerated next time WC_Comments::wp_count_comments()
  182. * is called.
  183. */
  184. public static function delete_comments_count_cache() {
  185. delete_transient( 'wc_count_comments' );
  186. }
  187. /**
  188. * Remove order notes and webhook delivery logs from wp_count_comments().
  189. *
  190. * @since 2.2
  191. * @param object $stats Comment stats.
  192. * @param int $post_id Post ID.
  193. * @return object
  194. */
  195. public static function wp_count_comments( $stats, $post_id ) {
  196. global $wpdb;
  197. if ( 0 === $post_id ) {
  198. $stats = get_transient( 'wc_count_comments' );
  199. if ( ! $stats ) {
  200. $stats = array(
  201. 'total_comments' => 0,
  202. 'all' => 0,
  203. );
  204. $count = $wpdb->get_results(
  205. "
  206. SELECT comment_approved, COUNT(*) AS num_comments
  207. FROM {$wpdb->comments}
  208. WHERE comment_type NOT IN ('order_note', 'webhook_delivery')
  209. GROUP BY comment_approved
  210. ", ARRAY_A
  211. );
  212. $approved = array(
  213. '0' => 'moderated',
  214. '1' => 'approved',
  215. 'spam' => 'spam',
  216. 'trash' => 'trash',
  217. 'post-trashed' => 'post-trashed',
  218. );
  219. foreach ( (array) $count as $row ) {
  220. // Don't count post-trashed toward totals.
  221. if ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash', 'spam' ), true ) ) {
  222. $stats['all'] += $row['num_comments'];
  223. $stats['total_comments'] += $row['num_comments'];
  224. } elseif ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash' ), true ) ) {
  225. $stats['total_comments'] += $row['num_comments'];
  226. }
  227. if ( isset( $approved[ $row['comment_approved'] ] ) ) {
  228. $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
  229. }
  230. }
  231. foreach ( $approved as $key ) {
  232. if ( empty( $stats[ $key ] ) ) {
  233. $stats[ $key ] = 0;
  234. }
  235. }
  236. $stats = (object) $stats;
  237. set_transient( 'wc_count_comments', $stats );
  238. }
  239. }
  240. return $stats;
  241. }
  242. /**
  243. * Make sure WP displays avatars for comments with the `review` type.
  244. *
  245. * @since 2.3
  246. * @param array $comment_types Comment types.
  247. * @return array
  248. */
  249. public static function add_avatar_for_review_comment_type( $comment_types ) {
  250. return array_merge( $comment_types, array( 'review' ) );
  251. }
  252. /**
  253. * Determine if a review is from a verified owner at submission.
  254. *
  255. * @param int $comment_id Comment ID.
  256. * @return bool
  257. */
  258. public static function add_comment_purchase_verification( $comment_id ) {
  259. $comment = get_comment( $comment_id );
  260. $verified = false;
  261. if ( 'product' === get_post_type( $comment->comment_post_ID ) ) {
  262. $verified = wc_customer_bought_product( $comment->comment_author_email, $comment->user_id, $comment->comment_post_ID );
  263. add_comment_meta( $comment_id, 'verified', (int) $verified, true );
  264. }
  265. return $verified;
  266. }
  267. /**
  268. * Get product rating for a product. Please note this is not cached.
  269. *
  270. * @since 3.0.0
  271. * @param WC_Product $product Product instance.
  272. * @return float
  273. */
  274. public static function get_average_rating_for_product( &$product ) {
  275. global $wpdb;
  276. $count = $product->get_rating_count();
  277. if ( $count ) {
  278. $ratings = $wpdb->get_var(
  279. $wpdb->prepare(
  280. "
  281. SELECT SUM(meta_value) FROM $wpdb->commentmeta
  282. LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
  283. WHERE meta_key = 'rating'
  284. AND comment_post_ID = %d
  285. AND comment_approved = '1'
  286. AND meta_value > 0
  287. ", $product->get_id()
  288. )
  289. );
  290. $average = number_format( $ratings / $count, 2, '.', '' );
  291. } else {
  292. $average = 0;
  293. }
  294. $product->set_average_rating( $average );
  295. $data_store = $product->get_data_store();
  296. $data_store->update_average_rating( $product );
  297. return $average;
  298. }
  299. /**
  300. * Get product review count for a product (not replies). Please note this is not cached.
  301. *
  302. * @since 3.0.0
  303. * @param WC_Product $product Product instance.
  304. * @return int
  305. */
  306. public static function get_review_count_for_product( &$product ) {
  307. global $wpdb;
  308. $count = $wpdb->get_var(
  309. $wpdb->prepare(
  310. "
  311. SELECT COUNT(*) FROM $wpdb->comments
  312. WHERE comment_parent = 0
  313. AND comment_post_ID = %d
  314. AND comment_approved = '1'
  315. ", $product->get_id()
  316. )
  317. );
  318. $product->set_review_count( $count );
  319. $data_store = $product->get_data_store();
  320. $data_store->update_review_count( $product );
  321. return $count;
  322. }
  323. /**
  324. * Get product rating count for a product. Please note this is not cached.
  325. *
  326. * @since 3.0.0
  327. * @param WC_Product $product Product instance.
  328. * @return int[]
  329. */
  330. public static function get_rating_counts_for_product( &$product ) {
  331. global $wpdb;
  332. $counts = array();
  333. $raw_counts = $wpdb->get_results(
  334. $wpdb->prepare(
  335. "
  336. SELECT meta_value, COUNT( * ) as meta_value_count FROM $wpdb->commentmeta
  337. LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
  338. WHERE meta_key = 'rating'
  339. AND comment_post_ID = %d
  340. AND comment_approved = '1'
  341. AND meta_value > 0
  342. GROUP BY meta_value
  343. ", $product->get_id()
  344. )
  345. );
  346. foreach ( $raw_counts as $count ) {
  347. $counts[ $count->meta_value ] = absint( $count->meta_value_count ); // WPCS: slow query ok.
  348. }
  349. $product->set_rating_counts( $counts );
  350. $data_store = $product->get_data_store();
  351. $data_store->update_rating_counts( $product );
  352. return $counts;
  353. }
  354. }
  355. WC_Comments::init();