PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.20
Timetics – Appointment Booking Calendar & Scheduling v1.0.20
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / customers / customer.php

customer.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.20, at core/customers/customer.php

419 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cutomer Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Customers;
8
9 use Timetics\Core\Bookings\Booking;
10
11 /**
12 * Customer class
13 *
14 * @since 1.0.0
15 */
16 class Customer {
17 /**
18 * Store customer id
19 *
20 * @var integer
21 */
22 protected $id;
23
24 /**
25 * Store meta prefix
26 *
27 * @var string
28 */
29 protected $meta_prefix = '_timetics_customer_';
30
31 /**
32 * Store error
33 *
34 * @var Object
35 */
36 public $error;
37
38 /**
39 * Customer data
40 *
41 * @var array
42 */
43 protected $data = [
44 'image' => '',
45 'first_name' => '',
46 'last_name' => '',
47 'email' => '',
48 'phone' => '',
49 'password' => '',
50 ];
51
52 /**
53 * Customer constructor
54 *
55 * @return void
56 */
57 public function __construct( $customer = 0 ) {
58 if ( $customer instanceof self ) {
59 $this->set_id( $customer->get_id() );
60 } elseif ( ! empty( $customer->ID ) ) {
61 $this->set_id( $customer->ID );
62 } elseif ( is_numeric( $customer ) && $customer > 0 ) {
63 $this->set_id( $customer );
64 }
65 }
66
67 /**
68 * Get customer id
69 *
70 * @return integer
71 */
72 public function get_id() {
73 return $this->id;
74 }
75
76 /**
77 * Get first name
78 *
79 * @return string
80 */
81 public function get_first_name() {
82 return get_user_meta( $this->id, 'first_name', true );
83 }
84
85 /**
86 * Get last name
87 *
88 * @return string
89 */
90 public function get_last_name() {
91 return get_user_meta( $this->id, 'last_name', true );
92 }
93
94 /**
95 * Get customer display name
96 *
97 * @return string
98 */
99 public function get_display_name() {
100 return $this->get_first_name() . ' ' . $this->get_last_name();
101 }
102
103 /**
104 * Get email
105 *
106 * @return string
107 */
108 public function get_email() {
109 $customer = get_userdata( $this->id );
110
111 if ( $customer ) {
112 return $customer->user_email;
113 }
114
115 return '';
116 }
117
118 /**
119 * Get phone
120 *
121 * @return string
122 */
123 public function get_phone() {
124 return $this->get_prop( 'phone' );
125 }
126
127 /**
128 * Get customer image
129 *
130 * @return string
131 */
132 public function get_image() {
133 $image = $this->get_prop( 'image' );
134
135 if ( ! $image ) {
136 return get_avatar_url( $this->id );
137 }
138
139 return wp_get_attachment_image_url( $image );
140 }
141
142 /**
143 * Get password
144 *
145 * @return string
146 */
147 public function get_password() {
148 $user = get_user_by( 'id', $this->id );
149 return $user->user_pass;
150 }
151
152 /**
153 * Get prop
154 *
155 * @param string
156 *
157 * @return string
158 */
159 public function get_prop( $prop = '' ) {
160 return $this->get_metadata( $prop );
161 }
162
163 /**
164 * Get customer metadata
165 *
166 * @param string $prop
167 *
168 * @return string
169 */
170 public function get_metadata( $prop = '' ) {
171 if( $prop == 'first_name' || $prop == 'last_name' ) {
172 $meta_key = $prop;
173 } else {
174 $meta_key = $this->meta_prefix . $prop;
175 }
176
177 return get_user_meta( $this->id, $meta_key, true );
178
179 }
180
181 // Setters.
182 /**
183 * Set customer id
184 *
185 * @param integer $id
186 *
187 * @return void
188 */
189 public function set_id( $id ) {
190 $this->id = $id;
191 }
192
193 /**
194 * Set props
195 *
196 * @param array $args Customer Data
197 *
198 * @return void
199 */
200 public function set_props( $args = [] ) {
201 $this->data = wp_parse_args( $args, $this->data );
202 }
203
204 /**
205 * Update meta data
206 *
207 * @return void
208 */
209 public function save_metadata() {
210 foreach ( $this->data as $key => $value ) {
211 // Prepare meta key.
212 if( $key == 'first_name' || $key == 'last_name' ) {
213 $meta_key = $key;
214 } else {
215 $meta_key = $this->meta_prefix . $key;
216 }
217
218 // Update appointment meta data.
219 update_user_meta( $this->id, $meta_key, $value );
220 }
221 }
222
223 /**
224 * Check user is valid staff
225 *
226 * @return bool
227 */
228 public function is_customer() {
229 $user = get_userdata( $this->id );
230
231 if ( $user ) {
232 return in_array( 'timetics-customer', $user->roles, true );
233 }
234
235 return true;
236 }
237
238 /**
239 * Save customer
240 *
241 * @return void
242 */
243 public function save() {
244 $args = [
245 'first_name' => $this->data['first_name'],
246 'last_name' => $this->data['last_name'],
247 'user_login' => $this->data['email'],
248 'user_email' => $this->data['email'],
249 ];
250
251 $user_data = get_user_by( 'email', $this->data['email'] );
252
253 if ( $user_data && ! in_array( 'timetics-customer', $user_data->roles, true ) ) {
254 $user_data->add_role( 'timetics-customer' );
255
256 return $user_data->ID;
257 }
258
259 if ( $this->id ) {
260 $args['ID'] = $this->id;
261 $args['user_pass'] = $this->data['password'];
262 $customer_id = wp_update_user( $args );
263 } else {
264 $args['role'] = 'timetics-customer';
265 $args['user_pass'] = wp_generate_password();
266 $customer_id = wp_insert_user( $args );
267 }
268
269 if ( is_wp_error( $customer_id ) ) {
270 $this->error = $customer_id;
271 return;
272 }
273
274 $this->set_id( $customer_id );
275 $this->save_metadata();
276 }
277
278 /**
279 * Delete customer
280 *
281 * @return bool
282 */
283 public function delete() {
284 require_once ABSPATH . 'wp-admin/includes/user.php';
285 $user = get_userdata( $this->id );
286
287 // Get all bookings for this customer.
288 $bookings = $this->get_bookings();
289
290 // Delete all bookings for this customer.
291 foreach ( $bookings as $booking ) {
292 $booking = new Booking( $booking );
293 $booking->delete();
294 }
295
296 if ( count( $user->roles ) > 1 ) {
297 $user->remove_role('timetics-customer');
298
299 return true;
300 }
301
302 return wp_delete_user( $this->id );
303 }
304
305 /**
306 * Make customer using email
307 *
308 * @param array Customer data
309 *
310 * @return Customer
311 */
312 public function make( $args = [] ) {
313 $defaults = [
314 'first_name' => '',
315 'last_name' => '',
316 'email' => '',
317 'phone' => '',
318 'password' => '',
319 ];
320
321 $args = wp_parse_args( $args, $defaults );
322
323 if ( email_exists( $args['email'] ) ) {
324 $user = get_user_by( 'email', $args['email'] );
325
326 if ( $user && ! in_array( 'timetics-customer', $user->roles, true ) ) {
327 $user->add_role( 'timetics-customer' );
328 $this->set_id( $user->ID );
329
330 return $user->ID;
331 }
332
333 $this->set_id( $user->ID );
334 $this->set_props( $args );
335 $this->save_metadata( $args );
336
337 } else {
338 $this->set_props( $args );
339 $this->save();
340 }
341 }
342
343 /**
344 * Get total booking for a customer
345 *
346 * @return integer
347 */
348 public function get_total_booking() {
349 $bookings = $this->booking_query();
350
351 return $bookings->found_posts;
352 }
353
354 /**
355 * Get all bookings by customer
356 *
357 * @return array
358 */
359 public function get_bookings() {
360 $bookings = $this->booking_query();
361 $data = [];
362
363 if ( ! empty( $bookings->posts ) ) {
364 foreach( $bookings->posts as $booking ) {
365 $object = new Booking( $booking->ID );
366 $data[] = $object->to_array();
367 }
368 }
369
370 return $data;
371 }
372
373 /**
374 * Get query for bookings
375 *
376 * @return WP_Query
377 */
378 private function booking_query() {
379 $default_booking_status = timetics_get_option( 'default_booking_status', 'approved' );
380
381 $query = new \WP_Query(
382 [
383 'post_type' => 'timetics-booking',
384 'post_status' => $default_booking_status,
385 // @codingStandardsIgnoreStart
386 'meta_key' => '_tt_booking_customer',
387 'meta_value' => $this->id,
388 // @codingStandardsIgnoreEnd
389 ]
390 );
391
392 return $query;
393 }
394
395 /**
396 * Get all customer
397 *
398 * @param array $args Customer
399 *
400 * @return array
401 */
402 public static function all( $args = [] ) {
403 $defaults = [
404 'role' => 'timetics-customer',
405 'number' => 20,
406 'paged' => 1,
407 ];
408
409 $args = wp_parse_args( $args, $defaults );
410
411 $users = new \WP_User_Query( $args );
412
413 return [
414 'total' => $users->get_total(),
415 'items' => $users->get_results(),
416 ];
417 }
418 }
419