WC_REST_Shipping_Zone_Locations_Controller

REST API Shipping Zone Locations class.

Defined (1)

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

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