Bez popisu

class-wc-rest-tax-classes-v1-controller.php 9.5KB

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