Completed
Push — master ( b3c0be...0745f1 )
by Claudio
07:57
created

WC_Customer::delete_and_reassign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
include_once( 'legacy/class-wc-legacy-customer.php' );
3
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * The WooCommerce customer class handles storage of the current customer's data, such as location.
10
 *
11
 * @class    WC_Customer
12
 * @version  2.7.0
13
 * @package  WooCommerce/Classes
14
 * @category Class
15
 * @author   WooThemes
16
 */
17
class WC_Customer extends WC_Legacy_Customer {
18
19
	/**
20
	 * Stores customer data.
21
	 * @var array
22
	 */
23
	protected $_data = array(
24
		'id'                  => 0,
25
		'email'               => '',
26
		'first_name'          => '',
27
		'last_name'           => '',
28
		'role'                => 'customer',
29
		'last_order_id'       => null, // read only
30
		'last_order_date'     => null, // read only
31
		'orders_count'        => 0, // read only
32
		'total_spent'         => 0, // read only
33
		'username'            => '', // read only on existing users
34
		'password'            => '', // write only
35
		'date_created'        => '', // read only
36
		'date_modified'       => '', // read only
37
		'billing_first_name'  => '',
38
		'billing_last_name'   => '',
39
		'billing_company'     => '',
40
		'billing_phone'       => '',
41
		'billing_email'       => '',
42
		'billing_postcode'    => '',
43
		'billing_city'        => '',
44
		'billing_address_1'   => '',
45
		'billing_address_2'   => '',
46
		'billing_state'       => '',
47
		'billing_country'     => '',
48
		'shipping_first_name' => '',
49
		'shipping_last_name'  => '',
50
		'shipping_company'    => '',
51
		'shipping_postcode'   => '',
52
		'shipping_city'       => '',
53
		'shipping_address_1'  => '',
54
		'shipping_address_2'  => '',
55
		'shipping_state'      => '',
56
		'shipping_country'    => '',
57
		'is_paying_customer'  => false,
58
		'is_vat_exempt'       => false, // session only.
59
		'calculated_shipping' => false, // session only
60
	);
61
62
	/**
63
	 * Keys which are also stored in a session (so we can make sure they get updated...)
64
	 * @var array
65
	 */
66
	protected $_session_keys = array(
67
		'billing_first_name', 'billing_last_name', 'billing_company', 'billing_postcode',
68
		'billing_city', 'billing_address_1', 'billing_address', 'billing_address_2',
69
		'billing_state', 'billing_country', 'shipping_first_name', 'shipping_last_name',
70
		'shipping_company', 'shipping_postcode', 'shipping_city', 'shipping_address_1',
71
		'shipping_address','shipping_address_2', 'shipping_state', 'shipping_country',
72
		'is_vat_exempt', 'calculated_shipping', 'billing_email', 'billing_phone',
73
	);
74
75
	/**
76
	 * Data stored in meta keys, but not considered "meta"
77
	 * @since 2.7.0
78
	 * @var array
79
	 */
80
	protected $_internal_meta_keys = array(
81
		'billing_first_name', 'billing_last_name', 'billing_company','billing_postcode',
82
		'billing_city', 'billing_address_1', 'billing_address_2', 'billing_state',
83
		'billing_country', 'shipping_first_name', 'shipping_last_name', 'shipping_company',
84
		'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address_2',
85
		'shipping_state', 'shipping_country', 'paying_customer', 'last_update',
86
		'first_name', 'last_name', 'billing_email', 'billing_phone',
87
	);
88
89
	/**
90
	 * Internal meta type used to store user data.
91
	 * @var string
92
	 */
93
	protected $_meta_type = 'user';
94
95
	/**
96
	 * Was data changed in the database for this class?
97
	 * @var boolean
98
	 */
99
	protected $_changed = false;
100
101
	/**
102
	 * If this is the customer session, this is true. When true, guest accounts will not be saved to the DB.
103
	 * @var boolean
104
	 */
105
	protected $_is_session = false;
106
107
	/**
108
	 * Load customer data based on how WC_Customer is called.
109
	 *
110
	 * If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
111
	 * data will be pulled from the session for the current user/customer.
112
	 *
113
	 * @param int $customer_id Customer ID
114
	 * @param bool $is_session True if this is the customer session
115
	 */
116
	public function __construct( $customer_id = 0, $is_session = false ) {
117
		if ( $customer_id > 0 ) {
118
			$this->read( $customer_id );
119
		}
120
		if ( $is_session ) {
121
			$this->_is_session = true;
122
			$this->load_session();
123
			add_action( 'shutdown', array( $this, 'save_session_if_changed' ), 10 );
124
		}
125
	}
126
127
	/**
128
	 * Saves customer information to the current session if any data changed.
129
	 * @since 2.7.0
130
	 */
131
	public function save_session_if_changed() {
132
		if ( $this->_changed ) {
133
			$this->save_to_session();
134
		}
135
	}
136
137
	/**
138
	 * Loads a WC session into the customer class.
139
	 */
140
	public function load_session() {
141
		$data = (array) WC()->session->get( 'customer' );
142
		if ( ! empty( $data ) ) {
143
			foreach ( $this->_session_keys as $session_key ) {
144
				$function_key = $session_key;
145
				if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
146
					$session_key = str_replace( 'billing_', '', $session_key );
147
				}
148
				if ( ! empty( $data[ $session_key ] ) && is_callable( array( $this, "set_{$function_key}" ) ) ) {
149
					$this->{"set_{$function_key}"}( $data[ $session_key ] );
150
				}
151
			}
152
		}
153
		$this->load_defaults();
154
	}
