class-wc-rest-order-notes-controller.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * REST API Order Notes controller
  4. *
  5. * Handles requests to the /orders/<order_id>/notes endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Order Notes controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_ControllerWC_REST_Order_Notes_V1_Controller
  16. */
  17. class WC_REST_Order_Notes_Controller extends WC_REST_Order_Notes_V1_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Get order notes from an order.
  26. *
  27. * @param WP_REST_Request $request Request data.
  28. *
  29. * @return array|WP_Error
  30. */
  31. public function get_items( $request ) {
  32. $order = wc_get_order( (int) $request['order_id'] );
  33. if ( ! $order || $this->post_type !== $order->get_type() ) {
  34. return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
  35. }
  36. $args = array(
  37. 'post_id' => $order->get_id(),
  38. 'approve' => 'approve',
  39. 'type' => 'order_note',
  40. );
  41. // Allow filter by order note type.
  42. if ( 'customer' === $request['type'] ) {
  43. $args['meta_query'] = array( // WPCS: slow query ok.
  44. array(
  45. 'key' => 'is_customer_note',
  46. 'value' => 1,
  47. 'compare' => '=',
  48. ),
  49. );
  50. } elseif ( 'internal' === $request['type'] ) {
  51. $args['meta_query'] = array( // WPCS: slow query ok.
  52. array(
  53. 'key' => 'is_customer_note',
  54. 'compare' => 'NOT EXISTS',
  55. ),
  56. );
  57. }
  58. remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
  59. $notes = get_comments( $args );
  60. add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
  61. $data = array();
  62. foreach ( $notes as $note ) {
  63. $order_note = $this->prepare_item_for_response( $note, $request );
  64. $order_note = $this->prepare_response_for_collection( $order_note );
  65. $data[] = $order_note;
  66. }
  67. return rest_ensure_response( $data );
  68. }
  69. /**
  70. * Prepare a single order note output for response.
  71. *
  72. * @param WP_Comment $note Order note object.
  73. * @param WP_REST_Request $request Request object.
  74. * @return WP_REST_Response $response Response data.
  75. */
  76. public function prepare_item_for_response( $note, $request ) {
  77. $data = array(
  78. 'id' => (int) $note->comment_ID,
  79. 'date_created' => wc_rest_prepare_date_response( $note->comment_date ),
  80. 'date_created_gmt' => wc_rest_prepare_date_response( $note->comment_date_gmt ),
  81. 'note' => $note->comment_content,
  82. 'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
  83. );
  84. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  85. $data = $this->add_additional_fields_to_object( $data, $request );
  86. $data = $this->filter_response_by_context( $data, $context );
  87. // Wrap the data in a response object.
  88. $response = rest_ensure_response( $data );
  89. $response->add_links( $this->prepare_links( $note ) );
  90. /**
  91. * Filter order note object returned from the REST API.
  92. *
  93. * @param WP_REST_Response $response The response object.
  94. * @param WP_Comment $note Order note object used to create response.
  95. * @param WP_REST_Request $request Request object.
  96. */
  97. return apply_filters( 'woocommerce_rest_prepare_order_note', $response, $note, $request );
  98. }
  99. /**
  100. * Get the Order Notes schema, conforming to JSON Schema.
  101. *
  102. * @return array
  103. */
  104. public function get_item_schema() {
  105. $schema = array(
  106. '$schema' => 'http://json-schema.org/draft-04/schema#',
  107. 'title' => 'order_note',
  108. 'type' => 'object',
  109. 'properties' => array(
  110. 'id' => array(
  111. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  112. 'type' => 'integer',
  113. 'context' => array( 'view', 'edit' ),
  114. 'readonly' => true,
  115. ),
  116. 'date_created' => array(
  117. 'description' => __( "The date the order note was created, in the site's timezone.", 'woocommerce' ),
  118. 'type' => 'date-time',
  119. 'context' => array( 'view', 'edit' ),
  120. 'readonly' => true,
  121. ),
  122. 'date_created_gmt' => array(
  123. 'description' => __( 'The date the order note was created, as GMT.', 'woocommerce' ),
  124. 'type' => 'date-time',
  125. 'context' => array( 'view', 'edit' ),
  126. 'readonly' => true,
  127. ),
  128. 'note' => array(
  129. 'description' => __( 'Order note content.', 'woocommerce' ),
  130. 'type' => 'string',
  131. 'context' => array( 'view', 'edit' ),
  132. ),
  133. 'customer_note' => array(
  134. 'description' => __( 'If true, the note will be shown to customers and they will be notified. If false, the note will be for admin reference only.', 'woocommerce' ),
  135. 'type' => 'boolean',
  136. 'default' => false,
  137. 'context' => array( 'view', 'edit' ),
  138. ),
  139. ),
  140. );
  141. return $this->add_additional_fields_schema( $schema );
  142. }
  143. /**
  144. * Get the query params for collections.
  145. *
  146. * @return array
  147. */
  148. public function get_collection_params() {
  149. $params = array();
  150. $params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
  151. $params['type'] = array(
  152. 'default' => 'any',
  153. 'description' => __( 'Limit result to customers or internal notes.', 'woocommerce' ),
  154. 'type' => 'string',
  155. 'enum' => array( 'any', 'customer', 'internal' ),
  156. 'sanitize_callback' => 'sanitize_key',
  157. 'validate_callback' => 'rest_validate_request_arg',
  158. );
  159. return $params;
  160. }
  161. }