Completed
Push — master ( 46b6ca...e99798 )
by Mike
08:40
created

WC_Customer::get_last_order()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
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
		'username'            => '', // read only on existing users
30
		'password'            => '', // write only
31
		'date_created'        => '', // read only
32
		'date_modified'		  => '', // read only
33
		'billing_first_name'  => '',
34
		'billing_last_name'   => '',
35
		'billing_company'     => '',
36
		'billing_phone'       => '',
37
		'billing_email'       => '',
38
		'billing_address_1'   => '',
39
		'billing_address_2'   => '',
40
		'billing_state'       => '',
41
		'billing_postcode'    => '',
42
		'billing_city'        => '',
43
		'billing_country'     => '',
44
		'shipping_first_name'  => '',
45
		'shipping_last_name'   => '',
46
		'shipping_company'     => '',
47
		'shipping_postcode'   => '',
48
		'shipping_city'       => '',
49
		'shipping_address_1'  => '',
50
		'shipping_address_2'  => '',
51
		'shipping_state'      => '',
52
		'shipping_country'    => '',
53
		'is_paying_customer'  => false,
54
		'is_vat_exempt'       => false, // session only.
55
		'calculated_shipping' => false, // session only
56
	);
57
58
	/**
59
	 * Keys which are also stored in a session (so we can make sure they get updated...)
60
	 * @var array
61
	 */
62
	protected $_session_keys = array(
63
		'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address', 'billing_address_2',
64
		'billing_state', 'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address',
65
		'shipping_address_2', 'shipping_state', 'shipping_country', 'is_vat_exempt', 'calculated_shipping',
66
		'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email',
67
		'shipping_first_name', 'shipping_last_name', 'shipping_company',
68
	);
69
70
	/**
71
	 * Data stored in meta keys, but not considered "meta"
72
	 * @since 2.7.0
73
	 * @var array
74
	 */
75
	protected $_internal_meta_keys = array(
76
		'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address_2', 'billing_state',
77
		'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1',
78
		'shipping_address_2', 'shipping_state', 'shipping_country', 'paying_customer',
79
		'last_update', 'first_name', 'last_name', 'show_admin_bar_front',
80
		'use_ssl', 'admin_color', 'rich_editing', 'comment_shortcuts', 'dismissed_wp_pointers', 'show_welcome_panel',
81
		'_woocommerce_persistent_cart', 'session_tokens',
82
		'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email',
83
		'shipping_first_name', 'shipping_last_name', 'shipping_company',
84
	);
85
86
	/**
87
	 *  Internal meta type used to store user data.
88
	 * @var string
89
	 */
90
	protected $_meta_type = 'user';
91
92
	/**
93
	 * Was data changed in the database for this class?
94
	 * @var boolean
95
	 */
96
	protected $_changed = false;
97
98
	/**
99
	 * If this is the customer session, this is true. When true, guest accounts will not be saved to the DB.
100
	 * @var boolean
101
	 */
102
	protected $_is_session = false;
103
104
	/**
105
	 * Load customer data based on how WC_Customer is called.
106
	 *
107
	 * If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
108
	 * data will be pulled from the session for the current user/customer.
109
	 *
110
	 * @param int $customer_id Customer ID
111
	 * @param bool $is_session True if this is the customer session
112
	 */
113
	public function __construct( $customer_id = 0, $is_session = false ) {
114
		if ( $customer_id > 0 ) {
115
			$this->read( $customer_id );
116
		}
117
		if ( $is_session ) {
118
			$this->_is_session = true;
119
			$this->load_session();
120
			add_action( 'shutdown', array( $this, 'save_session_if_changed' ), 10 );
121
		}
122
	}
123
124
	/**
125
	 * Saves customer information to the current session if any data changed.
126
	 * @since 2.7.0
127
	 */
128
	public function save_session_if_changed() {
129
		if ( $this->_changed ) {
130
			$this->save_to_session();
131
		}
132
	}
133
134
	/**
135
	 * Loads a WC session into the customer class.
136
	 */
