1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
<?php /** * REST API Reports customers controller * * Handles requests to the /reports/customers endpoint. */
namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\API\Reports\GenericController; use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits; use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface; use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
/** * REST API Reports customers controller class. * * @internal * @extends GenericController */ class Controller extends GenericController implements ExportableInterface { /** * Exportable traits. */ use ExportableTraits;
/** * Route base. * * @var string */ protected $rest_base = 'reports/customers';
/** * Get data from Customers\Query. * * @override GenericController::get_datastore_data() * * @param array $query_args Query arguments. * @return mixed Results from the data store. */ protected function get_datastore_data( $query_args = array() ) { $query = new Query( $query_args ); return $query->get_data(); }
/** * Maps query arguments from the REST request. * * @param array $request Request array. * @return array */ protected function prepare_reports_query( $request ) { $args = array(); $args['registered_before'] = $request['registered_before']; $args['registered_after'] = $request['registered_after']; $args['order_before'] = $request['before']; $args['order_after'] = $request['after']; $args['page'] = $request['page']; $args['per_page'] = $request['per_page']; $args['order'] = $request['order']; $args['orderby'] = $request['orderby']; $args['match'] = $request['match']; $args['search'] = $request['search']; $args['searchby'] = $request['searchby']; $args['name_includes'] = $request['name_includes']; $args['name_excludes'] = $request['name_excludes']; $args['username_includes'] = $request['username_includes']; $args['username_excludes'] = $request['username_excludes']; $args['email_includes'] = $request['email_includes']; $args['email_excludes'] = $request['email_excludes']; $args['country_includes'] = $request['country_includes']; $args['country_excludes'] = $request['country_excludes']; $args['last_active_before'] = $request['last_active_before']; $args['last_active_after'] = $request['last_active_after']; $args['orders_count_min'] = $request['orders_count_min']; $args['orders_count_max'] = $request['orders_count_max']; $args['total_spend_min'] = $request['total_spend_min']; $args['total_spend_max'] = $request['total_spend_max']; $args['avg_order_value_min'] = $request['avg_order_value_min']; $args['avg_order_value_max'] = $request['avg_order_value_max']; $args['last_order_before'] = $request['last_order_before']; $args['last_order_after'] = $request['last_order_after']; $args['customers'] = $request['customers']; $args['users'] = $request['users']; $args['force_cache_refresh'] = $request['force_cache_refresh']; $args['filter_empty'] = $request['filter_empty'];
$between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' ); $normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false ); $between_params_date = array( 'last_active', 'registered' ); $normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true ); $args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
return $args; }
/** * Get one report. * * @param WP_REST_Request $request Request data. * @return array|WP_Error */ public function get_item( $request ) { $query_args = $this->prepare_reports_query( $request ); $query_args['customers'] = array( $request->get_param( 'id' ) ); $customers_query = new Query( $query_args ); $report_data = $customers_query->get_data();
$data = array();
foreach ( $report_data->data as $customer_data ) { $item = $this->prepare_item_for_response( $customer_data, $request ); $data[] = $this->prepare_response_for_collection( $item ); }
$response = rest_ensure_response( $data ); $response->header( 'X-WP-Total', (int) $report_data->total ); $response->header( 'X-WP-TotalPages', (int) $report_data->pages );
return $response; }
/** * Prepare a report data item for serialization. * * @param array $report Report data item as returned from Data Store. * @param \WP_REST_Request $request Request object. * @return \WP_REST_Response */ public function prepare_item_for_response( $report, $request ) { $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $report, $request ); // Registered date is UTC. $data['date_registered_gmt'] = wc_rest_prepare_date_response( $data['date_registered'] ); $data['date_registered'] = wc_rest_prepare_date_response( $data['date_registered'], false ); // Last active date is local time. $data['date_last_active_gmt'] = wc_rest_prepare_date_response( $data['date_last_active'], false ); $data['date_last_active'] = wc_rest_prepare_date_response( $data['date_last_active'] ); $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. * @since 4.0.0 */ return apply_filters( 'woocommerce_rest_prepare_report_customers', $response, $report, $request ); }
/** * Prepare links for the request. * * @param array $object Object data. * @return array */ protected function prepare_links( $object ) { if ( empty( $object['user_id'] ) ) { return array(); }
return array( 'customer' => array( 'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object['id'] ) ), ), 'collection' => array( 'href' => rest_url( sprintf( '/%s/customers', $this->namespace ) ), ), ); }
/** * 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_customers', 'type' => 'object', 'properties' => array( 'id' => array( 'description' => __( 'Customer ID.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'user_id' => array( 'description' => __( 'User ID.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'name' => array( 'description' => __( 'Name.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'username' => array( 'description' => __( 'Username.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'country' => array( 'description' => __( 'Country / Region.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'city' => array( 'description' => __( 'City.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'state' => array( 'description' => __( 'Region.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'postcode' => array( 'description' => __( 'Postal code.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'date_registered' => array( 'description' => __( 'Date registered.', 'woocommerce' ), 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'date_registered_gmt' => array( 'description' => __( 'Date registered GMT.', 'woocommerce' ), 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'date_last_active' => array( 'description' => __( 'Date last active.', 'woocommerce' ), 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'date_last_active_gmt' => array( 'description' => __( 'Date last active GMT.', 'woocommerce' ), 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'orders_count' => array( 'description' => __( 'Order count.', 'woocommerce' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'total_spend' => array( 'description' => __( 'Total spend.', 'woocommerce' ), 'type' => 'number', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'avg_order_value' => array( 'description' => __( 'Avg order value.', 'woocommerce' ), 'type' => 'number', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ), ); return $this->add_additional_fields_schema( $schema ); }
/** * Get the query params for collections. * * @return array */ public function get_collection_params() { $params = parent::get_collection_params(); $params['registered_before'] = array( 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['registered_after'] = array( 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['orderby']['default'] = 'date_registered'; $params['orderby']['enum'] = $this->apply_custom_orderby_filters( array( 'username', 'name', 'country', 'city', 'state', 'postcode', 'date_registered', 'date_last_active', 'orders_count', 'total_spend', 'avg_order_value', ) ); $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: status_is, status_is_not, product_includes, product_excludes, coupon_includes, coupon_excludes, customer, categories', 'woocommerce' ), 'type' => 'string', 'default' => 'all', 'enum' => array( 'all', 'any', ), 'validate_callback' => 'rest_validate_request_arg', ); $params['search'] = array( 'description' => __( 'Limit response to objects with a customer field containing the search term. Searches the field provided by `searchby`.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['searchby'] = array( 'description' => 'Limit results with `search` and `searchby` to specific fields containing the search term.', 'type' => 'string', 'default' => 'name', 'enum' => array( 'name', 'username', 'email', 'all', ), ); $params['name_includes'] = array( 'description' => __( 'Limit response to objects with specific names.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['name_excludes'] = array( 'description' => __( 'Limit response to objects excluding specific names.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['username_includes'] = array( 'description' => __( 'Limit response to objects with specific usernames.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['username_excludes'] = array( 'description' => __( 'Limit response to objects excluding specific usernames.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['email_includes'] = array( 'description' => __( 'Limit response to objects including emails.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['email_excludes'] = array( 'description' => __( 'Limit response to objects excluding emails.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['country_includes'] = array( 'description' => __( 'Limit response to objects with specific countries.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['country_excludes'] = array( 'description' => __( 'Limit response to objects excluding specific countries.', 'woocommerce' ), 'type' => 'string', 'validate_callback' => 'rest_validate_request_arg', ); $params['last_active_before'] = array( 'description' => __( 'Limit response to objects last active before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['last_active_after'] = array( 'description' => __( 'Limit response to objects last active after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['last_active_between'] = array( 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'array', 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ), 'items' => array( 'type' => 'string', ), ); $params['registered_before'] = array( 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['registered_after'] = array( 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['registered_between'] = array( 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'array', 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ), 'items' => array( 'type' => 'string', ), ); $params['orders_count_min'] = array( 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce' ), 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ); $params['orders_count_max'] = array( 'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'woocommerce' ), 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ); $params['orders_count_between'] = array( 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce' ), 'type' => 'array', 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ), 'items' => array( 'type' => 'integer', ), ); $params['total_spend_min'] = array( 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce' ), 'type' => 'number', 'validate_callback' => 'rest_validate_request_arg', ); $params['total_spend_max'] = array( 'description' => __( 'Limit response to objects with a total order spend less than or equal to given number.', 'woocommerce' ), 'type' => 'number', 'validate_callback' => 'rest_validate_request_arg', ); $params['total_spend_between'] = array( 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce' ), 'type' => 'array', 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ), 'items' => array( 'type' => 'integer', ), ); $params['avg_order_value_min'] = array( 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce' ), 'type' => 'number', 'validate_callback' => 'rest_validate_request_arg', ); $params['avg_order_value_max'] = array( 'description' => __( 'Limit response to objects with an average order spend less than or equal to given number.', 'woocommerce' ), 'type' => 'number', 'validate_callback' => 'rest_validate_request_arg', ); $params['avg_order_value_between'] = array( 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce' ), 'type' => 'array', 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ), 'items' => array( 'type' => 'integer', ), ); $params['last_order_before'] = array( 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['last_order_after'] = array( 'description' => __( 'Limit response to objects with last order after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ), 'type' => 'string', 'format' => 'date-time', 'validate_callback' => 'rest_validate_request_arg', ); $params['customers'] = array( 'description' => __( 'Limit result to items with specified customer ids.', 'woocommerce' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'integer', ), ); $params['users'] = array( 'description' => __( 'Limit result to items with specified user ids.', 'woocommerce' ), 'type' => 'array', 'sanitize_callback' => 'wp_parse_id_list', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'integer', ), ); $params['filter_empty'] = array( 'description' => __( 'Filter out results where any of the passed fields are empty', 'woocommerce' ), 'type' => 'array', 'validate_callback' => 'rest_validate_request_arg', 'items' => array( 'type' => 'string', 'enum' => array( 'email', 'name', 'country', 'city', 'state', 'postcode', ), ), );
return $params; }
/** * Get the column names for export. * * @return array Key value pair of Column ID => Label. */ public function get_export_columns() { $export_columns = array( 'name' => __( 'Name', 'woocommerce' ), 'username' => __( 'Username', 'woocommerce' ), 'last_active' => __( 'Last Active', 'woocommerce' ), 'registered' => __( 'Sign Up', 'woocommerce' ), 'email' => __( 'Email', 'woocommerce' ), 'orders_count' => __( 'Orders', 'woocommerce' ), 'total_spend' => __( 'Total Spend', 'woocommerce' ), 'avg_order_value' => __( 'AOV', 'woocommerce' ), 'country' => __( 'Country / Region', 'woocommerce' ), 'city' => __( 'City', 'woocommerce' ), 'region' => __( 'Region', 'woocommerce' ), 'postcode' => __( 'Postal Code', 'woocommerce' ), );
/** * Filter to add or remove column names from the customers report for * export. * * @since 1.6.0 */ return apply_filters( 'woocommerce_report_customers_export_columns', $export_columns ); }
/** * Get the column values for export. * * @param array $item Single report item/row. * @return array Key value pair of Column ID => Row Value. */ public function prepare_item_for_export( $item ) { $export_item = array( 'name' => $item['name'], 'username' => $item['username'], 'last_active' => $item['date_last_active'], 'registered' => $item['date_registered'], 'email' => $item['email'], 'orders_count' => $item['orders_count'], 'total_spend' => self::csv_number_format( $item['total_spend'] ), 'avg_order_value' => self::csv_number_format( $item['avg_order_value'] ), 'country' => $item['country'], 'city' => $item['city'], 'region' => $item['state'], 'postcode' => $item['postcode'], );
/** * Filter the column values of an item being exported. * * @param object $export_item Key value pair of Column ID => Row Value. * @param object $item Single report item/row. * @since 4.0.0 */ return apply_filters( 'woocommerce_report_customers_prepare_export_item', $export_item, $item ); } }
|