2016-06-15 15:38:26 +00:00
< ? php
/**
* REST API Shipping Zone Locations controller
*
* Handles requests to the / shipping / zones /< id >/ locations endpoint .
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
2017-03-15 16:36:53 +00:00
* @ since 3.0 . 0
2016-06-15 15:38:26 +00:00
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Shipping Zone Locations class .
*
* @ package WooCommerce / API
2016-06-16 15:26:01 +00:00
* @ extends WC_REST_Shipping_Zones_Controller_Base
2016-06-15 15:38:26 +00:00
*/
2016-06-15 16:14:06 +00:00
class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_Controller_Base {
2016-06-15 15:38:26 +00:00
/**
2016-06-15 18:51:16 +00:00
* Register the routes for Shipping Zone Locations .
*/
public function register_routes () {
2017-04-28 14:49:51 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\d]+)/locations' , array (
2017-01-26 20:06:18 +00:00
'args' => array (
'id' => array (
'description' => __ ( 'Unique ID for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
),
),
2016-06-15 18:51:16 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
2016-06-17 21:14:39 +00:00
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
2016-06-15 18:51:16 +00:00
),
2016-06-15 20:18:35 +00:00
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_items' ),
2016-06-17 21:14:39 +00:00
'permission_callback' => array ( $this , 'update_items_permissions_check' ),
2016-06-15 20:18:35 +00:00
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
2016-06-15 18:51:16 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
) );
}
/**
2016-06-15 20:18:35 +00:00
* Get all Shipping Zone Locations .
2016-06-15 18:51:16 +00:00
*
* @ param WP_REST_Request $request
2016-06-15 20:18:35 +00:00
* @ return WP_REST_Response | WP_Error
2016-06-15 18:51:16 +00:00
*/
public function get_items ( $request ) {
2017-01-26 18:34:11 +00:00
$zone = $this -> get_zone ( ( int ) $request [ 'id' ] );
2016-06-15 18:51:16 +00:00
if ( is_wp_error ( $zone ) ) {
return $zone ;
}
2016-07-26 18:42:57 +00:00
2016-06-15 18:51:16 +00:00
$locations = $zone -> get_zone_locations ();
$data = array ();
foreach ( $locations as $location_obj ) {
$location = $this -> prepare_item_for_response ( $location_obj , $request );
$location = $this -> prepare_response_for_collection ( $location );
$data [] = $location ;
}
return rest_ensure_response ( $data );
}
2016-06-15 20:18:35 +00:00
/**
* Update all Shipping Zone Locations .
*
* @ param WP_REST_Request $request
* @ return WP_REST_Response | WP_Error
*/
public function update_items ( $request ) {
2017-01-26 18:34:11 +00:00
$zone = $this -> get_zone ( ( int ) $request [ 'id' ] );
2016-06-15 20:18:35 +00:00
if ( is_wp_error ( $zone ) ) {
return $zone ;
}
2017-05-22 14:18:55 +00:00
if ( 0 === $zone -> get_id () ) {
2017-07-11 07:19:42 +00:00
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 ) );
2017-05-22 14:18:55 +00:00
}
2016-06-15 20:18:35 +00:00
$raw_locations = $request -> get_json_params ();
$locations = array ();
2016-06-15 22:22:57 +00:00
foreach ( ( array ) $raw_locations as $raw_location ) {
2017-03-24 00:41:56 +00:00
if ( empty ( $raw_location [ 'code' ] ) ) {
2016-06-15 20:18:35 +00:00
continue ;
}
2017-03-24 00:41:56 +00:00
$type = ! empty ( $raw_location [ 'type' ] ) ? sanitize_text_field ( $raw_location [ 'type' ] ) : 'country' ;
if ( ! in_array ( $type , array ( 'postcode' , 'state' , 'country' , 'continent' ), true ) ) {
continue ;
}
$locations [] = array (
'code' => sanitize_text_field ( $raw_location [ 'code' ] ),
'type' => sanitize_text_field ( $type ),
);
2016-06-15 20:18:35 +00:00
}
$zone -> set_locations ( $locations );
$zone -> save ();
return $this -> get_items ( $request );
}
2016-06-15 18:51:16 +00:00
/**
* Prepare the Shipping Zone Location for the REST response .
2016-06-15 15:38:26 +00:00
*
2016-06-15 18:51:16 +00:00
* @ param array $item Shipping Zone Location .
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $response
2016-06-15 15:38:26 +00:00
*/
2016-06-15 18:51:16 +00:00
public function prepare_item_for_response ( $item , $request ) {
$context = empty ( $request [ 'context' ] ) ? 'view' : $request [ 'context' ];
$data = $this -> add_additional_fields_to_object ( $item , $request );
$data = $this -> filter_response_by_context ( $data , $context );
// Wrap the data in a response object.
$response = rest_ensure_response ( $data );
2017-01-26 18:34:11 +00:00
$response -> add_links ( $this -> prepare_links ( ( int ) $request [ 'id' ] ) );
2016-06-15 18:51:16 +00:00
return $response ;
}
2016-06-15 15:38:26 +00:00
/**
2016-06-15 18:51:16 +00:00
* Prepare links for the request .
2016-06-15 15:38:26 +00:00
*
2016-06-15 18:51:16 +00:00
* @ param int $zone_id Given Shipping Zone ID .
* @ return array Links for the given Shipping Zone Location .
2016-06-15 15:38:26 +00:00
*/
2016-06-15 18:51:16 +00:00
protected function prepare_links ( $zone_id ) {
$base = '/' . $this -> namespace . '/' . $this -> rest_base . '/' . $zone_id ;
$links = array (
'collection' => array (
'href' => rest_url ( $base . '/locations' ),
),
'describes' => array (
'href' => rest_url ( $base ),
),
);
return $links ;
}
2016-06-15 15:38:26 +00:00
/**
* Get the Shipping Zone Locations schema , conforming to JSON Schema
*
* @ return array
*/
public function get_item_schema () {
2016-06-17 20:59:36 +00:00
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => 'shipping_zone_location' ,
2016-06-15 15:38:26 +00:00
'type' => 'object' ,
'properties' => array (
'code' => array (
'description' => __ ( 'Shipping zone location code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'type' => array (
'description' => __ ( 'Shipping zone location type.' , 'woocommerce' ),
'type' => 'string' ,
2017-03-24 00:41:56 +00:00
'default' => 'country' ,
2016-06-15 15:38:26 +00:00
'enum' => array (
'postcode' ,
'state' ,
'country' ,
'continent' ,
),
2017-03-24 00:41:56 +00:00
'context' => array ( 'view' , 'edit' ),
2016-06-15 15:38:26 +00:00
),
),
);
2016-06-17 20:59:36 +00:00
return $this -> add_additional_fields_schema ( $schema );
2016-06-15 15:38:26 +00:00
}
2016-07-26 18:42:57 +00:00
}