155
156
	/**
157
	 * Load default values if props are unset.
158
	 */
159
	protected function load_defaults() {
160
		$default = wc_get_customer_default_location();
161
162
		// Set some defaults if some of our values are still not set.
163
		if ( ! $this->get_billing_country() ) {
164
			$this->set_billing_country( $default['country'] );
165
		}
166
167
		if ( ! $this->get_shipping_country() ) {
168
			$this->set_shipping_country( $this->get_billing_country() );
169
		}
170
171
		if ( ! $this->get_billing_state() ) {
172
			$this->set_billing_state( $default['state'] );
173
		}
174
175
		if ( ! $this->get_shipping_state() ) {
176
			$this->set_shipping_state( $this->get_billing_state() );
177
		}
178
	}
179
180
	/**
181
	 * Gets the customers last order.
182
	 * @return WC_Order|false
183
	 */
184
	public function get_last_order() {
185
		global $wpdb;
186
187
		$last_order = $wpdb->get_var( "SELECT posts.ID
188
			FROM $wpdb->posts AS posts
189
			LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
190
			WHERE meta.meta_key = '_customer_user'
191
			AND   meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
192
			AND   posts.post_type = 'shop_order'
193
			AND   posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
194
			ORDER BY posts.ID DESC
195
		" );
196
197
		if ( $last_order ) {
198
			return wc_get_order( absint( $last_order ) );
199
		} else {
200
			return false;
201
		}
202
	}
203
204
	/**
205
	 * Return the number of orders this customer has.
206
	 * @since 2.7.0
207
	 * @return integer
208
	 */
209
	public function get_order_count() {
210
		global $wpdb;
211
212
		$count = $wpdb->get_var( "SELECT COUNT(*)
213
			FROM $wpdb->posts as posts
214
			LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
215
			WHERE   meta.meta_key = '_customer_user'
216
			AND     posts.post_type = 'shop_order'
217
			AND     posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
218
			AND     meta_value = '" . esc_sql( $this->get_id() ) . "'
219
		" );
220
221
		return absint( $count );
222
	}
223
224
	/**
225
	 * Return how much money this customer has spent.
226
	 * @since 2.7.0
227
	 * @return float
228
	 */
229
	public function get_total_spent() {
230
		global $wpdb;
231
232
		$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
233
			FROM $wpdb->posts as posts
234
			LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
235
			LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
236
			WHERE   meta.meta_key       = '_customer_user'
237
			AND     meta.meta_value     = '" . esc_sql( $this->get_id() ) . "'
238
			AND     posts.post_type     = 'shop_order'
239
			AND     posts.post_status   IN ( 'wc-completed', 'wc-processing' )
240
			AND     meta2.meta_key      = '_order_total'
241
		" );
242
243
		if ( ! $spent ) {
244
			$spent = 0;
245
		}
246
247
		return wc_format_decimal( $spent );
248
	}
249
250
	/**
251
	 * Is customer outside base country (for tax purposes)?
252
	 * @return bool
253
	 */
254
	public function is_customer_outside_base() {
255
		list( $country, $state ) = $this->get_taxable_address();
256
		if ( $country ) {
257
			$default = wc_get_base_location();
258
			if ( $default['country'] !== $country ) {
259
				return true;
260
			}
261
			if ( $default['state'] && $default['state'] !== $state ) {
262
				return true;
263
			}
264
		}
265
		return false;
266
	}
267
268
	/*
269
	 |--------------------------------------------------------------------------
270
	 | Getters
271
	 |--------------------------------------------------------------------------
272
	 | Methods for getting data from the customer object.
273
	 */
274
275
	/**
276
	 * Return a customer's user ID. Logged out users have ID 0.
277
	 * @since 2.7.0
278
	 * @return int
279
	 */
280
	public function get_id() {
281
		return $this->_data['id'];
282
	}
283
284
	/**
285
	 * Return the customer's username.
286
	 * @since 2.7.0
287
	 * @return string
288
	 */
289
	public function get_username() {
290
		return $this->_data['username'];
291
	}
292
293
	/**
294
	 * Return the customer's email.
295
	 * @since 2.7.0
296
	 * @return string
297
	 */
298
	public function get_email() {
299
		return $this->_data['email'];
300
	}
301
302
	/**
303
	 * Return customer's first name.
304
	 * @since 2.7.0
305
	 * @return string
306
	 */
307
	public function get_first_name() {
308
		return $this->_data['first_name'];
309
	}
310
311
	/**
312
	 * Return customer's last name.
313
	 * @since 2.7.0
314
	 * @return string
315
	 */
316
	public function get_last_name() {
317
		return $this->_data['last_name'];
318
	}
319
320
	/**
321
	 * Return customer's user role.
322
	 * @since 2.7.0
323
	 * @return string
324
	 */
325
	public function get_role() {
326
		return $this->_data['role'];
327
	}
328
329
	/**
330
	 * Return this customer's avatar.
331
	 * @since 2.7.0
332
	 * @return string
333
	 */
334 View Code Duplication
	public function get_avatar_url() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
335
		$avatar_html = get_avatar( $this->get_email() );
336
337
		// Get the URL of the avatar from the provided HTML
338
		preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches );
339
340
		if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
341
			return esc_url( $matches[1] );
342
		}
