1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* REST Controller |
4
|
|
|
* |
5
|
|
|
* It's required to follow "Controller Classes" guide before extending this class: |
6
|
|
|
* <https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/> |
7
|
|
|
* |
8
|
|
|
* @class \WC_REST_Controller |
9
|
|
|
* @see https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/ |
10
|
|
|
* @package WooCommerce/RestApi |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4; |
14
|
|
|
|
15
|
|
|
defined( 'ABSPATH' ) || exit; |
16
|
|
|
|
17
|
|
|
use \WP_REST_Controller; |
18
|
|
|
use \WooCommerce\RestApi\Controllers\Version4\Utilities\Permissions; |
19
|
|
|
use \WooCommerce\RestApi\Controllers\Version4\Utilities\BatchTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract Rest Controller Class |
23
|
|
|
* |
24
|
|
|
* @package WooCommerce/RestApi |
25
|
|
|
* @extends WP_REST_Controller |
26
|
|
|
* @version 2.6.0 |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractController extends WP_REST_Controller { |
29
|
|
|
use BatchTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Endpoint namespace. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $namespace = 'wc/v4'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Route base. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $rest_base = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Permission to check. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $resource_type = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Register route for items requests. |
54
|
|
|
* |
55
|
|
|
* @param array $methods Supported methods. read, create. |
56
|
|
|
*/ |
57
|
|
|
protected function register_items_route( $methods = [ 'read', 'create' ] ) { |
58
|
|
|
$routes = []; |
59
|
|
|
$routes['schema'] = [ $this, 'get_public_item_schema' ]; |
60
|
|
|
|
61
|
|
|
if ( in_array( 'read', $methods, true ) ) { |
62
|
|
|
$routes[] = array( |
63
|
|
|
'methods' => \WP_REST_Server::READABLE, |
64
|
|
|
'callback' => array( $this, 'get_items' ), |
65
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
66
|
|
|
'args' => $this->get_collection_params(), |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ( in_array( 'create', $methods, true ) ) { |
71
|
|
|
$routes[] = array( |
72
|
|
|
'methods' => \WP_REST_Server::CREATABLE, |
73
|
|
|
'callback' => array( $this, 'create_item' ), |
74
|
|
|
'permission_callback' => array( $this, 'create_item_permissions_check' ), |
75
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
register_rest_route( |
80
|
|
|
$this->namespace, |
81
|
|
|
'/' . $this->rest_base, |
82
|
|
|
$routes, |
83
|
|
|
true |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Register route for item create/get/delete/update requests. |
89
|
|
|
* |
90
|
|
|
* @param array $methods Supported methods. read, create. |
91
|
|
|
*/ |
92
|
|
|
protected function register_item_route( $methods = [ 'read', 'edit', 'delete' ] ) { |
93
|
|
|
$routes = []; |
94
|
|
|
$routes['schema'] = [ $this, 'get_public_item_schema' ]; |
95
|
|
|
$routes['args'] = [ |
96
|
|
|
'id' => [ |
97
|
|
|
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
98
|
|
|
'type' => 'integer', |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if ( in_array( 'read', $methods, true ) ) { |
103
|
|
|
$routes[] = array( |
104
|
|
|
'methods' => \WP_REST_Server::READABLE, |
105
|
|
|
'callback' => array( $this, 'get_item' ), |
106
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
107
|
|
|
'args' => array( |
108
|
|
|
'context' => $this->get_context_param( |
109
|
|
|
array( |
110
|
|
|
'default' => 'view', |
111
|
|
|
) |
112
|
|
|
), |
113
|
|
|
), |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( in_array( 'edit', $methods, true ) ) { |
118
|
|
|
$routes[] = array( |
119
|
|
|
'methods' => \WP_REST_Server::EDITABLE, |
120
|
|
|
'callback' => array( $this, 'update_item' ), |
121
|
|
|
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
122
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ( in_array( 'delete', $methods, true ) ) { |
127
|
|
|
$routes[] = array( |
128
|
|
|
'methods' => \WP_REST_Server::DELETABLE, |
129
|
|
|
'callback' => array( $this, 'delete_item' ), |
130
|
|
|
'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
131
|
|
|
'args' => array( |
132
|
|
|
'force' => array( |
133
|
|
|
'default' => false, |
134
|
|
|
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
135
|
|
|
'type' => 'boolean', |
136
|
|
|
), |
137
|
|
|
), |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
register_rest_route( |
142
|
|
|
$this->namespace, |
143
|
|
|
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
144
|
|
|
$routes, |
145
|
|
|
true |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Add the schema from additional fields to an schema array. |
151
|
|
|
* |
152
|
|
|
* @param array $schema Schema array. |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function add_additional_fields_schema( $schema ) { |
156
|
|
|
$schema = parent::add_additional_fields_schema( $schema ); |
157
|
|
|
$object_type = $schema['title']; |
158
|
|
|
$schema['properties'] = apply_filters( 'woocommerce_rest_' . $object_type . '_schema', $schema['properties'] ); |
159
|
|
|
return $schema; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check whether a given request has permission to read webhooks. |
166
|
|
|
* |
167
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
168
|
|
|
* @return \WP_Error|boolean |
169
|
|
|
*/ |
170
|
|
|
public function get_items_permissions_check( $request ) { |
171
|
|
|
$permission = Permissions::user_can_list( $this->get_item_title() ); |
172
|
|
|
|
173
|
|
|
if ( false === $permission ) { |
174
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $permission; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Check if a given request has access create webhooks. |
182
|
|
|
* |
183
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
184
|
|
|
* |
185
|
|
|
* @return bool|\WP_Error |
186
|
|
|
*/ |
187
|
|
|
public function create_item_permissions_check( $request ) { |
188
|
|
|
$permission = Permissions::user_can_create( $this->get_item_title() ); |
189
|
|
|
|
190
|
|
|
if ( false === $permission ) { |
191
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $permission; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Check if a given request has access to read a webhook. |
199
|
|
|
* |
200
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
201
|
|
|
* @return \WP_Error|boolean |
202
|
|
|
*/ |
203
|
|
|
public function get_item_permissions_check( $request ) { |
204
|
|
|
$id = $request->get_param( 'id' ); |
205
|
|
|
$permission = Permissions::user_can_read( $this->get_item_title(), $id ); |
206
|
|
|
|
207
|
|
|
if ( false === $permission ) { |
208
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $permission; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Check if a given request has access update a webhook. |
216
|
|
|
* |
217
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
218
|
|
|
* |
219
|
|
|
* @return bool|\WP_Error |
220
|
|
|
*/ |
221
|
|
|
public function update_item_permissions_check( $request ) { |
222
|
|
|
$id = $request->get_param( 'id' ); |
223
|
|
|
$permission = Permissions::user_can_edit( $this->get_item_title(), $id ); |
224
|
|
|
|
225
|
|
|
if ( false === $permission ) { |
226
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $permission; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Check if a given request has access delete a webhook. |
234
|
|
|
* |
235
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
236
|
|
|
* |
237
|
|
|
* @return bool|\WP_Error |
238
|
|
|
*/ |
239
|
|
|
public function delete_item_permissions_check( $request ) { |
240
|
|
|
$id = $request->get_param( 'id' ); |
241
|
|
|
$permission = Permissions::user_can_delete( $this->get_item_title(), $id ); |
242
|
|
|
|
243
|
|
|
if ( false === $permission ) { |
244
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $permission; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Check if a given request has access batch create, update and delete items. |
252
|
|
|
* |
253
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
254
|
|
|
* |
255
|
|
|
* @return bool|\WP_Error |
256
|
|
|
*/ |
257
|
|
|
public function batch_items_permissions_check( $request ) { |
|
|
|
|
258
|
|
|
$permission = Permissions::user_can_batch( $this->get_item_title() ); |
259
|
|
|
|
260
|
|
|
if ( false === $permission ) { |
261
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $permission; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get context for the request. |
269
|
|
|
* |
270
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
protected function get_request_context( $request ) { |
274
|
|
|
return ! empty( $request['context'] ) ? $request['context'] : 'view'; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Prepare a single item for response. |
279
|
|
|
* |
280
|
|
|
* @param mixed $item Object used to create response. |
281
|
|
|
* @param \WP_REST_Request $request Request object. |
282
|
|
|
* @return \WP_REST_Response $response Response data. |
283
|
|
|
*/ |
284
|
|
|
public function prepare_item_for_response( $item, $request ) { |
285
|
|
|
try { |
286
|
|
|
$context = $this->get_request_context( $request ); |
287
|
|
|
$fields = $this->get_fields_for_response( $request ); |
288
|
|
|
$data = $this->get_data_for_response( $item, $request ); |
289
|
|
|
$data = array_intersect_key( $data, array_flip( $fields ) ); |
290
|
|
|
$data = $this->add_additional_fields_to_object( $data, $request ); |
291
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
292
|
|
|
$response = rest_ensure_response( $data ); |
293
|
|
|
$response->add_links( $this->prepare_links( $item, $request ) ); |
294
|
|
|
} catch ( \WC_REST_Exception $e ) { |
295
|
|
|
$response = rest_ensure_response( new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ) ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Filter object returned from the REST API. |
300
|
|
|
* |
301
|
|
|
* @param \WP_REST_Response $response The response object. |
302
|
|
|
* @param mixed $item Object used to create response. |
303
|
|
|
* @param \WP_REST_Request $request Request object. |
304
|
|
|
*/ |
305
|
|
|
return apply_filters( 'woocommerce_rest_prepare_' . $this->get_hook_suffix(), $response, $item, $request ); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Return suffix for item action hooks. |
310
|
|
|
* |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
|
|
protected function get_item_title() { |
314
|
|
|
$schema = $this->get_item_schema(); |
315
|
|
|
return $schema['title']; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Return suffix for item action hooks. |
320
|
|
|
* |
321
|
|
|
* @return string |
322
|
|
|
*/ |
323
|
|
|
protected function get_hook_suffix() { |
324
|
|
|
return $this->get_item_title(); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Get data for this object in the format of this endpoint's schema. |
329
|
|
|
* |
330
|
|
|
* @param mixed $object Object to prepare. |
331
|
|
|
* @param \WP_REST_Request $request Request object. |
332
|
|
|
* @return mixed Array of data in the correct format. |
333
|
|
|
*/ |
334
|
|
|
protected function get_data_for_response( $object, $request ) { |
335
|
|
|
return $object; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Prepare links for the request. |
340
|
|
|
* |
341
|
|
|
* @param mixed $item Object to prepare. |
342
|
|
|
* @param \WP_REST_Request $request Request object. |
343
|
|
|
* @return array |
344
|
|
|
*/ |
345
|
|
|
protected function prepare_links( $item, $request ) { |
346
|
|
|
return array(); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.