2015-10-07 14:42:25 +00:00
< ? php
/**
* WooCommerce API Taxes Class
*
* Handles requests to the / taxes endpoint
*
* @ author WooThemes
* @ category API
2020-09-17 14:56:08 +00:00
* @ package WooCommerce\RestApi
2015-10-07 14:42:25 +00:00
* @ since 2.5 . 0
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
class WC_API_Taxes extends WC_API_Resource {
/** @var string $base the route base */
protected $base = '/taxes' ;
/**
* Register the routes for this class
*
* GET / taxes
* GET / taxes / count
* GET / taxes /< id >
*
* @ since 2.1
* @ param array $routes
* @ return array
*/
public function register_routes ( $routes ) {
# GET/POST /taxes
$routes [ $this -> base ] = array (
2015-10-07 20:44:04 +00:00
array ( array ( $this , 'get_taxes' ), WC_API_Server :: READABLE ),
2015-10-07 14:42:25 +00:00
array ( array ( $this , 'create_tax' ), WC_API_Server :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
);
# GET /taxes/count
2016-08-27 03:11:30 +00:00
$routes [ $this -> base . '/count' ] = array (
2015-10-07 14:42:25 +00:00
array ( array ( $this , 'get_taxes_count' ), WC_API_Server :: READABLE ),
);
# GET/PUT/DELETE /taxes/<id>
$routes [ $this -> base . '/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_tax' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'edit_tax' ), WC_API_SERVER :: EDITABLE | WC_API_SERVER :: ACCEPT_DATA ),
array ( array ( $this , 'delete_tax' ), WC_API_SERVER :: DELETABLE ),
);
2015-10-08 01:35:34 +00:00
# GET/POST /taxes/classes
2015-10-07 20:44:04 +00:00
$routes [ $this -> base . '/classes' ] = array (
array ( array ( $this , 'get_tax_classes' ), WC_API_Server :: READABLE ),
2015-10-08 01:52:45 +00:00
array ( array ( $this , 'create_tax_class' ), WC_API_Server :: CREATABLE | WC_API_Server :: ACCEPT_DATA ),
2015-10-07 20:44:04 +00:00
);
2015-10-08 01:35:34 +00:00
# GET /taxes/classes/count
2016-08-27 03:11:30 +00:00
$routes [ $this -> base . '/classes/count' ] = array (
2015-10-08 01:35:34 +00:00
array ( array ( $this , 'get_tax_classes_count' ), WC_API_Server :: READABLE ),
);
# GET /taxes/classes/<slug>
2015-10-08 01:27:42 +00:00
$routes [ $this -> base . '/classes/(?P<slug>\w[\w\s\-]*)' ] = array (
array ( array ( $this , 'delete_tax_class' ), WC_API_SERVER :: DELETABLE ),
);
2015-10-07 14:42:25 +00:00
# POST|PUT /taxes/bulk
$routes [ $this -> base . '/bulk' ] = array (
array ( array ( $this , 'bulk' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
);
return $routes ;
}
2015-10-07 20:44:04 +00:00
/**
* Get all taxes
*
* @ since 2.5 . 0
*
* @ param string $fields
2015-10-07 22:00:36 +00:00
* @ param array $filter
* @ param string $class
* @ param int $page
2015-10-07 20:44:04 +00:00
*
* @ return array
*/
2015-10-07 22:00:36 +00:00
public function get_taxes ( $fields = null , $filter = array (), $class = null , $page = 1 ) {
if ( ! empty ( $class ) ) {
2015-10-07 22:07:45 +00:00
$filter [ 'tax_rate_class' ] = $class ;
2015-10-07 22:00:36 +00:00
}
$filter [ 'page' ] = $page ;
$query = $this -> query_tax_rates ( $filter );
$taxes = array ();
foreach ( $query [ 'results' ] as $tax ) {
$taxes [] = current ( $this -> get_tax ( $tax -> tax_rate_id , $fields ) );
}
2015-10-07 20:44:04 +00:00
2015-10-07 22:00:36 +00:00
// Set pagination headers
$this -> server -> add_pagination_headers ( $query [ 'headers' ] );
2015-10-08 21:49:05 +00:00
return array ( 'taxes' => $taxes );
2015-10-07 20:44:04 +00:00
}
/**
* Get the tax for the given ID
*
* @ since 2.5 . 0
*
2015-10-08 01:27:42 +00:00
* @ param int $id The tax ID
2015-10-07 20:44:04 +00:00
* @ param string $fields fields to include in response
*
* @ return array | WP_Error
*/
public function get_tax ( $id , $fields = null ) {
global $wpdb ;
try {
$id = absint ( $id );
// Permissions check
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
2015-10-08 21:49:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_tax' , __ ( 'You do not have permission to read tax rate' , 'woocommerce' ), 401 );
2015-10-07 20:44:04 +00:00
}
// Get tax rate details
2015-10-08 20:24:37 +00:00
$tax = WC_Tax :: _get_tax_rate ( $id );
2015-10-07 20:44:04 +00:00
2015-10-08 20:24:37 +00:00
if ( is_wp_error ( $tax ) || empty ( $tax ) ) {
2015-10-08 21:49:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_invalid_tax_id' , __ ( 'A tax rate with the provided ID could not be found' , 'woocommerce' ), 404 );
2015-10-07 20:44:04 +00:00
}
$tax_data = array (
2015-12-18 21:15:56 +00:00
'id' => ( int ) $tax [ 'tax_rate_id' ],
2015-10-08 20:24:37 +00:00
'country' => $tax [ 'tax_rate_country' ],
'state' => $tax [ 'tax_rate_state' ],
2015-10-07 20:44:04 +00:00
'postcode' => '' ,
'city' => '' ,
2015-10-08 20:24:37 +00:00
'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' ],
2016-08-27 01:46:45 +00:00
'class' => $tax [ 'tax_rate_class' ] ? $tax [ 'tax_rate_class' ] : 'standard' ,
2015-10-07 20:44:04 +00:00
);
// 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 ) {
$tax_data [ $locale -> location_type ] = $locale -> location_code ;
}
}
2015-10-08 21:49:05 +00:00
return array ( 'tax' => apply_filters ( 'woocommerce_api_tax_response' , $tax_data , $tax , $fields , $this ) );
2015-10-07 20:44:04 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-10-07 14:42:25 +00:00
/**
* Create a tax
*
* @ since 2.5 . 0
*
* @ param array $data
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-07 14:42:25 +00:00
*/
public function create_tax ( $data ) {
2015-10-08 20:15:00 +00:00
try {
2015-10-08 21:49:05 +00:00
if ( ! isset ( $data [ 'tax' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_tax_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'tax' ), 400 );
2015-10-08 20:15:00 +00:00
}
// Check permissions
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
2015-10-08 21:49:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_tax' , __ ( 'You do not have permission to create tax rates' , 'woocommerce' ), 401 );
2015-10-08 20:15:00 +00:00
}
2015-10-08 21:49:05 +00:00
$data = apply_filters ( 'woocommerce_api_create_tax_data' , $data [ 'tax' ], $this );
2015-10-07 14:42:25 +00:00
2015-10-08 20:15:00 +00:00
$tax_data = array (
'tax_rate_country' => '' ,
'tax_rate_state' => '' ,
'tax_rate' => '' ,
'tax_rate_name' => '' ,
'tax_rate_priority' => 1 ,
'tax_rate_compound' => 0 ,
'tax_rate_shipping' => 1 ,
'tax_rate_order' => 0 ,
'tax_rate_class' => '' ,
);
foreach ( $tax_data as $key => $value ) {
2015-10-08 21:34:47 +00:00
$new_key = str_replace ( 'tax_rate_' , '' , $key );
$new_key = 'tax_rate' === $new_key ? 'rate' : $new_key ;
2015-10-08 20:15:00 +00:00
2015-10-08 21:34:47 +00:00
if ( isset ( $data [ $new_key ] ) ) {
if ( in_array ( $new_key , array ( 'compound' , 'shipping' ) ) ) {
$tax_data [ $key ] = $data [ $new_key ] ? 1 : 0 ;
2015-10-08 20:15:00 +00:00
} else {
2015-10-08 21:34:47 +00:00
$tax_data [ $key ] = $data [ $new_key ];
2015-10-08 20:15:00 +00:00
}
}
}
// Create tax rate
$id = WC_Tax :: _insert_tax_rate ( $tax_data );
// Add locales
if ( ! empty ( $data [ 'postcode' ] ) ) {
WC_Tax :: _update_tax_rate_postcodes ( $id , wc_clean ( $data [ 'postcode' ] ) );
}
if ( ! empty ( $data [ 'city' ] ) ) {
WC_Tax :: _update_tax_rate_cities ( $id , wc_clean ( $data [ 'city' ] ) );
}
2015-11-06 04:50:07 +00:00
do_action ( 'woocommerce_api_create_tax' , $id , $data );
2015-10-08 20:15:00 +00:00
$this -> server -> send_status ( 201 );
return $this -> get_tax ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2015-10-07 14:42:25 +00:00
}
/**
* Edit a tax
*
* @ since 2.5 . 0
*
2015-10-08 01:27:42 +00:00
* @ param int $id The tax ID
2015-10-07 14:42:25 +00:00
* @ param array $data
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-07 14:42:25 +00:00
*/
public function edit_tax ( $id , $data ) {
2015-10-08 21:34:47 +00:00
try {
2015-10-08 21:49:05 +00:00
if ( ! isset ( $data [ 'tax' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_tax_data' , sprintf ( __ ( 'No %1$s data specified to edit %1$s' , 'woocommerce' ), 'tax' ), 400 );
2015-10-08 21:34:47 +00:00
}
// Check permissions
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
2015-10-08 21:49:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_edit_tax' , __ ( 'You do not have permission to edit tax rates' , 'woocommerce' ), 401 );
2015-10-08 21:34:47 +00:00
}
2015-10-08 21:49:05 +00:00
$data = $data [ 'tax' ];
2015-10-08 21:34:47 +00:00
// Get current tax rate data
$tax = $this -> get_tax ( $id );
if ( is_wp_error ( $tax ) ) {
$error_data = $tax -> get_error_data ();
throw new WC_API_Exception ( $tax -> get_error_code (), $tax -> get_error_message (), $error_data [ 'status' ] );
}
2015-10-08 21:49:05 +00:00
$current_data = $tax [ 'tax' ];
$data = apply_filters ( 'woocommerce_api_edit_tax_data' , $data , $this );
2015-10-08 21:34:47 +00:00
$tax_data = array ();
$default_fields = array (
'tax_rate_country' ,
'tax_rate_state' ,
'tax_rate' ,
'tax_rate_name' ,
'tax_rate_priority' ,
'tax_rate_compound' ,
'tax_rate_shipping' ,
'tax_rate_order' ,
2016-08-27 02:08:49 +00:00
'tax_rate_class' ,
2015-10-08 21:34:47 +00:00
);
foreach ( $data as $key => $value ) {
$new_key = 'rate' === $key ? 'tax_rate' : 'tax_rate_' . $key ;
// Check if the key is valid
if ( ! in_array ( $new_key , $default_fields ) ) {
continue ;
}
// Test new data against current data
if ( $value === $current_data [ $key ] ) {
continue ;
}
2017-07-17 10:10:52 +00:00
// Fix compound and shipping values
2015-10-08 21:34:47 +00:00
if ( in_array ( $key , array ( 'compound' , 'shipping' ) ) ) {
$value = $value ? 1 : 0 ;
}
$tax_data [ $new_key ] = $value ;
}
// Update tax rate
WC_Tax :: _update_tax_rate ( $id , $tax_data );
// Update locales
if ( ! empty ( $data [ 'postcode' ] ) && $current_data [ 'postcode' ] != $data [ 'postcode' ] ) {
WC_Tax :: _update_tax_rate_postcodes ( $id , wc_clean ( $data [ 'postcode' ] ) );
}
if ( ! empty ( $data [ 'city' ] ) && $current_data [ 'city' ] != $data [ 'city' ] ) {
WC_Tax :: _update_tax_rate_cities ( $id , wc_clean ( $data [ 'city' ] ) );
}
2015-11-06 04:44:02 +00:00
do_action ( 'woocommerce_api_edit_tax_rate' , $id , $data );
2015-10-08 21:34:47 +00:00
return $this -> get_tax ( $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2015-10-07 14:42:25 +00:00
}
/**
* Delete a tax
*
* @ since 2.5 . 0
*
2015-10-08 01:27:42 +00:00
* @ param int $id The tax ID
2015-10-07 14:42:25 +00:00
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-07 14:42:25 +00:00
*/
2015-10-08 01:10:33 +00:00
public function delete_tax ( $id ) {
global $wpdb ;
try {
// Check permissions
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
2015-10-08 21:49:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_tax' , __ ( 'You do not have permission to delete tax rates' , 'woocommerce' ), 401 );
2015-10-08 01:10:33 +00:00
}
$id = absint ( $id );
2015-10-07 14:42:25 +00:00
2015-10-08 01:10:33 +00:00
WC_Tax :: _delete_tax_rate ( $id );
if ( 0 === $wpdb -> rows_affected ) {
2015-10-08 21:49:05 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_tax' , __ ( 'Could not delete the tax rate' , 'woocommerce' ), 401 );
2015-10-08 01:10:33 +00:00
}
2015-10-08 21:49:05 +00:00
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'tax' ) );
2015-10-08 01:10:33 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2015-10-07 14:42:25 +00:00
}
2015-10-08 01:52:45 +00:00
/**
* Get the total number of taxes
*
* @ since 2.5 . 0
*
* @ param string $class
* @ param array $filter
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-08 01:52:45 +00:00
*/
public function get_taxes_count ( $class = null , $filter = array () ) {
try {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_taxes_count' , __ ( 'You do not have permission to read the taxes count' , 'woocommerce' ), 401 );
}
if ( ! empty ( $class ) ) {
$filter [ 'tax_rate_class' ] = $class ;
}
$query = $this -> query_tax_rates ( $filter , true );
return array ( 'count' => ( int ) $query [ 'headers' ] -> total );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-10-07 22:00:36 +00:00
/**
* Helper method to get tax rates objects
*
* @ since 2.5 . 0
*
* @ param array $args
2015-11-06 03:37:57 +00:00
* @ param bool $count_only
2015-10-07 22:00:36 +00:00
*
* @ return array
*/
2015-10-07 22:07:45 +00:00
protected function query_tax_rates ( $args , $count_only = false ) {
2015-10-07 22:00:36 +00:00
global $wpdb ;
2015-10-07 22:07:45 +00:00
$results = '' ;
2015-10-07 22:00:36 +00:00
// Set args
$args = $this -> merge_query_args ( $args , array () );
$query = "
SELECT tax_rate_id
FROM { $wpdb -> prefix } woocommerce_tax_rates
WHERE 1 = 1
" ;
// Filter by tax class
2015-10-07 22:07:45 +00:00
if ( ! empty ( $args [ 'tax_rate_class' ] ) ) {
$tax_rate_class = 'standard' !== $args [ 'tax_rate_class' ] ? sanitize_title ( $args [ 'tax_rate_class' ] ) : '' ;
$query .= " AND tax_rate_class = ' $tax_rate_class ' " ;
2015-10-07 22:00:36 +00:00
}
// Order tax rates
$order_by = ' ORDER BY tax_rate_order' ;
// Pagination
$per_page = isset ( $args [ 'posts_per_page' ] ) ? $args [ 'posts_per_page' ] : get_option ( 'posts_per_page' );
$offset = 1 < $args [ 'paged' ] ? ( $args [ 'paged' ] - 1 ) * $per_page : 0 ;
$pagination = sprintf ( ' LIMIT %d, %d' , $offset , $per_page );
2015-10-07 22:07:45 +00:00
if ( ! $count_only ) {
$results = $wpdb -> get_results ( $query . $order_by . $pagination );
}
2015-10-07 22:00:36 +00:00
$wpdb -> get_results ( $query );
$headers = new stdClass ;
$headers -> page = $args [ 'paged' ];
$headers -> total = ( int ) $wpdb -> num_rows ;
$headers -> is_single = $per_page > $headers -> total ;
$headers -> total_pages = ceil ( $headers -> total / $per_page );
return array (
'results' => $results ,
2016-08-27 01:46:45 +00:00
'headers' => $headers ,
2015-10-07 22:00:36 +00:00
);
}
2015-10-07 14:42:25 +00:00
/**
* Bulk update or insert taxes
* Accepts an array with taxes in the formats supported by
* WC_API_Taxes -> create_tax () and WC_API_Taxes -> edit_tax ()
*
* @ since 2.5 . 0
*
* @ param array $data
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-07 14:42:25 +00:00
*/
public function bulk ( $data ) {
2015-10-08 21:44:26 +00:00
try {
2015-10-08 21:49:05 +00:00
if ( ! isset ( $data [ 'taxes' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_taxes_data' , sprintf ( __ ( 'No %1$s data specified to create/edit %1$s' , 'woocommerce' ), 'taxes' ), 400 );
2015-10-08 21:44:26 +00:00
}
2015-10-08 21:49:05 +00:00
$data = $data [ 'taxes' ];
$limit = apply_filters ( 'woocommerce_api_bulk_limit' , 100 , 'taxes' );
2015-10-08 21:44:26 +00:00
// Limit bulk operation
if ( count ( $data ) > $limit ) {
2016-10-11 01:39:13 +00:00
throw new WC_API_Exception ( 'woocommerce_api_taxes_request_entity_too_large' , sprintf ( __ ( 'Unable to accept more than %s items for this request.' , 'woocommerce' ), $limit ), 413 );
2015-10-08 21:44:26 +00:00
}
$taxes = array ();
2015-10-07 14:42:25 +00:00
2015-10-08 21:44:26 +00:00
foreach ( $data as $_tax ) {
$tax_id = 0 ;
// Try to get the tax rate ID
if ( isset ( $_tax [ 'id' ] ) ) {
$tax_id = intval ( $_tax [ 'id' ] );
}
if ( $tax_id ) {
2016-09-02 03:15:49 +00:00
// Tax rate exists / edit tax rate
2015-10-08 21:49:05 +00:00
$edit = $this -> edit_tax ( $tax_id , array ( 'tax' => $_tax ) );
2015-10-08 21:44:26 +00:00
if ( is_wp_error ( $edit ) ) {
$taxes [] = array (
'id' => $tax_id ,
2016-08-27 01:46:45 +00:00
'error' => array ( 'code' => $edit -> get_error_code (), 'message' => $edit -> get_error_message () ),
2015-10-08 21:44:26 +00:00
);
} else {
2015-10-08 21:49:05 +00:00
$taxes [] = $edit [ 'tax' ];
2015-10-08 21:44:26 +00:00
}
2016-09-02 03:15:49 +00:00
} else {
2015-10-08 21:44:26 +00:00
2016-09-02 03:15:49 +00:00
// Tax rate don't exists / create tax rate
2015-10-08 21:49:05 +00:00
$new = $this -> create_tax ( array ( 'tax' => $_tax ) );
2015-10-08 21:44:26 +00:00
if ( is_wp_error ( $new ) ) {
$taxes [] = array (
'id' => $tax_id ,
2016-08-27 01:46:45 +00:00
'error' => array ( 'code' => $new -> get_error_code (), 'message' => $new -> get_error_message () ),
2015-10-08 21:44:26 +00:00
);
} else {
2015-10-08 21:49:05 +00:00
$taxes [] = $new [ 'tax' ];
2015-10-08 21:44:26 +00:00
}
}
}
2015-12-18 21:08:09 +00:00
return array ( 'taxes' => apply_filters ( 'woocommerce_api_taxes_bulk_response' , $taxes , $this ) );
2015-10-08 21:44:26 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
2015-10-07 14:42:25 +00:00
}
2015-10-08 01:27:42 +00:00
/**
* Get all tax classes
*
* @ since 2.5 . 0
*
* @ param string $fields
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-08 01:27:42 +00:00
*/
public function get_tax_classes ( $fields = null ) {
try {
// Permissions check
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_tax_classes' , __ ( 'You do not have permission to read tax classes' , 'woocommerce' ), 401 );
}
$tax_classes = array ();
// Add standard class
$tax_classes [] = array (
'slug' => 'standard' ,
2016-10-12 10:16:30 +00:00
'name' => __ ( 'Standard rate' , 'woocommerce' ),
2015-10-08 01:27:42 +00:00
);
$classes = WC_Tax :: get_tax_classes ();
foreach ( $classes as $class ) {
$tax_classes [] = apply_filters ( 'woocommerce_api_tax_class_response' , array (
'slug' => sanitize_title ( $class ),
2016-08-27 01:46:45 +00:00
'name' => $class ,
2015-10-08 01:27:42 +00:00
), $class , $fields , $this );
}
return array ( 'tax_classes' => apply_filters ( 'woocommerce_api_tax_classes_response' , $tax_classes , $classes , $fields , $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-10-08 01:35:34 +00:00
/**
2015-11-06 03:58:08 +00:00
* Create a tax class .
2015-10-08 01:35:34 +00:00
*
* @ since 2.5 . 0
*
2015-10-08 01:52:45 +00:00
* @ param array $data
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-08 01:35:34 +00:00
*/
2015-10-08 01:52:45 +00:00
public function create_tax_class ( $data ) {
2015-10-08 01:35:34 +00:00
try {
2015-10-08 01:52:45 +00:00
if ( ! isset ( $data [ 'tax_class' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_tax_class_data' , sprintf ( __ ( 'No %1$s data specified to create %1$s' , 'woocommerce' ), 'tax_class' ), 400 );
}
// Check permissions
2015-10-08 01:35:34 +00:00
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
2015-10-08 01:52:45 +00:00
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_create_tax_class' , __ ( 'You do not have permission to create tax classes' , 'woocommerce' ), 401 );
2015-10-08 01:35:34 +00:00
}
2015-10-08 01:52:45 +00:00
$data = $data [ 'tax_class' ];
2015-10-08 01:35:34 +00:00
2015-10-08 01:52:45 +00:00
if ( empty ( $data [ 'name' ] ) ) {
throw new WC_API_Exception ( 'woocommerce_api_missing_tax_class_name' , sprintf ( __ ( 'Missing parameter %s' , 'woocommerce' ), 'name' ), 400 );
}
2019-03-20 14:21:39 +00:00
$name = sanitize_text_field ( $data [ 'name' ] );
$tax_class = WC_Tax :: create_tax_class ( $name );
2015-10-08 01:52:45 +00:00
2019-03-20 14:21:39 +00:00
if ( is_wp_error ( $tax_class ) ) {
return new WP_Error ( 'woocommerce_api_' . $tax_class -> get_error_code (), $tax_class -> get_error_message (), 401 );
2015-10-08 01:52:45 +00:00
}
2019-03-20 14:21:39 +00:00
do_action ( 'woocommerce_api_create_tax_class' , $tax_class [ 'slug' ], $data );
2015-10-08 01:52:45 +00:00
$this -> server -> send_status ( 201 );
return array (
2019-03-20 14:21:39 +00:00
'tax_class' => $tax_class ,
2015-10-08 01:52:45 +00:00
);
2015-10-08 01:35:34 +00:00
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-10-08 01:27:42 +00:00
/**
* Delete a tax class
*
* @ since 2.5 . 0
*
* @ param int $slug The tax class slug
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-08 01:27:42 +00:00
*/
public function delete_tax_class ( $slug ) {
2015-12-18 22:46:28 +00:00
global $wpdb ;
2015-10-08 01:27:42 +00:00
try {
// Check permissions
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_delete_tax_class' , __ ( 'You do not have permission to delete tax classes' , 'woocommerce' ), 401 );
}
2019-03-20 14:21:39 +00:00
$slug = sanitize_title ( $slug );
$tax_class = WC_Tax :: get_tax_class_by ( 'slug' , $slug );
$deleted = WC_Tax :: delete_tax_class_by ( 'slug' , $slug );
2015-10-08 01:27:42 +00:00
2019-03-20 14:21:39 +00:00
if ( is_wp_error ( $deleted ) || ! $deleted ) {
2015-10-08 01:27:42 +00:00
throw new WC_API_Exception ( 'woocommerce_api_cannot_delete_tax_class' , __ ( 'Could not delete the tax class' , 'woocommerce' ), 401 );
}
return array ( 'message' => sprintf ( __ ( 'Deleted %s' , 'woocommerce' ), 'tax_class' ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-10-08 01:52:45 +00:00
/**
* Get the total number of tax classes
*
* @ since 2.5 . 0
*
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2015-10-08 01:52:45 +00:00
*/
public function get_tax_classes_count () {
try {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
throw new WC_API_Exception ( 'woocommerce_api_user_cannot_read_tax_classes_count' , __ ( 'You do not have permission to read the tax classes count' , 'woocommerce' ), 401 );
}
$total = count ( WC_Tax :: get_tax_classes () ) + 1 ; // +1 for Standard Rate
return array ( 'count' => $total );
} catch ( WC_API_Exception $e ) {
return new WP_Error ( $e -> getErrorCode (), $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
2015-10-07 14:42:25 +00:00
}