2018-09-17 14:32:03 +00:00
< ? php
/**
* REST API Reports downloads controller
*
* Handles requests to the / reports / downloads 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 downloads controller class .
*
* @ package WooCommerce / API
* @ extends WC_REST_Reports_Controller
*/
2019-02-28 14:14:21 +00:00
class WC_Admin_REST_Reports_Downloads_Controller extends WC_Admin_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/downloads' ;
2018-12-21 22:57:46 +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 ] ) ) {
$args [ $param_name ] = $request [ $param_name ];
}
}
$reports = new WC_Admin_Reports_Downloads_Query ( $args );
$downloads_data = $reports -> get_data ();
$data = array ();
foreach ( $downloads_data -> data as $download_data ) {
$item = $this -> prepare_item_for_response ( $download_data , $request );
$data [] = $this -> prepare_response_for_collection ( $item );
}
$response = rest_ensure_response ( $data );
$response -> header ( 'X-WP-Total' , ( int ) $downloads_data -> total );
$response -> header ( 'X-WP-TotalPages' , ( int ) $downloads_data -> pages );
$page = $downloads_data -> page_no ;
$max_pages = $downloads_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 ) );
$response -> data [ 'date' ] = get_date_from_gmt ( $data [ 'date_gmt' ], 'Y-m-d H:i:s' );
// Figure out file name.
// Matches https://github.com/woocommerce/woocommerce/blob/4be0018c092e617c5d2b8c46b800eb71ece9ddef/includes/class-wc-download-handler.php#L197.
2019-02-28 14:14:21 +00:00
$product_id = intval ( $data [ 'product_id' ] );
$_product = wc_get_product ( $product_id );
$file_path = $_product -> get_file_download_path ( $data [ 'download_id' ] );
$filename = basename ( $file_path );
$response -> data [ 'file_name' ] = apply_filters ( 'woocommerce_file_download_filename' , $filename , $product_id );
$response -> data [ 'file_path' ] = $file_path ;
$customer = new WC_Customer ( $data [ 'user_id' ] );
$response -> data [ 'username' ] = $customer -> get_username ();
$response -> data [ 'order_number' ] = $this -> get_order_number ( $data [ 'order_id' ] );
2018-12-21 22:57:46 +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_downloads' , $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' ] ) ),
'embeddable' => true ,
),
);
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_downloads' ,
'type' => 'object' ,
'properties' => array (
2019-03-13 17:14:02 +00:00
'id' => array (
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'ID.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-03-13 17:14:02 +00:00
'product_id' => array (
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Product ID.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-03-13 17:14:02 +00:00
'date' => array (
'description' => __ ( " The date of the download, in the site's timezone. " , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2019-03-13 17:14:02 +00:00
'date_gmt' => array (
'description' => __ ( 'The date of the download, as GMT.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2019-03-13 17:14:02 +00:00
'download_id' => array (
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Download ID.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-03-13 17:14:02 +00:00
'file_name' => array (
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'File name.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-03-13 17:14:02 +00:00
'file_path' => array (
2019-01-08 14:51:28 +00:00
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'File URL.' , 'woocommerce-admin' ),
2019-01-08 14:51:28 +00:00
),
2019-03-13 17:14:02 +00:00
'product_id' => array (
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Product ID.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-03-13 17:14:02 +00:00
'order_id' => array (
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Order ID.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-02-28 14:14:21 +00:00
'order_number' => array (
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Order Number.' , 'woocommerce-admin' ),
2019-02-28 14:14:21 +00:00
),
2019-03-13 17:14:02 +00:00
'user_id' => array (
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'User ID for the downloader.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
2019-03-13 17:14:02 +00:00
'username' => array (
2019-02-19 19:35:29 +00:00
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'User name of the downloader.' , 'woocommerce-admin' ),
2019-02-19 19:35:29 +00:00
),
2019-03-13 17:14:02 +00:00
'ip_address' => array (
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
2019-03-13 17:14:02 +00:00
'description' => __ ( 'IP address for the downloader.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
/**
* Get the query params for collections .
*
* @ return array
*/
public function get_collection_params () {
2019-03-13 17:14:02 +00:00
$params = array ();
$params [ 'context' ] = $this -> get_context_param ( array ( 'default' => 'view' ) );
$params [ 'page' ] = array (
'description' => __ ( 'Current page of the collection.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'default' => 1 ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
'minimum' => 1 ,
);
2019-03-13 17:14:02 +00:00
$params [ 'per_page' ] = array (
'description' => __ ( 'Maximum number of items to be returned in result set.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'integer' ,
'default' => 10 ,
'minimum' => 1 ,
'maximum' => 100 ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'after' ] = array (
'description' => __ ( 'Limit response to resources published after a given ISO8601 compliant date.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'format' => 'date-time' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'before' ] = array (
'description' => __ ( 'Limit response to resources published before a given ISO8601 compliant date.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'format' => 'date-time' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'order' ] = array (
'description' => __ ( 'Order sort attribute ascending or descending.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'default' => 'desc' ,
'enum' => array ( 'asc' , 'desc' ),
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'orderby' ] = array (
'description' => __ ( 'Sort collection by object attribute.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'default' => 'date' ,
'enum' => array (
'date' ,
2019-01-10 17:10:31 +00:00
'product' ,
2018-12-21 22:57:46 +00:00
),
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'match' ] = array (
'description' => __ ( 'Indicates whether all the conditions should be true for the resulting set, or if any one of them is sufficient. Match affects the following parameters: products, orders, username, ip_address.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'string' ,
'default' => 'all' ,
'enum' => array (
'all' ,
'any' ,
),
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'product_includes' ] = array (
'description' => __ ( 'Limit result set to items that have the specified product(s) assigned.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'items' => array (
'type' => 'integer' ,
),
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'product_excludes' ] = array (
'description' => __ ( 'Limit result set to items that don\'t have the specified product(s) assigned.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'items' => array (
'type' => 'integer' ,
),
'default' => array (),
'validate_callback' => 'rest_validate_request_arg' ,
'sanitize_callback' => 'wp_parse_id_list' ,
);
2019-03-13 17:14:02 +00:00
$params [ 'order_includes' ] = array (
'description' => __ ( 'Limit result set to items that have the specified order ids.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
2019-03-13 17:14:02 +00:00
$params [ 'order_excludes' ] = array (
'description' => __ ( 'Limit result set to items that don\'t have the specified order ids.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
2019-03-13 17:14:02 +00:00
$params [ 'customer_includes' ] = array (
'description' => __ ( 'Limit response to objects that have the specified user ids.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
2019-03-13 17:14:02 +00:00
$params [ 'customer_excludes' ] = array (
'description' => __ ( 'Limit response to objects that don\'t have the specified user ids.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
$params [ 'ip_address_includes' ] = array (
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Limit response to objects that have a specified ip address.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'string' ,
),
);
$params [ 'ip_address_excludes' ] = array (
2019-03-13 17:14:02 +00:00
'description' => __ ( 'Limit response to objects that don\'t have a specified ip address.' , 'woocommerce-admin' ),
2018-12-21 22:57:46 +00:00
'type' => 'array' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'string' ,
),
);
return $params ;
}
2018-09-17 14:32:03 +00:00
}