Added method to get single taxes
This commit is contained in:
parent
dcca3a1efe
commit
4f1551ff5b
|
@ -831,7 +831,7 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the query params for collections
|
||||
* Get the query params for collections.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
@ -87,6 +87,117 @@ class WC_REST_Taxes_Controller extends WP_REST_Controller {
|
|||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User's schema, conforming to JSON Schema
|
||||
*
|
||||
|
@ -95,7 +206,7 @@ class WC_REST_Taxes_Controller extends WP_REST_Controller {
|
|||
public function get_item_schema() {
|
||||
$schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'customer',
|
||||
'title' => 'tax',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'id' => array(
|
||||
|
@ -169,4 +280,64 @@ class WC_REST_Taxes_Controller extends WP_REST_Controller {
|
|||
|
||||
return $this->add_additional_fields_schema( $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue