2018-09-17 14:32:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Reports taxes controller
|
|
|
|
*
|
|
|
|
* Handles requests to the /reports/taxes endpoint.
|
|
|
|
*
|
2018-09-17 19:13:19 +00:00
|
|
|
* @package WooCommerce Admin/API
|
2018-09-17 14:32:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Reports taxes controller class.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @extends WC_REST_Reports_Controller
|
|
|
|
*/
|
2018-09-17 19:13:19 +00:00
|
|
|
class WC_Admin_REST_Reports_Taxes_Controller extends WC_REST_Reports_Controller {
|
2018-09-17 14:32:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-01-18 02:52:58 +00:00
|
|
|
protected $namespace = 'wc/v4';
|
2018-09-17 14:32:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rest_base = 'reports/taxes';
|
2018-09-18 11:27:34 +00:00
|
|
|
|
2018-12-11 00:58:05 +00:00
|
|
|
/**
|
|
|
|
* Maps query arguments from the REST request.
|
|
|
|
*
|
|
|
|
* @param array $request Request array.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepare_reports_query( $request ) {
|
|
|
|
$args = array();
|
|
|
|
$args['before'] = $request['before'];
|
|
|
|
$args['after'] = $request['after'];
|
|
|
|
$args['page'] = $request['page'];
|
|
|
|
$args['per_page'] = $request['per_page'];
|
|
|
|
$args['orderby'] = $request['orderby'];
|
|
|
|
$args['order'] = $request['order'];
|
|
|
|
$args['taxes'] = $request['taxes'];
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:27:34 +00:00
|
|
|
/**
|
|
|
|
* Get all reports.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request data.
|
|
|
|
* @return array|WP_Error
|
|
|
|
*/
|
|
|
|
public function get_items( $request ) {
|
|
|
|
$query_args = $this->prepare_reports_query( $request );
|
2018-12-11 00:58:05 +00:00
|
|
|
$taxes_query = new WC_Admin_Reports_Taxes_Query( $query_args );
|
2018-09-18 11:27:34 +00:00
|
|
|
$report_data = $taxes_query->get_data();
|
|
|
|
|
2018-12-11 00:58:05 +00:00
|
|
|
$data = array();
|
2018-09-18 11:27:34 +00:00
|
|
|
|
2018-12-11 00:58:05 +00:00
|
|
|
foreach ( $report_data->data as $tax_data ) {
|
|
|
|
$item = $this->prepare_item_for_response( (object) $tax_data, $request );
|
|
|
|
$data[] = $this->prepare_response_for_collection( $item );
|
2018-09-18 11:27:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 00:58:05 +00:00
|
|
|
$response = rest_ensure_response( $data );
|
2018-09-18 11:27:34 +00:00
|
|
|
$response->header( 'X-WP-Total', (int) $report_data->total );
|
|
|
|
$response->header( 'X-WP-TotalPages', (int) $report_data->pages );
|
|
|
|
|
|
|
|
$page = $report_data->page_no;
|
|
|
|
$max_pages = $report_data->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare a report object for serialization.
|
|
|
|
*
|
|
|
|
* @param stdClass $report Report data.
|
|
|
|
* @param WP_REST_Request $request Request object.
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public function prepare_item_for_response( $report, $request ) {
|
|
|
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
2018-12-11 00:58:05 +00:00
|
|
|
$report = $this->add_additional_fields_to_object( $report, $request );
|
|
|
|
$report = $this->filter_response_by_context( $report, $context );
|
2018-09-18 11:27:34 +00:00
|
|
|
|
|
|
|
// Wrap the data in a response object.
|
2018-12-11 00:58:05 +00:00
|
|
|
$response = rest_ensure_response( $report );
|
2018-09-18 11:27:34 +00:00
|
|
|
$response->add_links( $this->prepare_links( $report ) );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter a report returned from the API.
|
|
|
|
*
|
|
|
|
* Allows modification of the report data right before it is returned.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Response $response The response object.
|
|
|
|
* @param object $report The original report object.
|
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'woocommerce_rest_prepare_report_taxes', $response, $report, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare links for the request.
|
|
|
|
*
|
|
|
|
* @param WC_Reports_Query $object Object data.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepare_links( $object ) {
|
|
|
|
$links = array(
|
|
|
|
'tax' => array(
|
2018-12-11 00:58:05 +00:00
|
|
|
'href' => rest_url( sprintf( '/%s/taxes/%d', $this->namespace, $object->tax_rate_id ) ),
|
2018-09-18 11:27:34 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Report's schema, conforming to JSON Schema.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_item_schema() {
|
|
|
|
$schema = array(
|
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
|
|
|
'title' => 'report_taxes',
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'tax_rate_id' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Tax rate ID.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2018-12-11 00:58:05 +00:00
|
|
|
'name' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Tax rate name.', 'woocommerce-admin' ),
|
2018-12-11 00:58:05 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'tax_rate' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Tax rate.', 'woocommerce-admin' ),
|
2018-12-11 00:58:05 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'country' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Country.', 'woocommerce-admin' ),
|
2018-12-11 00:58:05 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'state' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'State.', 'woocommerce-admin' ),
|
2018-12-11 00:58:05 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'priority' => array(
|
|
|
|
'description' => __( 'Priority.', 'woocommerce-admin' ),
|
2018-12-11 00:58:05 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2018-09-18 11:27:34 +00:00
|
|
|
'total_tax' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Total tax.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'order_tax' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Order tax.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'shipping_tax' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Shipping tax.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'orders_count' => array(
|
2019-06-12 19:16:18 +00:00
|
|
|
'description' => __( 'Number of orders.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->add_additional_fields_schema( $schema );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query params for collections.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_collection_params() {
|
|
|
|
$params = array();
|
|
|
|
$params['context'] = $this->get_context_param( array( 'default' => 'view' ) );
|
|
|
|
$params['page'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Current page of the collection.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => 1,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'minimum' => 1,
|
|
|
|
);
|
|
|
|
$params['per_page'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => 10,
|
|
|
|
'minimum' => 1,
|
|
|
|
'maximum' => 100,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['after'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'date-time',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['before'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'date-time',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['order'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'desc',
|
|
|
|
'enum' => array( 'asc', 'desc' ),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['orderby'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Sort collection by object attribute.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'string',
|
2018-12-11 00:58:05 +00:00
|
|
|
'default' => 'tax_rate_id',
|
2018-09-18 11:27:34 +00:00
|
|
|
'enum' => array(
|
|
|
|
'name',
|
|
|
|
'tax_rate_id',
|
2019-03-15 20:44:03 +00:00
|
|
|
'tax_code',
|
2018-12-11 00:58:05 +00:00
|
|
|
'rate',
|
|
|
|
'order_tax',
|
|
|
|
'total_tax',
|
|
|
|
'shipping_tax',
|
2018-09-18 11:27:34 +00:00
|
|
|
'orders_count',
|
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
2018-12-11 00:58:05 +00:00
|
|
|
$params['taxes'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items assigned one or more tax rates.', 'woocommerce-admin' ),
|
2018-09-18 11:27:34 +00:00
|
|
|
'type' => 'array',
|
2018-12-11 00:58:05 +00:00
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
2018-09-18 11:27:34 +00:00
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
2018-09-17 14:32:03 +00:00
|
|
|
}
|