class-wc-rest-taxes-controller.php 22 KB

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