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