343
344
		return '';
345
	}
346
347
	/**
348
	 * Return the date this customer was created.
349
	 * @since 2.7.0
350
	 * @return integer
351
	 */
352
	public function get_date_created() {
353
		return absint( $this->_data['date_created'] );
354
	}
355
356
	/**
357
	 * Return the date this customer was last updated.
358
	 * @since 2.7.0
359
	 * @return integer
360
	 */
361
	public function get_date_modified() {
362
		return absint( $this->_data['date_modified'] );
363
	}
364
365
	/**
366
	 * Gets customer billing first name.
367
	 * @return string
368
	 */
369
	public function get_billing_first_name() {
370
		return $this->_data['billing_first_name'];
371
	}
372
373
	/**
374
	 * Gets customer billing last name.
375
	 * @return string
376
	 */
377
	public function get_billing_last_name() {
378
		return $this->_data['billing_last_name'];
379
	}
380
381
	/**
382
	 * Gets customer billing company.
383
	 * @return string
384
	 */
385
	public function get_billing_company() {
386
		return $this->_data['billing_company'];
387
	}
388
389
	/**
390
	 * Gets billing phone.
391
	 * @return string
392
	 */
393
	public function get_billing_phone() {
394
		return $this->_data['billing_phone'];
395
	}
396
397
	/**
398
	 * Gets billing email.
399
	 * @return string
400
	 */
401
	public function get_billing_email() {
402
		return $this->_data['billing_email'];
403
	}
404
405
	/**
406
	 * Gets customer postcode.
407
	 * @return string
408
	 */
409
	public function get_billing_postcode() {
410
		return wc_format_postcode( $this->_data['billing_postcode'], $this->get_billing_country() );
411
	}
412
413
	/**
414
	 * Get customer city.
415
	 * @return string
416
	 */
417
	public function get_billing_city() {
418
		return $this->_data['billing_city'];
419
	}
