PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.11.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.11.0
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / Customer.php
Customer.php
234 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Models;
4
5 use SureCart\Models\Traits\HasBillingAddress;
6 use SureCart\Models\Traits\HasDates;
7 use SureCart\Models\Traits\HasPurchases;
8 use SureCart\Models\Traits\HasShippingAddress;
9 use SureCart\Support\TimeDate;
10
11 /**
12 * Price model
13 */
14 class Customer extends Model {
15 use HasPurchases, HasShippingAddress, HasBillingAddress, HasDates;
16
17 /**
18 * Rest API endpoint
19 *
20 * @var string
21 */
22 protected $endpoint = 'customers';
23
24 /**
25 * Object name
26 *
27 * @var string
28 */
29 protected $object_name = 'customer';
30
31 /**
32 * Create a new model
33 *
34 * @param array $attributes Attributes to create.
35 * @param boolean $create_user Whether to create a corresponding WordPress user.
36 *
37 * @return $this|\WP_Error|false
38 */
39 protected function create( $attributes = [], $create_user = true ) {
40 /** @var Customer|\WP_Error $customer */
41 $customer = parent::create( $attributes );
42 if ( $this->isError( $customer ) ) {
43 return $customer;
44 }
45
46 // maybe create a WordPress user.
47 if ( $create_user ) {
48 // Find the user by email.
49 $user = User::getUserBy( 'email', $this->attributes['email'] );
50
51 // if no user, create one.
52 if ( empty( $user ) ) {
53 $user = User::create(
54 [
55 'user_name' => $this->attributes['name'] ?? null,
56 'first_name' => $this->attributes['first_name'] ?? null,
57 'last_name' => $this->attributes['last_name'] ?? null,
58 'user_email' => $this->attributes['email'],
59 ]
60 );
61 }
62
63 // handle error creating user.
64 if ( is_wp_error( $user ) ) {
65 return $user;
66 }
67
68 $linked = $user->setCustomerId( $this->attributes['id'], $this->live_mode ? 'live' : 'test' );
69
70 if ( is_wp_error( $linked ) ) {
71 return $linked;
72 }
73 }
74
75 return $this;
76 }
77
78 /**
79 * Delete the model.
80 *
81 * @param int $id Customer ID.
82 *
83 * @return $this|\WP_Error|false
84 */
85 protected function delete( $id = 0 ) {
86 $customer = self::find( $id );
87 $deleted = parent::delete( $id );
88
89 if ( ! is_wp_error( $deleted ) ) {
90 $user = User::findByCustomerId( $id );
91
92 if ( $user ) {
93 if ( is_wp_error( $customer ) ) {
94 return $customer;
95 }
96
97 $user->removeCustomerId( $customer->live_mode ? 'live' : 'test' );
98 }
99 }
100
101 return $deleted;
102 }
103
104 /**
105 * Expose media for a customer
106 *
107 * @param string $media_id The media id.
108 *
109 * @return \SureCart\Models\Media|false;
110 */
111 protected function exposeMedia( $media_id ) {
112 if ( empty( $this->attributes['id'] ) || empty( $media_id ) ) {
113 return false;
114 }
115
116 if ( $this->fireModelEvent( 'exposing_media' ) === false ) {
117 return false;
118 }
119
120 $media = \SureCart::request(
121 $this->endpoint . '/' . $this->attributes['id'] . '/expose/' . $media_id,
122 [
123 'method' => 'GET',
124 'query' => $this->query,
125 ]
126 );
127
128 if ( $this->isError( $media ) ) {
129 return $media;
130 }
131
132 $this->fireModelEvent( 'exposed_media' );
133
134 return new Media( $media );
135 }
136
137 /**
138 * Get a customer by their email address
139 *
140 * @param string $email Email address.
141 * @return this
142 */
143 protected function byEmail( $email ) {
144 return $this->where(
145 [
146 'email' => $email,
147 ]
148 )->first();
149 }
150
151 /**
152 * Get the customer's user.
153 *
154 * @return \SureCart\Models\User|false
155 */
156 public function getUser() {
157 return User::findByCustomerId( $this->id );
158 }
159
160 /**
161 * Create the user from the customer.
162 *
163 * @return \SureCart\Models\User|\WP_Error
164 */
165 public function createUser() {
166 if ( empty( $this->id ) && empty( $this->email ) ) {
167 return new \WP_Error( 'no_customer_id_or_email', __( 'No customer ID or email provided.', 'surecart' ) );
168 }
169
170 // if no user, create one with a password if provided.
171 $created = User::create(
172 [
173 'user_name' => $this->name ?? $this->checkout->name ?? null,
174 'user_email' => $this->email,
175 'first_name' => $this->first_name ?? null,
176 'last_name' => $this->last_name ?? null,
177 'phone' => $this->phone ?? null,
178 ]
179 );
180
181 if ( is_wp_error( $created ) ) {
182 return $created;
183 }
184
185 $created->setCustomerId( $this->id, ! empty( $this->live_mode ) ? 'live' : 'test' );
186
187 return $created;
188 }
189
190 /**
191 * Maybe also return the user when the id is set.
192 *
193 * @param string $value The user id.
194 * @return void
195 */
196 public function setIdAttribute( $value ) {
197 $this->attributes['id'] = $value;
198 if ( ! empty( $this->query['expand'] ) && in_array( 'user', $this->query['expand'] ) ) {
199 $this->attributes['user'] = $this->getUser();
200 }
201 }
202
203 /**
204 * Get the billing address attribute
205 *
206 * @return array|null The billing address.
207 */
208 public function getBillingAddressDisplayAttribute() {
209 if ( $this->billing_matches_shipping ) {
210 return $this->shipping_address;
211 }
212
213 return $this->attributes['billing_address'] ?? null;
214 }
215
216 /**
217 * Get the affiliation expires at date.
218 *
219 * @return string
220 */
221 public function getAffiliationExpiresAtDateAttribute() {
222 return ! empty( $this->affiliation_expires_at ) ? TimeDate::formatDate( $this->affiliation_expires_at ) : '';
223 }
224
225 /**
226 * Get the affiliation expires at date and time.
227 *
228 * @return string
229 */
230 public function getAffiliationExpiresAtDateTimeAttribute() {
231 return ! empty( $this->affiliation_expires_at ) ? TimeDate::formatDateAndTime( $this->affiliation_expires_at ) : '';
232 }
233 }
234