2016-02-17 19:29:09 +00:00
< ? php
/**
* REST API Taxes controller
*
* Handles requests to the / taxes endpoint .
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.6 . 0
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Taxes controller class .
*
* @ package WooCommerce / API
2016-02-22 19:43:52 +00:00
* @ extends WP_REST_Controller
2016-02-17 19:29:09 +00:00
*/
2016-02-22 19:43:52 +00:00
class WC_REST_Taxes_Controller extends WP_REST_Controller {
2016-02-17 19:29:09 +00:00
2016-03-07 18:36:17 +00:00
/**
* Endpoint namespace .
*
* @ var string
*/
2016-03-07 18:45:10 +00:00
public $namespace = 'wc/v1' ;
2016-03-07 18:36:17 +00:00
2016-02-17 19:29:09 +00:00
/**
* Route base .
*
* @ var string
*/
2016-02-22 18:49:38 +00:00
protected $rest_base = 'taxes' ;
2016-02-17 19:29:09 +00:00
/**
* Register the routes for coupons .
*/
public function register_routes () {
2016-03-08 22:51:36 +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' ),
'args' => $this -> get_collection_params (),
),
array (
'methods' => WP_REST_Server :: CREATABLE ,
'callback' => array ( $this , 'create_item' ),
'permission_callback' => array ( $this , 'create_item_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: CREATABLE ),
),
'schema' => array ( $this , 'get_public_item_schema' ),
) );
2016-02-17 19:29:09 +00:00
2016-03-08 22:51:36 +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_item_permissions_check' ),
'args' => array (
'context' => $this -> get_context_param ( array ( 'default' => 'view' ) ),
),
),
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_item' ),
'permission_callback' => array ( $this , 'update_item_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_item_permissions_check' ),
'args' => array (
'force' => array (
'default' => false ,
'description' => __ ( 'Required to be true, as resource does not support trashing.' , 'woocommerce' ),
),
'reassign' => array (),
),
),
'schema' => array ( $this , 'get_public_item_schema' ),
) );
}
2016-03-09 04:12:21 +00:00
/**
* Check if a given request has access to read a tax .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_item_permissions_check ( $request ) {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return new WP_Error ( 'woocommerce_rest_tax_cannot_view' , __ ( 'Sorry, you cannot view this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Get a single tax .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | WP_REST_Response
*/
public function get_item ( $request ) {
$id = ( int ) $request [ 'id' ];
$tax_obj = WC_Tax :: _get_tax_rate ( $id , OBJECT );
if ( empty ( $id ) || empty ( $tax_obj ) ) {
return new WP_Error ( 'woocommerce_rest_tax_invalid_id' , __ ( 'Invalid resource id.' , 'woocommerce' ), array ( 'status' => 404 ) );
}
$tax = $this -> prepare_item_for_response ( $tax_obj , $request );
$response = rest_ensure_response ( $tax );
return $response ;
}
/**
* Prepare a single tax output for response .
*
* @ param stdClass $tax Tax object .
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $response Response data .
*/
public function prepare_item_for_response ( $tax , $request ) {
global $wpdb ;
$id = ( int ) $tax -> tax_rate_id ;
$data = array (
'id' => $id ,
'country' => $tax -> tax_rate_country ,
'state' => $tax -> tax_rate_state ,
'postcode' => '' ,
'city' => '' ,
'rate' => $tax -> tax_rate ,
'name' => $tax -> tax_rate_name ,
'priority' => ( int ) $tax -> tax_rate_priority ,
'compound' => ( bool ) $tax -> tax_rate_compound ,
'shipping' => ( bool ) $tax -> tax_rate_shipping ,
'order' => ( int ) $tax -> tax_rate_order ,
'class' => $tax -> tax_rate_class ? $tax -> tax_rate_class : 'standard' ,
);
// Get locales from a tax rate.
$locales = $wpdb -> get_results ( $wpdb -> prepare ( "
SELECT location_code , location_type
FROM { $wpdb -> prefix } woocommerce_tax_rate_locations
WHERE tax_rate_id = % d
" , $id ) );
if ( ! is_wp_error ( $tax ) && ! is_null ( $tax ) ) {
foreach ( $locales as $locale ) {
$data [ $locale -> location_type ] = $locale -> location_code ;
}
}
$context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ;
$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 ( $tax ) );
/**
* Filter tax object returned from the REST API .
*
* @ param WP_REST_Response $response The response object .
* @ param stdClass $tax Tax object used to create response .
* @ param WP_REST_Request $request Request object .
*/
return apply_filters ( 'woocommerce_rest_prepare_tax' , $response , $tax , $request );
}
/**
* Prepare links for the request .
*
* @ param stdClass $tax Tax object .
* @ return array Links for the given user .
*/
protected function prepare_links ( $tax ) {
$links = array (
'self' => array (
'href' => rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $this -> rest_base , $tax -> tax_rate_id ) ),
),
'collection' => array (
'href' => rest_url ( sprintf ( '/%s/%s' , $this -> namespace , $this -> rest_base ) ),
),
);
return $links ;
}
2016-03-08 22:51:36 +00:00
/**
* Get the User ' s schema , conforming to JSON Schema
*
* @ return array
*/
public function get_item_schema () {
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
2016-03-09 04:12:21 +00:00
'title' => 'tax' ,
2016-03-08 22:51:36 +00:00
'type' => 'object' ,
'properties' => array (
'id' => array (
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'country' => array (
'description' => __ ( 'Country ISO 3166 code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'state' => array (
'description' => __ ( 'State code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'postcode' => array (
'description' => __ ( 'Postcode/ZIP.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'city' => array (
'description' => __ ( 'City name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'rate' => array (
'description' => __ ( 'Tax rate.' , 'woocommerce' ),
'type' => 'float' ,
'context' => array ( 'view' , 'edit' ),
),
'name' => array (
'description' => __ ( 'Tax rate name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'priority' => array (
'description' => __ ( 'Customer password.' , 'woocommerce' ),
'type' => 'integer' ,
'default' => 1 ,
'context' => array ( 'view' , 'edit' ),
),
'compound' => array (
'description' => __ ( 'Whether or not this is a compound rate.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
),
'shipping' => array (
'description' => __ ( 'Whether or not this tax rate also gets applied to shipping.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => true ,
'context' => array ( 'view' , 'edit' ),
),
'order' => array (
'description' => __ ( 'Indicates the order that will appear in queries.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'class' => array (
'description' => __ ( 'Tax class.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'standard' ,
'enum' => array_merge ( array ( 'standard' ), array_map ( 'sanitize_title' , WC_Tax :: get_tax_classes () ) ),
'context' => array ( 'view' , 'edit' ),
),
),
);
return $this -> add_additional_fields_schema ( $schema );
2016-02-17 19:29:09 +00:00
}
2016-03-09 04:12:21 +00:00
/**
* Get the query params for collections .
*
* @ return array
*/
public function get_collection_params () {
$query_params = parent :: get_collection_params ();
$query_params [ 'context' ][ 'default' ] = 'view' ;
$query_params [ 'exclude' ] = array (
'description' => __ ( 'Ensure result set excludes specific ids.' , 'woocommerce' ),
'type' => 'array' ,
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
);
$query_params [ 'include' ] = array (
'description' => __ ( 'Limit result set to specific ids.' , 'woocommerce' ),
'type' => 'array' ,
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
);
$query_params [ 'offset' ] = array (
'description' => __ ( 'Offset the result set by a specific number of items.' , 'woocommerce' ),
'type' => 'integer' ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$query_params [ 'order' ] = array (
'default' => 'asc' ,
'description' => __ ( 'Order sort attribute ascending or descending.' , 'woocommerce' ),
'enum' => array ( 'asc' , 'desc' ),
'sanitize_callback' => 'sanitize_key' ,
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$query_params [ 'orderby' ] = array (
'default' => 'name' ,
'description' => __ ( 'Sort collection by object attribute.' , 'woocommerce' ),
'enum' => array (
'id' ,
'include' ,
'name' ,
'registered_date' ,
),
'sanitize_callback' => 'sanitize_key' ,
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$query_params [ 'class' ] = array (
'description' => __ ( 'Sort by tax class.' , 'woocommerce' ),
'enum' => array_merge ( array ( 'standard' ), array_map ( 'sanitize_title' , WC_Tax :: get_tax_classes () ) ),
'sanitize_callback' => 'sanitize_key' ,
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
return $query_params ;
}
2016-02-17 19:29:09 +00:00
}