137
	public function load_session() {
138
		$data = (array) WC()->session->get( 'customer' );
139
		if ( ! empty( $data ) ) {
140
			foreach ( $this->_session_keys as $session_key ) {
141
				$function_key = $session_key;
142
				if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
143
					$session_key = str_replace( 'billing_', '', $session_key );
144
				}
145
				if ( ! empty( $data[ $session_key ] ) && is_callable( array( $this, "set_{$function_key}" ) ) ) {
146
					$this->{"set_{$function_key}"}( $data[ $session_key ] );
147
				}
148
			}
149
		}
150
		$this->load_defaults();
151
	}
152
153
	/**
154
	 * Load default values if props are unset.
155
	 */
156
	protected function load_defaults() {
157
		$default = wc_get_customer_default_location();
158
159
		// Set some defaults if some of our values are still not set.
160
		if ( ! $this->get_billing_country() ) {
161
			$this->set_billing_country( $default['country'] );
162
		}
163
164
		if ( ! $this->get_shipping_country() ) {
165
			$this->set_shipping_country( $this->get_billing_country() );
166
		}
167
168
		if ( ! $this->get_billing_state() ) {
169
			$this->set_billing_state( $default['state'] );
170
		}
171
172
		if ( ! $this->get_shipping_state() ) {
173
			$this->set_shipping_state( $this->get_billing_state() );
174
		}
175
	}
176
177
	/**
178
	 * Gets the customers last order.
179
	 * @return WC_Order|false
180
	 */
181
	public function get_last_order() {
182
		global $wpdb;
183
184
		$last_order = $wpdb->get_var( "SELECT posts.ID
185
			FROM $wpdb->posts AS posts
186
			LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
187
			WHERE meta.meta_key = '_customer_user'
188
			AND   meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
189
			AND   posts.post_type = 'shop_order'
190
			AND   posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
191
			ORDER BY posts.ID DESC
192
		" );
193
194
		if ( $last_order ) {
195
			return wc_get_order( absint( $last_order ) );
196
		} else {
197
			return false;
198
		}
199
	}
200
201
	/**
202
	 * Return the number of orders this customer has.
203
	 * @since 2.7.0
204
	 * @return integer
205
	 */
206
	public function get_order_count() {
207
		global $wpdb;
208
209
		$count = $wpdb->get_var( "SELECT COUNT(*)
210
			FROM $wpdb->posts as posts
211
			LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
212
			WHERE   meta.meta_key = '_customer_user'
213
			AND     posts.post_type = 'shop_order'
214
			AND     posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
215
			AND     meta_value = '" . esc_sql( $this->get_id() ) . "'
216
		" );
217
218
		return absint( $count );
219
	}
220
221
	/**
222
	 * Return how much money this customer has spent.
223
	 * @since 2.7.0
224
	 * @return float
225
	 */
226
	public function get_total_spent() {
227
		global $wpdb;
228
229
		$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
230
			FROM $wpdb->posts as posts
231
			LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
232
			LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
233
			WHERE   meta.meta_key       = '_customer_user'
234
			AND     meta.meta_value     = '" . esc_sql( $this->get_id() ) . "'
235
			AND     posts.post_type     = 'shop_order'
236
			AND     posts.post_status   IN ( 'wc-completed', 'wc-processing' )
237
			AND     meta2.meta_key      = '_order_total'
238
		" );
239
240
		if ( ! $spent ) {
241
			$spent = 0;
242
		}
243
244
		return wc_format_decimal( $spent );
245
	}
246
247
	/**
248
	 * Is customer outside base country (for tax purposes)?
249
	 * @return bool
250
	 */
251
	public function is_customer_outside_base() {
252
		list( $country, $state ) = $this->get_taxable_address();
253
		if ( $country ) {
254
			$default = wc_get_base_location();
255
			if ( $default['country'] !== $country ) {
256
				return true;
257
			}
258
			if ( $default['state'] && $default['state'] !== $state ) {
259
				return true;
260
			}
261
		}
262
		return false;
263
	}
264
265
	/*
266
	 |--------------------------------------------------------------------------
267
	 | Getters
268
	 |--------------------------------------------------------------------------
269
	 | Methods for getting data from the customer object.
270
	 */
271
272
	/**
273
	 * Return a customer's user ID. Logged out users have ID 0.
274
	 * @since 2.7.0
275
	 * @return int
276
	 */
277
	public function get_id() {
278
		return $this->_data['id'];
279
	}
280
281
	/**
282
	 * Return the customer's username.
283
	 * @since 2.7.0
284
	 * @return string
285
	 */
286
	public function get_username() {
287
		return $this->_data['username'];
288
	}
289
290
	/**
291
	 * Return the customer's email.
292
	 * @since 2.7.0
293
	 * @return string
294
	 */
295
	public function get_email() {
296
		return $this->_data['email'];
297
	}
298
299
	/**
300
	 * Return customer's first name.
301
	 * @since 2.7.0
302
	 * @return string
303
	 */
304
	public function get_first_name() {
305
		return $this->_data['first_name'];
306
	}
307
308
	/**
309
	 * Return customer's last name.
310
	 * @since 2.7.0
311
	 * @return string
312
	 */
313
	public function get_last_name() {
314
		return $this->_data['last_name'];
315
	}
316
317
	/**
318
	 * Return customer's user role.
319
	 * @since 2.7.0
320
	 * @return string
321
	 */
322
	public function get_role() {
323
		return $this->_data['role'];
324
	}
325
326
	/**
327
	 * Return this customer's avatar.
328
	 * @since 2.7.0
329
	 * @return string
330
	 */
331 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...
332
		$avatar_html = get_avatar( $this->get_email() );
333
334
		// Get the URL of the avatar from the provided HTML
335
		preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches );
336
337
		if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
338
			return esc_url( $matches[1] );
339
		}
