|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST API Shipping Zones controller |
|
4
|
|
|
* |
|
5
|
|
|
* Handles requests to the /shipping/zones endpoint. |
|
6
|
|
|
* |
|
7
|
|
|
* @author WooThemes |
|
8
|
|
|
* @category API |
|
9
|
|
|
* @package WooCommerce/API |
|
10
|
|
|
* @since 2.7.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* REST API Shipping Zones class. |
|
19
|
|
|
* |
|
20
|
|
|
* @package WooCommerce/API |
|
21
|
|
|
* @extends WC_REST_Shipping_Zones_Controller_Base |
|
22
|
|
|
*/ |
|
23
|
|
|
class WC_REST_Shipping_Zones_Controller extends WC_REST_Shipping_Zones_Controller_Base { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Register the routes for Shipping Zones. |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
public function register_routes() { |
|
|
|
|
|
|
29
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
|
30
|
|
|
array( |
|
31
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
32
|
|
|
'callback' => array( $this, 'get_items' ), |
|
33
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
34
|
|
|
), |
|
35
|
|
|
array( |
|
36
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
37
|
|
|
'callback' => array( $this, 'create_item' ), |
|
38
|
|
|
'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
39
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
|
40
|
|
|
), |
|
41
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
42
|
|
|
) ); |
|
43
|
|
|
|
|
44
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d-]+)', array( |
|
45
|
|
|
array( |
|
46
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
47
|
|
|
'callback' => array( $this, 'get_item' ), |
|
48
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
49
|
|
|
), |
|
50
|
|
|
array( |
|
51
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
|
52
|
|
|
'callback' => array( $this, 'update_item' ), |
|
53
|
|
|
'permission_callback' => array( $this, 'update_items_permissions_check' ), |
|
54
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
55
|
|
|
), |
|
56
|
|
|
array( |
|
57
|
|
|
'methods' => WP_REST_Server::DELETABLE, |
|
58
|
|
|
'callback' => array( $this, 'delete_item' ), |
|
59
|
|
|
'permission_callback' => array( $this, 'delete_items_permissions_check' ), |
|
60
|
|
|
'args' => array( |
|
61
|
|
|
'force' => array( |
|
62
|
|
|
'default' => false, |
|
63
|
|
|
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
|
64
|
|
|
), |
|
65
|
|
|
), |
|
66
|
|
|
), |
|
67
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
68
|
|
|
) ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get a single Shipping Zone. |
|
73
|
|
|
* |
|
74
|
|
|
* @param WP_REST_Request $request |
|
75
|
|
|
* @return WP_REST_Response |
|
76
|
|
|
*/ |
|
77
|
|
|
public function get_item( $request ) { |
|
78
|
|
|
$zone = $this->get_zone( $request->get_param( 'id' ) ); |
|
79
|
|
|
|
|
80
|
|
|
if ( is_wp_error( $zone ) ) { |
|
81
|
|
|
return $zone; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$data = $zone->get_data(); |
|
85
|
|
|
$data = $this->prepare_item_for_response( $data, $request ); |
|
86
|
|
|
$data = $this->prepare_response_for_collection( $data ); |
|
87
|
|
|
|
|
88
|
|
|
return rest_ensure_response( $data ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get all Shipping Zones. |
|
93
|
|
|
* |
|
94
|
|
|
* @param WP_REST_Request $request |
|
95
|
|
|
* @return WP_REST_Response |
|
96
|
|
|
*/ |
|
97
|
|
|
public function get_items( $request ) { |
|
98
|
|
|
$rest_of_the_world = WC_Shipping_Zones::get_zone_by( 'zone_id', 0 ); |
|
99
|
|
|
|
|
100
|
|
|
$zones = WC_Shipping_Zones::get_zones(); |
|
101
|
|
|
array_unshift( $zones, $rest_of_the_world->get_data() ); |
|
102
|
|
|
$data = array(); |
|
103
|
|
|
|
|
104
|
|
|
foreach ( $zones as $zone_obj ) { |
|
105
|
|
|
$zone = $this->prepare_item_for_response( $zone_obj, $request ); |
|
106
|
|
|
$zone = $this->prepare_response_for_collection( $zone ); |
|
107
|
|
|
$data[] = $zone; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return rest_ensure_response( $data ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Create a single Shipping Zone. |
|
115
|
|
|
* |
|
116
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
117
|
|
|
* @return WP_REST_Request|WP_Error |
|
118
|
|
|
*/ |
|
119
|
|
|
public function create_item( $request ) { |
|
120
|
|
|
$zone = new WC_Shipping_Zone( null ); |
|
121
|
|
|
|
|
122
|
|
|
if ( ! is_null( $request->get_param( 'name' ) ) ) { |
|
123
|
|
|
$zone->set_zone_name( $request->get_param( 'name' ) ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ( ! is_null( $request->get_param( 'order' ) ) ) { |
|
127
|
|
|
$zone->set_zone_order( $request->get_param( 'order' ) ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$zone->create(); |
|
131
|
|
|
|
|
132
|
|
|
if ( $zone->get_id() !== 0 ) { |
|
133
|
|
|
$request->set_param( 'id', $zone->get_id() ); |
|
134
|
|
|
$response = $this->get_item( $request ); |
|
135
|
|
|
$response->set_status( 201 ); |
|
136
|
|
|
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $zone->get_id() ) ) ); |
|
137
|
|
|
return $response; |
|
138
|
|
|
} else { |
|
139
|
|
|
return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( "Resource cannot be created. Check to make sure 'order' and 'name' are present.", 'woocommerce' ), array( 'status' => 500 ) ); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Update a single Shipping Zone. |
|
145
|
|
|
* |
|
146
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
147
|
|
|
* @return WP_REST_Request|WP_Error |
|
148
|
|
|
*/ |
|
149
|
|
|
public function update_item( $request ) { |
|
150
|
|
|
$zone = $this->get_zone( $request->get_param( 'id' ) ); |
|
151
|
|
|
|
|
152
|
|
|
if ( is_wp_error( $zone ) ) { |
|
153
|
|
|
return $zone; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$zone_changed = false; |
|
157
|
|
|
|
|
158
|
|
|
if ( ! is_null( $request->get_param( 'name' ) ) ) { |
|
159
|
|
|
$zone->set_zone_name( $request->get_param( 'name' ) ); |
|
160
|
|
|
$zone_changed = true; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ( ! is_null( $request->get_param( 'order' ) ) ) { |
|
164
|
|
|
$zone->set_zone_order( $request->get_param( 'order' ) ); |
|
165
|
|
|
$zone_changed = true; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ( $zone_changed ) { |
|
169
|
|
|
$zone->save(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this->get_item( $request ); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Delete a single Shipping Zone. |
|
177
|
|
|
* |
|
178
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
179
|
|
|
* @return WP_REST_Request|WP_Error |
|
180
|
|
|
*/ |
|
181
|
|
|
public function delete_item( $request ) { |
|
182
|
|
|
$zone = $this->get_zone( $request->get_param( 'id' ) ); |
|
183
|
|
|
|
|
184
|
|
|
if ( is_wp_error( $zone ) ) { |
|
185
|
|
|
return $zone; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$force = $request['force']; |
|
189
|
|
|
|
|
190
|
|
|
$response = $this->get_item( $request ); |
|
191
|
|
|
|
|
192
|
|
View Code Duplication |
if ( $force ) { |
|
|
|
|
|
|
193
|
|
|
$zone->delete(); |
|
194
|
|
|
} else { |
|
195
|
|
|
return new WP_Error( 'rest_trash_not_supported', __( 'Shipping zones do not support trashing.' ), array( 'status' => 501 ) ); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $response; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Prepare the Shipping Zone for the REST response. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $item Shipping Zone. |
|
205
|
|
|
* @param WP_REST_Request $request Request object. |
|
206
|
|
|
* @return WP_REST_Response $response |
|
207
|
|
|
*/ |
|
208
|
|
|
public function prepare_item_for_response( $item, $request ) { |
|
209
|
|
|
$data = array( |
|
210
|
|
|
'id' => (int) $item['zone_id'], |
|
211
|
|
|
'name' => $item['zone_name'], |
|
212
|
|
|
'order' => (int) $item['zone_order'], |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
$context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
216
|
|
|
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
217
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
|
218
|
|
|
|
|
219
|
|
|
// Wrap the data in a response object. |
|
220
|
|
|
$response = rest_ensure_response( $data ); |
|
221
|
|
|
|
|
222
|
|
|
$response->add_links( $this->prepare_links( $data['id'] ) ); |
|
223
|
|
|
|
|
224
|
|
|
return $response; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Prepare links for the request. |
|
229
|
|
|
* |
|
230
|
|
|
* @param int $zone_id Given Shipping Zone ID. |
|
231
|
|
|
* @return array Links for the given Shipping Zone. |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function prepare_links( $zone_id ) { |
|
234
|
|
|
$base = '/' . $this->namespace . '/' . $this->rest_base; |
|
235
|
|
|
$links = array( |
|
236
|
|
|
'self' => array( |
|
237
|
|
|
'href' => rest_url( trailingslashit( $base ) . $zone_id ), |
|
238
|
|
|
), |
|
239
|
|
|
'collection' => array( |
|
240
|
|
|
'href' => rest_url( $base ), |
|
241
|
|
|
), |
|
242
|
|
|
'describedby' => array( |
|
243
|
|
|
'href' => rest_url( trailingslashit( $base ) . $zone_id . '/locations' ), |
|
244
|
|
|
), |
|
245
|
|
|
); |
|
246
|
|
|
|
|
247
|
|
|
return $links; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get the Shipping Zones schema, conforming to JSON Schema |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
public function get_item_schema() { |
|
256
|
|
|
$schema = array( |
|
257
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
258
|
|
|
'title' => 'shipping_zone', |
|
259
|
|
|
'type' => 'object', |
|
260
|
|
|
'properties' => array( |
|
261
|
|
|
'id' => array( |
|
262
|
|
|
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
263
|
|
|
'type' => 'integer', |
|
264
|
|
|
'context' => array( 'view', 'edit' ), |
|
265
|
|
|
'readonly' => true, |
|
266
|
|
|
), |
|
267
|
|
|
'name' => array( |
|
268
|
|
|
'description' => __( 'Shipping zone name.', 'woocommerce' ), |
|
269
|
|
|
'type' => 'string', |
|
270
|
|
|
'context' => array( 'view', 'edit' ), |
|
271
|
|
|
'required' => true, |
|
272
|
|
|
'arg_options' => array( |
|
273
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
|
274
|
|
|
), |
|
275
|
|
|
), |
|
276
|
|
|
'order' => array( |
|
277
|
|
|
'description' => __( 'Shipping zone order.', 'woocommerce' ), |
|
278
|
|
|
'type' => 'integer', |
|
279
|
|
|
'context' => array( 'view', 'edit' ), |
|
280
|
|
|
'required' => false, |
|
281
|
|
|
'arg_options' => array( |
|
282
|
|
|
'sanitize_callback' => 'absint', |
|
283
|
|
|
), |
|
284
|
|
|
), |
|
285
|
|
|
), |
|
286
|
|
|
); |
|
287
|
|
|
|
|
288
|
|
|
return $this->add_additional_fields_schema( $schema ); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.