/includes/api/v1/class-wc-rest-taxes-controller.php

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