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
*/
2018-09-17 19:13:19 +00:00
class WC_Admin_REST_Reports_Downloads_Controller extends WC_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.
$product_id = intval ( $data [ 'product_id' ] );
$_product = wc_get_product ( $product_id );
$file_path = $_product -> get_file_download_path ( $data [ 'download_id' ] );
2019-01-08 14:51:28 +00:00
2018-12-21 22:57:46 +00:00
$filename = basename ( $file_path );
$response -> data [ 'file_name' ] = apply_filters ( 'woocommerce_file_download_filename' , $filename , $product_id );
2019-01-08 14:51:28 +00:00
$response -> data [ 'file_path' ] = $file_path ;
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 ,
),
'user' => array (
'href' => rest_url ( 'wp/v2/users/' . $object [ 'user_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 (
'id' => array (
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'ID.' , 'wc-admin' ),
),
'product_id' => array (
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'Product ID.' , 'wc-admin' ),
),
'date' => array (
'description' => __ ( " The date of the download, in the site's timezone. " , 'wc-admin' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'date_gmt' => array (
'description' => __ ( 'The date of the download, as GMT.' , 'wc-admin' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'download_id' => array (
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'Download ID.' , 'wc-admin' ),
),
'file_name' => array (
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'File name.' , 'wc-admin' ),
),
2019-01-08 14:51:28 +00:00
'file_path' => array (
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'File URL.' , 'wc-admin' ),
),
2018-12-21 22:57:46 +00:00
'product_id' => array (
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'Product ID.' , 'wc-admin' ),
),
'order_id' => array (
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'Order ID.' , 'wc-admin' ),
),
'user_id' => array (
'type' => 'integer' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'User ID for the downloader.' , 'wc-admin' ),
),
'ip_address' => array (
'type' => 'string' ,
'readonly' => true ,
'context' => array ( 'view' , 'edit' ),
'description' => __ ( 'IP address for the downloader.' , 'wc-admin' ),
),
),
);
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 (
'description' => __ ( 'Current page of the collection.' , 'wc-admin' ),
'type' => 'integer' ,
'default' => 1 ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
'minimum' => 1 ,
);
$params [ 'per_page' ] = array (
'description' => __ ( 'Maximum number of items to be returned in result set.' , 'wc-admin' ),
'type' => 'integer' ,
'default' => 10 ,
'minimum' => 1 ,
'maximum' => 100 ,
'sanitize_callback' => 'absint' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'after' ] = array (
'description' => __ ( 'Limit response to resources published after a given ISO8601 compliant date.' , 'wc-admin' ),
'type' => 'string' ,
'format' => 'date-time' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'before' ] = array (
'description' => __ ( 'Limit response to resources published before a given ISO8601 compliant date.' , 'wc-admin' ),
'type' => 'string' ,
'format' => 'date-time' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'order' ] = array (
'description' => __ ( 'Order sort attribute ascending or descending.' , 'wc-admin' ),
'type' => 'string' ,
'default' => 'desc' ,
'enum' => array ( 'asc' , 'desc' ),
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'orderby' ] = array (
'description' => __ ( 'Sort collection by object attribute.' , 'wc-admin' ),
'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' ,
);
$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.' , 'wc-admin' ),
'type' => 'string' ,
'default' => 'all' ,
'enum' => array (
'all' ,
'any' ,
),
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'product_includes' ] = array (
'description' => __ ( 'Limit result set to items that have the specified product(s) assigned.' , 'wc-admin' ),
'type' => 'array' ,
'items' => array (
'type' => 'integer' ,
),
'default' => array (),
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
$params [ 'product_excludes' ] = array (
'description' => __ ( 'Limit result set to items that don\'t have the specified product(s) assigned.' , 'wc-admin' ),
'type' => 'array' ,
'items' => array (
'type' => 'integer' ,
),
'default' => array (),
'validate_callback' => 'rest_validate_request_arg' ,
'sanitize_callback' => 'wp_parse_id_list' ,
);
$params [ 'order_includes' ] = array (
'description' => __ ( 'Limit result set to items that have the specified order ids.' , 'wc-admin' ),
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
$params [ 'order_excludes' ] = array (
'description' => __ ( 'Limit result set to items that don\'t have the specified order ids.' , 'wc-admin' ),
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
$params [ 'user_includes' ] = array (
'description' => __ ( 'Limit response to objects that have the specified user ids.' , 'wc-admin' ),
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
$params [ 'user_excludes' ] = array (
'description' => __ ( 'Limit response to objects that don\'t have the specified user ids.' , 'wc-admin' ),
'type' => 'array' ,
'sanitize_callback' => 'wp_parse_id_list' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'integer' ,
),
);
$params [ 'ip_address_includes' ] = array (
'description' => __ ( 'Limit response to objects that have a specified ip address.' , 'wc-admin' ),
'type' => 'array' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'string' ,
),
);
$params [ 'ip_address_excludes' ] = array (
'description' => __ ( 'Limit response to objects that don\'t have a specified ip address.' , 'wc-admin' ),
'type' => 'array' ,
'validate_callback' => 'rest_validate_request_arg' ,
'items' => array (
'type' => 'string' ,
),
);
return $params ;
}
2018-09-17 14:32:03 +00:00
}