class-wc-rest-dev-data-continents-controller.php
8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* REST API Data controller.
*
* Handles requests to the /data/continents endpoint.
*
* Directly copied from the wc-api-dev plugin. Delete this when the "v3" REST API is included in all the WC versions we support.
*
* @author Automattic
* @category API
* @package WooCommerce/API
* @since 3.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Data controller class.
*
* @package WooCommerce/API
* @extends WC_REST_Controller
*/
class WC_REST_Dev_Data_Continents_Controller extends WC_REST_Dev_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v3';
/**
* @var WC_Connect_Continents
*/
protected $continents;
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'data/continents';
public function __construct() {
parent::__construct();
$this->continents = new WC_Connect_Continents();
}
/**
* Register routes.
*
* @since 3.1.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<location>[\w-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'continent' => array(
'description' => __( '2 character continent code.', 'woocommerce' ),
'type' => 'string',
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Return the list of states for all continents.
*
* @since 3.1.0
* @param WP_REST_Request $request
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$continents = WC()->countries->get_continents();
$data = array();
foreach ( array_keys( $continents ) as $continent_code ) {
$continent = $this->continents->get_continent( $continent_code, $request );
$response = $this->prepare_item_for_response( $continent, $request );
$data[] = $this->prepare_response_for_collection( $response );
}
return rest_ensure_response( $data );
}
/**
* Return the list of locations for a given continent.
*
* @since 3.1.0
* @param WP_REST_Request $request
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$data = $this->continents->get_continent( strtoupper( $request['location'] ), $request );
if ( empty( $data ) ) {
return new WP_Error( 'woocommerce_rest_data_invalid_location', __( 'There are no locations matching these parameters.', 'woocommerce' ), array( 'status' => 404 ) );
}
return $this->prepare_item_for_response( $data, $request );
}
/**
* Prepare the data object for response.
*
* @since 3.1.0
* @param object $item Data object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response Response data.
*/
public function prepare_item_for_response( $item, $request ) {
$data = $this->add_additional_fields_to_object( $item, $request );
$data = $this->filter_response_by_context( $data, 'view' );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item ) );
/**
* Filter the location list returned from the API.
*
* Allows modification of the loction data right before it is returned.
*
* @param WP_REST_Response $response The response object.
* @param array $item The original list of continent(s), countries, and states.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'woocommerce_rest_prepare_data_continent', $response, $item, $request );
}
/**
* Prepare links for the request.
*
* @param object $item Data object.
* @return array Links for the given continent.
*/
protected function prepare_links( $item ) {
$continent_code = strtolower( $item['code'] );
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $continent_code ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
),
);
return $links;
}
/**
* Get the location schema, conforming to JSON Schema.
*
* @since 3.1.0
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'data_continents',
'type' => 'object',
'properties' => array(
'code' => array(
'type' => 'string',
'description' => __( '2 character continent code.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'name' => array(
'type' => 'string',
'description' => __( 'Full name of continent.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'countries' => array(
'type' => 'array',
'description' => __( 'List of countries on this continent.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'context' => array( 'view' ),
'readonly' => true,
'properties' => array(
'code' => array(
'type' => 'string',
'description' => __( 'ISO3166 alpha-2 country code.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'currency_code' => array(
'type' => 'string',
'description' => __( 'Default ISO4127 alpha-3 currency code for the country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'currency_pos' => array(
'type' => 'string',
'description' => __( 'Currency symbol position for this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'decimal_sep' => array(
'type' => 'string',
'description' => __( 'Decimal separator for displayed prices for this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'dimension_unit' => array(
'type' => 'string',
'description' => __( 'The unit lengths are defined in for this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'name' => array(
'type' => 'string',
'description' => __( 'Full name of country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'num_decimals' => array(
'type' => 'integer',
'description' => __( 'Number of decimal points shown in displayed prices for this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'states' => array(
'type' => 'array',
'description' => __( 'List of states in this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
'items' => array(
'type' => 'object',
'context' => array( 'view' ),
'readonly' => true,
'properties' => array(
'code' => array(
'type' => 'string',
'description' => __( 'State code.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'name' => array(
'type' => 'string',
'description' => __( 'Full name of state.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
),
),
),
'thousand_sep' => array(
'type' => 'string',
'description' => __( 'Thousands separator for displayed prices in this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
'weight_unit' => array(
'type' => 'string',
'description' => __( 'The unit weights are defined in for this country.', 'woocommerce' ),
'context' => array( 'view' ),
'readonly' => true,
),
),
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
}