WC_REST_Order_Notes_Controller

REST API Order Notes controller class.

Defined (1)

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

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