2018-09-17 14:32:03 +00:00
< ? php
/**
* REST API Reports controller extended by WC Admin plugin .
*
* Handles requests to the reports endpoint .
*
* @ package WooCommerce Admin / API
*/
defined ( 'ABSPATH' ) || exit ;
/**
* REST API Reports controller class .
*
* @ package WooCommerce Admin / API
* @ extends WC_REST_Reports_Controller
*/
class WC_Admin_REST_Reports_Controller extends WC_REST_Reports_Controller {
/**
* 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' ;
/**
* Register the routes for reports .
*/
public function register_routes () {
2018-10-22 16:20:14 +00:00
register_rest_route (
$this -> namespace ,
'/' . $this -> rest_base ,
2018-09-17 14:32:03 +00:00
array (
2018-10-22 16:20:14 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
),
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
2018-09-17 14:32:03 +00:00
}
/**
* Check whether a given request has permission to read reports .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
if ( ! wc_rest_check_manager_permissions ( 'reports' , 'read' ) ) {
2019-03-13 17:14:02 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list resources.' , 'woocommerce-admin' ), array ( 'status' => rest_authorization_required_code () ) );
2018-09-17 14:32:03 +00:00
}
return true ;
}
2019-01-14 19:39:47 +00:00
2018-09-17 14:32:03 +00:00
/**
* Get all reports .
*
* @ param WP_REST_Request $request Request data .
* @ return array | WP_Error
*/
public function get_items ( $request ) {
$data = array ();
$reports = array (
2019-01-09 21:08:39 +00:00
array (
'slug' => 'performance-indicators' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Batch endpoint for getting specific performance indicators from `stats` endpoints.' , 'woocommerce-admin' ),
2019-01-09 21:08:39 +00:00
),
2018-09-17 14:32:03 +00:00
array (
'slug' => 'revenue/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about revenue.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'orders/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about orders.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'products' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Products detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'products/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about products.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'categories' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Product categories detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'categories/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about product categories.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'coupons' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Coupons detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'coupons/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about coupons.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'taxes' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Taxes detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'taxes/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about taxes.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'downloads' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Product downloads detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'downloads/files' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Product download files detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'downloads/stats' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Stats about product downloads.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
array (
'slug' => 'customers' ,
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Customers detailed reports.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
),
);
2019-01-14 19:39:47 +00:00
/**
* Filter the list of allowed reports , so that data can be loaded from third party extensions in addition to WooCommerce core .
* Array items should be in format of array ( 'slug' => 'downloads/stats' , 'description' => '' ,
* 'url' => '' , and 'path' => '/wc-ext/v1/...' .
*
* @ param array $endpoints The list of allowed reports ..
*/
$reports = apply_filters ( 'woocommerce_admin_reports' , $reports );
2018-09-17 14:32:03 +00:00
foreach ( $reports as $report ) {
2019-01-14 19:39:47 +00:00
if ( empty ( $report [ 'slug' ] ) ) {
continue ;
}
if ( empty ( $report [ 'path' ] ) ) {
$report [ 'path' ] = '/' . $this -> namespace . '/reports/' . $report [ 'slug' ];
}
// Allows a different admin page to be loaded here,
// or allows an empty url if no report exists for a set of performance indicators.
if ( ! isset ( $report [ 'url' ] ) ) {
if ( '/stats' === substr ( $report [ 'slug' ], - 6 ) ) {
$url_slug = substr ( $report [ 'slug' ], 0 , - 6 );
} else {
$url_slug = $report [ 'slug' ];
}
2019-01-17 14:08:59 +00:00
$report [ 'url' ] = '/analytics/' . $url_slug ;
2019-01-14 19:39:47 +00:00
}
2018-09-17 14:32:03 +00:00
$item = $this -> prepare_item_for_response ( ( object ) $report , $request );
$data [] = $this -> prepare_response_for_collection ( $item );
}
return rest_ensure_response ( $data );
}
2019-02-28 14:14:21 +00:00
/**
* Get the order number for an order . If no filter is present for `woocommerce_order_number` , we can just return the ID .
*
* @ param int $order_id Order ID .
* @ return string
*/
public function get_order_number ( $order_id ) {
if ( ! has_filter ( 'woocommerce_order_number' ) ) {
return $order_id ;
}
2019-05-10 06:39:25 +00:00
2019-02-28 14:14:21 +00:00
$order = new WC_Order ( $order_id );
2019-05-10 06:39:25 +00:00
if ( 'shop_order' !== $order -> get_type () ) {
return $order_id ;
}
2019-02-28 14:14:21 +00:00
return $order -> get_order_number ();
}
2018-09-17 14:32:03 +00:00
/**
* 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 = array (
'slug' => $report -> slug ,
'description' => $report -> description ,
2019-01-14 19:39:47 +00:00
'path' => $report -> path ,
2018-09-17 14:32:03 +00:00
);
$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 );
2018-10-22 16:20:14 +00:00
$response -> add_links (
array (
'self' => array (
2019-01-14 19:39:47 +00:00
'href' => rest_url ( $report -> path ),
),
2019-03-13 17:14:02 +00:00
'report' => array (
2019-01-14 19:39:47 +00:00
'href' => $report -> url ,
2018-10-22 16:20:14 +00:00
),
'collection' => array (
'href' => rest_url ( sprintf ( '%s/%s' , $this -> namespace , $this -> rest_base ) ),
),
)
);
2018-09-17 14:32:03 +00:00
/**
* 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' , $response , $report , $request );
}
/**
* 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' ,
'type' => 'object' ,
'properties' => array (
'slug' => array (
2019-03-13 17:14:02 +00:00
'description' => __ ( 'An alphanumeric identifier for the resource.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
'type' => 'string' ,
'context' => array ( 'view' ),
'readonly' => true ,
),
'description' => array (
2019-03-13 17:14:02 +00:00
'description' => __ ( 'A human-readable description of the resource.' , 'woocommerce-admin' ),
2018-09-17 14:32:03 +00:00
'type' => 'string' ,
'context' => array ( 'view' ),
'readonly' => true ,
),
2019-01-14 19:39:47 +00:00
'path' => array (
2019-03-13 17:14:02 +00:00
'description' => __ ( 'API path.' , 'woocommerce-admin' ),
2019-01-14 19:39:47 +00:00
'type' => 'string' ,
'context' => array ( 'view' ),
'readonly' => true ,
),
2018-09-17 14:32:03 +00:00
),
);
return $this -> add_additional_fields_schema ( $schema );
}
/**
* Get the query params for collections .
*
* @ return array
*/
public function get_collection_params () {
return array (
'context' => $this -> get_context_param ( array ( 'default' => 'view' ) ),
);
}
2019-01-15 01:53:02 +00:00
/**
* Get order statuses without prefixes .
*
* @ return array
*/
public 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 ;
}
2018-09-17 14:32:03 +00:00
}