420
421
	/**
422
	 * Get customer address.
423
	 * @return string
424
	 */
425
	public function get_billing_address() {
426
		return $this->_data['billing_address_1'];
427
	}
428
429
	/**
430
	 * Get customer address.
431
	 * @return string
432
	 */
433
	public function get_billing_address_1() {
434
		return $this->get_billing_address();
435
	}
436
437
	/**
438
	 * Get customer's second address.
439
	 * @return string
440
	 */
441
	public function get_billing_address_2() {
442
		return $this->_data['billing_address_2'];
443
	}
444
445
	/**
446
	 * Get customer state.
447
	 * @return string
448
	 */
449
	public function get_billing_state() {
450
		return $this->_data['billing_state'];
451
	}
452
453
	/**
454
	 * Get customer country.
455
	 * @return string
456
	 */
457
	public function get_billing_country() {
458
		return $this->_data['billing_country'];
459
	}
460
461
	/**
462
	 * Gets customer shipping first name.
463
	 * @return string
464
	 */
465
	public function get_shipping_first_name() {
466
		return $this->_data['shipping_first_name'];
467
	}
468
469
	/**
470
	 * Gets customer shipping last name.
471
	 * @return string
472
	 */
473
	public function get_shipping_last_name() {
474
		return $this->_data['shipping_last_name'];
475
	}
476
477
	/**
478
	 * Gets customer shipping company.
479
	 * @return string
480
	 */
481
	public function get_shipping_company() {
482
		return $this->_data['shipping_company'];
483
	}
484
485
	/**
486
	 * Get customer's shipping state.
487
	 * @return string
488
	 */
489
	public function get_shipping_state() {
490
		return $this->_data['shipping_state'];
491
	}
492
493
	/**
494
	 * Get customer's shipping country.
495
	 * @return string
496
	 */
497
	public function get_shipping_country() {
498
		return $this->_data['shipping_country'];
499
	}
500
501
	/**
502
	 * Get customer's shipping postcode.
503
	 * @return string
504
	 */
505
	public function get_shipping_postcode() {
506
		return wc_format_postcode( $this->_data['shipping_postcode'], $this->get_shipping_country() );
507
	}
508
509
	/**
510
	 * Get customer's shipping city.
511
	 * @return string
512
	 */
513
	public function get_shipping_city() {
514
		return $this->_data['shipping_city'];
515
	}
516
517
	/**
518
	 * Get customer's shipping address.
519
	 * @return string
520
	 */
521
	public function get_shipping_address() {
522
		return $this->_data['shipping_address_1'];
523
	}
524
525
	/**
526
	 * Get customer address.
527
	 * @return string
528
	 */
529
	public function get_shipping_address_1() {
530
		return $this->get_shipping_address();
531
	}
532
533
	/**
534
	 * Get customer's second shipping address.
535
	 * @return string
536
	 */
537
	public function get_shipping_address_2() {
538
		return $this->_data['shipping_address_2'];
539
	}
540
541
	/**
542
	 * Get if customer is VAT exempt?
543
	 * @since 2.7.0
544
	 * @return bool
545
	 */
546
	public function get_is_vat_exempt() {
547
		return ( ! empty( $this->_data['is_vat_exempt'] ) ) ? true : false;
548
	}
549
550
	/**
551
	 * Has customer calculated shipping?
552
	 * @return bool
553
	 */
554
	public function get_calculated_shipping() {
555
		return ! empty( $this->_data['calculated_shipping'] );
556
	}
557
558
	/**
559
	 * Get taxable address.
560
	 * @return array
561
	 */
562
	public function get_taxable_address() {
563
		$tax_based_on = get_option( 'woocommerce_tax_based_on' );
564
565
		// Check shipping method at this point to see if we need special handling
566
		if ( true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && sizeof( array_intersect( wc_get_chosen_shipping_method_ids(), apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) ) ) ) > 0 ) {
567
			$tax_based_on = 'base';
568
		}
