class-wc-rest-shipping-zone-locations-controller.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * REST API Shipping Zone Locations controller
  4. *
  5. * Handles requests to the /shipping/zones/<id>/locations endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Shipping Zone Locations class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Shipping_Zones_Controller_Base
  16. */
  17. class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_Controller_Base {
  18. /**
  19. * Register the routes for Shipping Zone Locations.
  20. */
  21. public function register_routes() {
  22. register_rest_route(
  23. $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)/locations', array(
  24. 'args' => array(
  25. 'id' => array(
  26. 'description' => __( 'Unique ID for the resource.', 'woocommerce' ),
  27. 'type' => 'integer',
  28. ),
  29. ),
  30. array(
  31. 'methods' => WP_REST_Server::READABLE,
  32. 'callback' => array( $this, 'get_items' ),
  33. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  34. ),
  35. array(
  36. 'methods' => WP_REST_Server::EDITABLE,
  37. 'callback' => array( $this, 'update_items' ),
  38. 'permission_callback' => array( $this, 'update_items_permissions_check' ),
  39. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  40. ),
  41. 'schema' => array( $this, 'get_public_item_schema' ),
  42. )
  43. );
  44. }
  45. /**
  46. * Get all Shipping Zone Locations.
  47. *
  48. * @param WP_REST_Request $request Request data.
  49. * @return WP_REST_Response|WP_Error
  50. */
  51. public function get_items( $request ) {
  52. $zone = $this->get_zone( (int) $request['id'] );
  53. if ( is_wp_error( $zone ) ) {
  54. return $zone;
  55. }
  56. $locations = $zone->get_zone_locations();
  57. $data = array();
  58. foreach ( $locations as $location_obj ) {
  59. $location = $this->prepare_item_for_response( $location_obj, $request );
  60. $location = $this->prepare_response_for_collection( $location );
  61. $data[] = $location;
  62. }
  63. return rest_ensure_response( $data );
  64. }
  65. /**
  66. * Update all Shipping Zone Locations.
  67. *
  68. * @param WP_REST_Request $request Request data.
  69. * @return WP_REST_Response|WP_Error
  70. */
  71. public function update_items( $request ) {
  72. $zone = $this->get_zone( (int) $request['id'] );
  73. if ( is_wp_error( $zone ) ) {
  74. return $zone;
  75. }
  76. if ( 0 === $zone->get_id() ) {
  77. return new WP_Error( 'woocommerce_rest_shipping_zone_locations_invalid_zone', __( 'The "locations not covered by your other zones" zone cannot be updated.', 'woocommerce' ), array( 'status' => 403 ) );
  78. }
  79. $raw_locations = $request->get_json_params();
  80. $locations = array();
  81. foreach ( (array) $raw_locations as $raw_location ) {
  82. if ( empty( $raw_location['code'] ) ) {
  83. continue;
  84. }
  85. $type = ! empty( $raw_location['type'] ) ? sanitize_text_field( $raw_location['type'] ) : 'country';
  86. if ( ! in_array( $type, array( 'postcode', 'state', 'country', 'continent' ), true ) ) {
  87. continue;
  88. }
  89. $locations[] = array(
  90. 'code' => sanitize_text_field( $raw_location['code'] ),
  91. 'type' => sanitize_text_field( $type ),
  92. );
  93. }
  94. $zone->set_locations( $locations );
  95. $zone->save();
  96. return $this->get_items( $request );
  97. }
  98. /**
  99. * Prepare the Shipping Zone Location for the REST response.
  100. *
  101. * @param array $item Shipping Zone Location.
  102. * @param WP_REST_Request $request Request object.
  103. * @return WP_REST_Response $response
  104. */
  105. public function prepare_item_for_response( $item, $request ) {
  106. $context = empty( $request['context'] ) ? 'view' : $request['context'];
  107. $data = $this->add_additional_fields_to_object( $item, $request );
  108. $data = $this->filter_response_by_context( $data, $context );
  109. // Wrap the data in a response object.
  110. $response = rest_ensure_response( $data );
  111. $response->add_links( $this->prepare_links( (int) $request['id'] ) );
  112. return $response;
  113. }
  114. /**
  115. * Prepare links for the request.
  116. *
  117. * @param int $zone_id Given Shipping Zone ID.
  118. * @return array Links for the given Shipping Zone Location.
  119. */
  120. protected function prepare_links( $zone_id ) {
  121. $base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id;
  122. $links = array(
  123. 'collection' => array(
  124. 'href' => rest_url( $base . '/locations' ),
  125. ),
  126. 'describes' => array(
  127. 'href' => rest_url( $base ),
  128. ),
  129. );
  130. return $links;
  131. }
  132. /**
  133. * Get the Shipping Zone Locations schema, conforming to JSON Schema
  134. *
  135. * @return array
  136. */
  137. public function get_item_schema() {
  138. $schema = array(
  139. '$schema' => 'http://json-schema.org/draft-04/schema#',
  140. 'title' => 'shipping_zone_location',
  141. 'type' => 'object',
  142. 'properties' => array(
  143. 'code' => array(
  144. 'description' => __( 'Shipping zone location code.', 'woocommerce' ),
  145. 'type' => 'string',
  146. 'context' => array( 'view', 'edit' ),
  147. ),
  148. 'type' => array(
  149. 'description' => __( 'Shipping zone location type.', 'woocommerce' ),
  150. 'type' => 'string',
  151. 'default' => 'country',
  152. 'enum' => array(
  153. 'postcode',
  154. 'state',
  155. 'country',
  156. 'continent',
  157. ),
  158. 'context' => array( 'view', 'edit' ),
  159. ),
  160. ),
  161. );
  162. return $this->add_additional_fields_schema( $schema );
  163. }
  164. }