340
341
		return '';
342
	}
343
344
	/**
345
	 * Return the date this customer was created.
346
	 * @since 2.7.0
347
	 * @return integer
348
	 */
349
	public function get_date_created() {
350
		return absint( $this->_data['date_created'] );
351
	}
352
353
	/**
354
	 * Return the date this customer was last updated.
355
	 * @since 2.7.0
356
	 * @return integer
357
	 */
358
	public function get_date_modified() {
359
		return absint( $this->_data['date_modified'] );
360
	}
361
362
	/**
363
	 * Gets billing first name.
364
	 * @return string
365
	 */
366
	public function get_billing_first_name() {
367
		return $this->_data['billing_first_name'];
368
	}
369
370
	/**
371
	 * Gets billing last name.
372
	 * @return string
373
	 */
374
	public function get_billing_last_name() {
375
		return $this->_data['billing_last_name'];
376
	}
377
378
	/**
379
	 * Gets billing company.
380
	 * @return string
381
	 */
382
	public function get_billing_company() {
383
		return $this->_data['billing_company'];
384
	}
385
386
	/**
387
	 * Gets billing phone.
388
	 * @return string
389
	 */
390
	public function get_billing_phone() {
391
		return $this->_data['billing_phone'];
392
	}
393
394
	/**
395
	 * Gets billing email.
396
	 * @return string
397
	 */
398
	public function get_billing_email() {
399
		return $this->_data['billing_email'];
400
	}
401
402
	/**
403
	 * Gets customer postcode.
404
	 * @return string
405
	 */
406
	public function get_billing_postcode() {
407
		return wc_format_postcode( $this->_data['billing_postcode'], $this->get_billing_country() );
408
	}
409
410
	/**
411
	 * Get customer city.
412
	 * @return string
413
	 */
414
	public function get_billing_city() {
415
		return $this->_data['billing_city'];
416
	}
417
418
	/**
419
	 * Get customer address.
420
	 * @return string
421
	 */
422
	public function get_billing_address() {
423
		return $this->_data['billing_address_1'];
424
	}
425
426
	/**
427
	 * Get customer address.
428
	 * @return string
429
	 */
430
	public function get_billing_address_1() {
431
		return $this->get_billing_address();
432
	}