569
570
		if ( 'base' === $tax_based_on ) {
571
			$country  = WC()->countries->get_base_country();
572
			$state    = WC()->countries->get_base_state();
573
			$postcode = WC()->countries->get_base_postcode();
574
			$city     = WC()->countries->get_base_city();
575
		} elseif ( 'billing' === $tax_based_on ) {
576
			$country  = $this->get_billing_country();
577
			$state    = $this->get_billing_state();
578
			$postcode = $this->get_billing_postcode();
579
			$city     = $this->get_billing_city();
580
		} else {
581
			$country  = $this->get_shipping_country();
582
			$state    = $this->get_shipping_state();
583
			$postcode = $this->get_shipping_postcode();
584
			$city     = $this->get_shipping_city();
585
		}
586
587
		return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
588
	}
589
590
	/**
591
	 * Gets a customer's downloadable products.
592
	 * @return array Array of downloadable products
593
	 */
594
	public function get_downloadable_products() {
595
		$downloads = array();
596
		if ( $this->get_id() ) {
597
			$downloads = wc_get_customer_available_downloads( $this->get_id() );
598
		}
599
		return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
600
	}
601
602
	/**
603
	 * Is the user a paying customer?
604
	 * @since 2.7.0
605
	 * @return bool
606
	 */
607
	function get_is_paying_customer() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
608
		return (bool) $this->_data['is_paying_customer'];
609
	}
610
611
	/*
612
	|--------------------------------------------------------------------------
613
	| Setters
614
	|--------------------------------------------------------------------------
615
	| Functions for setting customer data. These should not update anything in the
616
	| database itself and should only change what is stored in the class
617
	| object.
618
	*/
619
620
	/**
621
	 * Set customer ID.
622
	 * @since 2.7.0
623
	 * @param int $value
624
	 */
625
	protected function set_id( $value ) {
626
		$this->_data['id'] = absint( $value );
627
	}
628
629
	/**
630
	 * Set customer's username.
631
	 * @since 2.7.0
632
	 * @param string $username
633
	 */
634
	public function set_username( $username ) {
635
		$this->_data['username'] = $username;
636
	}
637
638
	/**
639
	 * Set customer's email.
640
	 * @since 2.7.0
641
	 * @param string $email
642
	 */
643
	public function set_email( $email ) {
644
		$this->_data['email'] = sanitize_email( $email );
645
	}
646
647
	/**
648
	 * Set customer's first name.
649
	 * @since 2.7.0
650
	 * @param string $first_name
651
	 */
652
	public function set_first_name( $first_name ) {
653
		$this->_data['first_name'] = $first_name;
654
	}
655
656
	/**
657
	 * Set customer's last name.
658
	 * @since 2.7.0
659
	 * @param string $last_name
660
	 */
661
	public function set_last_name( $last_name ) {
662
		$this->_data['last_name'] = $last_name;
663
	}
664
665
	/**
666
	 * Set customer's user role(s).
667
	 * @since 2.7.0
668
	 * @param mixed $role
669
	 */
670
	public function set_role( $role ) {
671
		$this->_data['role'] = $role;
672
	}
673
674
	/**
675
	 * Set customer's password.
676
	 * @since 2.7.0
677
	 * @param string $password
678
	 */
679
	public function set_password( $password ) {
680
		$this->_data['password'] = wc_clean( $password );
681
	}
682
683
	/**
684
	 * Set the date this customer was last updated.
685
	 * @since 2.7.0
686
	 * @param integer $timestamp
687
	 */
688
	public function set_date_modified( $timestamp ) {
689
		$this->_data['date_modified'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
690
	}
691
692
	/**
693
	 * Set the date this customer was last updated.
694
	 * @since 2.7.0
695
	 * @param integer $timestamp
696
	 */
697
	public function set_date_created( $timestamp ) {
698
		$this->_data['date_created'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
699
	}
700
701
	/**
702
	 * Set customer address to match shop base address.
703
	 * @since 2.7.0
704
	 */
705 View Code Duplication
	public function set_billing_address_to_base() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
706
		$base = wc_get_customer_default_location();
707
		$this->_data['billing_country']  = $base['country'];
708
		$this->_data['billing_state']    = $base['state'];
709
		$this->_data['billing_postcode'] = '';
710
		$this->_data['billing_city']     = '';
711
	}
712
713
	/**
714
	 * Set customer shipping address to base address.
715
	 * @since 2.7.0
716
	 */
717 View Code Duplication
	public function set_shipping_address_to_base() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
718
		$base = wc_get_customer_default_location();
719
		$this->_data['shipping_country']  = $base['country'];
720
		$this->_data['shipping_state']    = $base['state'];
721
		$this->_data['shipping_postcode'] = '';
722
		$this->_data['shipping_city']     = '';
723
	}
