class-wc-meta-box-product-reviews.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Product Reviews
  4. *
  5. * Functions for displaying product reviews data meta box.
  6. *
  7. * @package WooCommerce/Admin/Meta Boxes
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * WC_Meta_Box_Product_Reviews
  12. */
  13. class WC_Meta_Box_Product_Reviews {
  14. /**
  15. * Output the metabox.
  16. *
  17. * @param object $comment Comment being shown.
  18. */
  19. public static function output( $comment ) {
  20. wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
  21. $current = get_comment_meta( $comment->comment_ID, 'rating', true );
  22. ?>
  23. <select name="rating" id="rating">
  24. <?php
  25. for ( $rating = 1; $rating <= 5; $rating ++ ) {
  26. printf( '<option value="%1$s"%2$s>%1$s</option>', $rating, selected( $current, $rating, false ) ); // WPCS: XSS ok.
  27. }
  28. ?>
  29. </select>
  30. <?php
  31. }
  32. /**
  33. * Save meta box data
  34. *
  35. * @param mixed $data Data to save.
  36. * @return mixed
  37. */
  38. public static function save( $data ) {
  39. // Not allowed, return regular value without updating meta.
  40. if ( ! isset( $_POST['woocommerce_meta_nonce'], $_POST['rating'] ) || ! wp_verify_nonce( wp_unslash( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) { // WPCS: input var ok, sanitization ok.
  41. return $data;
  42. }
  43. if ( $_POST['rating'] > 5 || $_POST['rating'] < 0 ) { // WPCS: input var ok.
  44. return $data;
  45. }
  46. $comment_id = $data['comment_ID'];
  47. update_comment_meta( $comment_id, 'rating', intval( wp_unslash( $_POST['rating'] ) ) ); // WPCS: input var ok.
  48. // Return regular value after updating.
  49. return $data;
  50. }
  51. }