433
434
	/**
435
	 * Get customer's second address.
436
	 * @return string
437
	 */
438
	public function get_billing_address_2() {
439
		return $this->_data['billing_address_2'];
440
	}
441
442
	/**
443
	 * Get customer state.
444
	 * @return string
445
	 */
446
	public function get_billing_state() {
447
		return $this->_data['billing_state'];
448
	}
449
450
	/**
451
	 * Get customer country.
452
	 * @return string
453
	 */
454
	public function get_billing_country() {
455
		return $this->_data['billing_country'];
456
	}
457
458
	/**
459
	 * Gets shipping first name.
460
	 * @return string
461
	 */
462
	public function get_shipping_first_name() {
463
		return $this->_data['shipping_first_name'];
464
	}
465
466
	/**
467
	 * Gets shipping last name.
468
	 * @return string
469
	 */
470
	public function get_shipping_last_name() {
471
		return $this->_data['shipping_last_name'];
472
	}
473
474
	/**
475
	 * Gets shipping company.
476
	 * @return string
477
	 */
478
	public function get_shipping_company() {
479
		return $this->_data['shipping_company'];
480
	}
481
482
	/**
483
	 * Get customer's shipping state.
484
	 * @return string
485
	 */
486
	public function get_shipping_state() {
487
		return $this->_data['shipping_state'];
488
	}
489
490
	/**
491
	 * Get customer's shipping country.
492
	 * @return string
493
	 */
494
	public function get_shipping_country() {
495
		return $this->_data['shipping_country'];
496
	}
497
498
	/**
499
	 * Get customer's shipping postcode.
500
	 * @return string
501
	 */
502
	public function get_shipping_postcode() {
503
		return wc_format_postcode( $this->_data['shipping_postcode'], $this->get_shipping_country() );
504
	}
505
506
	/**
507
	 * Get customer's shipping city.
508
	 * @return string
509
	 */
510
	public function get_shipping_city() {
511
		return $this->_data['shipping_city'];
512
	}
513
514
	/**
515
	 * Get customer's shipping address.
516
	 * @return string
517
	 */
518
	public function get_shipping_address() {
519
		return $this->_data['shipping_address_1'];
520
	}
521
522
	/**
523
	 * Get customer address.
524
	 * @return string
525
	 */
526
	public function get_shipping_address_1() {
527
		return $this->get_shipping_address();
528
	}
529
530
	/**
531
	 * Get customer's second shipping address.
532
	 * @return string
533
	 */
534
	public function get_shipping_address_2() {
535
		return $this->_data['shipping_address_2'];
536
	}
537
538
	/**
539
	 * Get if customer is VAT exempt?
540
	 * @since 2.7.0
541
	 * @return bool
542
	 */
543
	public function get_is_vat_exempt() {
544
		return ( ! empty( $this->_data['is_vat_exempt'] ) ) ? true : false;
545
	}
546
547
	/**
548
	 * Has customer calculated shipping?
549
	 * @return bool
550
	 */
551
	public function get_calculated_shipping() {
552
		return ! empty( $this->_data['calculated_shipping'] );
553
	}
554
555
	/**
556
	 * Get taxable address.
557
	 * @return array
558
	 */
559
	public function get_taxable_address() {
560
		$tax_based_on = get_option( 'woocommerce_tax_based_on' );
561
562
		// Check shipping method at this point to see if we need special handling
563
		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 ) {
564
			$tax_based_on = 'base';
565
		}
566
567
		if ( 'base' === $tax_based_on ) {
568
			$country  = WC()->countries->get_base_country();
569
			$state    = WC()->countries->get_base_state();
570
			$postcode = WC()->countries->get_base_postcode();
571
			$city     = WC()->countries->get_base_city();
572
		} elseif ( 'billing' === $tax_based_on ) {
573
			$country  = $this->get_billing_country();
574
			$state    = $this->get_billing_state();
575
			$postcode = $this->get_billing_postcode();
576
			$city     = $this->get_billing_city();
577
		} else {
578
			$country  = $this->get_shipping_country();
579
			$state    = $this->get_shipping_state();
580
			$postcode = $this->get_shipping_postcode();
581
			$city     = $this->get_shipping_city();
582
		}
