Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WC_Customer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Customer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 | 'date_created' => '', |
||
26 | 'date_modified' => '', |
||
27 | 'email' => '', |
||
28 | 'first_name' => '', |
||
29 | 'last_name' => '', |
||
30 | 'role' => 'customer', |
||
31 | 'username' => '', |
||
32 | 'billing' => array( |
||
33 | 'first_name' => '', |
||
34 | 'last_name' => '', |
||
35 | 'company' => '', |
||
36 | 'address_1' => '', |
||
37 | 'address_2' => '', |
||
38 | 'city' => '', |
||
39 | 'state' => '', |
||
40 | 'postcode' => '', |
||
41 | 'country' => '', |
||
42 | 'email' => '', |
||
43 | 'phone' => '', |
||
44 | ), |
||
45 | 'shipping' => array( |
||
46 | 'first_name' => '', |
||
47 | 'last_name' => '', |
||
48 | 'company' => '', |
||
49 | 'address_1' => '', |
||
50 | 'address_2' => '', |
||
51 | 'city' => '', |
||
52 | 'state' => '', |
||
53 | 'postcode' => '', |
||
54 | 'country' => '', |
||
55 | ), |
||
56 | 'is_paying_customer' => false, |
||
57 | ); |
||
58 | |||
59 | /** |
||
60 | * Keys which are also stored in a session (so we can make sure they get updated...) |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $_session_keys = array( |
||
64 | 'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address', 'billing_address_2', |
||
65 | 'billing_state', 'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address', |
||
66 | 'shipping_address_2', 'shipping_state', 'shipping_country', 'is_vat_exempt', 'calculated_shipping', |
||
67 | 'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email', |
||
68 | 'shipping_first_name', 'shipping_last_name', 'shipping_company', |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * Data stored in meta keys, but not considered "meta" |
||
73 | * @since 2.7.0 |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $_internal_meta_keys = array( |
||
77 | 'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address_2', 'billing_state', |
||
78 | 'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1', |
||
79 | 'shipping_address_2', 'shipping_state', 'shipping_country', 'paying_customer', |
||
80 | 'last_update', 'first_name', 'last_name', 'show_admin_bar_front', |
||
81 | 'use_ssl', 'admin_color', 'rich_editing', 'comment_shortcuts', 'dismissed_wp_pointers', 'show_welcome_panel', |
||
82 | '_woocommerce_persistent_cart', 'session_tokens', 'nickname', 'description', |
||
83 | 'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email', |
||
84 | 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'default_password_nag', |
||
85 | 'primary_blog', 'source_domain', |
||
86 | ); |
||
87 | |||
88 | /** |
||
89 | * Internal meta type used to store user data. |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $_meta_type = 'user'; |
||
93 | |||
94 | /** |
||
95 | * If this is the customer session, this is true. When true, guest accounts will not be saved to the DB. |
||
96 | * @var boolean |
||
97 | */ |
||
98 | protected $_is_session = false; |
||
99 | |||
100 | /** |
||
101 | * Stores a password if this needs to be changed. Write-only and hidden from _data. |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $_password = ''; |
||
105 | |||
106 | /** |
||
107 | * Stores if user is VAT exempt for this session. |
||
108 | * @var string |
||
109 | */ |
||
110 | protected $_is_vat_exempt = false; |
||
111 | |||
112 | /** |
||
113 | * Stores if user has calculated shipping in this session. |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $_calculated_shipping = false; |
||
117 | |||
118 | /** |
||
119 | * Load customer data based on how WC_Customer is called. |
||
120 | * |
||
121 | * If $customer is 'new', you can build a new WC_Customer object. If it's empty, some |
||
122 | * data will be pulled from the session for the current user/customer. |
||
123 | * |
||
124 | * @param int $customer_id Customer ID |
||
125 | * @param bool $is_session True if this is the customer session |
||
126 | */ |
||
127 | public function __construct( $customer_id = 0, $is_session = false ) { |
||
128 | if ( $customer_id > 0 ) { |
||
129 | $this->read( $customer_id ); |
||
130 | } |
||
131 | if ( $is_session ) { |
||
132 | $this->_is_session = true; |
||
133 | $this->load_session(); |
||
134 | add_action( 'shutdown', array( $this, 'save_to_session' ), 10 ); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Loads a WC session into the customer class. |
||
140 | */ |
||
141 | public function load_session() { |
||
156 | |||
157 | /** |
||
158 | * Load default values if props are unset. |
||
159 | */ |
||
160 | protected function load_defaults() { |
||
180 | |||
181 | /** |
||
182 | * Gets the customers last order. |
||
183 | * @return WC_Order|false |
||
184 | */ |
||
185 | public function get_last_order() { |
||
204 | |||
205 | /** |
||
206 | * Return the number of orders this customer has. |
||
207 | * @since 2.7.0 |
||
208 | * @return integer |
||
209 | */ |
||
210 | public function get_order_count() { |
||
224 | |||
225 | /** |
||
226 | * Return how much money this customer has spent. |
||
227 | * @since 2.7.0 |
||
228 | * @return float |
||
229 | */ |
||
230 | public function get_total_spent() { |
||
250 | |||
251 | /** |
||
252 | * Is customer outside base country (for tax purposes)? |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function is_customer_outside_base() { |
||
268 | |||
269 | /** |
||
270 | * Is customer VAT exempt? |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function is_vat_exempt() { |
||
276 | |||
277 | /** |
||
278 | * Has calculated shipping? |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function has_calculated_shipping() { |
||
284 | |||
285 | /* |
||
286 | |-------------------------------------------------------------------------- |
||
287 | | Getters |
||
288 | |-------------------------------------------------------------------------- |
||
289 | | Methods for getting data from the customer object. |
||
290 | */ |
||
291 | |||
292 | /** |
||
293 | * Return a customer's user ID. Logged out users have ID 0. |
||
294 | * @since 2.7.0 |
||
295 | * @return int |
||
296 | */ |
||
297 | public function get_id() { |
||
300 | |||
301 | /** |
||
302 | * Return the customer's username. |
||
303 | * @since 2.7.0 |
||
304 | * @return string |
||
305 | */ |
||
306 | public function get_username() { |
||
309 | |||
310 | /** |
||
311 | * Return the customer's email. |
||
312 | * @since 2.7.0 |
||
313 | * @return string |
||
314 | */ |
||
315 | public function get_email() { |
||
318 | |||
319 | /** |
||
320 | * Return customer's first name. |
||
321 | * @since 2.7.0 |
||
322 | * @return string |
||
323 | */ |
||
324 | public function get_first_name() { |
||
327 | |||
328 | /** |
||
329 | * Return customer's last name. |
||
330 | * @since 2.7.0 |
||
331 | * @return string |
||
332 | */ |
||
333 | public function get_last_name() { |
||
336 | |||
337 | /** |
||
338 | * Return customer's user role. |
||
339 | * @since 2.7.0 |
||
340 | * @return string |
||
341 | */ |
||
342 | public function get_role() { |
||
345 | |||
346 | /** |
||
347 | * Return this customer's avatar. |
||
348 | * @since 2.7.0 |
||
349 | * @return string |
||
350 | */ |
||
351 | View Code Duplication | public function get_avatar_url() { |
|
363 | |||
364 | /** |
||
365 | * Return the date this customer was created. |
||
366 | * @since 2.7.0 |
||
367 | * @return integer |
||
368 | */ |
||
369 | public function get_date_created() { |
||
372 | |||
373 | /** |
||
374 | * Return the date this customer was last updated. |
||
375 | * @since 2.7.0 |
||
376 | * @return integer |
||
377 | */ |
||
378 | public function get_date_modified() { |
||
381 | |||
382 | /** |
||
383 | * Gets customer billing first name. |
||
384 | * @return string |
||
385 | */ |
||
386 | public function get_billing_first_name() { |
||
389 | |||
390 | /** |
||
391 | * Gets customer billing last name. |
||
392 | * @return string |
||
393 | */ |
||
394 | public function get_billing_last_name() { |
||
397 | |||
398 | /** |
||
399 | * Gets customer billing company. |
||
400 | * @return string |
||
401 | */ |
||
402 | public function get_billing_company() { |
||
405 | |||
406 | /** |
||
407 | * Gets billing phone. |
||
408 | * @return string |
||
409 | */ |
||
410 | public function get_billing_phone() { |
||
413 | |||
414 | /** |
||
415 | * Gets billing email. |
||
416 | * @return string |
||
417 | */ |
||
418 | public function get_billing_email() { |
||
421 | |||
422 | /** |
||
423 | * Gets customer postcode. |
||
424 | * @return string |
||
425 | */ |
||
426 | public function get_billing_postcode() { |
||
429 | |||
430 | /** |
||
431 | * Get customer city. |
||
432 | * @return string |
||
433 | */ |
||
434 | public function get_billing_city() { |
||
437 | |||
438 | /** |
||
439 | * Get customer address. |
||
440 | * @return string |
||
441 | */ |
||
442 | public function get_billing_address() { |
||
445 | |||
446 | /** |
||
447 | * Get customer address. |
||
448 | * @return string |
||
449 | */ |
||
450 | public function get_billing_address_1() { |
||
453 | |||
454 | /** |
||
455 | * Get customer's second address. |
||
456 | * @return string |
||
457 | */ |
||
458 | public function get_billing_address_2() { |
||
461 | |||
462 | /** |
||
463 | * Get customer state. |
||
464 | * @return string |
||
465 | */ |
||
466 | public function get_billing_state() { |
||
469 | |||
470 | /** |
||
471 | * Get customer country. |
||
472 | * @return string |
||
473 | */ |
||
474 | public function get_billing_country() { |
||
477 | |||
478 | /** |
||
479 | * Gets customer shipping first name. |
||
480 | * @return string |
||
481 | */ |
||
482 | public function get_shipping_first_name() { |
||
485 | |||
486 | /** |
||
487 | * Gets customer shipping last name. |
||
488 | * @return string |
||
489 | */ |
||
490 | public function get_shipping_last_name() { |
||
493 | |||
494 | /** |
||
495 | * Gets customer shipping company. |
||
496 | * @return string |
||
497 | */ |
||
498 | public function get_shipping_company() { |
||
501 | |||
502 | /** |
||
503 | * Get customer's shipping state. |
||
504 | * @return string |
||
505 | */ |
||
506 | public function get_shipping_state() { |
||
509 | |||
510 | /** |
||
511 | * Get customer's shipping country. |
||
512 | * @return string |
||
513 | */ |
||
514 | public function get_shipping_country() { |
||
517 | |||
518 | /** |
||
519 | * Get customer's shipping postcode. |
||
520 | * @return string |
||
521 | */ |
||
522 | public function get_shipping_postcode() { |
||
525 | |||
526 | /** |
||
527 | * Get customer's shipping city. |
||
528 | * @return string |
||
529 | */ |
||
530 | public function get_shipping_city() { |
||
533 | |||
534 | /** |
||
535 | * Get customer's shipping address. |
||
536 | * @return string |
||
537 | */ |
||
538 | public function get_shipping_address() { |
||
541 | |||
542 | /** |
||
543 | * Get customer address. |
||
544 | * @return string |
||
545 | */ |
||
546 | public function get_shipping_address_1() { |
||
549 | |||
550 | /** |
||
551 | * Get customer's second shipping address. |
||
552 | * @return string |
||
553 | */ |
||
554 | public function get_shipping_address_2() { |
||
557 | |||
558 | /** |
||
559 | * Get if customer is VAT exempt? |
||
560 | * @since 2.7.0 |
||
561 | * @return bool |
||
562 | */ |
||
563 | public function get_is_vat_exempt() { |
||
566 | |||
567 | /** |
||
568 | * Has customer calculated shipping? |
||
569 | * @return bool |
||
570 | */ |
||
571 | public function get_calculated_shipping() { |
||
574 | |||
575 | /** |
||
576 | * Get taxable address. |
||
577 | * @return array |
||
578 | */ |
||
579 | public function get_taxable_address() { |
||
606 | |||
607 | /** |
||
608 | * Gets a customer's downloadable products. |
||
609 | * @return array Array of downloadable products |
||
610 | */ |
||
611 | public function get_downloadable_products() { |
||
618 | |||
619 | /** |
||
620 | * Is the user a paying customer? |
||
621 | * @since 2.7.0 |
||
622 | * @return bool |
||
623 | */ |
||
624 | function get_is_paying_customer() { |
||
627 | |||
628 | /* |
||
629 | |-------------------------------------------------------------------------- |
||
630 | | Setters |
||
631 | |-------------------------------------------------------------------------- |
||
632 | | Functions for setting customer data. These should not update anything in the |
||
633 | | database itself and should only change what is stored in the class |
||
634 | | object. |
||
635 | */ |
||
636 | |||
637 | /** |
||
638 | * Set customer ID. |
||
639 | * @since 2.7.0 |
||
640 | * @param int $value |
||
641 | * @throws WC_Data_Exception |
||
642 | */ |
||
643 | protected function set_id( $value ) { |
||
646 | |||
647 | /** |
||
648 | * Set customer's username. |
||
649 | * @since 2.7.0 |
||
650 | * @param string $username |
||
651 | * @throws WC_Data_Exception |
||
652 | */ |
||
653 | public function set_username( $username ) { |
||
656 | |||
657 | /** |
||
658 | * Set customer's email. |
||
659 | * @since 2.7.0 |
||
660 | * @param string $value |
||
661 | * @throws WC_Data_Exception |
||
662 | */ |
||
663 | public function set_email( $value ) { |
||
669 | |||
670 | /** |
||
671 | * Set customer's first name. |
||
672 | * @since 2.7.0 |
||
673 | * @param string $first_name |
||
674 | * @throws WC_Data_Exception |
||
675 | */ |
||
676 | public function set_first_name( $first_name ) { |
||
679 | |||
680 | /** |
||
681 | * Set customer's last name. |
||
682 | * @since 2.7.0 |
||
683 | * @param string $last_name |
||
684 | * @throws WC_Data_Exception |
||
685 | */ |
||
686 | public function set_last_name( $last_name ) { |
||
689 | |||
690 | /** |
||
691 | * Set customer's user role(s). |
||
692 | * @since 2.7.0 |
||
693 | * @param mixed $role |
||
694 | * @throws WC_Data_Exception |
||
695 | */ |
||
696 | public function set_role( $role ) { |
||
704 | |||
705 | /** |
||
706 | * Set customer's password. |
||
707 | * @since 2.7.0 |
||
708 | * @param string $password |
||
709 | * @throws WC_Data_Exception |
||
710 | */ |
||
711 | public function set_password( $password ) { |
||
714 | |||
715 | /** |
||
716 | * Set the date this customer was last updated. |
||
717 | * @since 2.7.0 |
||
718 | * @param integer $timestamp |
||
719 | * @throws WC_Data_Exception |
||
720 | */ |
||
721 | public function set_date_modified( $timestamp ) { |
||
724 | |||
725 | /** |
||
726 | * Set the date this customer was last updated. |
||
727 | * @since 2.7.0 |
||
728 | * @param integer $timestamp |
||
729 | * @throws WC_Data_Exception |
||
730 | */ |
||
731 | public function set_date_created( $timestamp ) { |
||
734 | |||
735 | /** |
||
736 | * Set customer address to match shop base address. |
||
737 | * @since 2.7.0 |
||
738 | * @throws WC_Data_Exception |
||
739 | */ |
||
740 | public function set_billing_address_to_base() { |
||
747 | |||
748 | /** |
||
749 | * Set customer shipping address to base address. |
||
750 | * @since 2.7.0 |
||
751 | * @throws WC_Data_Exception |
||
752 | */ |
||
753 | public function set_shipping_address_to_base() { |
||
760 | |||
761 | /** |
||
762 | * Sets all shipping info at once. |
||
763 | * @param string $country |
||
764 | * @param string $state |
||
765 | * @param string $postcode |
||
766 | * @param string $city |
||
767 | * @throws WC_Data_Exception |
||
768 | */ |
||
769 | View Code Duplication | public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) { |
|
775 | |||
776 | /** |
||
777 | * Sets all address info at once. |
||
778 | * @param string $country |
||
779 | * @param string $state |
||
780 | * @param string $postcode |
||
781 | * @param string $city |
||
782 | * @throws WC_Data_Exception |
||
783 | */ |
||
784 | View Code Duplication | public function set_billing_location( $country, $state, $postcode = '', $city = '' ) { |
|
790 | |||
791 | /** |
||
792 | * Set billing first name. |
||
793 | * @return string |
||
794 | * @throws WC_Data_Exception |
||
795 | */ |
||
796 | public function set_billing_first_name( $value ) { |
||
799 | |||
800 | /** |
||
801 | * Set billing last name. |
||
802 | * @return string |
||
803 | * @throws WC_Data_Exception |
||
804 | */ |
||
805 | public function set_billing_last_name( $value ) { |
||
808 | |||
809 | /** |
||
810 | * Set billing company. |
||
811 | * @return string |
||
812 | * @throws WC_Data_Exception |
||
813 | */ |
||
814 | public function set_billing_company( $value ) { |
||
817 | |||
818 | /** |
||
819 | * Set billing phone. |
||
820 | * @return string |
||
821 | * @throws WC_Data_Exception |
||
822 | */ |
||
823 | public function set_billing_phone( $value ) { |
||
826 | |||
827 | /** |
||
828 | * Set billing email. |
||
829 | * @param string $value |
||
830 | * @return string |
||
831 | * @throws WC_Data_Exception |
||
832 | */ |
||
833 | public function set_billing_email( $value ) { |
||
839 | |||
840 | /** |
||
841 | * Set customer country. |
||
842 | * @param mixed $country |
||
843 | * @throws WC_Data_Exception |
||
844 | */ |
||
845 | public function set_billing_country( $country ) { |
||
848 | |||
849 | /** |
||
850 | * Set customer state. |
||
851 | * @param mixed $state |
||
852 | * @throws WC_Data_Exception |
||
853 | */ |
||
854 | public function set_billing_state( $state ) { |
||
857 | |||
858 | /** |
||
859 | * Sets customer postcode. |
||
860 | * @param mixed $postcode |
||
861 | * @throws WC_Data_Exception |
||
862 | */ |
||
863 | public function set_billing_postcode( $postcode ) { |
||
866 | |||
867 | /** |
||
868 | * Sets customer city. |
||
869 | * @param mixed $city |
||
870 | * @throws WC_Data_Exception |
||
871 | */ |
||
872 | public function set_billing_city( $city ) { |
||
875 | |||
876 | /** |
||
877 | * Set customer address. |
||
878 | * @param mixed $address |
||
879 | * @throws WC_Data_Exception |
||
880 | */ |
||
881 | public function set_billing_address( $address ) { |
||
884 | |||
885 | /** |
||
886 | * Set customer address. |
||
887 | * @param mixed $address |
||
888 | * @throws WC_Data_Exception |
||
889 | */ |
||
890 | public function set_billing_address_1( $address ) { |
||
893 | |||
894 | /** |
||
895 | * Set customer's second address. |
||
896 | * @param mixed $address |
||
897 | * @throws WC_Data_Exception |
||
898 | */ |
||
899 | public function set_billing_address_2( $address ) { |
||
902 | |||
903 | /** |
||
904 | * Sets customer shipping first name. |
||
905 | * @param string $first_name |
||
906 | * @throws WC_Data_Exception |
||
907 | */ |
||
908 | public function set_shipping_first_name( $first_name ) { |
||
911 | |||
912 | /** |
||
913 | * Sets customer shipping last name. |
||
914 | * @param string $last_name |
||
915 | * @throws WC_Data_Exception |
||
916 | */ |
||
917 | public function set_shipping_last_name( $last_name ) { |
||
920 | |||
921 | /** |
||
922 | * Sets customer shipping company. |
||
923 | * @param string $company. |
||
924 | * @throws WC_Data_Exception |
||
925 | */ |
||
926 | public function set_shipping_company( $company ) { |
||
929 | |||
930 | /** |
||
931 | * Set shipping country. |
||
932 | * @param string $country |
||
933 | * @throws WC_Data_Exception |
||
934 | */ |
||
935 | public function set_shipping_country( $country ) { |
||
938 | |||
939 | /** |
||
940 | * Set shipping state. |
||
941 | * @param string $state |
||
942 | * @throws WC_Data_Exception |
||
943 | */ |
||
944 | public function set_shipping_state( $state ) { |
||
947 | |||
948 | /** |
||
949 | * Set shipping postcode. |
||
950 | * @param string $postcode |
||
951 | * @throws WC_Data_Exception |
||
952 | */ |
||
953 | public function set_shipping_postcode( $postcode ) { |
||
956 | |||
957 | /** |
||
958 | * Sets shipping city. |
||
959 | * @param string $city |
||
960 | * @throws WC_Data_Exception |
||
961 | */ |
||
962 | public function set_shipping_city( $city ) { |
||
965 | |||
966 | /** |
||
967 | * Set shipping address. |
||
968 | * @param string $address |
||
969 | * @throws WC_Data_Exception |
||
970 | */ |
||
971 | public function set_shipping_address( $address ) { |
||
974 | |||
975 | /** |
||
976 | * Set customer shipping address. |
||
977 | * @param mixed $address |
||
978 | * @throws WC_Data_Exception |
||
979 | */ |
||
980 | public function set_shipping_address_1( $address ) { |
||
983 | |||
984 | /** |
||
985 | * Set second shipping address. |
||
986 | * @param string $address |
||
987 | * @throws WC_Data_Exception |
||
988 | */ |
||
989 | public function set_shipping_address_2( $address ) { |
||
992 | |||
993 | /** |
||
994 | * Set if the user a paying customer. |
||
995 | * @since 2.7.0 |
||
996 | * @param boolean $is_paying_customer |
||
997 | * @throws WC_Data_Exception |
||
998 | */ |
||
999 | function set_is_paying_customer( $is_paying_customer ) { |
||
1002 | |||
1003 | /** |
||
1004 | * Set if customer has tax exemption. |
||
1005 | * @param bool $is_vat_exempt |
||
1006 | */ |
||
1007 | public function set_is_vat_exempt( $is_vat_exempt ) { |
||
1010 | |||
1011 | /** |
||
1012 | * Calculated shipping? |
||
1013 | * @param boolean $calculated |
||
1014 | */ |
||
1015 | public function set_calculated_shipping( $calculated = true ) { |
||
1018 | |||
1019 | /* |
||
1020 | |-------------------------------------------------------------------------- |
||
1021 | | CRUD methods |
||
1022 | |-------------------------------------------------------------------------- |
||
1023 | | Methods which create, read, update and delete from the database. |
||
1024 | | |
||
1025 | | A save method is included for convenience (chooses update or create based |
||
1026 | | on if the order exists yet). |
||
1027 | */ |
||
1028 | |||
1029 | /** |
||
1030 | * Create a customer. |
||
1031 | * @since 2.7.0. |
||
1032 | */ |
||
1033 | public function create() { |
||
1069 | |||
1070 | /** |
||
1071 | * Callback which flattens post meta (gets the first value). |
||
1072 | * @param array $value |
||
1073 | * @return mixed |
||
1074 | */ |
||
1075 | private function flatten_post_meta( $value ) { |
||
1078 | |||
1079 | /** |
||
1080 | * Read a customer from the database. |
||
1081 | * @since 2.7.0 |
||
1082 | * @param integer $id |
||
1083 | */ |
||
1084 | public function read( $id ) { |
||
1111 | |||
1112 | /** |
||
1113 | * Update a customer. |
||
1114 | * @since 2.7.0 |
||
1115 | */ |
||
1116 | public function update() { |
||
1117 | wp_update_user( array( 'ID' => $this->get_id(), 'user_email' => $this->get_email() ) ); |
||
1118 | // Only update password if a new one was set with set_password |
||
1119 | if ( ! empty( $this->_password ) ) { |
||
1120 | wp_update_user( array( 'ID' => $this->get_id(), 'user_pass' => $this->_password ) ); |
||
1121 | $this->_password = ''; |
||
1122 | } |
||
1123 | |||
1124 | update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() ); |
||
1125 | update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() ); |
||
1126 | update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() ); |
||
1127 | update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() ); |
||
1128 | update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() ); |
||
1129 | update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() ); |
||
1130 | update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() ); |
||
1131 | update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() ); |
||
1132 | update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() ); |
||
1133 | update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() ); |
||
1134 | update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() ); |
||
1135 | update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() ); |
||
1136 | update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() ); |
||
1137 | update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() ); |
||
1138 | update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() ); |
||
1139 | update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() ); |
||
1140 | update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() ); |
||
1141 | update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() ); |
||
1142 | update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() ); |
||
1143 | update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() ); |
||
1144 | update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() ); |
||
1145 | update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() ); |
||
1146 | update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() ); |
||
1147 | update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() ); |
||
1148 | update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() ); |
||
1149 | update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() ); |
||
1150 | wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) ); |
||
1151 | $this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) ); |
||
1152 | $this->save_meta_data(); |
||
1153 | } |
||
1154 | |||
1155 | /** |
||
1156 | * Delete a customer. |
||
1157 | * @since 2.7.0 |
||
1158 | */ |
||
1159 | public function delete() { |
||
1165 | |||
1166 | /** |
||
1167 | * Delete a customer and reassign posts.. |
||
1168 | * |
||
1169 | * @param int $reassign Reassign posts and links to new User ID. |
||
1170 | * @since 2.7.0 |
||
1171 | */ |
||
1172 | public function delete_and_reassign( $reassign = null ) { |
||
1178 | |||
1179 | /** |
||
1180 | * Save data. Create when creating a new user/class, update when editing |
||
1181 | * an existing user, and save session when working on a logged out guest |
||
1182 | * session. |
||
1183 | * @since 2.7.0 |
||
1184 | */ |
||
1185 | public function save() { |
||
1194 | |||
1195 | /** |
||
1196 | * Saves data to the session only (does not overwrite DB values). |
||
1197 | * @since 2.7.0 |
||
1198 | */ |
||
1199 | public function save_to_session() { |
||
1212 | |||
1213 | /** |
||
1214 | * Callback to remove unwanted meta data. |
||
1215 | * |
||
1216 | * @param object $meta |
||
1217 | * @return bool |
||
1218 | */ |
||
1219 | protected function exclude_internal_meta_keys( $meta ) { |
||
1227 | } |
||
1228 |
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.