PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / rest-api / Controllers / Version3 / class-wc-rest-order-notes-controller.php
class-wc-rest-order-notes-controller.php
168 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_V2_Controller
18 */
19 class WC_REST_Order_Notes_Controller extends WC_REST_Order_Notes_V2_Controller {
20
21 /**
22 * Endpoint namespace.
23 *
24 * @var string
25 */
26 protected $namespace = 'wc/v3';
27
28 /**
29 * Prepare a single order note output for response.
30 *
31 * @param WP_Comment $note Order note object.
32 * @param WP_REST_Request $request Request object.
33 * @return WP_REST_Response $response Response data.
34 */
35 public function prepare_item_for_response( $note, $request ) {
36 $data = array(
37 'id' => (int) $note->comment_ID,
38 'author' => __( 'woocommerce', 'woocommerce' ) === $note->comment_author ? 'system' : $note->comment_author,
39 'date_created' => wc_rest_prepare_date_response( $note->comment_date ),
40 'date_created_gmt' => wc_rest_prepare_date_response( $note->comment_date_gmt ),
41 'note' => $note->comment_content,
42 'customer_note' => (bool) get_comment_meta( $note->comment_ID, 'is_customer_note', true ),
43 );
44
45 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
46 $data = $this->add_additional_fields_to_object( $data, $request );
47 $data = $this->filter_response_by_context( $data, $context );
48
49 // Wrap the data in a response object.
50 $response = rest_ensure_response( $data );
51
52 $response->add_links( $this->prepare_links( $note ) );
53
54 /**
55 * Filter order note object returned from the REST API.
56 *
57 * @param WP_REST_Response $response The response object.
58 * @param WP_Comment $note Order note object used to create response.
59 * @param WP_REST_Request $request Request object.
60 */
61 return apply_filters( 'woocommerce_rest_prepare_order_note', $response, $note, $request );
62 }
63
64 /**
65 * Create a single order note.
66 *
67 * @param WP_REST_Request $request Full details about the request.
68 * @return WP_Error|WP_REST_Response
69 */
70 public function create_item( $request ) {
71 if ( ! empty( $request['id'] ) ) {
72 /* translators: %s: post type */
73 return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
74 }
75
76 $order = wc_get_order( (int) $request['order_id'] );
77
78 if ( ! $order || $this->post_type !== $order->get_type() ) {
79 return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
80 }
81
82 // Create the note.
83 $note_id = $order->add_order_note( $request['note'], $request['customer_note'], $request['added_by_user'] );
84
85 if ( ! $note_id ) {
86 return new WP_Error( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again.', 'woocommerce' ), array( 'status' => 500 ) );
87 }
88
89 $note = get_comment( $note_id );
90 $this->update_additional_fields_for_object( $note, $request );
91
92 /**
93 * Fires after a order note is created or updated via the REST API.
94 *
95 * @param WP_Comment $note New order note object.
96 * @param WP_REST_Request $request Request object.
97 * @param boolean $creating True when creating item, false when updating.
98 */
99 do_action( 'woocommerce_rest_insert_order_note', $note, $request, true );
100
101 $request->set_param( 'context', 'edit' );
102 $response = $this->prepare_item_for_response( $note, $request );
103 $response = rest_ensure_response( $response );
104 $response->set_status( 201 );
105 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, str_replace( '(?P<order_id>[\d]+)', $order->get_id(), $this->rest_base ), $note_id ) ) );
106
107 return $response;
108 }
109
110 /**
111 * Get the Order Notes schema, conforming to JSON Schema.
112 *
113 * @return array
114 */
115 public function get_item_schema() {
116 $schema = array(
117 '$schema' => 'http://json-schema.org/draft-04/schema#',
118 'title' => 'order_note',
119 'type' => 'object',
120 'properties' => array(
121 'id' => array(
122 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
123 'type' => 'integer',
124 'context' => array( 'view', 'edit' ),
125 'readonly' => true,
126 ),
127 'author' => array(
128 'description' => __( 'Order note author.', 'woocommerce' ),
129 'type' => 'string',
130 'context' => array( 'view', 'edit' ),
131 'readonly' => true,
132 ),
133 'date_created' => array(
134 'description' => __( "The date the order note was created, in the site's timezone.", 'woocommerce' ),
135 'type' => 'date-time',
136 'context' => array( 'view', 'edit' ),
137 'readonly' => true,
138 ),
139 'date_created_gmt' => array(
140 'description' => __( 'The date the order note was created, as GMT.', 'woocommerce' ),
141 'type' => 'date-time',
142 'context' => array( 'view', 'edit' ),
143 'readonly' => true,
144 ),
145 'note' => array(
146 'description' => __( 'Order note content.', 'woocommerce' ),
147 'type' => 'string',
148 'context' => array( 'view', 'edit' ),
149 ),
150 'customer_note' => array(
151 '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' ),
152 'type' => 'boolean',
153 'default' => false,
154 'context' => array( 'view', 'edit' ),
155 ),
156 'added_by_user' => array(
157 'description' => __( 'If true, this note will be attributed to the current user. If false, the note will be attributed to the system.', 'woocommerce' ),
158 'type' => 'boolean',
159 'default' => false,
160 'context' => array( 'edit' ),
161 ),
162 ),
163 );
164
165 return $this->add_additional_fields_schema( $schema );
166 }
167 }
168