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