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rc.2 3.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 / Customers / Controller.php
woocommerce / src / Admin / API / Reports / Customers Last commit date
Stats 3 months ago Controller.php 3 months ago DataStore.php 2 months ago Query.php 1 year ago
Controller.php
755 lines
1 <?php
2 /**
3 * REST API Reports customers controller
4 *
5 * Handles requests to the /reports/customers endpoint.
6 */
7
8 namespace Automattic\WooCommerce\Admin\API\Reports\Customers;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use Automattic\WooCommerce\Admin\API\Reports\GenericController;
13 use Automattic\WooCommerce\Admin\API\Reports\ExportableTraits;
14 use Automattic\WooCommerce\Admin\API\Reports\ExportableInterface;
15 use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
16
17 /**
18 * REST API Reports customers controller class.
19 *
20 * @internal
21 * @extends GenericController
22 */
23 class Controller extends GenericController implements ExportableInterface {
24 /**
25 * Exportable traits.
26 */
27 use ExportableTraits;
28
29 /**
30 * Route base.
31 *
32 * @var string
33 */
34 protected $rest_base = 'reports/customers';
35
36 /**
37 * Get data from Customers\Query.
38 *
39 * @override GenericController::get_datastore_data()
40 *
41 * @param array $query_args Query arguments.
42 * @return mixed Results from the data store.
43 */
44 protected function get_datastore_data( $query_args = array() ) {
45 $query = new Query( $query_args );
46 return $query->get_data();
47 }
48
49 /**
50 * Maps query arguments from the REST request.
51 *
52 * @param array $request Request array.
53 * @return array
54 */
55 protected function prepare_reports_query( $request ) {
56 $args = array();
57 $args['registered_before'] = $request['registered_before'];
58 $args['registered_after'] = $request['registered_after'];
59 $args['order_before'] = $request['before'];
60 $args['order_after'] = $request['after'];
61 $args['page'] = $request['page'];
62 $args['per_page'] = $request['per_page'];
63 $args['order'] = $request['order'];
64 $args['orderby'] = $request['orderby'];
65 $args['match'] = $request['match'];
66 $args['search'] = $request['search'];
67 $args['searchby'] = $request['searchby'];
68 $args['name_includes'] = $request['name_includes'];
69 $args['name_excludes'] = $request['name_excludes'];
70 $args['username_includes'] = $request['username_includes'];
71 $args['username_excludes'] = $request['username_excludes'];
72 $args['email_includes'] = $request['email_includes'];
73 $args['email_excludes'] = $request['email_excludes'];
74 $args['country_includes'] = $request['country_includes'];
75 $args['country_excludes'] = $request['country_excludes'];
76 $args['last_active_before'] = $request['last_active_before'];
77 $args['last_active_after'] = $request['last_active_after'];
78 $args['orders_count_min'] = $request['orders_count_min'];
79 $args['orders_count_max'] = $request['orders_count_max'];
80 $args['total_spend_min'] = $request['total_spend_min'];
81 $args['total_spend_max'] = $request['total_spend_max'];
82 $args['avg_order_value_min'] = $request['avg_order_value_min'];
83 $args['avg_order_value_max'] = $request['avg_order_value_max'];
84 $args['last_order_before'] = $request['last_order_before'];
85 $args['last_order_after'] = $request['last_order_after'];
86 $args['customers'] = $request['customers'];
87 $args['customers_exclude'] = $request['customers_exclude'];
88 $args['users'] = $request['users'];
89 $args['force_cache_refresh'] = $request['force_cache_refresh'];
90 $args['filter_empty'] = $request['filter_empty'];
91 $args['user_type'] = $request['user_type'];
92 $args['location_includes'] = $request['location_includes'];
93 $args['location_excludes'] = $request['location_excludes'];
94
95 $between_params_numeric = array( 'orders_count', 'total_spend', 'avg_order_value' );
96 $normalized_params_numeric = TimeInterval::normalize_between_params( $request, $between_params_numeric, false );
97 $between_params_date = array( 'last_active', 'registered' );
98 $normalized_params_date = TimeInterval::normalize_between_params( $request, $between_params_date, true );
99 $args = array_merge( $args, $normalized_params_numeric, $normalized_params_date );
100
101 $args = self::consolidate_customer_id_filters( $args );
102
103 return $args;
104 }
105
106 /**
107 * Consolidate customer identity filter IDs into customers/customers_exclude.
108 *
109 * When the frontend sends customer IDs via name_includes, email_includes, or
110 * username_includes, this method collects those IDs and merges them into the
111 * customers/customers_exclude params so the DataStore filters by customer_id.
112 *
113 * Only numeric values are consolidated. String values (e.g. actual email
114 * addresses or names) are left untouched for the DataStore's exact-match
115 * filtering.
116 *
117 * @param array $args Query arguments.
118 * @return array Modified query arguments.
119 */
120 public static function consolidate_customer_id_filters( $args ) {
121 $include_params = array( 'name_includes', 'email_includes', 'username_includes' );
122 $exclude_params = array( 'name_excludes', 'email_excludes', 'username_excludes' );
123 $match = $args['match'] ?? 'all';
124
125 $include_sets = array();
126 foreach ( $include_params as $param ) {
127 if ( ! empty( $args[ $param ] ) && self::is_id_list( $args[ $param ] ) ) {
128 $include_sets[] = wp_parse_id_list( $args[ $param ] );
129 $args[ $param ] = null;
130 }
131 }
132 if ( ! empty( $include_sets ) ) {
133 $consolidated = count( $include_sets ) > 1
134 ? ( 'all' === $match
135 ? call_user_func_array( 'array_intersect', $include_sets )
136 : array_unique( array_merge( ...$include_sets ) ) )
137 : $include_sets[0];
138
139 // Merge with any pre-existing customers filter.
140 if ( ! empty( $args['customers'] ) ) {
141 $existing = wp_parse_id_list( $args['customers'] );
142 $consolidated = 'all' === $match
143 ? array_intersect( $consolidated, $existing )
144 : array_unique( array_merge( $consolidated, $existing ) );
145 }
146
147 // When match=all and intersection is empty, force no-results.
148 $args['customers'] = empty( $consolidated ) ? array( 0 ) : array_values( $consolidated );
149 }
150
151 $exclude_sets = array();
152 foreach ( $exclude_params as $param ) {
153 if ( ! empty( $args[ $param ] ) && self::is_id_list( $args[ $param ] ) ) {
154 $exclude_sets[] = wp_parse_id_list( $args[ $param ] );
155 $args[ $param ] = null;
156 }
157 }
158 if ( ! empty( $exclude_sets ) ) {
159 $consolidated = count( $exclude_sets ) > 1
160 ? ( 'all' === $match
161 ? array_unique( array_merge( ...$exclude_sets ) )
162 : call_user_func_array( 'array_intersect', $exclude_sets ) )
163 : $exclude_sets[0];
164
165 // Merge with any pre-existing customers_exclude filter.
166 if ( ! empty( $args['customers_exclude'] ) ) {
167 $existing = wp_parse_id_list( $args['customers_exclude'] );
168 $consolidated = array_unique( array_merge( $consolidated, $existing ) );
169 }
170
171 $args['customers_exclude'] = array_values( $consolidated );
172 }
173
174 return $args;
175 }
176
177 /**
178 * Check if a value is a comma-separated list of numeric IDs.
179 *
180 * @param mixed $value The value to check.
181 * @return bool True if the value contains only numeric IDs.
182 */
183 private static function is_id_list( $value ) {
184 if ( is_array( $value ) ) {
185 $values = $value;
186 } elseif ( is_string( $value ) ) {
187 $values = explode( ',', $value );
188 } else {
189 return false;
190 }
191 foreach ( $values as $v ) {
192 if ( ! is_numeric( trim( $v ) ) ) {
193 return false;
194 }
195 }
196 return true;
197 }
198
199 /**
200 * Get one report.
201 *
202 * @param WP_REST_Request $request Request data.
203 * @return array|WP_Error
204 */
205 public function get_item( $request ) {
206 $query_args = $this->prepare_reports_query( $request );
207 $query_args['customers'] = array( $request->get_param( 'id' ) );
208 $customers_query = new Query( $query_args );
209 $report_data = $customers_query->get_data();
210
211 $data = array();
212
213 foreach ( $report_data->data as $customer_data ) {
214 $item = $this->prepare_item_for_response( $customer_data, $request );
215 $data[] = $this->prepare_response_for_collection( $item );
216 }
217
218 $response = rest_ensure_response( $data );
219 $response->header( 'X-WP-Total', (int) $report_data->total );
220 $response->header( 'X-WP-TotalPages', (int) $report_data->pages );
221
222 return $response;
223 }
224
225 /**
226 * Prepare a report data item for serialization.
227 *
228 * @param array $report Report data item as returned from Data Store.
229 * @param \WP_REST_Request $request Request object.
230 * @return \WP_REST_Response
231 */
232 public function prepare_item_for_response( $report, $request ) {
233 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
234 $data = $this->add_additional_fields_to_object( $report, $request );
235 // Trim name field to prevent whitespace issues.
236 $data['name'] = trim( $data['name'] );
237 // Registered date is UTC.
238 $data['date_registered_gmt'] = wc_rest_prepare_date_response( $data['date_registered'] );
239 $data['date_registered'] = wc_rest_prepare_date_response( $data['date_registered'], false );
240 // Last active date is local time.
241 $data['date_last_active_gmt'] = wc_rest_prepare_date_response( $data['date_last_active'], false );
242 $data['date_last_active'] = wc_rest_prepare_date_response( $data['date_last_active'] );
243 $data = $this->filter_response_by_context( $data, $context );
244
245 // Wrap the data in a response object.
246 $response = rest_ensure_response( $data );
247 $response->add_links( $this->prepare_links( $report ) );
248 /**
249 * Filter a report returned from the API.
250 *
251 * Allows modification of the report data right before it is returned.
252 *
253 * @param WP_REST_Response $response The response object.
254 * @param object $report The original report object.
255 * @param WP_REST_Request $request Request used to generate the response.
256 * @since 4.0.0
257 */
258 return apply_filters( 'woocommerce_rest_prepare_report_customers', $response, $report, $request );
259 }
260
261 /**
262 * Prepare links for the request.
263 *
264 * @param array $object Object data.
265 * @return array
266 */
267 protected function prepare_links( $object ) {
268 if ( empty( $object['user_id'] ) ) {
269 return array();
270 }
271
272 return array(
273 'customer' => array(
274 'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object['id'] ) ),
275 ),
276 'collection' => array(
277 'href' => rest_url( sprintf( '/%s/customers', $this->namespace ) ),
278 ),
279 );
280 }
281
282 /**
283 * Get the Report's schema, conforming to JSON Schema.
284 *
285 * @return array
286 */
287 public function get_item_schema() {
288 $schema = array(
289 '$schema' => 'http://json-schema.org/draft-04/schema#',
290 'title' => 'report_customers',
291 'type' => 'object',
292 'properties' => array(
293 'id' => array(
294 'description' => __( 'Customer ID.', 'woocommerce' ),
295 'type' => 'integer',
296 'context' => array( 'view', 'edit' ),
297 'readonly' => true,
298 ),
299 'user_id' => array(
300 'description' => __( 'User ID.', 'woocommerce' ),
301 'type' => 'integer',
302 'context' => array( 'view', 'edit' ),
303 'readonly' => true,
304 ),
305 'name' => array(
306 'description' => __( 'Name.', 'woocommerce' ),
307 'type' => 'string',
308 'context' => array( 'view', 'edit' ),
309 'readonly' => true,
310 ),
311 'first_name' => array(
312 'description' => __( 'First name.', 'woocommerce' ),
313 'type' => 'string',
314 'context' => array( 'view', 'edit' ),
315 'readonly' => true,
316 ),
317 'last_name' => array(
318 'description' => __( 'Last name.', 'woocommerce' ),
319 'type' => 'string',
320 'context' => array( 'view', 'edit' ),
321 'readonly' => true,
322 ),
323 'email' => array(
324 'description' => __( 'Email address.', 'woocommerce' ),
325 'type' => 'string',
326 'context' => array( 'view', 'edit' ),
327 'readonly' => true,
328 ),
329 'username' => array(
330 'description' => __( 'Username.', 'woocommerce' ),
331 'type' => 'string',
332 'context' => array( 'view', 'edit' ),
333 'readonly' => true,
334 ),
335 'country' => array(
336 'description' => __( 'Country / Region.', 'woocommerce' ),
337 'type' => 'string',
338 'context' => array( 'view', 'edit' ),
339 'readonly' => true,
340 ),
341 'city' => array(
342 'description' => __( 'City.', 'woocommerce' ),
343 'type' => 'string',
344 'context' => array( 'view', 'edit' ),
345 'readonly' => true,
346 ),
347 'state' => array(
348 'description' => __( 'Region.', 'woocommerce' ),
349 'type' => 'string',
350 'context' => array( 'view', 'edit' ),
351 'readonly' => true,
352 ),
353 'postcode' => array(
354 'description' => __( 'Postal code.', 'woocommerce' ),
355 'type' => 'string',
356 'context' => array( 'view', 'edit' ),
357 'readonly' => true,
358 ),
359 'date_registered' => array(
360 'description' => __( 'Date registered.', 'woocommerce' ),
361 'type' => 'date-time',
362 'context' => array( 'view', 'edit' ),
363 'readonly' => true,
364 ),
365 'date_registered_gmt' => array(
366 'description' => __( 'Date registered GMT.', 'woocommerce' ),
367 'type' => 'date-time',
368 'context' => array( 'view', 'edit' ),
369 'readonly' => true,
370 ),
371 'date_last_active' => array(
372 'description' => __( 'Date last active.', 'woocommerce' ),
373 'type' => 'date-time',
374 'context' => array( 'view', 'edit' ),
375 'readonly' => true,
376 ),
377 'date_last_active_gmt' => array(
378 'description' => __( 'Date last active GMT.', 'woocommerce' ),
379 'type' => 'date-time',
380 'context' => array( 'view', 'edit' ),
381 'readonly' => true,
382 ),
383 'orders_count' => array(
384 'description' => __( 'Order count.', 'woocommerce' ),
385 'type' => 'integer',
386 'context' => array( 'view', 'edit' ),
387 'readonly' => true,
388 ),
389 'total_spend' => array(
390 'description' => __( 'Total spend.', 'woocommerce' ),
391 'type' => 'number',
392 'context' => array( 'view', 'edit' ),
393 'readonly' => true,
394 ),
395 'avg_order_value' => array(
396 'description' => __( 'Avg order value.', 'woocommerce' ),
397 'type' => 'number',
398 'context' => array( 'view', 'edit' ),
399 'readonly' => true,
400 ),
401 ),
402 );
403 return $this->add_additional_fields_schema( $schema );
404 }
405
406 /**
407 * Get the query params for collections.
408 *
409 * @return array
410 */
411 public function get_collection_params() {
412 $params = parent::get_collection_params();
413 $params['registered_before'] = array(
414 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
415 'type' => 'string',
416 'format' => 'date-time',
417 'validate_callback' => 'rest_validate_request_arg',
418 );
419 $params['registered_after'] = array(
420 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
421 'type' => 'string',
422 'format' => 'date-time',
423 'validate_callback' => 'rest_validate_request_arg',
424 );
425 $params['orderby']['default'] = 'date_registered';
426 $params['orderby']['enum'] = $this->apply_custom_orderby_filters(
427 array(
428 'username',
429 'name',
430 'first_name',
431 'last_name',
432 'email',
433 'location',
434 'country',
435 'city',
436 'state',
437 'postcode',
438 'date_registered',
439 'date_last_active',
440 'orders_count',
441 'total_spend',
442 'avg_order_value',
443 )
444 );
445 $params['match'] = array(
446 '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' ),
447 'type' => 'string',
448 'default' => 'all',
449 'enum' => array(
450 'all',
451 'any',
452 ),
453 'validate_callback' => 'rest_validate_request_arg',
454 );
455 $params['search'] = array(
456 'description' => __( 'Limit response to objects with a customer field containing the search term. Searches the field provided by `searchby`.', 'woocommerce' ),
457 'type' => 'string',
458 'validate_callback' => 'rest_validate_request_arg',
459 );
460 $params['searchby'] = array(
461 'description' => 'Limit results with `search` and `searchby` to specific fields containing the search term.',
462 'type' => 'string',
463 'default' => 'name',
464 'enum' => array(
465 'name',
466 'username',
467 'email',
468 'all',
469 ),
470 );
471 $params['name_includes'] = array(
472 'description' => __( 'Limit response to objects with specific names.', 'woocommerce' ),
473 'type' => 'string',
474 'validate_callback' => 'rest_validate_request_arg',
475 );
476 $params['name_excludes'] = array(
477 'description' => __( 'Limit response to objects excluding specific names.', 'woocommerce' ),
478 'type' => 'string',
479 'validate_callback' => 'rest_validate_request_arg',
480 );
481 $params['username_includes'] = array(
482 'description' => __( 'Limit response to objects with specific usernames.', 'woocommerce' ),
483 'type' => 'string',
484 'validate_callback' => 'rest_validate_request_arg',
485 );
486 $params['username_excludes'] = array(
487 'description' => __( 'Limit response to objects excluding specific usernames.', 'woocommerce' ),
488 'type' => 'string',
489 'validate_callback' => 'rest_validate_request_arg',
490 );
491 $params['email_includes'] = array(
492 'description' => __( 'Limit response to objects including emails.', 'woocommerce' ),
493 'type' => 'string',
494 'validate_callback' => 'rest_validate_request_arg',
495 );
496 $params['email_excludes'] = array(
497 'description' => __( 'Limit response to objects excluding emails.', 'woocommerce' ),
498 'type' => 'string',
499 'validate_callback' => 'rest_validate_request_arg',
500 );
501 $params['country_includes'] = array(
502 'description' => __( 'Limit response to objects with specific countries.', 'woocommerce' ),
503 'type' => 'string',
504 'validate_callback' => 'rest_validate_request_arg',
505 );
506 $params['country_excludes'] = array(
507 'description' => __( 'Limit response to objects excluding specific countries.', 'woocommerce' ),
508 'type' => 'string',
509 'validate_callback' => 'rest_validate_request_arg',
510 );
511 $params['last_active_before'] = array(
512 'description' => __( 'Limit response to objects last active before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
513 'type' => 'string',
514 'format' => 'date-time',
515 'validate_callback' => 'rest_validate_request_arg',
516 );
517 $params['last_active_after'] = array(
518 'description' => __( 'Limit response to objects last active after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
519 'type' => 'string',
520 'format' => 'date-time',
521 'validate_callback' => 'rest_validate_request_arg',
522 );
523 $params['last_active_between'] = array(
524 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ),
525 'type' => 'array',
526 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
527 'items' => array(
528 'type' => 'string',
529 ),
530 );
531 $params['registered_before'] = array(
532 'description' => __( 'Limit response to objects registered before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
533 'type' => 'string',
534 'format' => 'date-time',
535 'validate_callback' => 'rest_validate_request_arg',
536 );
537 $params['registered_after'] = array(
538 'description' => __( 'Limit response to objects registered after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
539 'type' => 'string',
540 'format' => 'date-time',
541 'validate_callback' => 'rest_validate_request_arg',
542 );
543 $params['registered_between'] = array(
544 'description' => __( 'Limit response to objects last active between two given ISO8601 compliant datetime.', 'woocommerce' ),
545 'type' => 'array',
546 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_date_arg' ),
547 'items' => array(
548 'type' => 'string',
549 ),
550 );
551 $params['orders_count_min'] = array(
552 'description' => __( 'Limit response to objects with an order count greater than or equal to given integer.', 'woocommerce' ),
553 'type' => 'integer',
554 'sanitize_callback' => 'absint',
555 'validate_callback' => 'rest_validate_request_arg',
556 );
557 $params['orders_count_max'] = array(
558 'description' => __( 'Limit response to objects with an order count less than or equal to given integer.', 'woocommerce' ),
559 'type' => 'integer',
560 'sanitize_callback' => 'absint',
561 'validate_callback' => 'rest_validate_request_arg',
562 );
563 $params['orders_count_between'] = array(
564 'description' => __( 'Limit response to objects with an order count between two given integers.', 'woocommerce' ),
565 'type' => 'array',
566 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
567 'items' => array(
568 'type' => 'integer',
569 ),
570 );
571 $params['total_spend_min'] = array(
572 'description' => __( 'Limit response to objects with a total order spend greater than or equal to given number.', 'woocommerce' ),
573 'type' => 'number',
574 'validate_callback' => 'rest_validate_request_arg',
575 );
576 $params['total_spend_max'] = array(
577 'description' => __( 'Limit response to objects with a total order spend less than or equal to given number.', 'woocommerce' ),
578 'type' => 'number',
579 'validate_callback' => 'rest_validate_request_arg',
580 );
581 $params['total_spend_between'] = array(
582 'description' => __( 'Limit response to objects with a total order spend between two given numbers.', 'woocommerce' ),
583 'type' => 'array',
584 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
585 'items' => array(
586 'type' => 'integer',
587 ),
588 );
589 $params['avg_order_value_min'] = array(
590 'description' => __( 'Limit response to objects with an average order spend greater than or equal to given number.', 'woocommerce' ),
591 'type' => 'number',
592 'validate_callback' => 'rest_validate_request_arg',
593 );
594 $params['avg_order_value_max'] = array(
595 'description' => __( 'Limit response to objects with an average order spend less than or equal to given number.', 'woocommerce' ),
596 'type' => 'number',
597 'validate_callback' => 'rest_validate_request_arg',
598 );
599 $params['avg_order_value_between'] = array(
600 'description' => __( 'Limit response to objects with an average order spend between two given numbers.', 'woocommerce' ),
601 'type' => 'array',
602 'validate_callback' => array( '\Automattic\WooCommerce\Admin\API\Reports\TimeInterval', 'rest_validate_between_numeric_arg' ),
603 'items' => array(
604 'type' => 'integer',
605 ),
606 );
607 $params['last_order_before'] = array(
608 'description' => __( 'Limit response to objects with last order before (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
609 'type' => 'string',
610 'format' => 'date-time',
611 'validate_callback' => 'rest_validate_request_arg',
612 );
613 $params['last_order_after'] = array(
614 'description' => __( 'Limit response to objects with last order after (or at) a given ISO8601 compliant datetime.', 'woocommerce' ),
615 'type' => 'string',
616 'format' => 'date-time',
617 'validate_callback' => 'rest_validate_request_arg',
618 );
619 $params['customers'] = array(
620 'description' => __( 'Limit result to items with specified customer ids.', 'woocommerce' ),
621 'type' => 'array',
622 'sanitize_callback' => 'wp_parse_id_list',
623 'validate_callback' => 'rest_validate_request_arg',
624 'items' => array(
625 'type' => 'integer',
626 ),
627 );
628 $params['customers_exclude'] = array(
629 'description' => __( 'Limit result to exclude items with specified customer ids.', 'woocommerce' ),
630 'type' => 'array',
631 'sanitize_callback' => 'wp_parse_id_list',
632 'validate_callback' => 'rest_validate_request_arg',
633 'items' => array(
634 'type' => 'integer',
635 ),
636 );
637 $params['users'] = array(
638 'description' => __( 'Limit result to items with specified user ids.', 'woocommerce' ),
639 'type' => 'array',
640 'sanitize_callback' => 'wp_parse_id_list',
641 'validate_callback' => 'rest_validate_request_arg',
642 'items' => array(
643 'type' => 'integer',
644 ),
645 );
646 $params['filter_empty'] = array(
647 'description' => __( 'Filter out results where any of the passed fields are empty', 'woocommerce' ),
648 'type' => 'array',
649 'validate_callback' => 'rest_validate_request_arg',
650 'items' => array(
651 'type' => 'string',
652 'enum' => array(
653 'email',
654 'name',
655 'country',
656 'city',
657 'state',
658 'postcode',
659 ),
660 ),
661 );
662 $params['user_type'] = array(
663 'description' => __( 'Limit result to items with specified user type.', 'woocommerce' ),
664 'type' => 'string',
665 'default' => 'all',
666 'validate_callback' => 'rest_validate_request_arg',
667 'enum' => array(
668 'all',
669 'registered',
670 'guest',
671 ),
672 );
673 $params['location_includes'] = array(
674 'description' => __( 'Includes customers by location (state, country). Provide a comma-separated list of locations. Each location can be a country code (e.g. GB) or combination of country and state (e.g. US:CA).', 'woocommerce' ),
675 'type' => 'string',
676 'validate_callback' => 'rest_validate_request_arg',
677 );
678 $params['location_excludes'] = array(
679 'description' => __( 'Excludes customers by location (state, country). Provide a comma-separated list of locations. Each location can be a country code (e.g. GB) or combination of country and state (e.g. US:CA).', 'woocommerce' ),
680 'type' => 'string',
681 'validate_callback' => 'rest_validate_request_arg',
682 );
683 return $params;
684 }
685
686 /**
687 * Get the column names for export.
688 *
689 * @return array Key value pair of Column ID => Label.
690 */
691 public function get_export_columns() {
692 $export_columns = array(
693 'name' => __( 'Name', 'woocommerce' ),
694 'username' => __( 'Username', 'woocommerce' ),
695 'last_active' => __( 'Last Active', 'woocommerce' ),
696 'registered' => __( 'Sign Up', 'woocommerce' ),
697 'email' => __( 'Email', 'woocommerce' ),
698 'orders_count' => __( 'Orders', 'woocommerce' ),
699 'total_spend' => __( 'Total Spend', 'woocommerce' ),
700 'avg_order_value' => __( 'AOV', 'woocommerce' ),
701 'country' => __( 'Country / Region', 'woocommerce' ),
702 'city' => __( 'City', 'woocommerce' ),
703 'region' => __( 'Region', 'woocommerce' ),
704 'postcode' => __( 'Postal Code', 'woocommerce' ),
705 );
706
707 /**
708 * Filter to add or remove column names from the customers report for
709 * export.
710 *
711 * @since 1.6.0
712 */
713 return apply_filters(
714 'woocommerce_report_customers_export_columns',
715 $export_columns
716 );
717 }
718
719 /**
720 * Get the column values for export.
721 *
722 * @param array $item Single report item/row.
723 * @return array Key value pair of Column ID => Row Value.
724 */
725 public function prepare_item_for_export( $item ) {
726 $export_item = array(
727 'name' => $item['name'],
728 'username' => $item['username'],
729 'last_active' => $item['date_last_active'],
730 'registered' => $item['date_registered'],
731 'email' => $item['email'],
732 'orders_count' => $item['orders_count'],
733 'total_spend' => self::csv_number_format( $item['total_spend'] ),
734 'avg_order_value' => self::csv_number_format( $item['avg_order_value'] ),
735 'country' => $item['country'],
736 'city' => $item['city'],
737 'region' => $item['state'],
738 'postcode' => $item['postcode'],
739 );
740
741 /**
742 * Filter the column values of an item being exported.
743 *
744 * @param object $export_item Key value pair of Column ID => Row Value.
745 * @param object $item Single report item/row.
746 * @since 4.0.0
747 */
748 return apply_filters(
749 'woocommerce_report_customers_prepare_export_item',
750 $export_item,
751 $item
752 );
753 }
754 }
755