724
725
	/**
726
	 * Sets all shipping info at once.
727
	 * @param string $country
728
	 * @param string $state
729
	 * @param string $postcode
730
	 * @param string $city
731
	 */
732
	public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
733
		$this->_data['shipping_country']  = $country;
734
		$this->_data['shipping_state']    = $state;
735
		$this->_data['shipping_postcode'] = $postcode;
736
		$this->_data['shipping_city']     = $city;
737
	}
738
739
	/**
740
	 * Sets all address info at once.
741
	 * @param string $country
742
	 * @param string $state
743
	 * @param string $postcode
744
	 * @param string $city
745
	 */
746
	public function set_billing_location( $country, $state, $postcode = '', $city = '' ) {
747
		$this->_data['billing_country']  = $country;
748
		$this->_data['billing_state']    = $state;
749
		$this->_data['billing_postcode'] = $postcode;
750
		$this->_data['billing_city']     = $city;
751
	}
752
753
	/**
754
	 * Set billing first name.
755
	 * @return string
756
	 */
757
	public function set_billing_first_name( $value ) {
758
		$this->_data['billing_first_name'] = $value;
759
	}
760
761
	/**
762
	 * Set billing last name.
763
	 * @return string
764
	 */
765
	public function set_billing_last_name( $value ) {
766
		$this->_data['billing_last_name'] = $value;
767
	}
768
769
	/**
770
	 * Set billing company.
771
	 * @return string
772
	 */
773
	public function set_billing_company( $value ) {
774
		$this->_data['billing_company'] = $value;
775
	}
776
777
	/**
778
	 * Set billing phone.
779
	 * @return string
780
	 */
781
	public function set_billing_phone( $value ) {
782
		$this->_data['billing_phone'] = $value;
783
	}
784
785
	/**
786
	 * Set billing email.
787
	 * @return string
788
	 */
789
	public function set_billing_email( $email ) {
790
		$this->_data['billing_email'] = sanitize_email( $email );
791
	}
792
793
	/**
794
	 * Set customer country.
795
	 * @param mixed $country
796
	 */
797
	public function set_billing_country( $country ) {
798
		$this->_data['billing_country'] = $country;
799
	}
800
801
	/**
802
	 * Set customer state.
803
	 * @param mixed $state
804
	 */
805
	public function set_billing_state( $state ) {
806
		$this->_data['billing_state'] = $state;
807
	}
808
809
	/**
810
	 * Sets customer postcode.
811
	 * @param mixed $postcode
812
	 */
813
	public function set_billing_postcode( $postcode ) {
814
		$this->_data['billing_postcode'] = $postcode;
815
	}
816
817
	/**
818
	 * Sets customer city.
819
	 * @param mixed $city
820
	 */
821
	public function set_billing_city( $city ) {
822
		$this->_data['billing_city'] = $city;
823
	}
824
825
	/**
826
	 * Set customer address.
827
	 * @param mixed $address
828
	 */
829
	public function set_billing_address( $address ) {
830
		$this->_data['billing_address_1'] = $address;
831
	}
832
833
	/**
834
	 * Set customer address.
835
	 * @param mixed $address
836
	 */
837
	public function set_billing_address_1( $address ) {
838
		$this->set_billing_address( $address );
839
	}
840
841
	/**
842
	 * Set customer's second address.
843
	 * @param mixed $address
844
	 */
845
	public function set_billing_address_2( $address ) {
846
		$this->_data['billing_address_2'] = $address;
847
	}
848
849
	/**
850
	 * Sets customer shipping first name.
851
	 * @param string $first_name
852
	 */
853
	public function set_shipping_first_name( $first_name ) {
854
		$this->_data['shipping_first_name'] = $first_name;
855
	}
856
857
	/**
858
	 * Sets customer shipping last name.
859
	 * @param string $last_name
860
	 */
861
	public function set_shipping_last_name( $last_name ) {
862
		$this->_data['shipping_last_name'] = $last_name;
863
	}
864
865
	/**
866
	 * Sets customer shipping company.
867
	 * @param string $company.
0 ignored issues
show
Documentation introduced by
There is no parameter named $company.. Did you maybe mean $company?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
868
	 */
