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;
use \WC_REST_Terms_Controller;
use \WC_REST_Controller;
/**
* REST API Tax Class controller class.

View File

@ -4,30 +4,26 @@
*
* Handles requests to the /taxes endpoint.
*
* @author WooThemes
* @category API
* @package WooCommerce/RestApi
* @since 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
namespace WooCommerce\RestApi\Version4\Controllers;
defined( 'ABSPATH' ) || exit;
use \WC_REST_Controller;
/**
* 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.
*
* @var string
*/
protected $namespace = 'wc/v1';
protected $namespace = 'wc/v4';
/**
* Route base.
@ -40,7 +36,10 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
* Register the routes for taxes.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'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 ),
),
'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(
'id' => array(
'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' ),
) );
)
);
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/batch',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'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 ),
),
'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.
* @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['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.
@ -235,6 +245,21 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
$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_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'] );
// Query taxes.
$results = $wpdb->get_results( $query . $order_by . $pagination );
$results = $wpdb->get_results( $query . $order_by . $pagination ); // @codingStandardsIgnoreLine.
$taxes = array();
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 );
// 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.
$total_taxes = (int) $wpdb->num_rows;
@ -321,16 +346,16 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
// Add to data array.
switch ( $key ) {
case 'tax_rate_priority' :
case 'tax_rate_compound' :
case 'tax_rate_shipping' :
case 'tax_rate_order' :
case 'tax_rate_priority':
case 'tax_rate_compound':
case 'tax_rate_shipping':
case 'tax_rate_order':
$data[ $field ] = absint( $request[ $key ] );
break;
case 'tax_rate_class' :
case 'tax_rate_class':
$data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : '';
break;
default :
default:
$data[ $field ] = wc_clean( $request[ $key ] );
break;
}
@ -511,11 +536,16 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
);
// Get locales from a tax rate.
$locales = $wpdb->get_results( $wpdb->prepare( "
$locales = $wpdb->get_results(
$wpdb->prepare(
"
SELECT location_code, location_type
FROM {$wpdb->prefix}woocommerce_tax_rate_locations
WHERE tax_rate_id = %d
", $id ) );
",
$id
)
);
if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) {
foreach ( $locales as $locale ) {
@ -703,6 +733,20 @@ class WC_REST_Taxes_V1_Controller extends WC_REST_Controller {
'type' => 'string',
'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;
}

View File

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