WC_REST_Tax_Classes_V1_Controller

REST API Tax Classes controller class.

Defined (1)

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

/includes/api/v1/class-wc-rest-tax-classes-controller.php  
  1. class WC_REST_Tax_Classes_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/classes'; 
  14.  
  15. /** 
  16. * Register the routes for tax classes. 
  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<slug>\w[\w\s\-]*)', array( 
  36. 'args' => array( 
  37. 'slug' => array( 
  38. 'description' => __( 'Unique slug for the resource.', 'woocommerce' ),  
  39. 'type' => 'string',  
  40. ),  
  41. ),  
  42. array( 
  43. 'methods' => WP_REST_Server::DELETABLE,  
  44. 'callback' => array( $this, 'delete_item' ),  
  45. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),  
  46. 'args' => array( 
  47. 'force' => array( 
  48. 'default' => false,  
  49. 'type' => 'boolean',  
  50. 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),  
  51. ),  
  52. ),  
  53. ),  
  54. 'schema' => array( $this, 'get_public_item_schema' ),  
  55. ) ); 
  56.  
  57. /** 
  58. * Check whether a given request has permission to read tax classes. 
  59. * @param WP_REST_Request $request Full details about the request. 
  60. * @return WP_Error|boolean 
  61. */ 
  62. public function get_items_permissions_check( $request ) { 
  63. if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { 
  64. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  65.  
  66. return true; 
  67.  
  68. /** 
  69. * Check if a given request has access create tax classes. 
  70. * @param WP_REST_Request $request Full details about the request. 
  71. * @return boolean 
  72. */ 
  73. public function create_item_permissions_check( $request ) { 
  74. if ( ! wc_rest_check_manager_permissions( 'settings', 'create' ) ) { 
  75. return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  76.  
  77. return true; 
  78.  
  79. /** 
  80. * Check if a given request has access delete a tax. 
  81. * @param WP_REST_Request $request Full details about the request. 
  82. * @return boolean 
  83. */ 
  84. public function delete_item_permissions_check( $request ) { 
  85. if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) { 
  86. return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  87.  
  88. return true; 
  89.  
  90. /** 
  91. * Get all tax classes. 
  92. * @param WP_REST_Request $request 
  93. * @return array 
  94. */ 
  95. public function get_items( $request ) { 
  96. $tax_classes = array(); 
  97.  
  98. // Add standard class. 
  99. $tax_classes[] = array( 
  100. 'slug' => 'standard',  
  101. 'name' => __( 'Standard rate', 'woocommerce' ),  
  102. ); 
  103.  
  104. $classes = WC_Tax::get_tax_classes(); 
  105.  
  106. foreach ( $classes as $class ) { 
  107. $tax_classes[] = array( 
  108. 'slug' => sanitize_title( $class ),  
  109. 'name' => $class,  
  110. ); 
  111.  
  112. $data = array(); 
  113. foreach ( $tax_classes as $tax_class ) { 
  114. $class = $this->prepare_item_for_response( $tax_class, $request ); 
  115. $class = $this->prepare_response_for_collection( $class ); 
  116. $data[] = $class; 
  117.  
  118. return rest_ensure_response( $data ); 
  119.  
  120. /** 
  121. * Create a single tax. 
  122. * @param WP_REST_Request $request Full details about the request. 
  123. * @return WP_Error|WP_REST_Response 
  124. */ 
  125. public function create_item( $request ) { 
  126. $exists = false; 
  127. $classes = WC_Tax::get_tax_classes(); 
  128. $tax_class = array( 
  129. 'slug' => sanitize_title( $request['name'] ),  
  130. 'name' => $request['name'],  
  131. ); 
  132.  
  133. // Check if class exists. 
  134. foreach ( $classes as $key => $class ) { 
  135. if ( sanitize_title( $class ) === $tax_class['slug'] ) { 
  136. $exists = true; 
  137. break; 
  138.  
  139. // Return error if tax class already exists. 
  140. if ( $exists ) { 
  141. return new WP_Error( 'woocommerce_rest_tax_class_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) ); 
  142.  
  143. // Add the new class. 
  144. $classes[] = $tax_class['name']; 
  145.  
  146. update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); 
  147.  
  148. $this->update_additional_fields_for_object( $tax_class, $request ); 
  149.  
  150. /** 
  151. * Fires after a tax class is created or updated via the REST API. 
  152. * @param stdClass $tax_class Data used to create the tax class. 
  153. * @param WP_REST_Request $request Request object. 
  154. * @param boolean $creating True when creating tax class, false when updating tax class. 
  155. */ 
  156. do_action( 'woocommerce_rest_insert_tax_class', (object) $tax_class, $request, true ); 
  157.  
  158. $request->set_param( 'context', 'edit' ); 
  159. $response = $this->prepare_item_for_response( $tax_class, $request ); 
  160. $response = rest_ensure_response( $response ); 
  161. $response->set_status( 201 ); 
  162. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tax_class['slug'] ) ) ); 
  163.  
  164. return $response; 
  165.  
  166. /** 
  167. * Delete a single tax class. 
  168. * @param WP_REST_Request $request Full details about the request. 
  169. * @return WP_Error|WP_REST_Response 
  170. */ 
  171. public function delete_item( $request ) { 
  172. global $wpdb; 
  173.  
  174. $force = isset( $request['force'] ) ? (bool) $request['force'] : false; 
  175.  
  176. // We don't support trashing for this type, error out. 
  177. if ( ! $force ) { 
  178. return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) ); 
  179.  
  180. $tax_class = array( 
  181. 'slug' => sanitize_title( $request['slug'] ),  
  182. 'name' => '',  
  183. ); 
  184. $classes = WC_Tax::get_tax_classes(); 
  185. $deleted = false; 
  186.  
  187. foreach ( $classes as $key => $class ) { 
  188. if ( sanitize_title( $class ) === $tax_class['slug'] ) { 
  189. $tax_class['name'] = $class; 
  190. unset( $classes[ $key ] ); 
  191. $deleted = true; 
  192. break; 
  193.  
  194. if ( ! $deleted ) { 
  195. return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) ); 
  196.  
  197. update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); 
  198.  
  199. // Delete tax rate locations locations from the selected class. 
  200. $wpdb->query( $wpdb->prepare( " 
  201. DELETE locations.* 
  202. FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations 
  203. INNER JOIN 
  204. {$wpdb->prefix}woocommerce_tax_rates AS rates 
  205. ON rates.tax_rate_id = locations.tax_rate_id 
  206. WHERE rates.tax_rate_class = '%s' 
  207. ", $tax_class['slug'] ) ); 
  208.  
  209. // Delete tax rates in the selected class. 
  210. $wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $tax_class['slug'] ), array( '%s' ) ); 
  211.  
  212. $request->set_param( 'context', 'edit' ); 
  213. $response = $this->prepare_item_for_response( $tax_class, $request ); 
  214.  
  215. /** 
  216. * Fires after a tax class is deleted via the REST API. 
  217. * @param stdClass $tax_class The tax data. 
  218. * @param WP_REST_Response $response The response returned from the API. 
  219. * @param WP_REST_Request $request The request sent to the API. 
  220. */ 
  221. do_action( 'woocommerce_rest_delete_tax', (object) $tax_class, $response, $request ); 
  222.  
  223. return $response; 
  224.  
  225. /** 
  226. * Prepare a single tax class output for response. 
  227. * @param array $tax_class Tax class data. 
  228. * @param WP_REST_Request $request Request object. 
  229. * @return WP_REST_Response $response Response data. 
  230. */ 
  231. public function prepare_item_for_response( $tax_class, $request ) { 
  232. $data = $tax_class; 
  233.  
  234. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  235. $data = $this->add_additional_fields_to_object( $data, $request ); 
  236. $data = $this->filter_response_by_context( $data, $context ); 
  237.  
  238. // Wrap the data in a response object. 
  239. $response = rest_ensure_response( $data ); 
  240.  
  241. $response->add_links( $this->prepare_links() ); 
  242.  
  243. /** 
  244. * Filter tax object returned from the REST API. 
  245. * @param WP_REST_Response $response The response object. 
  246. * @param stdClass $tax_class Tax object used to create response. 
  247. * @param WP_REST_Request $request Request object. 
  248. */ 
  249. return apply_filters( 'woocommerce_rest_prepare_tax', $response, (object) $tax_class, $request ); 
  250.  
  251. /** 
  252. * Prepare links for the request. 
  253. * @return array Links for the given tax class. 
  254. */ 
  255. protected function prepare_links() { 
  256. $links = array( 
  257. 'collection' => array( 
  258. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),  
  259. ),  
  260. ); 
  261.  
  262. return $links; 
  263.  
  264. /** 
  265. * Get the Tax Classes schema, conforming to JSON Schema 
  266. * @return array 
  267. */ 
  268. public function get_item_schema() { 
  269. $schema = array( 
  270. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  271. 'title' => 'tax_class',  
  272. 'type' => 'object',  
  273. 'properties' => array( 
  274. 'slug' => array( 
  275. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  276. 'type' => 'string',  
  277. 'context' => array( 'view', 'edit' ),  
  278. 'readonly' => true,  
  279. ),  
  280. 'name' => array( 
  281. 'description' => __( 'Tax class name.', 'woocommerce' ),  
  282. 'type' => 'string',  
  283. 'context' => array( 'view', 'edit' ),  
  284. 'required' => true,  
  285. 'arg_options' => array( 
  286. 'sanitize_callback' => 'sanitize_text_field',  
  287. ),  
  288. ),  
  289. ),  
  290. ); 
  291.  
  292. return $this->add_additional_fields_schema( $schema ); 
  293.  
  294. /** 
  295. * Get the query params for collections. 
  296. * @return array 
  297. */ 
  298. public function get_collection_params() { 
  299. return array( 
  300. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  301. );