WC_REST_Taxes_V1_Controller

REST API Taxes controller class.

Defined (1)

The class is defined in the following location(s).

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