This commit is contained in:
Mike Jolley 2019-05-30 14:17:37 +01:00
parent ed3daf8676
commit ebc4c95960
3 changed files with 132 additions and 87 deletions

View File

@ -11,7 +11,7 @@ namespace WooCommerce\RestApi\Version4\Controllers;
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
use \WC_REST_Terms_Controller; use \WC_REST_Controller;
/** /**
* REST API Tax Class controller class. * REST API Tax Class controller class.

View File

@ -4,30 +4,26 @@
* *
* Handles requests to the /taxes endpoint. * Handles requests to the /taxes endpoint.
* *
* @author WooThemes
* @category API
* @package WooCommerce/RestApi * @package WooCommerce/RestApi
* @since 3.0.0
*/ */
if ( ! defined( 'ABSPATH' ) ) { namespace WooCommerce\RestApi\Version4\Controllers;
exit;
} defined( 'ABSPATH' ) || exit;
use \WC_REST_Controller;
/** /**
* REST API Taxes controller class. * REST API Taxes controller class.
*
* @package WooCommerce/RestApi
* @extends WC_REST_Controller
*/ */
class WC_REST_Taxes_V1_Controller extends WC_REST_Controller { class Taxes extends WC_REST_Controller {
/** /**
* Endpoint namespace. * Endpoint namespace.
* *
* @var string * @var string
*/ */
protected $namespace = 'wc/v1'; protected $namespace = 'wc/v4';
/** /**
* Route base. * Route base.
@ -40,7 +36,10 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
* Register the routes for taxes. * Register the routes for taxes.
*/ */
public function register_routes() { public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array( register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ), 'callback' => array( $this, 'get_items' ),
@ -54,9 +53,13 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
), ),
'schema' => array( $this, 'get_public_item_schema' ), 'schema' => array( $this, 'get_public_item_schema' ),
) ); )
);
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array( 'args' => array(
'id' => array( 'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
@ -90,9 +93,13 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
), ),
), ),
'schema' => array( $this, 'get_public_item_schema' ), 'schema' => array( $this, 'get_public_item_schema' ),
) ); )
);
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/batch',
array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ), 'callback' => array( $this, 'batch_items' ),
@ -100,7 +107,8 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
), ),
'schema' => array( $this, 'get_public_batch_schema' ), 'schema' => array( $this, 'get_public_batch_schema' ),
) ); )
);
} }
/** /**
@ -192,7 +200,7 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
} }
/** /**
* Get all taxes. * Get all taxes and allow filtering by tax code.
* *
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
@ -214,6 +222,8 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
); );
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
$prepared_args['class'] = $request['class']; $prepared_args['class'] = $request['class'];
$prepared_args['code'] = $request['code'];
$prepared_args['include'] = $request['include'];
/** /**
* Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API. * Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API.
@ -235,6 +245,21 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
$query .= " AND tax_rate_class = '$class'"; $query .= " AND tax_rate_class = '$class'";
} }
// Filter by tax code.
$tax_code_search = $prepared_args['code'];
if ( $tax_code_search ) {
$tax_code_search = $wpdb->esc_like( $tax_code_search );
$tax_code_search = ' \'%' . $tax_code_search . '%\'';
$query .= ' AND CONCAT_WS( "-", NULLIF(tax_rate_country, ""), NULLIF(tax_rate_state, ""), NULLIF(tax_rate_name, ""), NULLIF(tax_rate_priority, "") ) LIKE ' . $tax_code_search;
}
// Filter by included tax rate IDs.
$included_taxes = $prepared_args['include'];
if ( ! empty( $included_taxes ) ) {
$included_taxes = implode( ',', $prepared_args['include'] );
$query .= " AND tax_rate_id IN ({$included_taxes})";
}
// Order tax rates. // Order tax rates.
$order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) ); $order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) );
@ -242,7 +267,7 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
$pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] ); $pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] );
// Query taxes. // Query taxes.
$results = $wpdb->get_results( $query . $order_by . $pagination ); $results = $wpdb->get_results( $query . $order_by . $pagination ); // @codingStandardsIgnoreLine.
$taxes = array(); $taxes = array();
foreach ( $results as $tax ) { foreach ( $results as $tax ) {
@ -257,7 +282,7 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
// Query only for ids. // Query only for ids.
$wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) ); $wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) ); // @codingStandardsIgnoreLine.
// Calculate totals. // Calculate totals.
$total_taxes = (int) $wpdb->num_rows; $total_taxes = (int) $wpdb->num_rows;
@ -321,16 +346,16 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
// Add to data array. // Add to data array.
switch ( $key ) { switch ( $key ) {
case 'tax_rate_priority' : case 'tax_rate_priority':
case 'tax_rate_compound' : case 'tax_rate_compound':
case 'tax_rate_shipping' : case 'tax_rate_shipping':
case 'tax_rate_order' : case 'tax_rate_order':
$data[ $field ] = absint( $request[ $key ] ); $data[ $field ] = absint( $request[ $key ] );
break; break;
case 'tax_rate_class' : case 'tax_rate_class':
$data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : ''; $data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : '';
break; break;
default : default:
$data[ $field ] = wc_clean( $request[ $key ] ); $data[ $field ] = wc_clean( $request[ $key ] );
break; break;
} }
@ -511,11 +536,16 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
); );
// Get locales from a tax rate. // Get locales from a tax rate.
$locales = $wpdb->get_results( $wpdb->prepare( " $locales = $wpdb->get_results(
$wpdb->prepare(
"
SELECT location_code, location_type SELECT location_code, location_type
FROM {$wpdb->prefix}woocommerce_tax_rate_locations FROM {$wpdb->prefix}woocommerce_tax_rate_locations
WHERE tax_rate_id = %d WHERE tax_rate_id = %d
", $id ) ); ",
$id
)
);
if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) { if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) {
foreach ( $locales as $locale ) { foreach ( $locales as $locale ) {
@ -703,6 +733,20 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
'type' => 'string', 'type' => 'string',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['code'] = array(
'description' => __( 'Search by similar tax code.', 'woocommerce' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
);
$params['include'] = array(
'description' => __( 'Limit result set to items that have the specified rate ID(s) assigned.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
'validate_callback' => 'rest_validate_request_arg',
);
return $params; return $params;
} }

View File

@ -8,6 +8,7 @@
- Product Reviews - Updated response links. - Product Reviews - Updated response links.
- Products - Added `low_in_stock` and `search` parameter. - Products - Added `low_in_stock` and `search` parameter.
- Reports - Updated with updated list of available reports. - Reports - Updated with updated list of available reports.
- Taxes - Added `code` and `include` params.
## New endpoints ## New endpoints