2016-06-14 15:14:03 +00:00
< ? php
/**
* REST API Shipping Zones controller
*
* Handles requests to the / shipping / zones endpoint .
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.7 . 0
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Shipping Zones class .
*
* @ package WooCommerce / API
* @ extends WC_REST_Controller
*/
class WC_REST_Shipping_Zones_Controller extends WC_REST_Controller {
/**
* Endpoint namespace .
*
* @ var string
*/
protected $namespace = 'wc/v1' ;
/**
* Route base .
*
* @ var string
*/
protected $rest_base = 'shipping/zones' ;
2016-06-14 16:33:25 +00:00
/**
* Register the routes for Shipping Zones .
*/
public function register_routes () {
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' ),
),
'schema' => array ( $this , 'get_public_item_schema' ),
) );
2016-06-14 17:43:54 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\d-]+)' , array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_item' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
),
2016-06-14 20:16:14 +00:00
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_item' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
2016-06-14 17:43:54 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
) );
}
2016-06-14 20:15:14 +00:00
/**
* Retrieve a Shipping Zone by it ' s ID .
*
* @ param int $zone_id Shipping Zone ID .
* @ return WC_Shipping_Zone | WP_Error
*/
protected function get_zone ( $zone_id ) {
$zone = WC_Shipping_Zones :: get_zone_by ( 'zone_id' , $zone_id );
if ( false === $zone ) {
return new WP_Error ( 'woocommerce_rest_shipping_zone_invalid' , __ ( " Resource doesn't exist. " , 'woocommerce' ), array ( 'status' => 404 ) );
}
return $zone ;
}
2016-06-14 17:43:54 +00:00
/**
* Get a single Shipping Zone .
*
* @ param WP_REST_Request $request
* @ return WP_REST_Response
*/
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 .
*
* @ param WP_REST_Request $request
* @ return WP_REST_Response
*/
public function get_items ( $request ) {
$zones = WC_Shipping_Zones :: get_zones ();
$data = array ();
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 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 ;
}
$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-06-14 17:21:23 +00:00
/**
* Prepare the Shipping Zone for the REST response .
*
* @ param array $item Shipping Zone .
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $response
*/
public function prepare_item_for_response ( $item , $request ) {
$data = array (
2016-06-14 20:15:56 +00:00
'id' => ( int ) $item [ 'zone_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 (
'self' => array (
'href' => rest_url ( trailingslashit ( $base ) . $zone_id ),
),
'collection' => array (
'href' => rest_url ( $base ),
),
);
return $links ;
}
2016-06-14 16:33:25 +00:00
/**
* Check whether a given request has permission to read Shipping Zones .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
if ( ! wc_shipping_enabled () ) {
return new WP_Error ( 'rest_no_route' , __ ( 'Shipping is disabled.' ), array ( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions ( 'settings' , 'read' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list resources.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
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 (
'id' => array (
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'name' => array (
'description' => __ ( 'Shipping zone name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'required' => true ,
'arg_options' => array (
'sanitize_callback' => 'sanitize_text_field' ,
),
),
'order' => array (
'description' => __ ( 'Shipping zone order.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'required' => false ,
'arg_options' => array (
'sanitize_callback' => 'absint' ,
),
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
}