class-wc-rest-tax-classes-controller.php 10 KB

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