WC_REST_Product_Reviews_Controller

REST API Product Reviews Controller Class.

Defined (1)

The class is defined in the following location(s).

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