869
	public function set_shipping_company( $company ) {
870
		$this->_data['shipping_company'] = $company;
871
	}
872
873
	/**
874
	 * Set shipping country.
875
	 * @param string $country
876
	 */
877
	public function set_shipping_country( $country ) {
878
		$this->_data['shipping_country'] = $country;
879
	}
880
881
	/**
882
	 * Set shipping state.
883
	 * @param string $state
884
	 */
885
	public function set_shipping_state( $state ) {
886
		$this->_data['shipping_state'] = $state;
887
	}
888
889
	/**
890
	 * Set shipping postcode.
891
	 * @param string $postcode
892
	 */
893
	public function set_shipping_postcode( $postcode ) {
894
		$this->_data['shipping_postcode'] = $postcode;
895
	}
896
897
	/**
898
	 * Sets shipping city.
899
	 * @param string $city
900
	 */
901
	public function set_shipping_city( $city ) {
902
		$this->_data['shipping_city'] = $city;
903
	}
904
905
	/**
906
	 * Set shipping address.
907
	 * @param string $address
908
	 */
909
	public function set_shipping_address( $address ) {
910
		$this->_data['shipping_address_1'] = $address;
911
	}
912
913
	/**
914
	 * Set customer shipping address.
915
	 * @param mixed $address
916
	 */
917
	public function set_shipping_address_1( $address ) {
918
		$this->set_shipping_address( $address );
919
	}
920
921
	/**
922
	 * Set second shipping address.
923
	 * @param string $address
924
	 */
925
	public function set_shipping_address_2( $address ) {
926
		$this->_data['shipping_address_2'] = $address;
927
	}
928
929
	/**
930
	 * Set if customer has tax exemption.
931
	 * @param bool $is_vat_exempt
932
	 */
933
	public function set_is_vat_exempt( $is_vat_exempt ) {
934
		$this->_data['is_vat_exempt'] = $is_vat_exempt;
935
	}
936
937
	/**
938
	 * Calculated shipping?
939
	 * @param boolean $calculated
940
	 */
941
	public function set_calculated_shipping( $calculated = true ) {
942
		$this->_data['calculated_shipping'] = $calculated;
943
	}
944
945
	/**
946
	 * Set if the user a paying customer.
947
	 * @since 2.7.0
948
	 * @param boolean $is_paying_customer
949
	 */
950
	function set_is_paying_customer( $is_paying_customer ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
951
		$this->_data['is_paying_customer'] = (bool) $is_paying_customer;
952
	}
953
954
	/*
955
	 |--------------------------------------------------------------------------
956
	 | CRUD methods
957
	 |--------------------------------------------------------------------------
958
	 | Methods which create, read, update and delete from the database.
959
	 |
960
	 | A save method is included for convenience (chooses update or create based
961
	 | on if the order exists yet).
962
	 */
963
964
	 /**
965
	  * Create a customer.
966
	  * @since 2.7.0.
967
	  */
968
	public function create() {
969
		$customer_id = wc_create_new_customer( $this->get_email(), $this->get_username(), $this->_data['password'] );
970
971
		if ( ! is_wp_error( $customer_id ) ) {
972
			$this->_data['id'] = $customer_id;
973
			update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
974
			update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
975
			update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
976
			update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
977
			update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
978
			update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
979
			update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
980
			update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
981
			update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
982
			update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
983
			update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
984
			update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
985
			update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
986
			update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
987
			update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
988
			update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
989
			update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
990
			update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() );
991
			update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
992
			update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
993
			update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
994
			update_user_meta( $this->get_id(), 'last_update',  $this->get_date_modified() );
995
			update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
996
			update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
997
			wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
998
			$wp_user = new WP_User( $this->get_id() );
999
			$this->set_date_created( strtotime( $wp_user->user_registered ) );
1000
			$this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) );
1001
			$this->read_meta_data();
1002
		}
1003
	}
1004
1005
	/**
1006
	 * Read a customer from the database.
1007
	 * @since 2.7.0
1008
	 * @param integer $id
1009
	 */
