2018-09-17 14:32:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Reports orders stats controller
|
|
|
|
*
|
|
|
|
* Handles requests to the /reports/orders/stats 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 orders stats controller class.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @extends WC_REST_Reports_Controller
|
|
|
|
*/
|
2018-09-17 19:13:19 +00:00
|
|
|
class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_REST_Reports_Controller {
|
2018-09-17 14:32:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $namespace = 'wc/v3';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rest_base = 'reports/orders/stats';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps query arguments from the REST request.
|
|
|
|
*
|
|
|
|
* @param array $request Request array.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepare_reports_query( $request ) {
|
2018-11-01 11:37:49 +00:00
|
|
|
$args = array();
|
|
|
|
$args['before'] = $request['before'];
|
|
|
|
$args['after'] = $request['after'];
|
|
|
|
$args['interval'] = $request['interval'];
|
|
|
|
$args['page'] = $request['page'];
|
|
|
|
$args['per_page'] = $request['per_page'];
|
|
|
|
$args['orderby'] = $request['orderby'];
|
|
|
|
$args['order'] = $request['order'];
|
|
|
|
|
|
|
|
$args['match'] = $request['match'];
|
|
|
|
$args['status_is'] = (array) $request['status_is'];
|
|
|
|
$args['status_is_not'] = (array) $request['status_is_not'];
|
|
|
|
$args['product_includes'] = (array) $request['product_includes'];
|
|
|
|
$args['product_excludes'] = (array) $request['product_excludes'];
|
|
|
|
$args['coupon_code_includes'] = (array) $request['coupon_code_includes'];
|
|
|
|
$args['coupon_code_excludes'] = (array) $request['coupon_code_excludes'];
|
|
|
|
$args['coupon_code_is'] = (array) $request['coupon_code_is'];
|
|
|
|
$args['coupon_code_is_not'] = (array) $request['coupon_code_is_not'];
|
|
|
|
$args['customer'] = (array) $request['customer'];
|
|
|
|
$args['categories'] = (array) $request['categories'];
|
2018-09-17 14:32:03 +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 );
|
2018-09-17 19:13:19 +00:00
|
|
|
$orders_query = new WC_Admin_Reports_Orders_Stats_Query( $query_args );
|
2018-09-17 14:32:03 +00:00
|
|
|
$report_data = $orders_query->get_data();
|
|
|
|
|
|
|
|
$out_data = array(
|
|
|
|
'totals' => get_object_vars( $report_data->totals ),
|
|
|
|
'intervals' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $report_data->intervals as $interval_data ) {
|
|
|
|
$item = $this->prepare_item_for_response( $interval_data, $request );
|
|
|
|
$out_data['intervals'][] = $this->prepare_response_for_collection( $item );
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = rest_ensure_response( $out_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 Array $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 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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_stats', $response, $report, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Report's schema, conforming to JSON Schema.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_item_schema() {
|
|
|
|
$totals = array(
|
|
|
|
'net_revenue' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Net revenue.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'avg_order_value' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Average order value.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'orders_count' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Amount of orders', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'avg_items_per_order' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Average items per order', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$schema = array(
|
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
|
|
|
'title' => 'report_orders_stats',
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'totals' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Totals data.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'object',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
'properties' => $totals,
|
|
|
|
),
|
|
|
|
'intervals' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Reports data grouped by intervals.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'interval' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Type of interval.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
'enum' => array( 'day', 'week', 'month', 'year' ),
|
|
|
|
),
|
|
|
|
'date_start' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( "The date the report start, in the site's timezone.", 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_start_gmt' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'The date the report start, as GMT.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_end' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( "The date the report end, in the site's timezone.", 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'date_end_gmt' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'The date the report end, as GMT.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'date-time',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
),
|
|
|
|
'subtotals' => array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Interval subtotals.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'object',
|
|
|
|
'context' => array( 'view', 'edit' ),
|
|
|
|
'readonly' => true,
|
|
|
|
'properties' => $totals,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
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(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Current page of the collection.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => 1,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'minimum' => 1,
|
|
|
|
);
|
|
|
|
$params['per_page'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Maximum number of items to be returned in result set.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'default' => 10,
|
|
|
|
'minimum' => 1,
|
|
|
|
'maximum' => 100,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['after'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'date-time',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['before'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'date-time',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['order'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Order sort attribute ascending or descending.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'desc',
|
|
|
|
'enum' => array( 'asc', 'desc' ),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['orderby'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Sort collection by object attribute.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'date',
|
|
|
|
'enum' => array(
|
|
|
|
'date',
|
|
|
|
'net_revenue',
|
|
|
|
'orders_count',
|
|
|
|
'avg_order_value',
|
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['interval'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Time interval to use for buckets in the returned data.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'week',
|
|
|
|
'enum' => array(
|
|
|
|
'hour',
|
|
|
|
'day',
|
|
|
|
'week',
|
|
|
|
'month',
|
|
|
|
'quarter',
|
|
|
|
'year',
|
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['categories'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Limit result set to all items that have the specified term assigned in the categories taxonomy.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['coupons'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Limit result set to all items that have the specified coupon assigned.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['products'] = array(
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Limit result set to all items that have the specified product assigned.', 'wc-admin' ),
|
2018-09-17 14:32:03 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['order_status'] = array(
|
|
|
|
'default' => array( 'completed', 'processing', 'on-hold' ),
|
2018-09-28 15:34:33 +00:00
|
|
|
'description' => __( 'Limit result set to orders assigned one or more statuses', 'wc-admin' ),
|
2018-09-17 14:32:03 +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',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get order statuses without prefixes.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_order_statuses() {
|
|
|
|
$order_statuses = array();
|
|
|
|
|
|
|
|
foreach ( array_keys( wc_get_order_statuses() ) as $status ) {
|
|
|
|
$order_statuses[] = str_replace( 'wc-', '', $status );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $order_statuses;
|
|
|
|
}
|
|
|
|
}
|