583
584
		return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
585
	}
586
587
	/**
588
	 * Gets a customer's downloadable products.
589
	 * @return array Array of downloadable products
590
	 */
591
	public function get_downloadable_products() {
592
		$downloads = array();
593
		if ( $this->get_id() ) {
594
			$downloads = wc_get_customer_available_downloads( $this->get_id() );
595
		}
596
		return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
597
	}
598
599
	/**
600
	 * Is the user a paying customer?
601
	 * @since 2.7.0
602
	 * @return bool
603
	 */
604
	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...
605
		return (bool) $this->_data['is_paying_customer'];
606
	}
607
608
	/*
609
	|--------------------------------------------------------------------------
610
	| Setters
611
	|--------------------------------------------------------------------------
612
	| Functions for setting customer data. These should not update anything in the
613
	| database itself and should only change what is stored in the class
614
	| object.
615
	*/
616
617
	/**
618
	 * Set customer ID.
619
	 * @since 2.7.0
620
	 * @param int $value
621
	 */
622
	protected function set_id( $value ) {
623
		$this->_data['id'] = absint( $value );
624
	}
625
626
	/**
627
	 * Set customer's username.
628
	 * @since 2.7.0
629
	 * @param string $username
630
	 */
631
	public function set_username( $username ) {
632
		$this->_data['username'] = $username;
633
	}
634
635
	/**
636
	 * Set customer's email.
637
	 * @since 2.7.0
638
	 * @param string $email
639
	 */
640
	public function set_email( $email ) {
641
		$this->_data['email'] = sanitize_email( $email );
642
	}
643
644
	/**
645
	 * Set customer's first name.
646
	 * @since 2.7.0
647
	 * @param string $first_name
648
	 */
649
	public function set_first_name( $first_name ) {
650
		$this->_data['first_name'] = $first_name;
651
	}
652
653
	/**
654
	 * Set customer's last name.
655
	 * @since 2.7.0
656
	 * @param string $last_name
657
	 */
658
	public function set_last_name( $last_name ) {
659
		$this->_data['last_name'] = $last_name;
660
	}
661
662
	/**
663
	 * Set customer's user role(s).
664
	 * @since 2.7.0
665
	 * @param mixed $role
666
	 */
667
	public function set_role( $role ) {
668
		$this->_data['role'] = $role;
669
	}
670
671
	/**
672
	 * Set customer's password.
673
	 * @since 2.7.0
674
	 * @param string $password
675
	 */
676
	public function set_password( $password ) {
677
		$this->_data['password'] = wc_clean( $password );
678
	}
679
680
	/**
681
	 * Set the date this customer was last updated.
682
	 * @since 2.7.0
683
	 * @param integer $timestamp
684
	 */
685
	public function set_date_modified( $timestamp ) {
686
		$this->_data['date_modified'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
687
	}
688
689
	/**
690
	 * Set the date this customer was last updated.
691
	 * @since 2.7.0
692
	 * @param integer $timestamp
693
	 */
694
	public function set_date_created( $timestamp ) {
695
		$this->_data['date_created'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
696
	}
697
698
	/**
699
	 * Set customer address to match shop base address.
700
	 * @since 2.7.0
701
	 */
702 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...
703
		$base = wc_get_customer_default_location();
704
		$this->_data['billing_country']  = $base['country'];
705
		$this->_data['billing_state']    = $base['state'];
706
		$this->_data['billing_postcode'] = '';
707
		$this->_data['billing_city']     = '';
708
	}
709
710
	/**
711
	 * Set customer shipping address to base address.
712
	 * @since 2.7.0
713
	 */
714 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...
715
		$base = wc_get_customer_default_location();
716
		$this->_data['shipping_country']  = $base['country'];
717
		$this->_data['shipping_state']    = $base['state'];
718
		$this->_data['shipping_postcode'] = '';
719
		$this->_data['shipping_city']     = '';
720
	}
