2016-06-14 15:14:03 +00:00
< ? php
/**
* REST API Shipping Zones controller
*
* Handles requests to the / shipping / zones endpoint .
*
2018-03-06 18:04:58 +00:00
* @ package WooCommerce / API
* @ since 3.0 . 0
2016-06-14 15:14:03 +00:00
*/
2018-03-06 18:04:58 +00:00
defined ( 'ABSPATH' ) || exit ;
2016-06-14 15:14:03 +00:00
/**
* REST API Shipping Zones class .
*
* @ package WooCommerce / API
2016-06-16 15:26:01 +00:00
* @ extends WC_REST_Shipping_Zones_Controller_Base
2016-06-14 15:14:03 +00:00
*/
2016-06-15 16:14:06 +00:00
class WC_REST_Shipping_Zones_Controller extends WC_REST_Shipping_Zones_Controller_Base {
2016-06-14 15:14:03 +00:00
2016-06-14 16:33:25 +00:00
/**
* Register the routes for Shipping Zones .
*/
public function register_routes () {
2018-03-05 20:53:06 +00:00
register_rest_route (
$this -> namespace , '/' . $this -> rest_base , array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
),
array (
'methods' => WP_REST_Server :: CREATABLE ,
'callback' => array ( $this , 'create_item' ),
'permission_callback' => array ( $this , 'create_item_permissions_check' ),
'args' => array_merge (
$this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: CREATABLE ), array (
'name' => array (
'required' => true ,
'type' => 'string' ,
'description' => __ ( 'Shipping zone name.' , 'woocommerce' ),
),
)
2017-03-23 23:51:21 +00:00
),
2018-03-05 20:53:06 +00:00
),
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
2016-06-14 17:43:54 +00:00
2018-03-05 20:53:06 +00:00
register_rest_route (
$this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\d-]+)' , array (
'args' => array (
'id' => array (
'description' => __ ( 'Unique ID for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
),
2017-01-26 20:06:18 +00:00
),
2018-03-05 20:53:06 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_item' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
),
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_item' ),
'permission_callback' => array ( $this , 'update_items_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
array (
'methods' => WP_REST_Server :: DELETABLE ,
'callback' => array ( $this , 'delete_item' ),
'permission_callback' => array ( $this , 'delete_items_permissions_check' ),
'args' => array (
'force' => array (
'default' => false ,
'type' => 'boolean' ,
'description' => __ ( 'Whether to bypass trash and force deletion.' , 'woocommerce' ),
),
2016-08-29 17:19:28 +00:00
),
),
2018-03-05 20:53:06 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
2016-06-14 17:43:54 +00:00
}
/**
* Get a single Shipping Zone .
*
2018-03-06 18:04:58 +00:00
* @ param WP_REST_Request $request Request data .
2017-05-15 11:50:52 +00:00
* @ return WP_REST_Response | WP_Error
2016-06-14 17:43:54 +00:00
*/
public function get_item ( $request ) {
2016-06-14 20:15:14 +00:00
$zone = $this -> get_zone ( $request -> get_param ( 'id' ) );
2016-06-14 17:43:54 +00:00
2016-06-14 20:15:14 +00:00
if ( is_wp_error ( $zone ) ) {
return $zone ;
2016-06-14 17:43:54 +00:00
}
$data = $zone -> get_data ();
$data = $this -> prepare_item_for_response ( $data , $request );
$data = $this -> prepare_response_for_collection ( $data );
return rest_ensure_response ( $data );
2016-06-14 16:33:25 +00:00
}
2016-06-14 17:21:23 +00:00
/**
* Get all Shipping Zones .
*
2018-03-06 18:04:58 +00:00
* @ param WP_REST_Request $request Request data .
2016-06-14 17:21:23 +00:00
* @ return WP_REST_Response
*/
public function get_items ( $request ) {
2016-06-18 00:01:47 +00:00
$rest_of_the_world = WC_Shipping_Zones :: get_zone_by ( 'zone_id' , 0 );
2016-06-17 22:01:29 +00:00
2016-06-14 17:21:23 +00:00
$zones = WC_Shipping_Zones :: get_zones ();
2016-06-17 22:01:29 +00:00
array_unshift ( $zones , $rest_of_the_world -> get_data () );
2018-03-05 20:53:06 +00:00
$data = array ();
2016-06-14 17:21:23 +00:00
foreach ( $zones as $zone_obj ) {
$zone = $this -> prepare_item_for_response ( $zone_obj , $request );
$zone = $this -> prepare_response_for_collection ( $zone );
$data [] = $zone ;
}
return rest_ensure_response ( $data );
}
2016-06-14 21:01:49 +00:00
/**
* Create a single Shipping Zone .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Request | WP_Error
*/
public function create_item ( $request ) {
$zone = new WC_Shipping_Zone ( null );
if ( ! is_null ( $request -> get_param ( 'name' ) ) ) {
$zone -> set_zone_name ( $request -> get_param ( 'name' ) );
}
if ( ! is_null ( $request -> get_param ( 'order' ) ) ) {
$zone -> set_zone_order ( $request -> get_param ( 'order' ) );
}
2016-11-23 16:27:18 +00:00
$zone -> save ();
2016-06-14 21:01:49 +00:00
2016-07-26 18:48:44 +00:00
if ( $zone -> get_id () !== 0 ) {
$request -> set_param ( 'id' , $zone -> get_id () );
2016-08-02 17:26:26 +00:00
$response = $this -> get_item ( $request );
$response -> set_status ( 201 );
$response -> header ( 'Location' , rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $this -> rest_base , $zone -> get_id () ) ) );
return $response ;
2016-07-26 18:48:44 +00:00
} else {
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 ) );
}
2016-06-14 21:01:49 +00:00
}
2016-06-14 20:16:14 +00:00
/**
* Update a single Shipping Zone .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Request | WP_Error
*/
public function update_item ( $request ) {
$zone = $this -> get_zone ( $request -> get_param ( 'id' ) );
if ( is_wp_error ( $zone ) ) {
return $zone ;
}
2017-05-22 14:19:04 +00:00
if ( 0 === $zone -> get_id () ) {
2018-03-05 20:53:06 +00:00
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 ) );
2017-05-22 14:19:04 +00:00
}
2016-06-14 20:16:14 +00:00
$zone_changed = false ;
if ( ! is_null ( $request -> get_param ( 'name' ) ) ) {
$zone -> set_zone_name ( $request -> get_param ( 'name' ) );
$zone_changed = true ;
}
if ( ! is_null ( $request -> get_param ( 'order' ) ) ) {
$zone -> set_zone_order ( $request -> get_param ( 'order' ) );
$zone_changed = true ;
}
if ( $zone_changed ) {
$zone -> save ();
}
return $this -> get_item ( $request );
}
2016-08-29 17:19:28 +00:00
/**
* Delete a single Shipping Zone .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_REST_Request | WP_Error
*/
public function delete_item ( $request ) {
$zone = $this -> get_zone ( $request -> get_param ( 'id' ) );
if ( is_wp_error ( $zone ) ) {
return $zone ;
}
$force = $request [ 'force' ];
$response = $this -> get_item ( $request );
if ( $force ) {
$zone -> delete ();
} else {
2016-10-12 10:05:37 +00:00
return new WP_Error ( 'rest_trash_not_supported' , __ ( 'Shipping zones do not support trashing.' , 'woocommerce' ), array ( 'status' => 501 ) );
2016-08-29 17:19:28 +00:00
}
return $response ;
}
2016-06-14 17:21:23 +00:00
/**
* Prepare the Shipping Zone for the REST response .
*
2018-03-05 20:53:06 +00:00
* @ param array $item Shipping Zone .
2016-06-14 17:21:23 +00:00
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $response
*/
public function prepare_item_for_response ( $item , $request ) {
$data = array (
2016-09-09 12:34:49 +00:00
'id' => ( int ) $item [ 'id' ],
2016-06-14 17:21:23 +00:00
'name' => $item [ 'zone_name' ],
2016-06-14 20:15:56 +00:00
'order' => ( int ) $item [ 'zone_order' ],
2016-06-14 17:21:23 +00:00
);
$context = empty ( $request [ 'context' ] ) ? 'view' : $request [ 'context' ];
$data = $this -> add_additional_fields_to_object ( $data , $request );
$data = $this -> filter_response_by_context ( $data , $context );
// Wrap the data in a response object.
$response = rest_ensure_response ( $data );
$response -> add_links ( $this -> prepare_links ( $data [ 'id' ] ) );
return $response ;
}
/**
* Prepare links for the request .
*
* @ param int $zone_id Given Shipping Zone ID .
* @ return array Links for the given Shipping Zone .
*/
protected function prepare_links ( $zone_id ) {
$base = '/' . $this -> namespace . '/' . $this -> rest_base ;
$links = array (
2018-03-05 20:53:06 +00:00
'self' => array (
2016-06-14 17:21:23 +00:00
'href' => rest_url ( trailingslashit ( $base ) . $zone_id ),
),
2018-03-05 20:53:06 +00:00
'collection' => array (
2016-06-14 17:21:23 +00:00
'href' => rest_url ( $base ),
),
2016-06-15 18:51:48 +00:00
'describedby' => array (
'href' => rest_url ( trailingslashit ( $base ) . $zone_id . '/locations' ),
),
2016-06-14 17:21:23 +00:00
);
return $links ;
}
2016-06-14 15:14:03 +00:00
/**
* Get the Shipping Zones schema , conforming to JSON Schema
*
* @ return array
*/
public function get_item_schema () {
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => 'shipping_zone' ,
'type' => 'object' ,
'properties' => array (
2018-03-05 20:53:06 +00:00
'id' => array (
2016-06-14 15:14:03 +00:00
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-03-05 20:53:06 +00:00
'name' => array (
2016-06-14 15:14:03 +00:00
'description' => __ ( 'Shipping zone name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'arg_options' => array (
'sanitize_callback' => 'sanitize_text_field' ,
),
),
'order' => array (
'description' => __ ( 'Shipping zone order.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
}