1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Convert data in the customer schema format to a product object. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce/RestApi |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4\Requests; |
9
|
|
|
|
10
|
|
|
defined( 'ABSPATH' ) || exit; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* CustomerRequest class. |
14
|
|
|
*/ |
15
|
|
|
class CustomerRequest extends AbstractObjectRequest { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Convert request to object. |
19
|
|
|
* |
20
|
|
|
* @throws \WC_REST_Exception Will throw an exception if the customer is invalid. |
21
|
|
|
* @return \WC_Customer |
22
|
|
|
*/ |
23
|
|
|
public function prepare_object() { |
24
|
|
|
$id = (int) $this->get_param( 'id', 0 ); |
25
|
|
|
$object = new \WC_Customer( $id ); |
26
|
|
|
|
27
|
|
|
if ( $id !== $object->get_id() ) { |
28
|
|
|
throw new \WC_REST_Exception( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), 400 ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->set_props( $object ); |
32
|
|
|
$this->set_meta_data( $object ); |
33
|
|
|
|
34
|
|
|
return $object; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set order props. |
39
|
|
|
* |
40
|
|
|
* @throws \WC_REST_Exception Will throw an exception if the customer is invalid. |
41
|
|
|
* @param \WC_Customer $object Order object reference. |
42
|
|
|
*/ |
43
|
|
|
protected function set_props( &$object ) { |
44
|
|
|
$props = [ |
45
|
|
|
'username', |
46
|
|
|
'password', |
47
|
|
|
'email', |
48
|
|
|
'first_name', |
49
|
|
|
'last_name', |
50
|
|
|
'billing', |
51
|
|
|
'shipping', |
52
|
|
|
'billing', |
53
|
|
|
'shipping', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$request_props = array_intersect_key( $this->request, array_flip( $props ) ); |
57
|
|
|
$prop_values = []; |
58
|
|
|
|
59
|
|
|
foreach ( $request_props as $prop => $value ) { |
60
|
|
|
switch ( $prop ) { |
61
|
|
|
case 'email': |
62
|
|
|
if ( email_exists( $value ) && $value !== $object->get_email() ) { |
63
|
|
|
throw new \WC_REST_Exception( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), 400 ); |
64
|
|
|
} |
65
|
|
|
$prop_values[ $prop ] = $value; |
66
|
|
|
break; |
67
|
|
|
case 'username': |
68
|
|
|
if ( $object->get_id() && $value !== $object->get_username() ) { |
69
|
|
|
throw new \WC_REST_Exception( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), 400 ); |
70
|
|
|
} |
71
|
|
|
$prop_values[ $prop ] = $value; |
72
|
|
|
break; |
73
|
|
|
case 'billing': |
74
|
|
|
case 'shipping': |
75
|
|
|
$address = $this->parse_address_field( $value, $object, $prop ); |
76
|
|
|
$prop_values = array_merge( $prop_values, $address ); |
77
|
|
|
break; |
78
|
|
|
default: |
79
|
|
|
$prop_values[ $prop ] = $value; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ( $prop_values as $prop => $value ) { |
84
|
|
|
$object->{"set_$prop"}( $value ); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Parse address data. |
90
|
|
|
* |
91
|
|
|
* @param array $data Posted data. |
92
|
|
|
* @param \WC_Customer $object Customer object. |
93
|
|
|
* @param string $type Address type. |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function parse_address_field( $data, $object, $type = 'billing' ) { |
97
|
|
|
$address = []; |
98
|
|
|
foreach ( $data as $key => $value ) { |
99
|
|
|
if ( is_callable( array( $object, "set_{$type}_{$key}" ) ) ) { |
100
|
|
|
$address[ "{$type}_{$key}" ] = $value; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return $address; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|