WC_REST_Shipping_Zones_Controller

REST API Shipping Zones class.

Defined (1)

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

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