|
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
|
|
|
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, 'update_items_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
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
57
|
|
|
) ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get a single Shipping Zone. |
|
62
|
|
|
* |
|
63
|
|
|
* @param WP_REST_Request $request |
|
64
|
|
|
* @return WP_REST_Response |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get_item( $request ) { |
|
67
|
|
|
$zone = $this->get_zone( $request->get_param( 'id' ) ); |
|
68
|
|
|
|
|
69
|
|
|
if ( is_wp_error( $zone ) ) { |
|
70
|
|
|
return $zone; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$data = $zone->get_data(); |
|
74
|
|
|
$data = $this->prepare_item_for_response( $data, $request ); |
|
75
|
|
|
$data = $this->prepare_response_for_collection( $data ); |
|
76
|
|
|
|
|
77
|
|
|
return rest_ensure_response( $data ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get all Shipping Zones. |
|
82
|
|
|
* |
|
83
|
|
|
* @param WP_REST_Request $request |
|
84
|
|
|
* @return WP_REST_Response |
|
85
|
|
|
*/ |
|
86
|
|
|
public function get_items( $request ) { |
|
87
|
|
|
$rest_of_the_world = WC_Shipping_Zones::get_zone_by( 'zone_id', 0 ); |
|
88
|
|
|
|
|
89
|
|
|
$zones = WC_Shipping_Zones::get_zones(); |
|
90
|
|
|
array_unshift( $zones, $rest_of_the_world->get_data() ); |
|
91
|
|
|
$data = array(); |
|
92
|
|
|
|
|
93
|
|
|
foreach ( $zones as $zone_obj ) { |
|
94
|
|
|
$zone = $this->prepare_item_for_response( $zone_obj, $request ); |
|
95
|
|
|
$zone = $this->prepare_response_for_collection( $zone ); |
|
96
|
|
|
$data[] = $zone; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return rest_ensure_response( $data ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Create a single Shipping Zone. |
|
104
|
|
|
* |
|
105
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
106
|
|
|
* @return WP_REST_Request|WP_Error |
|
107
|
|
|
*/ |
|
108
|
|
|
public function create_item( $request ) { |
|
109
|
|
|
$zone = new WC_Shipping_Zone( null ); |
|
110
|
|
|
|
|
111
|
|
|
if ( ! is_null( $request->get_param( 'name' ) ) ) { |
|
112
|
|
|
$zone->set_zone_name( $request->get_param( 'name' ) ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ( ! is_null( $request->get_param( 'order' ) ) ) { |
|
116
|
|
|
$zone->set_zone_order( $request->get_param( 'order' ) ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$zone->create(); |
|
120
|
|
|
$request->set_param( 'id', $zone->get_id() ); |
|
121
|
|
|
|
|
122
|
|
|
return $this->get_item( $request ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Update a single Shipping Zone. |
|
127
|
|
|
* |
|
128
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
129
|
|
|
* @return WP_REST_Request|WP_Error |
|
130
|
|
|
*/ |
|
131
|
|
|
public function update_item( $request ) { |
|
132
|
|
|
$zone = $this->get_zone( $request->get_param( 'id' ) ); |
|
133
|
|
|
|
|
134
|
|
|
if ( is_wp_error( $zone ) ) { |
|
135
|
|
|
return $zone; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$zone_changed = false; |
|
139
|
|
|
|
|
140
|
|
|
if ( ! is_null( $request->get_param( 'name' ) ) ) { |
|
141
|
|
|
$zone->set_zone_name( $request->get_param( 'name' ) ); |
|
142
|
|
|
$zone_changed = true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ( ! is_null( $request->get_param( 'order' ) ) ) { |
|
146
|
|
|
$zone->set_zone_order( $request->get_param( 'order' ) ); |
|
147
|
|
|
$zone_changed = true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if ( $zone_changed ) { |
|
151
|
|
|
$zone->save(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $this->get_item( $request ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Prepare the Shipping Zone for the REST response. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $item Shipping Zone. |
|
161
|
|
|
* @param WP_REST_Request $request Request object. |
|
162
|
|
|
* @return WP_REST_Response $response |
|
163
|
|
|
*/ |
|
164
|
|
|
public function prepare_item_for_response( $item, $request ) { |
|
165
|
|
|
$data = array( |
|
166
|
|
|
'id' => (int) $item['zone_id'], |
|
167
|
|
|
'name' => $item['zone_name'], |
|
168
|
|
|
'order' => (int) $item['zone_order'], |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
$context = empty( $request['context'] ) ? 'view' : $request['context']; |
|
172
|
|
|
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
173
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
|
174
|
|
|
|
|
175
|
|
|
// Wrap the data in a response object. |
|
176
|
|
|
$response = rest_ensure_response( $data ); |
|
177
|
|
|
|
|
178
|
|
|
$response->add_links( $this->prepare_links( $data['id'] ) ); |
|
179
|
|
|
|
|
180
|
|
|
return $response; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Prepare links for the request. |
|
185
|
|
|
* |
|
186
|
|
|
* @param int $zone_id Given Shipping Zone ID. |
|
187
|
|
|
* @return array Links for the given Shipping Zone. |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function prepare_links( $zone_id ) { |
|
190
|
|
|
$base = '/' . $this->namespace . '/' . $this->rest_base; |
|
191
|
|
|
$links = array( |
|
192
|
|
|
'self' => array( |
|
193
|
|
|
'href' => rest_url( trailingslashit( $base ) . $zone_id ), |
|
194
|
|
|
), |
|
195
|
|
|
'collection' => array( |
|
196
|
|
|
'href' => rest_url( $base ), |
|
197
|
|
|
), |
|
198
|
|
|
'describedby' => array( |
|
199
|
|
|
'href' => rest_url( trailingslashit( $base ) . $zone_id . '/locations' ), |
|
200
|
|
|
), |
|
201
|
|
|
); |
|
202
|
|
|
|
|
203
|
|
|
return $links; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the Shipping Zones schema, conforming to JSON Schema |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
|
public function get_item_schema() { |
|
212
|
|
|
$schema = array( |
|
213
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
214
|
|
|
'title' => 'shipping_zone', |
|
215
|
|
|
'type' => 'object', |
|
216
|
|
|
'properties' => array( |
|
217
|
|
|
'id' => array( |
|
218
|
|
|
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
219
|
|
|
'type' => 'integer', |
|
220
|
|
|
'context' => array( 'view', 'edit' ), |
|
221
|
|
|
'readonly' => true, |
|
222
|
|
|
), |
|
223
|
|
|
'name' => array( |
|
224
|
|
|
'description' => __( 'Shipping zone name.', 'woocommerce' ), |
|
225
|
|
|
'type' => 'string', |
|
226
|
|
|
'context' => array( 'view', 'edit' ), |
|
227
|
|
|
'required' => true, |
|
228
|
|
|
'arg_options' => array( |
|
229
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
|
230
|
|
|
), |
|
231
|
|
|
), |
|
232
|
|
|
'order' => array( |
|
233
|
|
|
'description' => __( 'Shipping zone order.', 'woocommerce' ), |
|
234
|
|
|
'type' => 'integer', |
|
235
|
|
|
'context' => array( 'view', 'edit' ), |
|
236
|
|
|
'required' => false, |
|
237
|
|
|
'arg_options' => array( |
|
238
|
|
|
'sanitize_callback' => 'absint', |
|
239
|
|
|
), |
|
240
|
|
|
), |
|
241
|
|
|
), |
|
242
|
|
|
); |
|
243
|
|
|
|
|
244
|
|
|
return $this->add_additional_fields_schema( $schema ); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|