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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. /**
  3. * REST API Product Reviews Controller
  4. *
  5. * Handles requests to /products/<product_id>/reviews.
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce/API
  10. * @since 3.0.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * REST API Product Reviews Controller Class.
  17. *
  18. * @package WooCommerce/API
  19. * @extends WC_REST_Controller
  20. */
  21. class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
  22. /**
  23. * Endpoint namespace.
  24. *
  25. * @var string
  26. */
  27. protected $namespace = 'wc/v1';
  28. /**
  29. * Route base.
  30. *
  31. * @var string
  32. */
  33. protected $rest_base = 'products/(?P<product_id>[\d]+)/reviews';
  34. /**
  35. * Register the routes for product reviews.
  36. */
  37. public function register_routes() {
  38. register_rest_route( $this->namespace, '/' . $this->rest_base, array(
  39. 'args' => array(
  40. 'product_id' => array(
  41. 'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
  42. 'type' => 'integer',
  43. ),
  44. 'id' => array(
  45. 'description' => __( 'Unique identifier for the variation.', 'woocommerce' ),
  46. 'type' => 'integer',
  47. ),
  48. ),
  49. array(
  50. 'methods' => WP_REST_Server::READABLE,
  51. 'callback' => array( $this, 'get_items' ),
  52. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  53. 'args' => $this->get_collection_params(),
  54. ),
  55. array(
  56. 'methods' => WP_REST_Server::CREATABLE,
  57. 'callback' => array( $this, 'create_item' ),
  58. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  59. 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
  60. 'review' => array(
  61. 'required' => true,
  62. 'type' => 'string',
  63. 'description' => __( 'Review content.', 'woocommerce' ),
  64. ),
  65. 'name' => array(
  66. 'required' => true,
  67. 'type' => 'string',
  68. 'description' => __( 'Name of the reviewer.', 'woocommerce' ),
  69. ),
  70. 'email' => array(
  71. 'required' => true,
  72. 'type' => 'string',
  73. 'description' => __( 'Email of the reviewer.', 'woocommerce' ),
  74. ),
  75. ) ),
  76. ),
  77. 'schema' => array( $this, 'get_public_item_schema' ),
  78. ) );
  79. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
  80. 'args' => array(
  81. 'product_id' => array(
  82. 'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ),
  83. 'type' => 'integer',
  84. ),
  85. 'id' => array(
  86. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  87. 'type' => 'integer',
  88. ),
  89. ),
  90. array(
  91. 'methods' => WP_REST_Server::READABLE,
  92. 'callback' => array( $this, 'get_item' ),
  93. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  94. 'args' => array(
  95. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  96. ),
  97. ),
  98. array(
  99. 'methods' => WP_REST_Server::EDITABLE,
  100. 'callback' => array( $this, 'update_item' ),
  101. 'permission_callback' => array( $this, 'update_item_permissions_check' ),
  102. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  103. ),
  104. array(
  105. 'methods' => WP_REST_Server::DELETABLE,
  106. 'callback' => array( $this, 'delete_item' ),
  107. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  108. 'args' => array(
  109. 'force' => array(
  110. 'default' => false,
  111. 'type' => 'boolean',
  112. 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
  113. ),
  114. ),
  115. ),
  116. 'schema' => array( $this, 'get_public_item_schema' ),
  117. ) );
  118. }
  119. /**
  120. * Check whether a given request has permission to read webhook deliveries.
  121. *
  122. * @param WP_REST_Request $request Full details about the request.
  123. * @return WP_Error|boolean
  124. */
  125. public function get_items_permissions_check( $request ) {
  126. if ( ! wc_rest_check_post_permissions( 'product', 'read' ) ) {
  127. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  128. }
  129. return true;
  130. }
  131. /**
  132. * Check if a given request has access to read a product review.
  133. *
  134. * @param WP_REST_Request $request Full details about the request.
  135. * @return WP_Error|boolean
  136. */
  137. public function get_item_permissions_check( $request ) {
  138. $post = get_post( (int) $request['product_id'] );
  139. if ( $post && ! wc_rest_check_post_permissions( 'product', 'read', $post->ID ) ) {
  140. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  141. }
  142. return true;
  143. }
  144. /**
  145. * Check if a given request has access to create a new product review.
  146. *
  147. * @param WP_REST_Request $request Full details about the request.
  148. * @return WP_Error|boolean
  149. */
  150. public function create_item_permissions_check( $request ) {
  151. $post = get_post( (int) $request['product_id'] );
  152. if ( $post && ! wc_rest_check_post_permissions( 'product', 'create', $post->ID ) ) {
  153. return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  154. }
  155. return true;
  156. }
  157. /**
  158. * Check if a given request has access to update a product review.
  159. *
  160. * @param WP_REST_Request $request Full details about the request.
  161. * @return WP_Error|boolean
  162. */
  163. public function update_item_permissions_check( $request ) {
  164. $post = get_post( (int) $request['product_id'] );
  165. if ( $post && ! wc_rest_check_post_permissions( 'product', 'edit', $post->ID ) ) {
  166. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  167. }
  168. return true;
  169. }
  170. /**
  171. * Check if a given request has access to delete a product review.
  172. *
  173. * @param WP_REST_Request $request Full details about the request.
  174. * @return WP_Error|boolean
  175. */
  176. public function delete_item_permissions_check( $request ) {
  177. $post = get_post( (int) $request['product_id'] );
  178. if ( $post && ! wc_rest_check_post_permissions( 'product', 'delete', $post->ID ) ) {
  179. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  180. }
  181. return true;
  182. }
  183. /**
  184. * Get all reviews from a product.
  185. *
  186. * @param WP_REST_Request $request
  187. *
  188. * @return array|WP_Error
  189. */
  190. public function get_items( $request ) {
  191. $product_id = (int) $request['product_id'];
  192. if ( 'product' !== get_post_type( $product_id ) ) {
  193. return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
  194. }
  195. $reviews = get_approved_comments( $product_id );
  196. $data = array();
  197. foreach ( $reviews as $review_data ) {
  198. $review = $this->prepare_item_for_response( $review_data, $request );
  199. $review = $this->prepare_response_for_collection( $review );
  200. $data[] = $review;
  201. }
  202. return rest_ensure_response( $data );
  203. }
  204. /**
  205. * Get a single product review.
  206. *
  207. * @param WP_REST_Request $request Full details about the request.
  208. * @return WP_Error|WP_REST_Response
  209. */
  210. public function get_item( $request ) {
  211. $id = (int) $request['id'];
  212. $product_id = (int) $request['product_id'];
  213. if ( 'product' !== get_post_type( $product_id ) ) {
  214. return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
  215. }
  216. $review = get_comment( $id );
  217. if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
  218. return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
  219. }
  220. $delivery = $this->prepare_item_for_response( $review, $request );
  221. $response = rest_ensure_response( $delivery );
  222. return $response;
  223. }
  224. /**
  225. * Create a product review.
  226. *
  227. * @param WP_REST_Request $request Full details about the request.
  228. * @return WP_Error|WP_REST_Response
  229. */
  230. public function create_item( $request ) {
  231. $product_id = (int) $request['product_id'];
  232. if ( 'product' !== get_post_type( $product_id ) ) {
  233. return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
  234. }
  235. $prepared_review = $this->prepare_item_for_database( $request );
  236. /**
  237. * Filter a product review (comment) before it is inserted via the REST API.
  238. *
  239. * Allows modification of the comment right before it is inserted via `wp_insert_comment`.
  240. *
  241. * @param array $prepared_review The prepared comment data for `wp_insert_comment`.
  242. * @param WP_REST_Request $request Request used to insert the comment.
  243. */
  244. $prepared_review = apply_filters( 'rest_pre_insert_product_review', $prepared_review, $request );
  245. $product_review_id = wp_insert_comment( $prepared_review );
  246. if ( ! $product_review_id ) {
  247. return new WP_Error( 'rest_product_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
  248. }
  249. update_comment_meta( $product_review_id, 'rating', ( ! empty( $request['rating'] ) ? $request['rating'] : '0' ) );
  250. $product_review = get_comment( $product_review_id );
  251. $this->update_additional_fields_for_object( $product_review, $request );
  252. /**
  253. * Fires after a single item is created or updated via the REST API.
  254. *
  255. * @param WP_Comment $product_review Inserted object.
  256. * @param WP_REST_Request $request Request object.
  257. * @param boolean $creating True when creating item, false when updating.
  258. */
  259. do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
  260. $request->set_param( 'context', 'edit' );
  261. $response = $this->prepare_item_for_response( $product_review, $request );
  262. $response = rest_ensure_response( $response );
  263. $response->set_status( 201 );
  264. $base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
  265. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $product_review_id ) ) );
  266. return $response;
  267. }
  268. /**
  269. * Update a single product review.
  270. *
  271. * @param WP_REST_Request $request Full details about the request.
  272. * @return WP_Error|WP_REST_Response
  273. */
  274. public function update_item( $request ) {
  275. $product_review_id = (int) $request['id'];
  276. $product_id = (int) $request['product_id'];
  277. if ( 'product' !== get_post_type( $product_id ) ) {
  278. return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
  279. }
  280. $review = get_comment( $product_review_id );
  281. if ( empty( $product_review_id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
  282. return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
  283. }
  284. $prepared_review = $this->prepare_item_for_database( $request );
  285. $updated = wp_update_comment( $prepared_review );
  286. if ( 0 === $updated ) {
  287. return new WP_Error( 'rest_product_review_failed_edit', __( 'Updating product review failed.', 'woocommerce' ), array( 'status' => 500 ) );
  288. }
  289. if ( ! empty( $request['rating'] ) ) {
  290. update_comment_meta( $product_review_id, 'rating', $request['rating'] );
  291. }
  292. $product_review = get_comment( $product_review_id );
  293. $this->update_additional_fields_for_object( $product_review, $request );
  294. /**
  295. * Fires after a single item is created or updated via the REST API.
  296. *
  297. * @param WP_Comment $comment Inserted object.
  298. * @param WP_REST_Request $request Request object.
  299. * @param boolean $creating True when creating item, false when updating.
  300. */
  301. do_action( "woocommerce_rest_insert_product_review", $product_review, $request, true );
  302. $request->set_param( 'context', 'edit' );
  303. $response = $this->prepare_item_for_response( $product_review, $request );
  304. return rest_ensure_response( $response );
  305. }
  306. /**
  307. * Delete a product review.
  308. *
  309. * @param WP_REST_Request $request Full details about the request
  310. *
  311. * @return bool|WP_Error|WP_REST_Response
  312. */
  313. public function delete_item( $request ) {
  314. $product_review_id = absint( is_array( $request['id'] ) ? $request['id']['id'] : $request['id'] );
  315. $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
  316. $product_review = get_comment( $product_review_id );
  317. if ( empty( $product_review_id ) || empty( $product_review->comment_ID ) || empty( $product_review->comment_post_ID ) ) {
  318. return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
  319. }
  320. /**
  321. * Filter whether a product review is trashable.
  322. *
  323. * Return false to disable trash support for the product review.
  324. *
  325. * @param boolean $supports_trash Whether the object supports trashing.
  326. * @param WP_Post $product_review The object being considered for trashing support.
  327. */
  328. $supports_trash = apply_filters( 'rest_product_review_trashable', ( EMPTY_TRASH_DAYS > 0 ), $product_review );
  329. $request->set_param( 'context', 'edit' );
  330. $response = $this->prepare_item_for_response( $product_review, $request );
  331. if ( $force ) {
  332. $result = wp_delete_comment( $product_review_id, true );
  333. } else {
  334. if ( ! $supports_trash ) {
  335. return new WP_Error( 'rest_trash_not_supported', __( 'The product review does not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
  336. }
  337. if ( 'trash' === $product_review->comment_approved ) {
  338. return new WP_Error( 'rest_already_trashed', __( 'The comment has already been trashed.', 'woocommerce' ), array( 'status' => 410 ) );
  339. }
  340. $result = wp_trash_comment( $product_review->comment_ID );
  341. }
  342. if ( ! $result ) {
  343. return new WP_Error( 'rest_cannot_delete', __( 'The product review cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) );
  344. }
  345. /**
  346. * Fires after a product review is deleted via the REST API.
  347. *
  348. * @param object $product_review The deleted item.
  349. * @param WP_REST_Response $response The response data.
  350. * @param WP_REST_Request $request The request sent to the API.
  351. */
  352. do_action( 'rest_delete_product_review', $product_review, $response, $request );
  353. return $response;
  354. }
  355. /**
  356. * Prepare a single product review output for response.
  357. *
  358. * @param WP_Comment $review Product review object.
  359. * @param WP_REST_Request $request Request object.
  360. * @return WP_REST_Response $response Response data.
  361. */
  362. public function prepare_item_for_response( $review, $request ) {
  363. $data = array(
  364. 'id' => (int) $review->comment_ID,
  365. 'date_created' => wc_rest_prepare_date_response( $review->comment_date_gmt ),
  366. 'review' => $review->comment_content,
  367. 'rating' => (int) get_comment_meta( $review->comment_ID, 'rating', true ),
  368. 'name' => $review->comment_author,
  369. 'email' => $review->comment_author_email,
  370. 'verified' => wc_review_is_from_verified_owner( $review->comment_ID ),
  371. );
  372. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  373. $data = $this->add_additional_fields_to_object( $data, $request );
  374. $data = $this->filter_response_by_context( $data, $context );
  375. // Wrap the data in a response object.
  376. $response = rest_ensure_response( $data );
  377. $response->add_links( $this->prepare_links( $review, $request ) );
  378. /**
  379. * Filter product reviews object returned from the REST API.
  380. *
  381. * @param WP_REST_Response $response The response object.
  382. * @param WP_Comment $review Product review object used to create response.
  383. * @param WP_REST_Request $request Request object.
  384. */
  385. return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request );
  386. }
  387. /**
  388. * Prepare a single product review to be inserted into the database.
  389. *
  390. * @param WP_REST_Request $request Request object.
  391. * @return array|WP_Error $prepared_review
  392. */
  393. protected function prepare_item_for_database( $request ) {
  394. $prepared_review = array( 'comment_approved' => 1, 'comment_type' => 'review' );
  395. if ( isset( $request['id'] ) ) {
  396. $prepared_review['comment_ID'] = (int) $request['id'];
  397. }
  398. if ( isset( $request['review'] ) ) {
  399. $prepared_review['comment_content'] = $request['review'];
  400. }
  401. if ( isset( $request['product_id'] ) ) {
  402. $prepared_review['comment_post_ID'] = (int) $request['product_id'];
  403. }
  404. if ( isset( $request['name'] ) ) {
  405. $prepared_review['comment_author'] = $request['name'];
  406. }
  407. if ( isset( $request['email'] ) ) {
  408. $prepared_review['comment_author_email'] = $request['email'];
  409. }
  410. if ( isset( $request['date_created'] ) ) {
  411. $prepared_review['comment_date'] = $request['date_created'];
  412. }
  413. if ( isset( $request['date_created_gmt'] ) ) {
  414. $prepared_review['comment_date_gmt'] = $request['date_created_gmt'];
  415. }
  416. return apply_filters( 'rest_preprocess_product_review', $prepared_review, $request );
  417. }
  418. /**
  419. * Prepare links for the request.
  420. *
  421. * @param WP_Comment $review Product review object.
  422. * @param WP_REST_Request $request Request object.
  423. * @return array Links for the given product review.
  424. */
  425. protected function prepare_links( $review, $request ) {
  426. $product_id = (int) $request['product_id'];
  427. $base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
  428. $links = array(
  429. 'self' => array(
  430. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $review->comment_ID ) ),
  431. ),
  432. 'collection' => array(
  433. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
  434. ),
  435. 'up' => array(
  436. 'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product_id ) ),
  437. ),
  438. );
  439. return $links;
  440. }
  441. /**
  442. * Get the Product Review's schema, conforming to JSON Schema.
  443. *
  444. * @return array
  445. */
  446. public function get_item_schema() {
  447. $schema = array(
  448. '$schema' => 'http://json-schema.org/draft-04/schema#',
  449. 'title' => 'product_review',
  450. 'type' => 'object',
  451. 'properties' => array(
  452. 'id' => array(
  453. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  454. 'type' => 'integer',
  455. 'context' => array( 'view', 'edit' ),
  456. 'readonly' => true,
  457. ),
  458. 'review' => array(
  459. 'description' => __( 'The content of the review.', 'woocommerce' ),
  460. 'type' => 'string',
  461. 'context' => array( 'view', 'edit' ),
  462. ),
  463. 'date_created' => array(
  464. 'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ),
  465. 'type' => 'date-time',
  466. 'context' => array( 'view', 'edit' ),
  467. ),
  468. 'rating' => array(
  469. 'description' => __( 'Review rating (0 to 5).', 'woocommerce' ),
  470. 'type' => 'integer',
  471. 'context' => array( 'view', 'edit' ),
  472. ),
  473. 'name' => array(
  474. 'description' => __( 'Reviewer name.', 'woocommerce' ),
  475. 'type' => 'string',
  476. 'context' => array( 'view', 'edit' ),
  477. ),
  478. 'email' => array(
  479. 'description' => __( 'Reviewer email.', 'woocommerce' ),
  480. 'type' => 'string',
  481. 'context' => array( 'view', 'edit' ),
  482. ),
  483. 'verified' => array(
  484. 'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ),
  485. 'type' => 'boolean',
  486. 'context' => array( 'view', 'edit' ),
  487. 'readonly' => true,
  488. ),
  489. ),
  490. );
  491. return $this->add_additional_fields_schema( $schema );
  492. }
  493. /**
  494. * Get the query params for collections.
  495. *
  496. * @return array
  497. */
  498. public function get_collection_params() {
  499. return array(
  500. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  501. );
  502. }
  503. }