class-wc-rest-product-reviews-controller.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * REST API Product Reviews Controller
  4. *
  5. * Handles requests to /products/<product_id>/reviews.
  6. *
  7. * @package WooCommerce/API
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Product Reviews Controller Class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Product_Reviews_V1_Controller
  16. */
  17. class WC_REST_Product_Reviews_Controller extends WC_REST_Product_Reviews_V1_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Route base.
  26. *
  27. * @var string
  28. */
  29. protected $rest_base = 'products/(?P<product_id>[\d]+)/reviews';
  30. /**
  31. * Register the routes for product reviews.
  32. */
  33. public function register_routes() {
  34. parent::register_routes();
  35. register_rest_route(
  36. $this->namespace, '/' . $this->rest_base . '/batch', array(
  37. 'args' => array(
  38. 'product_id' => array(
  39. 'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
  40. 'type' => 'integer',
  41. ),
  42. ),
  43. array(
  44. 'methods' => WP_REST_Server::EDITABLE,
  45. 'callback' => array( $this, 'batch_items' ),
  46. 'permission_callback' => array( $this, 'batch_items_permissions_check' ),
  47. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  48. ),
  49. 'schema' => array( $this, 'get_public_batch_schema' ),
  50. )
  51. );
  52. }
  53. /**
  54. * Check if a given request has access to batch manage product reviews.
  55. *
  56. * @param WP_REST_Request $request Full details about the request.
  57. * @return WP_Error|boolean
  58. */
  59. public function batch_items_permissions_check( $request ) {
  60. if ( ! wc_rest_check_post_permissions( 'product', 'batch' ) ) {
  61. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  62. }
  63. return true;
  64. }
  65. /**
  66. * Prepare a single product review output for response.
  67. *
  68. * @param WP_Comment $review Product review object.
  69. * @param WP_REST_Request $request Request object.
  70. * @return WP_REST_Response $response Response data.
  71. */
  72. public function prepare_item_for_response( $review, $request ) {
  73. $data = array(
  74. 'id' => (int) $review->comment_ID,
  75. 'date_created' => wc_rest_prepare_date_response( $review->comment_date ),
  76. 'date_created_gmt' => wc_rest_prepare_date_response( $review->comment_date_gmt ),
  77. 'review' => $review->comment_content,
  78. 'rating' => (int) get_comment_meta( $review->comment_ID, 'rating', true ),
  79. 'name' => $review->comment_author,
  80. 'email' => $review->comment_author_email,
  81. 'verified' => wc_review_is_from_verified_owner( $review->comment_ID ),
  82. );
  83. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  84. $data = $this->add_additional_fields_to_object( $data, $request );
  85. $data = $this->filter_response_by_context( $data, $context );
  86. // Wrap the data in a response object.
  87. $response = rest_ensure_response( $data );
  88. $response->add_links( $this->prepare_links( $review, $request ) );
  89. /**
  90. * Filter product reviews object returned from the REST API.
  91. *
  92. * @param WP_REST_Response $response The response object.
  93. * @param WP_Comment $review Product review object used to create response.
  94. * @param WP_REST_Request $request Request object.
  95. */
  96. return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request );
  97. }
  98. /**
  99. * Bulk create, update and delete items.
  100. *
  101. * @since 3.0.0
  102. * @param WP_REST_Request $request Full details about the request.
  103. * @return array Of WP_Error or WP_REST_Response.
  104. */
  105. public function batch_items( $request ) {
  106. $items = array_filter( $request->get_params() );
  107. $params = $request->get_url_params();
  108. $product_id = $params['product_id'];
  109. $body_params = array();
  110. foreach ( array( 'update', 'create', 'delete' ) as $batch_type ) {
  111. if ( ! empty( $items[ $batch_type ] ) ) {
  112. $injected_items = array();
  113. foreach ( $items[ $batch_type ] as $item ) {
  114. $injected_items[] = is_array( $item ) ? array_merge( array( 'product_id' => $product_id ), $item ) : $item;
  115. }
  116. $body_params[ $batch_type ] = $injected_items;
  117. }
  118. }
  119. $request = new WP_REST_Request( $request->get_method() );
  120. $request->set_body_params( $body_params );
  121. return parent::batch_items( $request );
  122. }
  123. /**
  124. * Get the Product Review's schema, conforming to JSON Schema.
  125. *
  126. * @return array
  127. */
  128. public function get_item_schema() {
  129. $schema = array(
  130. '$schema' => 'http://json-schema.org/draft-04/schema#',
  131. 'title' => 'product_review',
  132. 'type' => 'object',
  133. 'properties' => array(
  134. 'id' => array(
  135. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  136. 'type' => 'integer',
  137. 'context' => array( 'view', 'edit' ),
  138. 'readonly' => true,
  139. ),
  140. 'review' => array(
  141. 'description' => __( 'The content of the review.', 'woocommerce' ),
  142. 'type' => 'string',
  143. 'context' => array( 'view', 'edit' ),
  144. ),
  145. 'date_created' => array(
  146. 'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ),
  147. 'type' => 'date-time',
  148. 'context' => array( 'view', 'edit' ),
  149. ),
  150. 'date_created_gmt' => array(
  151. 'description' => __( 'The date the review was created, as GMT.', 'woocommerce' ),
  152. 'type' => 'date-time',
  153. 'context' => array( 'view', 'edit' ),
  154. ),
  155. 'rating' => array(
  156. 'description' => __( 'Review rating (0 to 5).', 'woocommerce' ),
  157. 'type' => 'integer',
  158. 'context' => array( 'view', 'edit' ),
  159. ),
  160. 'name' => array(
  161. 'description' => __( 'Reviewer name.', 'woocommerce' ),
  162. 'type' => 'string',
  163. 'context' => array( 'view', 'edit' ),
  164. ),
  165. 'email' => array(
  166. 'description' => __( 'Reviewer email.', 'woocommerce' ),
  167. 'type' => 'string',
  168. 'context' => array( 'view', 'edit' ),
  169. ),
  170. 'verified' => array(
  171. 'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ),
  172. 'type' => 'boolean',
  173. 'context' => array( 'view', 'edit' ),
  174. 'readonly' => true,
  175. ),
  176. ),
  177. );
  178. return $this->add_additional_fields_schema( $schema );
  179. }
  180. }