2018-11-26 02:58:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Reports products controller
|
|
|
|
*
|
|
|
|
* Handles requests to the /reports/products endpoint.
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/API
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST API Reports products controller class.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @extends WC_REST_Reports_Controller
|
|
|
|
*/
|
|
|
|
class WC_Admin_REST_Reports_Variations_Controller extends WC_REST_Reports_Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-01-18 02:52:58 +00:00
|
|
|
protected $namespace = 'wc/v4';
|
2018-11-26 02:58:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Route base.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rest_base = 'reports/variations';
|
|
|
|
|
2018-12-03 22:40:21 +00:00
|
|
|
/**
|
|
|
|
* Mapping between external parameter name and name used in query class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $param_mapping = array(
|
|
|
|
'products' => 'product_includes',
|
|
|
|
);
|
|
|
|
|
2018-11-26 02:58:19 +00:00
|
|
|
/**
|
|
|
|
* Get items.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request data.
|
|
|
|
*
|
|
|
|
* @return array|WP_Error
|
|
|
|
*/
|
|
|
|
public function get_items( $request ) {
|
|
|
|
$args = array();
|
|
|
|
$registered = array_keys( $this->get_collection_params() );
|
|
|
|
foreach ( $registered as $param_name ) {
|
|
|
|
if ( isset( $request[ $param_name ] ) ) {
|
2018-12-03 22:40:21 +00:00
|
|
|
if ( isset( $this->param_mapping[ $param_name ] ) ) {
|
|
|
|
$args[ $this->param_mapping[ $param_name ] ] = $request[ $param_name ];
|
|
|
|
} else {
|
|
|
|
$args[ $param_name ] = $request[ $param_name ];
|
|
|
|
}
|
2018-11-26 02:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$reports = new WC_Admin_Reports_Variations_Query( $args );
|
|
|
|
$products_data = $reports->get_data();
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
foreach ( $products_data->data as $product_data ) {
|
|
|
|
$item = $this->prepare_item_for_response( $product_data, $request );
|
|
|
|
$data[] = $this->prepare_response_for_collection( $item );
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = rest_ensure_response( $data );
|
|
|
|
$response->header( 'X-WP-Total', (int) $products_data->total );
|
|
|
|
$response->header( 'X-WP-TotalPages', (int) $products_data->pages );
|
|
|
|
|
|
|
|
$page = $products_data->page_no;
|
|
|
|
$max_pages = $products_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 );
|
|
|
|
$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_variations', $response, $report, $request );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare links for the request.
|
|
|
|
*
|
|
|
|
* @param array $object Object data.
|
|
|
|
* @return array Links for the given post.
|
|
|
|
*/
|
|
|
|
protected function prepare_links( $object ) {
|
|
|
|
$links = array(
|
|
|
|
'product' => array(
|
|
|
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ),
|
|
|
|
),
|
|
|
|
'variation' => array(
|
|
|
|
'href' => rest_url( sprintf( '/%s/%s/%d/%s/%d', $this->namespace, 'products', $object['product_id'], 'variation', $object['variation_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_varitations',
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array(
|
|
|
|
'product_id' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product ID.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
|
|
|
'variation_id' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product ID.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
|
|
|
'items_sold' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Number of items sold.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'net_revenue' => array(
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Total net revenue of all items sold.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
|
|
|
'orders_count' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Number of orders product appeared in.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
|
|
|
'extended_info' => array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'name' => array(
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product name.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'price' => array(
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product price.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'image' => array(
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product image.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'permalink' => array(
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product link.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'attributes' => array(
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product attributes.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'stock_status' => array(
|
2018-12-03 22:40:21 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product inventory status.', 'woocommerce-admin' ),
|
2018-12-03 22:40:21 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'stock_quantity' => array(
|
2018-12-03 22:40:21 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product inventory quantity.', 'woocommerce-admin' ),
|
2018-12-03 22:40:21 +00:00
|
|
|
),
|
2019-03-13 17:14:02 +00:00
|
|
|
'low_stock_amount' => array(
|
2018-12-03 22:40:21 +00:00
|
|
|
'type' => 'integer',
|
|
|
|
'readonly' => true,
|
|
|
|
'context' => array( 'view', 'edit' ),
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Product inventory threshold for low stock.', 'woocommerce-admin' ),
|
2018-12-03 22:40:21 +00:00
|
|
|
),
|
2018-11-26 02:58:19 +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' ),
|
2018-11-26 02:58:19 +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-11-26 02:58:19 +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-11-26 02:58:19 +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-11-26 02:58:19 +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-11-26 02:58:19 +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-11-26 02:58:19 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'default' => 'date',
|
|
|
|
'enum' => array(
|
|
|
|
'date',
|
2019-02-05 18:12:58 +00:00
|
|
|
'net_revenue',
|
2018-11-26 02:58:19 +00:00
|
|
|
'orders_count',
|
|
|
|
'items_sold',
|
2019-03-31 18:53:48 +00:00
|
|
|
'sku',
|
2018-11-26 02:58:19 +00:00
|
|
|
),
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
$params['products'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result to items with specified product ids.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$params['variations'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Limit result to items with specified variation ids.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'array',
|
|
|
|
'sanitize_callback' => 'wp_parse_id_list',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
'items' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$params['extended_info'] = array(
|
2019-03-13 17:14:02 +00:00
|
|
|
'description' => __( 'Add additional piece of info about each product to the report.', 'woocommerce-admin' ),
|
2018-11-26 02:58:19 +00:00
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
'sanitize_callback' => 'wc_string_to_bool',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|