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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / API / Reports / Downloads / Controller.php
woocommerce / src / Admin / API / Reports / Downloads Last commit date
Files 4 years ago Stats 1 year ago Controller.php 1 year ago DataStore.php 1 year ago Query.php 1 year ago
Controller.php
370 lines
1 <?php
2 /**
3 * REST API Reports downloads controller
4 *
5 * Handles requests to the /reports/downloads endpoint.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
13 use Automattic\WooCommerce\Admin\API\Reports\GenericController;
14 use Automattic\WooCommerce\Admin\API\Reports\GenericQuery;
15 use Automattic\WooCommerce\Admin\API\Reports\OrderAwareControllerTrait;
16
17 /**
18 * REST API Reports downloads controller class.
19 *
20 * @internal
21 * @extends Automattic\WooCommerce\Admin\API\Reports\GenericController
22 */
23 class Controller extends GenericController implements ExportableInterface {
24
25 use OrderAwareControllerTrait;
26
27 /**
28 * Route base.
29 *
30 * @var string
31 */
32 protected $rest_base = 'reports/downloads';
33
34 /**
35 * Get data from `'downloads'` GenericQuery.
36 *
37 * @override GenericController::get_datastore_data()
38 *
39 * @param array $query_args Query arguments.
40 * @return mixed Results from the data store.
41 */
42 protected function get_datastore_data( $query_args = array() ) {
43 $query = new GenericQuery( $query_args, 'downloads' );
44 return $query->get_data();
45 }
46
47 /**
48 * Prepare a report data item for serialization.
49 *
50 * @param Array $report Report data item as returned from Data Store.
51 * @param WP_REST_Request $request Request object.
52 * @return WP_REST_Response
53 */
54 public function prepare_item_for_response( $report, $request ) {
55 // Wrap the data in a response object.
56 $response = parent::prepare_item_for_response( $report, $request );
57 $response->add_links( $this->prepare_links( $report ) );
58
59 $response->data['date'] = get_date_from_gmt( $report['date_gmt'], 'Y-m-d H:i:s' );
60
61 // Figure out file name.
62 // Matches https://github.com/woocommerce/woocommerce/blob/4be0018c092e617c5d2b8c46b800eb71ece9ddef/includes/class-wc-download-handler.php#L197.
63 $product_id = intval( $report['product_id'] );
64 $_product = wc_get_product( $product_id );
65
66 // Make sure the product hasn't been deleted.
67 if ( $_product ) {
68 $file_path = $_product->get_file_download_path( $report['download_id'] );
69 $filename = basename( $file_path );
70 $response->data['file_name'] = apply_filters( 'woocommerce_file_download_filename', $filename, $product_id );
71 $response->data['file_path'] = $file_path;
72 } else {
73 $response->data['file_name'] = '';
74 $response->data['file_path'] = '';
75 }
76
77 $customer = new \WC_Customer( $report['user_id'] );
78 $response->data['username'] = $customer->get_username();
79 $response->data['order_number'] = $this->get_order_number( $report['order_id'] );
80
81 /**
82 * Filter a report returned from the API.
83 *
84 * Allows modification of the report data right before it is returned.
85 *
86 * @param WP_REST_Response $response The response object.
87 * @param object $report The original report object.
88 * @param WP_REST_Request $request Request used to generate the response.
89 */
90 return apply_filters( 'woocommerce_rest_prepare_report_downloads', $response, $report, $request );
91 }
92
93 /**
94 * Prepare links for the request.
95 *
96 * @param Array $object Object data.
97 * @return array Links for the given post.
98 */
99 protected function prepare_links( $object ) {
100 $links = array(
101 'product' => array(
102 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'products', $object['product_id'] ) ),
103 'embeddable' => true,
104 ),
105 );
106
107 return $links;
108 }
109
110 /**
111 * Maps query arguments from the REST request.
112 *
113 * @param array $request Request array.
114 * @return array
115 */
116 protected function prepare_reports_query( $request ) {
117 $args = array();
118 $registered = array_keys( $this->get_collection_params() );
119 foreach ( $registered as $param_name ) {
120 if ( isset( $request[ $param_name ] ) ) {
121 $args[ $param_name ] = $request[ $param_name ];
122 }
123 }
124 return $args;
125 }
126 /**
127 * Get the Report's schema, conforming to JSON Schema.
128 *
129 * @return array
130 */
131 public function get_item_schema() {
132 $schema = array(
133 '$schema' => 'http://json-schema.org/draft-04/schema#',
134 'title' => 'report_downloads',
135 'type' => 'object',
136 'properties' => array(
137 'id' => array(
138 'type' => 'integer',
139 'readonly' => true,
140 'context' => array( 'view', 'edit' ),
141 'description' => __( 'ID.', 'woocommerce' ),
142 ),
143 'product_id' => array(
144 'type' => 'integer',
145 'readonly' => true,
146 'context' => array( 'view', 'edit' ),
147 'description' => __( 'Product ID.', 'woocommerce' ),
148 ),
149 'date' => array(
150 'description' => __( "The date of the download, in the site's timezone.", 'woocommerce' ),
151 'type' => 'date-time',
152 'context' => array( 'view', 'edit' ),
153 'readonly' => true,
154 ),
155 'date_gmt' => array(
156 'description' => __( 'The date of the download, as GMT.', 'woocommerce' ),
157 'type' => 'date-time',
158 'context' => array( 'view', 'edit' ),
159 'readonly' => true,
160 ),
161 'download_id' => array(
162 'type' => 'string',
163 'readonly' => true,
164 'context' => array( 'view', 'edit' ),
165 'description' => __( 'Download ID.', 'woocommerce' ),
166 ),
167 'file_name' => array(
168 'type' => 'string',
169 'readonly' => true,
170 'context' => array( 'view', 'edit' ),
171 'description' => __( 'File name.', 'woocommerce' ),
172 ),
173 'file_path' => array(
174 'type' => 'string',
175 'readonly' => true,
176 'context' => array( 'view', 'edit' ),
177 'description' => __( 'File URL.', 'woocommerce' ),
178 ),
179 'order_id' => array(
180 'type' => 'integer',
181 'readonly' => true,
182 'context' => array( 'view', 'edit' ),
183 'description' => __( 'Order ID.', 'woocommerce' ),
184 ),
185 'order_number' => array(
186 'type' => 'string',
187 'readonly' => true,
188 'context' => array( 'view', 'edit' ),
189 'description' => __( 'Order Number.', 'woocommerce' ),
190 ),
191 'user_id' => array(
192 'type' => 'integer',
193 'readonly' => true,
194 'context' => array( 'view', 'edit' ),
195 'description' => __( 'User ID for the downloader.', 'woocommerce' ),
196 ),
197 'username' => array(
198 'type' => 'string',
199 'readonly' => true,
200 'context' => array( 'view', 'edit' ),
201 'description' => __( 'User name of the downloader.', 'woocommerce' ),
202 ),
203 'ip_address' => array(
204 'type' => 'string',
205 'readonly' => true,
206 'context' => array( 'view', 'edit' ),
207 'description' => __( 'IP address for the downloader.', 'woocommerce' ),
208 ),
209 ),
210 );
211
212 return $this->add_additional_fields_schema( $schema );
213 }
214
215 /**
216 * Get the query params for collections.
217 *
218 * @return array
219 */
220 public function get_collection_params() {
221 $params = parent::get_collection_params();
222 $params['orderby']['enum'] = $this->apply_custom_orderby_filters(
223 array(
224 'date',
225 'product',
226 )
227 );
228 $params['match'] = array(
229 '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' ),
230 'type' => 'string',
231 'default' => 'all',
232 'enum' => array(
233 'all',
234 'any',
235 ),
236 'validate_callback' => 'rest_validate_request_arg',
237 );
238 $params['product_includes'] = array(
239 'description' => __( 'Limit result set to items that have the specified product(s) assigned.', 'woocommerce' ),
240 'type' => 'array',
241 'items' => array(
242 'type' => 'integer',
243 ),
244 'default' => array(),
245 'sanitize_callback' => 'wp_parse_id_list',
246 'validate_callback' => 'rest_validate_request_arg',
247 );
248 $params['product_excludes'] = array(
249 'description' => __( 'Limit result set to items that don\'t have the specified product(s) assigned.', 'woocommerce' ),
250 'type' => 'array',
251 'items' => array(
252 'type' => 'integer',
253 ),
254 'default' => array(),
255 'validate_callback' => 'rest_validate_request_arg',
256 'sanitize_callback' => 'wp_parse_id_list',
257 );
258 $params['order_includes'] = array(
259 'description' => __( 'Limit result set to items that have the specified order ids.', 'woocommerce' ),
260 'type' => 'array',
261 'sanitize_callback' => 'wp_parse_id_list',
262 'validate_callback' => 'rest_validate_request_arg',
263 'items' => array(
264 'type' => 'integer',
265 ),
266 );
267 $params['order_excludes'] = array(
268 'description' => __( 'Limit result set to items that don\'t have the specified order ids.', 'woocommerce' ),
269 'type' => 'array',
270 'sanitize_callback' => 'wp_parse_id_list',
271 'validate_callback' => 'rest_validate_request_arg',
272 'items' => array(
273 'type' => 'integer',
274 ),
275 );
276 $params['customer_includes'] = array(
277 'description' => __( 'Limit response to objects that have the specified user ids.', 'woocommerce' ),
278 'type' => 'array',
279 'sanitize_callback' => 'wp_parse_id_list',
280 'validate_callback' => 'rest_validate_request_arg',
281 'items' => array(
282 'type' => 'integer',
283 ),
284 );
285 $params['customer_excludes'] = array(
286 'description' => __( 'Limit response to objects that don\'t have the specified user ids.', 'woocommerce' ),
287 'type' => 'array',
288 'sanitize_callback' => 'wp_parse_id_list',
289 'validate_callback' => 'rest_validate_request_arg',
290 'items' => array(
291 'type' => 'integer',
292 ),
293 );
294 $params['ip_address_includes'] = array(
295 'description' => __( 'Limit response to objects that have a specified ip address.', 'woocommerce' ),
296 'type' => 'array',
297 'validate_callback' => 'rest_validate_request_arg',
298 'items' => array(
299 'type' => 'string',
300 ),
301 );
302 $params['ip_address_excludes'] = array(
303 'description' => __( 'Limit response to objects that don\'t have a specified ip address.', 'woocommerce' ),
304 'type' => 'array',
305 'validate_callback' => 'rest_validate_request_arg',
306 'items' => array(
307 'type' => 'string',
308 ),
309 );
310
311 return $params;
312 }
313
314 /**
315 * Get the column names for export.
316 *
317 * @return array Key value pair of Column ID => Label.
318 */
319 public function get_export_columns() {
320 $export_columns = array(
321 'date' => __( 'Date', 'woocommerce' ),
322 'product' => __( 'Product title', 'woocommerce' ),
323 'file_name' => __( 'File name', 'woocommerce' ),
324 'order_number' => __( 'Order #', 'woocommerce' ),
325 'user_id' => __( 'User Name', 'woocommerce' ),
326 'ip_address' => __( 'IP', 'woocommerce' ),
327 );
328
329 /**
330 * Filter to add or remove column names from the downloads report for
331 * export.
332 *
333 * @since 1.6.0
334 */
335 return apply_filters(
336 'woocommerce_filter_downloads_export_columns',
337 $export_columns
338 );
339 }
340
341 /**
342 * Get the column values for export.
343 *
344 * @param array $item Single report item/row.
345 * @return array Key value pair of Column ID => Row Value.
346 */
347 public function prepare_item_for_export( $item ) {
348 $export_item = array(
349 'date' => $item['date'],
350 'product' => $item['_embedded']['product'][0]['name'],
351 'file_name' => $item['file_name'],
352 'order_number' => $item['order_number'],
353 'user_id' => $item['username'],
354 'ip_address' => $item['ip_address'],
355 );
356
357 /**
358 * Filter to prepare extra columns in the export item for the downloads
359 * report.
360 *
361 * @since 1.6.0
362 */
363 return apply_filters(
364 'woocommerce_report_downloads_prepare_export_item',
365 $export_item,
366 $item
367 );
368 }
369 }
370