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:51:36 +00:00
/**
* Check whether a given request has permission to read taxes .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list taxes.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
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 ;
}
2016-03-09 04:51:36 +00:00
/**
* Get all customers .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | WP_REST_Response
*/
public function get_items ( $request ) {
global $wpdb ;
$prepared_args = array ();
$prepared_args [ 'exclude' ] = $request [ 'exclude' ];
$prepared_args [ 'include' ] = $request [ 'include' ];
$prepared_args [ 'order' ] = $request [ 'order' ];
$prepared_args [ 'number' ] = $request [ 'per_page' ];
if ( ! empty ( $request [ 'offset' ] ) ) {
$prepared_args [ 'offset' ] = $request [ 'offset' ];
} else {
$prepared_args [ 'offset' ] = ( $request [ 'page' ] - 1 ) * $prepared_args [ 'number' ];
}
$orderby_possibles = array (
'id' => 'tax_rate_id' ,
'order' => 'tax_rate_order' ,
);
$prepared_args [ 'orderby' ] = $orderby_possibles [ $request [ 'orderby' ] ];
$prepared_args [ 'class' ] = $request [ 'class' ];
/**
* Filter arguments , before passing to $wpdb -> get_results (), when querying taxes via the REST API .
*
* @ param array $prepared_args Array of arguments for $wpdb -> get_results () .
* @ param WP_REST_Request $request The current request .
*/
$prepared_args = apply_filters ( 'woocommerce_rest_tax_query' , $prepared_args , $request );
$query = "
SELECT *
FROM { $wpdb -> prefix } woocommerce_tax_rates
WHERE 1 = 1
" ;
// Filter by tax class.
if ( ! empty ( $prepared_args [ 'class' ] ) ) {
$class = 'standard' !== $prepared_args [ 'class' ] ? sanitize_title ( $prepared_args [ 'class' ] ) : '' ;
$query .= " AND tax_rate_class = ' $class ' " ;
}
// Order tax rates.
$order_by = sprintf ( ' ORDER BY %s' , sanitize_key ( $prepared_args [ 'orderby' ] ) );
// Pagination.
$pagination = sprintf ( ' LIMIT %d, %d' , $prepared_args [ 'offset' ], $prepared_args [ 'number' ] );
// Query taxes.
$results = $wpdb -> get_results ( $query . $order_by . $pagination );
$taxes = array ();
foreach ( $results as $tax ) {
$data = $this -> prepare_item_for_response ( $tax , $request );
$taxes [] = $this -> prepare_response_for_collection ( $data );
}
$response = rest_ensure_response ( $taxes );
// Store pagation values for headers then unset for count query.
$per_page = ( int ) $prepared_args [ 'number' ];
$page = ceil ( ( ( ( int ) $prepared_args [ 'offset' ] ) / $per_page ) + 1 );
// Query only for ids.
$wpdb -> get_results ( str_replace ( 'SELECT *' , 'SELECT tax_rate_id' , $query ) );
// Calcule totals.
$total_taxes = ( int ) $wpdb -> num_rows ;
$response -> header ( 'X-WP-Total' , ( int ) $total_taxes );
$max_pages = ceil ( $total_taxes / $per_page );
$response -> header ( 'X-WP-TotalPages' , ( int ) $max_pages );
$base = add_query_arg ( $request -> get_query_params (), rest_url ( sprintf ( '/%s/%s' , $this -> namespace , $this -> rest_base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1 ;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages ;
}
$prev_link = add_query_arg ( 'page' , $prev_page , $base );
$response -> link_header ( 'prev' , $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1 ;
$next_link = add_query_arg ( 'page' , $next_page , $base );
$response -> link_header ( 'next' , $next_link );
}
return $response ;
}
2016-03-09 04:12:21 +00:00
/**
* 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 () {
2016-03-09 04:51:36 +00:00
$params = parent :: get_collection_params ();
2016-03-09 04:12:21 +00:00
2016-03-09 04:51:36 +00:00
$params [ 'context' ][ 'default' ] = 'view' ;
2016-03-09 04:12:21 +00:00
2016-03-09 04:51:36 +00:00
$params [ 'exclude' ] = array (
2016-03-09 04:12:21 +00:00
'description' => __ ( 'Ensure result set excludes specific ids.' , 'woocommerce' ),
'type' => 'array' ,
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
);
2016-03-09 04:51:36 +00:00
$params [ 'include' ] = array (
2016-03-09 04:12:21 +00:00
'description' => __ ( 'Limit result set to specific ids.' , 'woocommerce' ),
'type' => 'array' ,
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
);
2016-03-09 04:51:36 +00:00
$params [ 'offset' ] = array (
2016-03-09 04:12:21 +00:00
'description' => __ ( 'Offset the result set by a specific number of items.' , 'woocommerce' ),
'type' => 'integer' ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-09 04:51:36 +00:00
$params [ 'order' ] = array (
2016-03-09 04:12:21 +00:00
'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' ,
);
2016-03-09 04:51:36 +00:00
$params [ 'orderby' ] = array (
'default' => 'order' ,
2016-03-09 04:12:21 +00:00
'description' => __ ( 'Sort collection by object attribute.' , 'woocommerce' ),
'enum' => array (
'id' ,
2016-03-09 04:51:36 +00:00
'order' ,
2016-03-09 04:12:21 +00:00
),
'sanitize_callback' => 'sanitize_key' ,
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-09 04:51:36 +00:00
$params [ 'class' ] = array (
2016-03-09 04:12:21 +00:00
'description' => __ ( 'Sort by tax class.' , 'woocommerce' ),
'enum' => array_merge ( array ( 'standard' ), array_map ( 'sanitize_title' , WC_Tax :: get_tax_classes () ) ),
2016-03-09 04:51:36 +00:00
'sanitize_callback' => 'sanitize_title' ,
2016-03-09 04:12:21 +00:00
'type' => 'string' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2016-03-09 04:51:36 +00:00
return $params ;
2016-03-09 04:12:21 +00:00
}
2016-02-17 19:29:09 +00:00
}