721
722
	/**
723
	 * Sets all shipping info at once.
724
	 * @param string $country
725
	 * @param string $state
726
	 * @param string $postcode
727
	 * @param string $city
728
	 */
729
	public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
730
		$this->_data['shipping_country']  = $country;
731
		$this->_data['shipping_state']    = $state;
732
		$this->_data['shipping_postcode'] = $postcode;
733
		$this->_data['shipping_city']     = $city;
734
	}
735
736
	/**
737
	 * Sets all address info at once.
738
	 * @param string $country
739
	 * @param string $state
740
	 * @param string $postcode
741
	 * @param string $city
742
	 */
743
	public function set_billing_location( $country, $state, $postcode = '', $city = '' ) {
744
		$this->_data['billing_country']  = $country;
745
		$this->_data['billing_state']    = $state;
746
		$this->_data['billing_postcode'] = $postcode;
747
		$this->_data['billing_city']     = $city;
748
	}
749
750
	/**
751
	 * Set billing first name.
752
	 * @return string
753
	 */
754
	public function set_billing_first_name( $value ) {
755
		$this->_data['billing_first_name'] = $value;
756
	}
757
758
	/**
759
	 * Set billing last name.
760
	 * @return string
761
	 */
762
	public function set_billing_last_name( $value ) {
763
		$this->_data['billing_last_name'] = $value;
764
	}
765
766
	/**
767
	 * Set billing company.
768
	 * @return string
769
	 */
770
	public function set_billing_company( $value ) {
771
		$this->_data['billing_company'] = $value;
772
	}
773
774
	/**
775
	 * Set billing phone.
776
	 * @return string
777
	 */
778
	public function set_billing_phone( $value ) {
779
		$this->_data['billing_phone'] = $value;
780
	}
781
782
	/**
783
	 * Set billing email.
784
	 * @return string
785
	 */
786
	public function set_billing_email( $email ) {
787
		$this->_data['billing_email'] = sanitize_email( $email );
788
	}
789
790
	/**
791
	 * Set customer country.
792
	 * @param mixed $country
793
	 */
794
	public function set_billing_country( $country ) {
795
		$this->_data['billing_country'] = $country;
796
	}
797
798
	/**
799
	 * Set customer state.
800
	 * @param mixed $state
801
	 */
802
	public function set_billing_state( $state ) {
803
		$this->_data['billing_state'] = $state;
804
	}
805
806
	/**
807
	 * Sets customer postcode.
808
	 * @param mixed $postcode
809
	 */
810
	public function set_billing_postcode( $postcode ) {
811
		$this->_data['billing_postcode'] = $postcode;
812
	}
813
814
	/**
815
	 * Sets customer city.
816
	 * @param mixed $city
817
	 */
818
	public function set_billing_city( $city ) {
819
		$this->_data['billing_city'] = $city;
820
	}
821
822
	/**
823
	 * Set customer address.
824
	 * @param mixed $address
825
	 */
826
	public function set_billing_address( $address ) {
827
		$this->_data['billing_address_1'] = $address;
828
	}
829
830
	/**
831
	 * Set customer address.
832
	 * @param mixed $address
833
	 */
834
	public function set_billing_address_1( $address ) {
835
		$this->set_billing_address( $address );
836
	}
837
838
	/**
839
	 * Set customer's second address.
840
	 * @param mixed $address
841
	 */
842
	public function set_billing_address_2( $address ) {
843
		$this->_data['billing_address_2'] = $address;
844
	}
845
846
	/**
847
	 * Set shipping first name.
848
	 * @return string
849
	 */
850
	public function set_shipping_first_name( $value ) {
851
		$this->_data['shipping_first_name'] = $value;
852
	}
853
854
	/**
855
	 * Set shipping last name.
856
	 * @return string
857
	 */
858
	public function set_shipping_last_name( $value ) {
859
		$this->_data['shipping_last_name'] = $value;
860
	}