1010
	public function read( $id ) {
1011
		global $wpdb;
1012
1013
		// User object is required.
1014
		if ( ! $id || ! ( $user_object = get_user_by( 'id', $id ) ) || empty( $user_object->ID ) ) {
1015
			$this->set_id( 0 );
1016
			return;
1017
		}
1018
1019
		// Only users on this site should be read.
1020
		if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
1021
			$this->set_id( 0 );
1022
			return;
1023
		}
1024
1025
		$this->set_id( $user_object->ID );
1026
1027
		foreach ( array_keys( $this->_data ) as $key ) {
1028
			$meta_value = get_user_meta( $id, $key, true );
1029
			if ( $meta_value && is_callable( array( $this, "set_{$key}" ) ) ) {
1030
				$this->{"set_{$key}"}( $meta_value );
1031
			}
1032
		}
1033
1034
		$this->set_is_paying_customer( get_user_meta( $id, 'paying_customer', true ) );
1035
		$this->set_email( $user_object->user_email );
1036
		$this->set_username( $user_object->user_login );
1037
		$this->set_date_created( strtotime( $user_object->user_registered ) );
1038
		$this->set_date_modified( get_user_meta( $id, 'last_update', true ) );
1039
		$this->set_role( ( ! empty ( $user_object->roles[0] ) ? $user_object->roles[0] : 'customer' ) );
1040
		$this->read_meta_data();
1041
1042
		unset( $this->_data['password'] ); // password is write only, never ever read it
1043
	}
1044
1045
	/**
1046
	 * Update a customer.
1047
	 * @since 2.7.0
1048
	 */
1049
	public function update() {
1050
		$customer_ID = $this->get_id();
1051
1052
		wp_update_user( array( 'ID' => $customer_ID, 'user_email' => $this->get_email() ) );
1053
		// Only update password if a new one was set with set_password
1054
		if ( isset( $this->_data['password'] ) ) {
1055
			wp_update_user( array( 'ID' => $customer_ID, 'user_pass' => $this->_data['password'] ) );
1056
			unset( $this->_data['password'] );
1057
		}
1058
1059
		update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
1060
		update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
1061
		update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
1062
		update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
1063
		update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
1064
		update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
1065
		update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
1066
		update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
1067
		update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
1068
		update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
1069
		update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
1070
		update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
1071
		update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
1072
		update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
1073
		update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
1074
		update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
1075
		update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
1076
		update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
1077
		update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
1078
		update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
1079
		update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() );
1080
		update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
1081
		update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
1082
		update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
1083
		update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
1084
		update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
1085
		wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
1086
		$this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) );
1087
		$this->save_meta_data();
1088
	}
1089
1090
	/**
1091
	 * Delete a customer.
1092
	 * @since 2.7.0
1093
	 */
1094
	public function delete() {
1095
		if ( ! $this->get_id() ) {
1096
			return;
1097
		}
1098
		return wp_delete_user( $this->get_id() );
1099
	}
1100
1101
	/**
1102
	 * Delete a customer and reassign posts..
1103
	 *
1104
	 * @param int $reassign Reassign posts and links to new User ID.
1105
	 * @since 2.7.0
1106
	 */
1107
	public function delete_and_reassign( $reassign = null ) {
1108
		if ( ! $this->get_id() ) {
1109
			return;
1110
		}
1111
		return wp_delete_user( $this->get_id(), $reassign );
1112
	}
1113
1114
	/**
1115
	 * Save data. Create when creating a new user/class, update when editing
1116
	 * an existing user, and save session when working on a logged out guest
1117
	 * session.
1118
	 * @since 2.7.0
1119
	 */
1120
	public function save() {
1121
		if ( $this->_is_session ) {
1122
			$this->save_session_if_changed();
1123
		} elseif ( ! $this->get_id() ) {
1124
			$this->create();
1125
		} else {
1126
			$this->update();
1127
		}
1128
	}
1129
1130
	/**
1131
	 * Saves data to the session only (does not overwrite DB values).
1132
	 * @since 2.7.0
1133
	 */
1134
	public function save_to_session() {
1135
		$data = array();
1136
		foreach ( $this->_session_keys as $session_key ) {
1137
			$function_key = $session_key;
1138
			if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
1139
				$session_key = str_replace( 'billing_', '', $session_key );
1140
			}
1141
			$data[ $session_key ] = $this->{"get_$function_key"}();
1142
		}
1143
		WC()->session->set( 'customer', $data );
1144
	}
1145
1146
}
1147