class-wc-rest-shipping-zones-controller.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * REST API Shipping Zones controller
  4. *
  5. * Handles requests to the /shipping/zones endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Shipping Zones class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Shipping_Zones_Controller_Base
  16. */
  17. class WC_REST_Shipping_Zones_Controller extends WC_REST_Shipping_Zones_Controller_Base {
  18. /**
  19. * Register the routes for Shipping Zones.
  20. */
  21. public function register_routes() {
  22. register_rest_route(
  23. $this->namespace, '/' . $this->rest_base, array(
  24. array(
  25. 'methods' => WP_REST_Server::READABLE,
  26. 'callback' => array( $this, 'get_items' ),
  27. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  28. ),
  29. array(
  30. 'methods' => WP_REST_Server::CREATABLE,
  31. 'callback' => array( $this, 'create_item' ),
  32. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  33. 'args' => array_merge(
  34. $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
  35. 'name' => array(
  36. 'required' => true,
  37. 'type' => 'string',
  38. 'description' => __( 'Shipping zone name.', 'woocommerce' ),
  39. ),
  40. )
  41. ),
  42. ),
  43. 'schema' => array( $this, 'get_public_item_schema' ),
  44. )
  45. );
  46. register_rest_route(
  47. $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d-]+)', array(
  48. 'args' => array(
  49. 'id' => array(
  50. 'description' => __( 'Unique ID for the resource.', 'woocommerce' ),
  51. 'type' => 'integer',
  52. ),
  53. ),
  54. array(
  55. 'methods' => WP_REST_Server::READABLE,
  56. 'callback' => array( $this, 'get_item' ),
  57. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  58. ),
  59. array(
  60. 'methods' => WP_REST_Server::EDITABLE,
  61. 'callback' => array( $this, 'update_item' ),
  62. 'permission_callback' => array( $this, 'update_items_permissions_check' ),
  63. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  64. ),
  65. array(
  66. 'methods' => WP_REST_Server::DELETABLE,
  67. 'callback' => array( $this, 'delete_item' ),
  68. 'permission_callback' => array( $this, 'delete_items_permissions_check' ),
  69. 'args' => array(
  70. 'force' => array(
  71. 'default' => false,
  72. 'type' => 'boolean',
  73. 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),
  74. ),
  75. ),
  76. ),
  77. 'schema' => array( $this, 'get_public_item_schema' ),
  78. )
  79. );
  80. }
  81. /**
  82. * Get a single Shipping Zone.
  83. *
  84. * @param WP_REST_Request $request Request data.
  85. * @return WP_REST_Response|WP_Error
  86. */
  87. public function get_item( $request ) {
  88. $zone = $this->get_zone( $request->get_param( 'id' ) );
  89. if ( is_wp_error( $zone ) ) {
  90. return $zone;
  91. }
  92. $data = $zone->get_data();
  93. $data = $this->prepare_item_for_response( $data, $request );
  94. $data = $this->prepare_response_for_collection( $data );
  95. return rest_ensure_response( $data );
  96. }
  97. /**
  98. * Get all Shipping Zones.
  99. *
  100. * @param WP_REST_Request $request Request data.
  101. * @return WP_REST_Response
  102. */
  103. public function get_items( $request ) {
  104. $rest_of_the_world = WC_Shipping_Zones::get_zone_by( 'zone_id', 0 );
  105. $zones = WC_Shipping_Zones::get_zones();
  106. array_unshift( $zones, $rest_of_the_world->get_data() );
  107. $data = array();
  108. foreach ( $zones as $zone_obj ) {
  109. $zone = $this->prepare_item_for_response( $zone_obj, $request );
  110. $zone = $this->prepare_response_for_collection( $zone );
  111. $data[] = $zone;
  112. }
  113. return rest_ensure_response( $data );
  114. }
  115. /**
  116. * Create a single Shipping Zone.
  117. *
  118. * @param WP_REST_Request $request Full details about the request.
  119. * @return WP_REST_Request|WP_Error
  120. */
  121. public function create_item( $request ) {
  122. $zone = new WC_Shipping_Zone( null );
  123. if ( ! is_null( $request->get_param( 'name' ) ) ) {
  124. $zone->set_zone_name( $request->get_param( 'name' ) );
  125. }
  126. if ( ! is_null( $request->get_param( 'order' ) ) ) {
  127. $zone->set_zone_order( $request->get_param( 'order' ) );
  128. }
  129. $zone->save();
  130. if ( $zone->get_id() !== 0 ) {
  131. $request->set_param( 'id', $zone->get_id() );
  132. $response = $this->get_item( $request );
  133. $response->set_status( 201 );
  134. $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $zone->get_id() ) ) );
  135. return $response;
  136. } else {
  137. return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( "Resource cannot be created. Check to make sure 'order' and 'name' are present.", 'woocommerce' ), array( 'status' => 500 ) );
  138. }
  139. }
  140. /**
  141. * Update a single Shipping Zone.
  142. *
  143. * @param WP_REST_Request $request Full details about the request.
  144. * @return WP_REST_Request|WP_Error
  145. */
  146. public function update_item( $request ) {
  147. $zone = $this->get_zone( $request->get_param( 'id' ) );
  148. if ( is_wp_error( $zone ) ) {
  149. return $zone;
  150. }
  151. if ( 0 === $zone->get_id() ) {
  152. return new WP_Error( 'woocommerce_rest_shipping_zone_invalid_zone', __( 'The "locations not covered by your other zones" zone cannot be updated.', 'woocommerce' ), array( 'status' => 403 ) );
  153. }
  154. $zone_changed = false;
  155. if ( ! is_null( $request->get_param( 'name' ) ) ) {
  156. $zone->set_zone_name( $request->get_param( 'name' ) );
  157. $zone_changed = true;
  158. }
  159. if ( ! is_null( $request->get_param( 'order' ) ) ) {
  160. $zone->set_zone_order( $request->get_param( 'order' ) );
  161. $zone_changed = true;
  162. }
  163. if ( $zone_changed ) {
  164. $zone->save();
  165. }
  166. return $this->get_item( $request );
  167. }
  168. /**
  169. * Delete a single Shipping Zone.
  170. *
  171. * @param WP_REST_Request $request Full details about the request.
  172. * @return WP_REST_Request|WP_Error
  173. */
  174. public function delete_item( $request ) {
  175. $zone = $this->get_zone( $request->get_param( 'id' ) );
  176. if ( is_wp_error( $zone ) ) {
  177. return $zone;
  178. }
  179. $force = $request['force'];
  180. $response = $this->get_item( $request );
  181. if ( $force ) {
  182. $zone->delete();
  183. } else {
  184. return new WP_Error( 'rest_trash_not_supported', __( 'Shipping zones do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
  185. }
  186. return $response;
  187. }
  188. /**
  189. * Prepare the Shipping Zone for the REST response.
  190. *
  191. * @param array $item Shipping Zone.
  192. * @param WP_REST_Request $request Request object.
  193. * @return WP_REST_Response $response
  194. */
  195. public function prepare_item_for_response( $item, $request ) {
  196. $data = array(
  197. 'id' => (int) $item['id'],
  198. 'name' => $item['zone_name'],
  199. 'order' => (int) $item['zone_order'],
  200. );
  201. $context = empty( $request['context'] ) ? 'view' : $request['context'];
  202. $data = $this->add_additional_fields_to_object( $data, $request );
  203. $data = $this->filter_response_by_context( $data, $context );
  204. // Wrap the data in a response object.
  205. $response = rest_ensure_response( $data );
  206. $response->add_links( $this->prepare_links( $data['id'] ) );
  207. return $response;
  208. }
  209. /**
  210. * Prepare links for the request.
  211. *
  212. * @param int $zone_id Given Shipping Zone ID.
  213. * @return array Links for the given Shipping Zone.
  214. */
  215. protected function prepare_links( $zone_id ) {
  216. $base = '/' . $this->namespace . '/' . $this->rest_base;
  217. $links = array(
  218. 'self' => array(
  219. 'href' => rest_url( trailingslashit( $base ) . $zone_id ),
  220. ),
  221. 'collection' => array(
  222. 'href' => rest_url( $base ),
  223. ),
  224. 'describedby' => array(
  225. 'href' => rest_url( trailingslashit( $base ) . $zone_id . '/locations' ),
  226. ),
  227. );
  228. return $links;
  229. }
  230. /**
  231. * Get the Shipping Zones schema, conforming to JSON Schema
  232. *
  233. * @return array
  234. */
  235. public function get_item_schema() {
  236. $schema = array(
  237. '$schema' => 'http://json-schema.org/draft-04/schema#',
  238. 'title' => 'shipping_zone',
  239. 'type' => 'object',
  240. 'properties' => array(
  241. 'id' => array(
  242. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  243. 'type' => 'integer',
  244. 'context' => array( 'view', 'edit' ),
  245. 'readonly' => true,
  246. ),
  247. 'name' => array(
  248. 'description' => __( 'Shipping zone name.', 'woocommerce' ),
  249. 'type' => 'string',
  250. 'context' => array( 'view', 'edit' ),
  251. 'arg_options' => array(
  252. 'sanitize_callback' => 'sanitize_text_field',
  253. ),
  254. ),
  255. 'order' => array(
  256. 'description' => __( 'Shipping zone order.', 'woocommerce' ),
  257. 'type' => 'integer',
  258. 'context' => array( 'view', 'edit' ),
  259. ),
  260. ),
  261. );
  262. return $this->add_additional_fields_schema( $schema );
  263. }
  264. }