861
862
	/**
863
	 * Set shipping company.
864
	 * @return string
865
	 */
866
	public function set_shipping_company( $value ) {
867
		$this->_data['shipping_company'] = $value;
868
	}
869
870
	/**
871
	 * Set shipping country.
872
	 * @param string $country
873
	 */
874
	public function set_shipping_country( $country ) {
875
		$this->_data['shipping_country'] = $country;
876
	}
877
878
	/**
879
	 * Set shipping state.
880
	 * @param string $state
881
	 */
882
	public function set_shipping_state( $state ) {
883
		$this->_data['shipping_state'] = $state;
884
	}
885
886
	/**
887
	 * Set shipping postcode.
888
	 * @param string $postcode
889
	 */
890
	public function set_shipping_postcode( $postcode ) {
891
		$this->_data['shipping_postcode'] = $postcode;
892
	}
893
894
	/**
895
	 * Sets shipping city.
896
	 * @param string $city
897
	 */
898
	public function set_shipping_city( $city ) {
899
		$this->_data['shipping_city'] = $city;
900
	}
901
902
	/**
903
	 * Set shipping address.
904
	 * @param string $address
905
	 */
906
	public function set_shipping_address( $address ) {
907
		$this->_data['shipping_address_1'] = $address;
908
	}
909
910
	/**
911
	 * Set customer shipping address.
912
	 * @param mixed $address
913
	 */
914
	public function set_shipping_address_1( $address ) {
915
		$this->set_shipping_address( $address );
916
	}
917
918
	/**
919
	 * Set second shipping address.
920
	 * @param string $address
921
	 */
922
	public function set_shipping_address_2( $address ) {
923
		$this->_data['shipping_address_2'] = $address;
924
	}
925
926
	/**
927
	 * Set if customer has tax exemption.
928
	 * @param bool $is_vat_exempt
929
	 */
930
	public function set_is_vat_exempt( $is_vat_exempt ) {
931
		$this->_data['is_vat_exempt'] = $is_vat_exempt;
932
	}
933
934
	/**
935
	 * Calculated shipping?
936
	 * @param boolean $calculated
937
	 */
938
	public function set_calculated_shipping( $calculated = true ) {
939
		$this->_data['calculated_shipping'] = $calculated;
940
	}
941
942
	/**
943
	 * Set if the user a paying customer.
944
	 * @since 2.7.0
945
	 * @param boolean $is_paying_customer
946
	 */
947
	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...
948
		$this->_data['is_paying_customer'] = (bool) $is_paying_customer;
949
	}
950
951
	/*
952
	 |--------------------------------------------------------------------------
953
	 | CRUD methods
954
	 |--------------------------------------------------------------------------
955
	 | Methods which create, read, update and delete from the database.
956
	 |
957
	 | A save method is included for convenience (chooses update or create based
958
	 | on if the order exists yet).
959
	 */
960
961
	 /**
962
	  * Create a customer.
963
	  * @since 2.7.0.
964
	  */
965
	public function create() {
966
		$customer_id = wc_create_new_customer( $this->get_email(), $this->get_username(), $this->_data['password'] );
967
968
		if ( ! is_wp_error( $customer_id ) ) {
969
			$this->_data['id'] = $customer_id;
970
			update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
971
			update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
972
			update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
973
			update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
974
			update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
975
			update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
976
			update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
977
			update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
978
			update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
979
			update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
980
			update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
981
			update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
982
			update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
983
			update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
984
			update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
985
			update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
986
			update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
987
			update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() );
988
			update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
989
			update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
990
			update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
991
			update_user_meta( $this->get_id(), 'last_update',  $this->get_date_modified() );
992
			update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
993
			update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
994
			wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
995
			$wp_user = new WP_User( $this->get_id() );
996
			$this->set_date_created( strtotime( $wp_user->user_registered ) );
997
			$this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) );
998
			$this->read_meta_data();
999
		}
1000
	}
1001
1002
	/**
1003
	 * Read a customer from the database.
1004
	 * @since 2.7.0
1005
	 * @param integer $id
1006
	 */
