2019-01-15 01:53:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Reports orders controller
|
|
|
|
*
|
|
|
|
* Handles requests to the /reports/orders endpoint.
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/API
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Reports orders controller class.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @extends WC_Admin_REST_Reports_Controller
|
|
|
|
*/
|
|
|
|
class WC_Admin_REST_Reports_Orders_Controller extends WC_Admin_REST_Reports_Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $namespace = 'wc/v4';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rest_base = 'reports/orders';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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['product_includes'] = (array) $request['product_includes'];
|
|
|
|
$args['product_excludes'] = (array) $request['product_excludes'];
|
|
|
|
$args['coupon_includes'] = (array) $request['coupon_includes'];
|
|
|
|
$args['coupon_excludes'] = (array) $request['coupon_excludes'];
|
|
|
|
$args['status_is'] = (array) $request['status_is'];
|
|
|
|
$args['status_is_not'] = (array) $request['status_is_not'];
|
|
|
|
$args['customer_type'] = $request['customer_type'];
|
|
|
|
$args['extended_info'] = $request['extended_info'];
|
2019-05-10 06:56:57 +00:00
|
|
|
$args['refunds'] = $request['refunds'];
|
2019-05-14 17:54:53 +00:00
|
|
|
$args['match'] = $request['match'];
|
|
|
|
|
2019-01-15 01:53:02 +00:00
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
$orders_query = new WC_Admin_Reports_Orders_Query( $query_args );
|
|
|
|
$report_data = $orders_query->get_data();
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
foreach ( $report_data->data as $orders_data ) {
|
2019-02-28 14:14:21 +00:00
|
|
|
$orders_data['order_number'] = $this->get_order_number( $orders_data['order_id'] );
|
|
|
|
$item = $this->prepare_item_for_response( $orders_data, $request );
|
|
|
|
$data[] = $this->prepare_response_for_collection( $item );
|
2019-01-15 01:53:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = rest_ensure_response( $data );
|
|
|
|
$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 ) {
|
|
|
|
$data = $report;
|
|
|
|
|
|
|
|
$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( $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_orders', $response, $report, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare links for the request.
|
|
|
|
*
|
|
|
|
* @param WC_Reports_Query $object Object data.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepare_links( $object ) {
|
|
|
|
$links = array(
|
|
|
|
'order' => array(
|
|
|
|
'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object['order_id'] ) ),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
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_orders',
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
2019-06-12 09:21:56 +00:00
|
|
|
'order_id' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Order ID.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'order_number' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Order Number.', 'woocommerce-admin' ),
|
2019-02-28 14:14:21 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'date_created' => array(
|
|
|
|
'description' => __( "Date the order was created, in the site's timezone.", 'woocommerce-admin' ),
|
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_created_gmt' => array(
|
|
|
|
'description' => __( 'Date the order was created, as GMT.', 'woocommerce-admin' ),
|
|
|
|
'type' => 'date-time',
|
2019-01-15 01:53:02 +00:00
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'status' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Order status.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'customer_id' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Customer ID.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'num_items_sold' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Number of items sold.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'net_total' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Net total revenue.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'float',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'customer_type' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Returning or new customer.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
2019-06-12 09:21:56 +00:00
|
|
|
'extended_info' => array(
|
2019-01-15 01:53:02 +00:00
|
|
|
'products' => array(
|
|
|
|
'type' => 'array',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'List of product IDs and names.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
),
|
|
|
|
'categories' => array(
|
|
|
|
'type' => 'array',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Category IDs.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
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' ),
|
2019-01-15 01:53:02 +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' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => 10,
|
2019-03-19 10:57:11 +00:00
|
|
|
'minimum' => 0,
|
2019-01-15 01:53:02 +00:00
|
|
|
'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' ),
|
2019-01-15 01:53:02 +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' ),
|
2019-01-15 01:53:02 +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' ),
|
2019-01-15 01:53:02 +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' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'date',
|
|
|
|
'enum' => array(
|
|
|
|
'date',
|
|
|
|
'num_items_sold',
|
|
|
|
'net_total',
|
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['product_includes'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'default' => array(),
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['product_excludes'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'default' => array(),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
);
|
|
|
|
$params['coupon_includes'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items that have the specified coupon(s) assigned.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'default' => array(),
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['coupon_excludes'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items that don\'t have the specified coupon(s) assigned.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
'default' => array(),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
);
|
|
|
|
$params['status_is'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items that have the specified order status.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'sanitize_callback' => 'wp_parse_slug_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'items' => array(
|
|
|
|
'enum' => $this->get_order_statuses(),
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$params['status_is_not'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to items that don\'t have the specified order status.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'sanitize_callback' => 'wp_parse_slug_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'items' => array(
|
|
|
|
'enum' => $this->get_order_statuses(),
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$params['customer_type'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result set to returning or new customers.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => '',
|
|
|
|
'enum' => array(
|
|
|
|
'',
|
|
|
|
'returning',
|
|
|
|
'new',
|
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
2019-05-10 06:56:57 +00:00
|
|
|
$params['refunds'] = array(
|
|
|
|
'description' => __( 'Limit result set to specific types of refunds.', 'woocommerce-admin' ),
|
|
|
|
'type' => 'string',
|
|
|
|
'default' => '',
|
|
|
|
'enum' => array(
|
|
|
|
'',
|
|
|
|
'all',
|
|
|
|
'partial',
|
|
|
|
'full',
|
|
|
|
'none',
|
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
2019-01-15 01:53:02 +00:00
|
|
|
$params['extended_info'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Add additional piece of info about each coupon to the report.', 'woocommerce-admin' ),
|
2019-01-15 01:53:02 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'sanitize_callback' => 'wc_string_to_bool',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|