1 | <?php |
||||
2 | /** |
||||
3 | * REST API Taxes controller |
||||
4 | * |
||||
5 | * Handles requests to the /taxes endpoint. |
||||
6 | * |
||||
7 | * @package Automattic/WooCommerce/RestApi |
||||
8 | */ |
||||
9 | |||||
10 | namespace Automattic\WooCommerce\RestApi\Controllers\Version4; |
||||
11 | |||||
12 | defined( 'ABSPATH' ) || exit; |
||||
13 | |||||
14 | use Automattic\WooCommerce\RestApi\Controllers\Version4\Utilities\Pagination; |
||||
15 | |||||
16 | /** |
||||
17 | * REST API Taxes controller class. |
||||
18 | */ |
||||
19 | class Taxes extends AbstractController { |
||||
20 | |||||
21 | /** |
||||
22 | * Route base. |
||||
23 | * |
||||
24 | * @var string |
||||
25 | */ |
||||
26 | protected $rest_base = 'taxes'; |
||||
27 | |||||
28 | /** |
||||
29 | * Permission to check. |
||||
30 | * |
||||
31 | * @var string |
||||
32 | */ |
||||
33 | protected $resource_type = 'settings'; |
||||
34 | |||||
35 | /** |
||||
36 | * Register the routes for taxes. |
||||
37 | */ |
||||
38 | public function register_routes() { |
||||
39 | register_rest_route( |
||||
40 | $this->namespace, |
||||
41 | '/' . $this->rest_base, |
||||
42 | array( |
||||
43 | array( |
||||
44 | 'methods' => \WP_REST_Server::READABLE, |
||||
45 | 'callback' => array( $this, 'get_items' ), |
||||
46 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||||
47 | 'args' => $this->get_collection_params(), |
||||
48 | ), |
||||
49 | array( |
||||
50 | 'methods' => \WP_REST_Server::CREATABLE, |
||||
51 | 'callback' => array( $this, 'create_item' ), |
||||
52 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||||
53 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
||||
54 | ), |
||||
55 | 'schema' => array( $this, 'get_public_item_schema' ), |
||||
56 | ), |
||||
57 | true |
||||
58 | ); |
||||
59 | |||||
60 | register_rest_route( |
||||
61 | $this->namespace, |
||||
62 | '/' . $this->rest_base . '/(?P<id>[\d]+)', |
||||
63 | array( |
||||
64 | 'args' => array( |
||||
65 | 'id' => array( |
||||
66 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce-rest-api' ), |
||||
67 | 'type' => 'integer', |
||||
68 | ), |
||||
69 | ), |
||||
70 | array( |
||||
71 | 'methods' => \WP_REST_Server::READABLE, |
||||
72 | 'callback' => array( $this, 'get_item' ), |
||||
73 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||||
74 | 'args' => array( |
||||
75 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||||
76 | ), |
||||
77 | ), |
||||
78 | array( |
||||
79 | 'methods' => \WP_REST_Server::EDITABLE, |
||||
80 | 'callback' => array( $this, 'update_item' ), |
||||
81 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||||
82 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
||||
83 | ), |
||||
84 | array( |
||||
85 | 'methods' => \WP_REST_Server::DELETABLE, |
||||
86 | 'callback' => array( $this, 'delete_item' ), |
||||
87 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||||
88 | 'args' => array( |
||||
89 | 'force' => array( |
||||
90 | 'default' => false, |
||||
91 | 'type' => 'boolean', |
||||
92 | 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce-rest-api' ), |
||||
93 | ), |
||||
94 | ), |
||||
95 | ), |
||||
96 | 'schema' => array( $this, 'get_public_item_schema' ), |
||||
97 | ), |
||||
98 | true |
||||
99 | ); |
||||
100 | |||||
101 | $this->register_batch_route(); |
||||
102 | } |
||||
103 | |||||
104 | /** |
||||
105 | * Get all taxes and allow filtering by tax code. |
||||
106 | * |
||||
107 | * @param \WP_REST_Request $request Full details about the request. |
||||
108 | * @return \WP_Error|\WP_REST_Response |
||||
109 | */ |
||||
110 | public function get_items( $request ) { |
||||
111 | global $wpdb; |
||||
112 | |||||
113 | $prepared_args = array(); |
||||
114 | $prepared_args['order'] = $request['order']; |
||||
115 | $prepared_args['number'] = $request['per_page']; |
||||
116 | if ( ! empty( $request['offset'] ) ) { |
||||
117 | $prepared_args['offset'] = $request['offset']; |
||||
118 | } else { |
||||
119 | $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; |
||||
120 | } |
||||
121 | $orderby_possibles = array( |
||||
122 | 'id' => 'tax_rate_id', |
||||
123 | 'order' => 'tax_rate_order', |
||||
124 | ); |
||||
125 | $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; |
||||
126 | $prepared_args['class'] = $request['class']; |
||||
127 | $prepared_args['code'] = $request['code']; |
||||
128 | $prepared_args['include'] = $request['include']; |
||||
129 | |||||
130 | /** |
||||
131 | * Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API. |
||||
132 | * |
||||
133 | * @param array $prepared_args Array of arguments for $wpdb->get_results(). |
||||
134 | * @param \WP_REST_Request $request The current request. |
||||
135 | */ |
||||
136 | $prepared_args = apply_filters( 'woocommerce_rest_tax_query', $prepared_args, $request ); |
||||
137 | |||||
138 | $query = " |
||||
139 | SELECT * |
||||
140 | FROM {$wpdb->prefix}woocommerce_tax_rates |
||||
141 | WHERE 1 = 1 |
||||
142 | "; |
||||
143 | |||||
144 | // Filter by tax class. |
||||
145 | if ( ! empty( $prepared_args['class'] ) ) { |
||||
146 | $class = 'standard' !== $prepared_args['class'] ? sanitize_title( $prepared_args['class'] ) : ''; |
||||
147 | $query .= " AND tax_rate_class = '$class'"; |
||||
148 | } |
||||
149 | |||||
150 | // Filter by tax code. |
||||
151 | $tax_code_search = $prepared_args['code']; |
||||
152 | if ( $tax_code_search ) { |
||||
153 | $tax_code_search = $wpdb->esc_like( $tax_code_search ); |
||||
154 | $tax_code_search = ' \'%' . $tax_code_search . '%\''; |
||||
155 | $query .= ' AND CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") ) LIKE ' . $tax_code_search; |
||||
156 | } |
||||
157 | |||||
158 | // Filter by included tax rate IDs. |
||||
159 | $included_taxes = $prepared_args['include']; |
||||
160 | if ( ! empty( $included_taxes ) ) { |
||||
161 | $included_taxes = implode( ',', $prepared_args['include'] ); |
||||
162 | $query .= " AND tax_rate_id IN ({$included_taxes})"; |
||||
163 | } |
||||
164 | |||||
165 | // Order tax rates. |
||||
166 | $order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) ); |
||||
167 | |||||
168 | // Pagination. |
||||
169 | $pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] ); |
||||
170 | |||||
171 | // Query taxes. |
||||
172 | $results = $wpdb->get_results( $query . $order_by . $pagination ); // @codingStandardsIgnoreLine. |
||||
173 | |||||
174 | $taxes = array(); |
||||
175 | foreach ( $results as $tax ) { |
||||
176 | $data = $this->prepare_item_for_response( $tax, $request ); |
||||
177 | $taxes[] = $this->prepare_response_for_collection( $data ); |
||||
178 | } |
||||
179 | |||||
180 | // Store pagination values for headers then unset for count query. |
||||
181 | $per_page = (int) $prepared_args['number']; |
||||
182 | $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); |
||||
183 | |||||
184 | // Query only for ids. |
||||
185 | $wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) ); // @codingStandardsIgnoreLine. |
||||
186 | |||||
187 | // Calculate totals. |
||||
188 | $total_taxes = (int) $wpdb->num_rows; |
||||
189 | $max_pages = ceil( $total_taxes / $per_page ); |
||||
190 | |||||
191 | $response = rest_ensure_response( $taxes ); |
||||
192 | $response = Pagination::add_pagination_headers( $response, $request, $total_taxes, $max_pages ); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
193 | |||||
194 | return $response; |
||||
195 | } |
||||
196 | |||||
197 | /** |
||||
198 | * Take tax data from the request and return the updated or newly created rate. |
||||
199 | * |
||||
200 | * @param \WP_REST_Request $request Full details about the request. |
||||
201 | * @param \stdClass|null $current Existing tax object. |
||||
202 | * @return object |
||||
203 | */ |
||||
204 | protected function create_or_update_tax( $request, $current = null ) { |
||||
205 | $id = absint( isset( $request['id'] ) ? $request['id'] : 0 ); |
||||
206 | $data = array(); |
||||
207 | $fields = array( |
||||
208 | 'tax_rate_country', |
||||
209 | 'tax_rate_state', |
||||
210 | 'tax_rate', |
||||
211 | 'tax_rate_name', |
||||
212 | 'tax_rate_priority', |
||||
213 | 'tax_rate_compound', |
||||
214 | 'tax_rate_shipping', |
||||
215 | 'tax_rate_order', |
||||
216 | 'tax_rate_class', |
||||
217 | ); |
||||
218 | |||||
219 | foreach ( $fields as $field ) { |
||||
220 | // Keys via API differ from the stored names returned by _get_tax_rate. |
||||
221 | $key = 'tax_rate' === $field ? 'rate' : str_replace( 'tax_rate_', '', $field ); |
||||
222 | |||||
223 | // Remove data that was not posted. |
||||
224 | if ( ! isset( $request[ $key ] ) ) { |
||||
225 | continue; |
||||
226 | } |
||||
227 | |||||
228 | // Test new data against current data. |
||||
229 | if ( $current && $current->$field === $request[ $key ] ) { |
||||
230 | continue; |
||||
231 | } |
||||
232 | |||||
233 | // Add to data array. |
||||
234 | switch ( $key ) { |
||||
235 | case 'tax_rate_priority': |
||||
236 | case 'tax_rate_compound': |
||||
237 | case 'tax_rate_shipping': |
||||
238 | case 'tax_rate_order': |
||||
239 | $data[ $field ] = absint( $request[ $key ] ); |
||||
240 | break; |
||||
241 | case 'tax_rate_class': |
||||
242 | $data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : ''; |
||||
243 | break; |
||||
244 | default: |
||||
245 | $data[ $field ] = wc_clean( $request[ $key ] ); |
||||
246 | break; |
||||
247 | } |
||||
248 | } |
||||
249 | |||||
250 | if ( $id ) { |
||||
251 | \WC_Tax::_update_tax_rate( $id, $data ); |
||||
252 | } else { |
||||
253 | $id = \WC_Tax::_insert_tax_rate( $data ); |
||||
254 | } |
||||
255 | |||||
256 | // Add locales. |
||||
257 | if ( ! empty( $request['postcode'] ) ) { |
||||
258 | \WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $request['postcode'] ) ); |
||||
0 ignored issues
–
show
It seems like
wc_clean($request['postcode']) can also be of type array ; however, parameter $postcodes of WC_Tax::_update_tax_rate_postcodes() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
259 | } |
||||
260 | if ( ! empty( $request['city'] ) ) { |
||||
261 | \WC_Tax::_update_tax_rate_cities( $id, wc_clean( $request['city'] ) ); |
||||
0 ignored issues
–
show
It seems like
wc_clean($request['city']) can also be of type array ; however, parameter $cities of WC_Tax::_update_tax_rate_cities() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
262 | } |
||||
263 | |||||
264 | return \WC_Tax::_get_tax_rate( $id, OBJECT ); |
||||
265 | } |
||||
266 | |||||
267 | /** |
||||
268 | * Create a single tax. |
||||
269 | * |
||||
270 | * @param \WP_REST_Request $request Full details about the request. |
||||
271 | * @return \WP_Error|\WP_REST_Response |
||||
272 | */ |
||||
273 | public function create_item( $request ) { |
||||
274 | if ( ! empty( $request['id'] ) ) { |
||||
275 | return new \WP_Error( 'woocommerce_rest_tax_exists', __( 'Cannot create existing resource.', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); |
||||
276 | } |
||||
277 | |||||
278 | $tax = $this->create_or_update_tax( $request ); |
||||
279 | |||||
280 | $this->update_additional_fields_for_object( $tax, $request ); |
||||
0 ignored issues
–
show
$tax of type object is incompatible with the type array expected by parameter $object of WP_REST_Controller::upda...nal_fields_for_object() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
281 | |||||
282 | /** |
||||
283 | * Fires after a tax is created or updated via the REST API. |
||||
284 | * |
||||
285 | * @param \stdClass $tax Data used to create the tax. |
||||
286 | * @param \WP_REST_Request $request Request object. |
||||
287 | * @param boolean $creating True when creating tax, false when updating tax. |
||||
288 | */ |
||||
289 | do_action( 'woocommerce_rest_insert_tax', $tax, $request, true ); |
||||
290 | |||||
291 | $request->set_param( 'context', 'edit' ); |
||||
292 | $response = $this->prepare_item_for_response( $tax, $request ); |
||||
293 | $response = rest_ensure_response( $response ); |
||||
294 | $response->set_status( 201 ); |
||||
295 | $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ) ); |
||||
296 | |||||
297 | return $response; |
||||
298 | } |
||||
299 | |||||
300 | /** |
||||
301 | * Get a single tax. |
||||
302 | * |
||||
303 | * @param \WP_REST_Request $request Full details about the request. |
||||
304 | * @return \WP_Error|\WP_REST_Response |
||||
305 | */ |
||||
306 | public function get_item( $request ) { |
||||
307 | $id = (int) $request['id']; |
||||
308 | $tax_obj = \WC_Tax::_get_tax_rate( $id, OBJECT ); |
||||
309 | |||||
310 | if ( empty( $id ) || empty( $tax_obj ) ) { |
||||
311 | return new \WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||||
312 | } |
||||
313 | |||||
314 | $tax = $this->prepare_item_for_response( $tax_obj, $request ); |
||||
315 | $response = rest_ensure_response( $tax ); |
||||
316 | |||||
317 | return $response; |
||||
318 | } |
||||
319 | |||||
320 | /** |
||||
321 | * Update a single tax. |
||||
322 | * |
||||
323 | * @param \WP_REST_Request $request Full details about the request. |
||||
324 | * @return \WP_Error|\WP_REST_Response |
||||
325 | */ |
||||
326 | public function update_item( $request ) { |
||||
327 | $id = (int) $request['id']; |
||||
328 | $tax_obj = \WC_Tax::_get_tax_rate( $id, OBJECT ); |
||||
329 | |||||
330 | if ( empty( $id ) || empty( $tax_obj ) ) { |
||||
331 | return new \WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce-rest-api' ), array( 'status' => 404 ) ); |
||||
332 | } |
||||
333 | |||||
334 | $tax = $this->create_or_update_tax( $request, $tax_obj ); |
||||
0 ignored issues
–
show
It seems like
$tax_obj can also be of type array ; however, parameter $current of Automattic\WooCommerce\R...:create_or_update_tax() does only seem to accept null|stdClass , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
335 | |||||
336 | $this->update_additional_fields_for_object( $tax, $request ); |
||||
0 ignored issues
–
show
$tax of type object is incompatible with the type array expected by parameter $object of WP_REST_Controller::upda...nal_fields_for_object() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
337 | |||||
338 | /** |
||||
339 | * Fires after a tax is created or updated via the REST API. |
||||
340 | * |
||||
341 | * @param \stdClass $tax Data used to create the tax. |
||||
342 | * @param \WP_REST_Request $request Request object. |
||||
343 | * @param boolean $creating True when creating tax, false when updating tax. |
||||
344 | */ |
||||
345 | do_action( 'woocommerce_rest_insert_tax', $tax, $request, false ); |
||||
346 | |||||
347 | $request->set_param( 'context', 'edit' ); |
||||
348 | $response = $this->prepare_item_for_response( $tax, $request ); |
||||
349 | $response = rest_ensure_response( $response ); |
||||
350 | |||||
351 | return $response; |
||||
352 | } |
||||
353 | |||||
354 | /** |
||||
355 | * Delete a single tax. |
||||
356 | * |
||||
357 | * @param \WP_REST_Request $request Full details about the request. |
||||
358 | * @return \WP_Error|\WP_REST_Response |
||||
359 | */ |
||||
360 | public function delete_item( $request ) { |
||||
361 | global $wpdb; |
||||
362 | |||||
363 | $id = (int) $request['id']; |
||||
364 | $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
||||
365 | |||||
366 | // We don't support trashing for this type, error out. |
||||
367 | if ( ! $force ) { |
||||
368 | return new \WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce-rest-api' ), array( 'status' => 501 ) ); |
||||
369 | } |
||||
370 | |||||
371 | $tax = \WC_Tax::_get_tax_rate( $id, OBJECT ); |
||||
372 | |||||
373 | if ( empty( $id ) || empty( $tax ) ) { |
||||
374 | return new \WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); |
||||
375 | } |
||||
376 | |||||
377 | $request->set_param( 'context', 'edit' ); |
||||
378 | $response = $this->prepare_item_for_response( $tax, $request ); |
||||
379 | |||||
380 | \WC_Tax::_delete_tax_rate( $id ); |
||||
381 | |||||
382 | if ( 0 === $wpdb->rows_affected ) { |
||||
383 | return new \WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce-rest-api' ), array( 'status' => 500 ) ); |
||||
384 | } |
||||
385 | |||||
386 | /** |
||||
387 | * Fires after a tax is deleted via the REST API. |
||||
388 | * |
||||
389 | * @param \stdClass $tax The tax data. |
||||
390 | * @param \WP_REST_Response $response The response returned from the API. |
||||
391 | * @param \WP_REST_Request $request The request sent to the API. |
||||
392 | */ |
||||
393 | do_action( 'woocommerce_rest_delete_tax', $tax, $response, $request ); |
||||
394 | |||||
395 | return $response; |
||||
396 | } |
||||
397 | |||||
398 | /** |
||||
399 | * Get data for this object in the format of this endpoint's schema. |
||||
400 | * |
||||
401 | * @param \stdClass $object Object to prepare. |
||||
402 | * @param \WP_REST_Request $request Request object. |
||||
403 | * @return array Array of data in the correct format. |
||||
404 | */ |
||||
405 | protected function get_data_for_response( $object, $request ) { |
||||
406 | global $wpdb; |
||||
407 | |||||
408 | $id = (int) $object->tax_rate_id; |
||||
409 | $data = array( |
||||
410 | 'id' => $id, |
||||
411 | 'country' => $object->tax_rate_country, |
||||
412 | 'state' => $object->tax_rate_state, |
||||
413 | 'postcode' => '', |
||||
414 | 'city' => '', |
||||
415 | 'rate' => $object->tax_rate, |
||||
416 | 'name' => $object->tax_rate_name, |
||||
417 | 'priority' => (int) $object->tax_rate_priority, |
||||
418 | 'compound' => (bool) $object->tax_rate_compound, |
||||
419 | 'shipping' => (bool) $object->tax_rate_shipping, |
||||
420 | 'order' => (int) $object->tax_rate_order, |
||||
421 | 'class' => $object->tax_rate_class ? $object->tax_rate_class : 'standard', |
||||
422 | ); |
||||
423 | |||||
424 | // Get locales from a tax rate. |
||||
425 | $locales = $wpdb->get_results( |
||||
426 | $wpdb->prepare( |
||||
427 | " |
||||
428 | SELECT location_code, location_type |
||||
429 | FROM {$wpdb->prefix}woocommerce_tax_rate_locations |
||||
430 | WHERE tax_rate_id = %d |
||||
431 | ", |
||||
432 | $id |
||||
433 | ) |
||||
434 | ); |
||||
435 | |||||
436 | if ( ! is_wp_error( $locales ) && ! is_null( $locales ) ) { |
||||
437 | foreach ( $locales as $locale ) { |
||||
438 | $data[ $locale->location_type ] = $locale->location_code; |
||||
439 | } |
||||
440 | } |
||||
441 | return $data; |
||||
442 | } |
||||
443 | |||||
444 | /** |
||||
445 | * Prepare links for the request. |
||||
446 | * |
||||
447 | * @param mixed $item Object to prepare. |
||||
448 | * @param \WP_REST_Request $request Request object. |
||||
449 | * @return array |
||||
450 | */ |
||||
451 | protected function prepare_links( $item, $request ) { |
||||
452 | $links = array( |
||||
453 | 'self' => array( |
||||
454 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->tax_rate_id ) ), |
||||
455 | ), |
||||
456 | 'collection' => array( |
||||
457 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
||||
458 | ), |
||||
459 | ); |
||||
460 | |||||
461 | return $links; |
||||
462 | } |
||||
463 | |||||
464 | /** |
||||
465 | * Get the Taxes schema, conforming to JSON Schema. |
||||
466 | * |
||||
467 | * @return array |
||||
468 | */ |
||||
469 | public function get_item_schema() { |
||||
470 | $schema = array( |
||||
471 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||||
472 | 'title' => 'tax', |
||||
473 | 'type' => 'object', |
||||
474 | 'properties' => array( |
||||
475 | 'id' => array( |
||||
476 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce-rest-api' ), |
||||
477 | 'type' => 'integer', |
||||
478 | 'context' => array( 'view', 'edit' ), |
||||
479 | 'readonly' => true, |
||||
480 | ), |
||||
481 | 'country' => array( |
||||
482 | 'description' => __( 'Country ISO 3166 code.', 'woocommerce-rest-api' ), |
||||
483 | 'type' => 'string', |
||||
484 | 'context' => array( 'view', 'edit' ), |
||||
485 | ), |
||||
486 | 'state' => array( |
||||
487 | 'description' => __( 'State code.', 'woocommerce-rest-api' ), |
||||
488 | 'type' => 'string', |
||||
489 | 'context' => array( 'view', 'edit' ), |
||||
490 | ), |
||||
491 | 'postcode' => array( |
||||
492 | 'description' => __( 'Postcode / ZIP.', 'woocommerce-rest-api' ), |
||||
493 | 'type' => 'string', |
||||
494 | 'context' => array( 'view', 'edit' ), |
||||
495 | ), |
||||
496 | 'city' => array( |
||||
497 | 'description' => __( 'City name.', 'woocommerce-rest-api' ), |
||||
498 | 'type' => 'string', |
||||
499 | 'context' => array( 'view', 'edit' ), |
||||
500 | ), |
||||
501 | 'rate' => array( |
||||
502 | 'description' => __( 'Tax rate.', 'woocommerce-rest-api' ), |
||||
503 | 'type' => 'string', |
||||
504 | 'context' => array( 'view', 'edit' ), |
||||
505 | ), |
||||
506 | 'name' => array( |
||||
507 | 'description' => __( 'Tax rate name.', 'woocommerce-rest-api' ), |
||||
508 | 'type' => 'string', |
||||
509 | 'context' => array( 'view', 'edit' ), |
||||
510 | ), |
||||
511 | 'priority' => array( |
||||
512 | 'description' => __( 'Tax priority.', 'woocommerce-rest-api' ), |
||||
513 | 'type' => 'integer', |
||||
514 | 'default' => 1, |
||||
515 | 'context' => array( 'view', 'edit' ), |
||||
516 | ), |
||||
517 | 'compound' => array( |
||||
518 | 'description' => __( 'Whether or not this is a compound rate.', 'woocommerce-rest-api' ), |
||||
519 | 'type' => 'boolean', |
||||
520 | 'default' => false, |
||||
521 | 'context' => array( 'view', 'edit' ), |
||||
522 | ), |
||||
523 | 'shipping' => array( |
||||
524 | 'description' => __( 'Whether or not this tax rate also gets applied to shipping.', 'woocommerce-rest-api' ), |
||||
525 | 'type' => 'boolean', |
||||
526 | 'default' => true, |
||||
527 | 'context' => array( 'view', 'edit' ), |
||||
528 | ), |
||||
529 | 'order' => array( |
||||
530 | 'description' => __( 'Indicates the order that will appear in queries.', 'woocommerce-rest-api' ), |
||||
531 | 'type' => 'integer', |
||||
532 | 'context' => array( 'view', 'edit' ), |
||||
533 | ), |
||||
534 | 'class' => array( |
||||
535 | 'description' => __( 'Tax class.', 'woocommerce-rest-api' ), |
||||
536 | 'type' => 'string', |
||||
537 | 'default' => 'standard', |
||||
538 | 'enum' => array_merge( array( 'standard' ), \WC_Tax::get_tax_class_slugs() ), |
||||
539 | 'context' => array( 'view', 'edit' ), |
||||
540 | ), |
||||
541 | ), |
||||
542 | ); |
||||
543 | |||||
544 | return $this->add_additional_fields_schema( $schema ); |
||||
545 | } |
||||
546 | |||||
547 | /** |
||||
548 | * Get the query params for collections. |
||||
549 | * |
||||
550 | * @return array |
||||
551 | */ |
||||
552 | public function get_collection_params() { |
||||
553 | $params = array(); |
||||
554 | $params['context'] = $this->get_context_param(); |
||||
555 | $params['context']['default'] = 'view'; |
||||
556 | |||||
557 | $params['page'] = array( |
||||
558 | 'description' => __( 'Current page of the collection.', 'woocommerce-rest-api' ), |
||||
559 | 'type' => 'integer', |
||||
560 | 'default' => 1, |
||||
561 | 'sanitize_callback' => 'absint', |
||||
562 | 'validate_callback' => 'rest_validate_request_arg', |
||||
563 | 'minimum' => 1, |
||||
564 | ); |
||||
565 | $params['per_page'] = array( |
||||
566 | 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce-rest-api' ), |
||||
567 | 'type' => 'integer', |
||||
568 | 'default' => 10, |
||||
569 | 'minimum' => 1, |
||||
570 | 'maximum' => 100, |
||||
571 | 'sanitize_callback' => 'absint', |
||||
572 | 'validate_callback' => 'rest_validate_request_arg', |
||||
573 | ); |
||||
574 | $params['offset'] = array( |
||||
575 | 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce-rest-api' ), |
||||
576 | 'type' => 'integer', |
||||
577 | 'sanitize_callback' => 'absint', |
||||
578 | 'validate_callback' => 'rest_validate_request_arg', |
||||
579 | ); |
||||
580 | $params['order'] = array( |
||||
581 | 'default' => 'asc', |
||||
582 | 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce-rest-api' ), |
||||
583 | 'enum' => array( 'asc', 'desc' ), |
||||
584 | 'sanitize_callback' => 'sanitize_key', |
||||
585 | 'type' => 'string', |
||||
586 | 'validate_callback' => 'rest_validate_request_arg', |
||||
587 | ); |
||||
588 | $params['orderby'] = array( |
||||
589 | 'default' => 'order', |
||||
590 | 'description' => __( 'Sort collection by object attribute.', 'woocommerce-rest-api' ), |
||||
591 | 'enum' => array( |
||||
592 | 'id', |
||||
593 | 'order', |
||||
594 | ), |
||||
595 | 'sanitize_callback' => 'sanitize_key', |
||||
596 | 'type' => 'string', |
||||
597 | 'validate_callback' => 'rest_validate_request_arg', |
||||
598 | ); |
||||
599 | $params['class'] = array( |
||||
600 | 'description' => __( 'Sort by tax class.', 'woocommerce-rest-api' ), |
||||
601 | 'enum' => array_merge( array( 'standard' ), \WC_Tax::get_tax_class_slugs() ), |
||||
602 | 'sanitize_callback' => 'sanitize_title', |
||||
603 | 'type' => 'string', |
||||
604 | 'validate_callback' => 'rest_validate_request_arg', |
||||
605 | ); |
||||
606 | $params['code'] = array( |
||||
607 | 'description' => __( 'Search by similar tax code.', 'woocommerce-rest-api' ), |
||||
608 | 'type' => 'string', |
||||
609 | 'validate_callback' => 'rest_validate_request_arg', |
||||
610 | ); |
||||
611 | $params['include'] = array( |
||||
612 | 'description' => __( 'Limit result set to items that have the specified rate ID(s) assigned.', 'woocommerce-rest-api' ), |
||||
613 | 'type' => 'array', |
||||
614 | 'items' => array( |
||||
615 | 'type' => 'integer', |
||||
616 | ), |
||||
617 | 'default' => array(), |
||||
618 | 'validate_callback' => 'rest_validate_request_arg', |
||||
619 | ); |
||||
620 | |||||
621 | return $params; |
||||
622 | } |
||||
623 | } |
||||
624 |