1007
	public function read( $id ) {
1008
		global $wpdb;
1009
1010
		// User object is required.
1011
		if ( ! $id || ! ( $user_object = get_user_by( 'id', $id ) ) || empty( $user_object->ID ) ) {
1012
			$this->set_id( 0 );
1013
			return;
1014
		}
1015
1016
		// Only users on this site should be read.
1017
		if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
1018
			$this->set_id( 0 );
1019
			return;
1020
		}
1021
1022
		$this->set_id( $user_object->ID );
1023
1024
		foreach ( array_keys( $this->_data ) as $key ) {
1025
			$meta_value = get_user_meta( $id, $key, true );
1026
			if ( $meta_value && is_callable( array( $this, "set_{$key}" ) ) ) {
1027
				$this->{"set_{$key}"}( $meta_value );
1028
			}
1029
		}
1030
1031
		$this->set_is_paying_customer( get_user_meta( $id, 'paying_customer', true ) );
1032
		$this->set_email( $user_object->user_email );
1033
		$this->set_username( $user_object->user_login );
1034
		$this->set_date_created( strtotime( $user_object->user_registered ) );
1035
		$this->set_date_modified( get_user_meta( $id, 'last_update', true ) );
1036
		$this->set_role( ( ! empty ( $user_object->roles[0] ) ? $user_object->roles[0] : 'customer' ) );
1037
		$this->read_meta_data();
1038
1039
		unset( $this->_data['password'] ); // password is write only, never ever read it
1040
	}
1041
1042
	/**
1043
	 * Update a customer.
1044
	 * @since 2.7.0
1045
	 */
1046
	public function update() {
1047
		$customer_ID = $this->get_id();
1048
1049
		wp_update_user( array( 'ID' => $customer_ID, 'user_email' => $this->get_email() ) );
1050
		// Only update password if a new one was set with set_password
1051
		if ( isset( $this->_data['password'] ) ) {
1052
			wp_update_user( array( 'ID' => $customer_ID, 'user_pass' => $this->_data['password'] ) );
1053
			unset( $this->_data['password'] );
1054
		}
1055
1056
		update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
1057
		update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
1058
		update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
1059
		update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
1060
		update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
1061
		update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
1062
		update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
1063
		update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
1064
		update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
1065
		update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
1066
		update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
1067
		update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
1068
		update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
1069
		update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
1070
		update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
1071
		update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
1072
		update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
1073
		update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() );
1074
		update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
1075
		update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
1076
		update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
1077
		update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
1078
		update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
1079
		wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
1080
		$this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) );
1081
		$this->save_meta_data();
1082
	}
1083
1084
	/**
1085
	 * Delete a customer.
1086
	 * @since 2.7.0
1087
	 */
1088
	public function delete() {
1089
		if ( ! $this->get_id() ) {
1090
			return;
1091
		}
1092
		wp_delete_user( $this->get_id() );
1093
	}
1094
1095
	/**
1096
	 * Save data. Create when creating a new user/class, update when editing
1097
	 * an existing user, and save session when working on a logged out guest
1098
	 * session.
1099
	 * @since 2.7.0
1100
	 */
1101
	public function save() {
1102
		if ( $this->_is_session ) {
1103
			$this->save_session_if_changed();
1104
		} elseif ( ! $this->get_id() ) {
1105
			$this->create();
1106
		} else {
1107
			$this->update();
1108
		}
1109
	}
1110
1111
	/**
1112
	 * Saves data to the session only (does not overwrite DB values).
1113
	 * @since 2.7.0
1114
	 */
1115
	public function save_to_session() {
1116
		$data = array();
1117
		foreach ( $this->_session_keys as $session_key ) {
1118
			$function_key = $session_key;
1119
			if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
1120
				$session_key = str_replace( 'billing_', '', $session_key );
1121
			}
1122
			$data[ $session_key ] = $this->{"get_$function_key"}();
1123
		}
1124
		WC()->session->set( 'customer', $data );
1125
	}
1126
1127
}
1128