class-wc-cli-customer.php
19.3 KB
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<?php
/**
* Manage Customers.
*
* @since 2.5.0
* @package WooCommerce/CLI
* @category CLI
* @author WooThemes
*/
class WC_CLI_Customer extends WC_CLI_Command {
/**
* Create a customer.
*
* ## OPTIONS
*
* <email>
* : The email address of the customer to create.
*
* [--<field>=<value>]
* : Associative args for the new customer.
*
* [--porcelain]
* : Outputs just the new customer id.
*
* ## AVAILABLE FIELDS
*
* These fields are optionally available for create command:
*
* * username
* * password
* * first_name
* * last_name
*
* Billing address fields:
*
* * billing_address.first_name
* * billing_address.last_name
* * billing_address.company
* * billing_address.address_1
* * billing_address.address_2
* * billing_address.city
* * billing_address.state
* * billing_address.postcode
* * billing_address.country
* * billing_address.email
* * billing_address.phone
*
* Shipping address fields:
*
* * shipping_address.first_name
* * shipping_address.last_name
* * shipping_address.company
* * shipping_address.address_1
* * shipping_address.address_2
* * shipping_address.city
* * shipping_address.state
* * shipping_address.postcode
* * shipping_address.country
*
* ## EXAMPLES
*
* wp wc customer create new-customer@example.com --first_name=Akeda
*
* @since 2.5.0
*/
public function create( $args, $assoc_args ) {
global $wpdb;
try {
$porcelain = isset( $assoc_args['porcelain'] );
unset( $assoc_args['porcelain'] );
$assoc_args['email'] = $args[0];
$data = apply_filters( 'woocommerce_cli_create_customer_data', $this->unflatten_array( $assoc_args ) );
// Sets the username.
$data['username'] = ! empty( $data['username'] ) ? $data['username'] : '';
// Sets the password.
$data['password'] = ! empty( $data['password'] ) ? $data['password'] : '';
// Attempts to create the new customer.
$id = wc_create_new_customer( $data['email'], $data['username'], $data['password'] );
// Checks for an error in the customer creation.
if ( is_wp_error( $id ) ) {
throw new WC_CLI_Exception( $id->get_error_code(), $id->get_error_message() );
}
// Added customer data.
$this->update_customer_data( $id, $data );
do_action( 'woocommerce_cli_create_customer', $id, $data );
if ( $porcelain ) {
WP_CLI::line( $id );
} else {
WP_CLI::success( "Created customer $id." );
}
} catch ( WC_CLI_Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
}
/**
* Delete one or more customers.
*
* ## OPTIONS
*
* <customer>...
* : The customer ID, email, or username to delete.
*
* ## EXAMPLES
*
* wp wc customer delete 123
*
* wp wc customer delete $(wp wc customer list --format=ids)
*
* @since 2.5.0
*/
public function delete( $args, $assoc_args ) {
$exit_code = 0;
foreach ( $args as $arg ) {
try {
$customer = $this->get_user( $arg );
do_action( 'woocommerce_cli_delete_customer', $customer['id'] );
$r = wp_delete_user( $customer['id'] );
if ( $r ) {
WP_CLI::success( "Deleted customer {$customer['id']}." );
} else {
$exit_code += 1;
WP_CLI::warning( "Failed deleting customer {$customer['id']}." );
}
} catch ( WC_CLI_Exception $e ) {
WP_CLI::warning( $e->getMessage() );
}
}
exit( $exit_code ? 1 : 0 );
}
/**
* View customer downloads.
*
* ## OPTIONS
*
* <customer>
* : The customer ID, email or username.
*
* [--field=<field>]
* : Instead of returning the whole customer fields, returns the value of a single fields.
*
* [--fields=<fields>]
* : Get a specific subset of the customer's fields.
*
* [--format=<format>]
* : Accepted values: table, json, csv. Default: table.
*
* ## AVAILABLE FIELDS
*
* * download_id
* * download_name
* * access_expires
*
* ## EXAMPLES
*
* wp wc customer downloads 123
*
* @since 2.5.0
*/
public function downloads( $args, $assoc_args ) {
try {
$user = $this->get_user( $args[0] );
$downloads = array();
foreach ( wc_get_customer_available_downloads( $user['id'] ) as $key => $download ) {
$downloads[ $key ] = $download;
$downloads[ $key ]['access_expires'] = $this->format_datetime( $download['access_expires'] );
}
$downloads = apply_filters( 'woocommerce_cli_customer_downloads', $downloads, $user, $assoc_args );
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = $this->get_customer_download_fields();
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $downloads );
} catch ( WC_CLI_Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
}
/**
* Get a customer.
*
* ## OPTIONS
*
* <customer>
* : Customer ID, email, or username.
*
* [--field=<field>]
* : Instead of returning the whole customer fields, returns the value of a single fields.
*
* [--fields=<fields>]
* : Get a specific subset of the customer's fields.
*
* [--format=<format>]
* : Accepted values: table, json, csv. Default: table.
*
* ## AVAILABLE FIELDS
*
* * id
* * email
* * first_name
* * last_name
* * created_at
* * username
* * last_order_id
* * last_order_date
* * orders_count
* * total_spent
* * avatar_url
*
* Billing address fields:
*
* * billing_address.first_name
* * billing_address.last_name
* * billing_address.company
* * billing_address.address_1
* * billing_address.address_2
* * billing_address.city
* * billing_address.state
* * billing_address.postcode
* * billing_address.country
* * billing_address.email
* * billing_address.phone
*
* Shipping address fields:
*
* * shipping_address.first_name
* * shipping_address.last_name
* * shipping_address.company
* * shipping_address.address_1
* * shipping_address.address_2
* * shipping_address.city
* * shipping_address.state
* * shipping_address.postcode
* * shipping_address.country
*
* Fields for filtering query result also available:
*
* * role Filter customers associated with certain role.
* * q Filter customers with search query.
* * created_at_min Filter customers whose registered after this date.
* * created_at_max Filter customers whose registered before this date.
* * limit The maximum returned number of results.
* * offset Offset the returned results.
* * order Accepted values: ASC and DESC. Default: DESC.
* * orderby Sort retrieved customers by parameter. One or more options can be passed.
*
* ## EXAMPLES
*
* wp wc customer get 123 --field=email
*
* wp wc customer get customer-login --format=json
*
* @since 2.5.0
*/
public function get( $args, $assoc_args ) {
try {
$user = $this->get_user( $args[0] );
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = array_keys( $user );
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $user );
} catch ( WC_CLI_Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
}
/**
* List customers.
*
* ## OPTIONS
*
* [--<field>=<value>]
* : Filter customer based on customer property.
*
* [--field=<field>]
* : Prints the value of a single field for each customer.
*
* [--fields=<fields>]
* : Limit the output to specific customer fields.
*
* [--format=<format>]
* : Acceptec values: table, csv, json, count, ids. Default: table.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each customer:
*
* * id
* * email
* * first_name
* * last_name
* * created_at
*
* These fields are optionally available:
*
* * username
* * last_order_id
* * last_order_date
* * orders_count
* * total_spent
* * avatar_url
*
* Billing address fields:
*
* * billing_address.first_name
* * billing_address.last_name
* * billing_address.company
* * billing_address.address_1
* * billing_address.address_2
* * billing_address.city
* * billing_address.state
* * billing_address.postcode
* * billing_address.country
* * billing_address.email
* * billing_address.phone
*
* Shipping address fields:
*
* * shipping_address.first_name
* * shipping_address.last_name
* * shipping_address.company
* * shipping_address.address_1
* * shipping_address.address_2
* * shipping_address.city
* * shipping_address.state
* * shipping_address.postcode
* * shipping_address.country
*
* Fields for filtering query result also available:
*
* * role Filter customers associated with certain role.
* * q Filter customers with search query.
* * created_at_min Filter customers whose registered after this date.
* * created_at_max Filter customers whose registered before this date.
* * limit The maximum returned number of results.
* * offset Offset the returned results.
* * order Accepted values: ASC and DESC. Default: DESC.
* * orderby Sort retrieved customers by parameter. One or more options can be passed.
*
* ## EXAMPLES
*
* wp wc customer list
*
* wp wc customer list --field=id
*
* wp wc customer list --fields=id,email,first_name --format=json
*
* @subcommand list
* @since 2.5.0
*/
public function list_( $__, $assoc_args ) {
$query_args = $this->merge_wp_user_query_args( $this->get_list_query_args(), $assoc_args );
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$query_args['fields'] = 'ids';
$query = new WP_User_Query( $query_args );
echo implode( ' ', $query->results );
} else {
$query = new WP_User_Query( $query_args );
$items = $this->format_users_to_items( $query->results );
$formatter->display_items( $items );
}
}
/**
* View customer orders.
*
* ## OPTIONS
*
* <customer>
* : The customer ID, email or username.
*
* [--field=<field>]
* : Instead of returning the whole customer fields, returns the value of a single fields.
*
* [--fields=<fields>]
* : Get a specific subset of the customer's fields.
*
* [--format=<format>]
* : Accepted values: table, json, csv. Default: table.
*
* ## AVAILABLE FIELDS
*
* For more fields, see: wp wc order list --help
*
* ## EXAMPLES
*
* wp wc customer orders 123
*
* @since 2.5.0
*/
public function orders( $args, $assoc_args ) {
try {
WP_CLI::run_command( array( 'wc', 'order', 'list' ), array( 'customer_id' => $args[0] ) );
} catch ( WC_CLI_Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
}
/**
* Update one or more customers.
*
* ## OPTIONS
*
* <customer>
* : Customer ID, email, or username.
*
* [--<field>=<value>]
* : One or more fields to update.
*
* ## AVAILABLE FIELDS
*
* These fields are available for update command:
*
* * email
* * password
* * first_name
* * last_name
*
* Billing address fields:
*
* * billing_address.first_name
* * billing_address.last_name
* * billing_address.company
* * billing_address.address_1
* * billing_address.address_2
* * billing_address.city
* * billing_address.state
* * billing_address.postcode
* * billing_address.country
* * billing_address.email
* * billing_address.phone
*
* Shipping address fields:
*
* * shipping_address.first_name
* * shipping_address.last_name
* * shipping_address.company
* * shipping_address.address_1
* * shipping_address.address_2
* * shipping_address.city
* * shipping_address.state
* * shipping_address.postcode
* * shipping_address.country
*
* ## EXAMPLES
*
* wp wc customer update customer-login --first_name=akeda --last_name=bagus
*
* wp wc customer update customer@example.com --password=new-password
*
* @since 2.5.0
*/
public function update( $args, $assoc_args ) {
try {
$user = $this->get_user( $args[0] );
$data = $this->unflatten_array( $assoc_args );
$data = apply_filters( 'woocommerce_cli_update_customer_data', $data );
// Customer email.
if ( isset( $data['email'] ) ) {
wp_update_user( array( 'ID' => $user['id'], 'user_email' => sanitize_email( $data['email'] ) ) );
}
// Customer password.
if ( isset( $data['password'] ) ) {
wp_update_user( array( 'ID' => $user['id'], 'user_pass' => wc_clean( $data['password'] ) ) );
}
// Update customer data.
$this->update_customer_data( $user['id'], $data );
do_action( 'woocommerce_cli_update_customer', $user['id'], $data );
WP_CLI::success( "Updated customer {$user['id']}." );
} catch ( WC_CLI_Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
}
/**
* Get query args for list subcommand.
*
* @since 2.5.0
* @return array
*/
protected function get_list_query_args() {
return array(
'role' => 'customer',
'orderby' => 'registered',
);
}
/**
* Get default format fields that will be used in `list` and `get` subcommands.
*
* @since 2.5.0
* @return string
*/
protected function get_default_format_fields() {
return 'id,email,first_name,last_name,created_at';
}
/**
* Format users from WP_User_Query result to items in which each item contain
* common properties of item.
*
* @since 2.5.0
* @param array $users Array of user
* @return array Items
*/
protected function format_users_to_items( $users ) {
$items = array();
foreach ( $users as $user ) {
try {
$items[] = $this->get_user( $user->ID );
} catch ( WC_CLI_Exception $e ) {
WP_CLI::warning( $e->getMessage() );
}
}
return $items;
}
/**
* Get user from given user ID, email, or login
*
* @throws WC_CLI_Exception
*
* @since 2.5.0
* @param mixed $id_email_or_login
* @return array|WP_Error
*/
protected function get_user( $id_email_or_login ) {
global $wpdb;
if ( is_numeric( $id_email_or_login ) ) {
$user = get_user_by( 'id', $id_email_or_login );
} else if ( is_email( $id_email_or_login ) ) {
$user = get_user_by( 'email', $id_email_or_login );
} else {
$user = get_user_by( 'login', $id_email_or_login );
}
if ( ! $user ) {
throw new WC_CLI_Exception( 'woocommerce_cli_invalid_customer', sprintf( __( 'Invalid customer "%s"', 'woocommerce' ), $id_email_or_login ) );
}
// Get info about user's last order
$last_order = $wpdb->get_row( "SELECT id, post_date_gmt
FROM $wpdb->posts AS posts
LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = {$user->ID}
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' )
ORDER BY posts.ID DESC
" );
$customer = array(
'id' => $user->ID,
'created_at' => $this->format_datetime( $user->user_registered ),
'email' => $user->user_email,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'username' => $user->user_login,
'role' => $user->roles[0],
'last_order_id' => is_object( $last_order ) ? $last_order->id : null,
'last_order_date' => is_object( $last_order ) ? $this->format_datetime( $last_order->post_date_gmt ) : null,
'orders_count' => wc_get_customer_order_count( $user->ID ),
'total_spent' => wc_format_decimal( wc_get_customer_total_spent( $user->ID ), 2 ),
'avatar_url' => $this->get_avatar_url( $user->customer_email ),
'billing_address' => array(
'first_name' => $user->billing_first_name,
'last_name' => $user->billing_last_name,
'company' => $user->billing_company,
'address_1' => $user->billing_address_1,
'address_2' => $user->billing_address_2,
'city' => $user->billing_city,
'state' => $user->billing_state,
'postcode' => $user->billing_postcode,
'country' => $user->billing_country,
'email' => $user->billing_email,
'phone' => $user->billing_phone,
),
'shipping_address' => array(
'first_name' => $user->shipping_first_name,
'last_name' => $user->shipping_last_name,
'company' => $user->shipping_company,
'address_1' => $user->shipping_address_1,
'address_2' => $user->shipping_address_2,
'city' => $user->shipping_city,
'state' => $user->shipping_state,
'postcode' => $user->shipping_postcode,
'country' => $user->shipping_country,
),
);
// Allow dot notation for nested array so that user can specifies field
// like 'billing_address.first_name'.
return $this->flatten_array( $customer );
}
/**
* Wrapper for @see get_avatar() which doesn't simply return
* the URL so we need to pluck it from the HTML img tag
*
* Kudos to https://github.com/WP-API/WP-API for offering a better solution
*
* @since 2.5.0
* @param string $email the customer's email
* @return string the URL to the customer's avatar
*/
protected function get_avatar_url( $email ) {
$avatar_html = get_avatar( $email );
// Get the URL of the avatar from the provided HTML
preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches );
if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
return esc_url_raw( $matches[1] );
}
return null;
}
/**
* Add/Update customer data.
*
* @since 2.5.0
* @param int $id The customer ID
* @param array $data
*/
protected function update_customer_data( $id, $data ) {
// Customer first name.
if ( isset( $data['first_name'] ) ) {
update_user_meta( $id, 'first_name', wc_clean( $data['first_name'] ) );
}
// Customer last name.
if ( isset( $data['last_name'] ) ) {
update_user_meta( $id, 'last_name', wc_clean( $data['last_name'] ) );
}
// Customer billing address.
if ( isset( $data['billing_address'] ) ) {
foreach ( $this->get_customer_billing_address_fields() as $address ) {
if ( isset( $data['billing_address'][ $address ] ) ) {
update_user_meta( $id, 'billing_' . $address, wc_clean( $data['billing_address'][ $address ] ) );
}
}
}
// Customer shipping address.
if ( isset( $data['shipping_address'] ) ) {
foreach ( $this->get_customer_shipping_address_fields() as $address ) {
if ( isset( $data['shipping_address'][ $address ] ) ) {
update_user_meta( $id, 'shipping_' . $address, wc_clean( $data['shipping_address'][ $address ] ) );
}
}
}
do_action( 'woocommerce_cli_update_customer_data', $id, $data );
}
/**
* Get customer billing address fields.
*
* @since 2.5.0
* @return array
*/
protected function get_customer_billing_address_fields() {
return apply_filters( 'woocommerce_cli_customer_billing_address_fields', array(
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'city',
'state',
'postcode',
'country',
'email',
'phone',
) );
}
/**
* Get customer shipping address fields.
*
* @since 2.5.0
* @return array
*/
protected function get_customer_shipping_address_fields() {
return apply_filters( 'woocommerce_cli_customer_shipping_address_fields', array(
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'city',
'state',
'postcode',
'country',
) );
}
/**
* Get customer download fields.
*
* @since 2.5.0
* @return array
*/
protected function get_customer_download_fields() {
return apply_filters( 'woocommerce_cli_customer_download_fields', array(
'download_id',
'download_name',
'access_expires',
) );
}
}