PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.13
Timetics – Appointment Booking Calendar & Scheduling v1.0.13
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.13, at core/customers/customer.php

404 lines 8.2 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 if ( $this->id ) {
252 $args['ID'] = $this->id;
253 $args['user_pass'] = $this->data['password'];
254 $customer_id = wp_update_user( $args );
255 } else {
256 $args['role'] = 'timetics-customer';
257 $args['user_pass'] = wp_generate_password();
258 $customer_id = wp_insert_user( $args );
259 }
260
261 if ( is_wp_error( $customer_id ) ) {
262 $this->error = $customer_id;
263 return;
264 }
265
266 $this->set_id( $customer_id );
267 $this->save_metadata();
268 }
269
270 /**
271 * Delete customer
272 *
273 * @return bool
274 */
275 public function delete() {
276 require_once ABSPATH . 'wp-admin/includes/user.php';
277 $user = get_userdata( $this->id );
278
279 // Get all bookings for this customer.
280 $bookings = $this->get_bookings();
281
282 // Delete all bookings for this customer.
283 foreach ( $bookings as $booking ) {
284 $booking = new Booking( $booking );
285 $booking->delete();
286 }
287
288 if ( count( $user->roles ) > 1 ) {
289 $user->remove_role('timetics-customer');
290
291 return true;
292 }
293
294 return wp_delete_user( $this->id );
295 }
296
297 /**
298 * Make customer using email
299 *
300 * @param array Customer data
301 *
302 * @return Customer
303 */
304 public function make( $args = [] ) {
305 $defaults = [
306 'first_name' => '',
307 'last_name' => '',
308 'email' => '',
309 'phone' => '',
310 'password' => '',
311 ];
312
313 $args = wp_parse_args( $args, $defaults );
314
315 if ( email_exists( $args['email'] ) ) {
316 $user = get_user_by( 'email', $args['email'] );
317
318 if ( ! $this->is_customer() ) {
319 $user->add_role( 'timetics-customer' );
320 }
321 $this->set_id( $user->ID );
322 $this->set_props( $args );
323 $this->save_metadata();
324 } else {
325 $this->set_props( $args );
326 $this->save();
327 }
328 }
329
330 /**
331 * Get total booking for a customer
332 *
333 * @return integer
334 */
335 public function get_total_booking() {
336 $bookings = $this->booking_query();
337
338 return $bookings->found_posts;
339 }
340
341 /**
342 * Get all bookings by customer
343 *
344 * @return array
345 */
346 public function get_bookings() {
347 $bookings = $this->booking_query();
348 $data = [];
349
350 if ( ! empty( $bookings->posts ) ) {
351 foreach( $bookings->posts as $booking ) {
352 $object = new Booking( $booking->ID );
353 $data[] = $object->to_array();
354 }
355 }
356
357 return $data;
358 }
359
360 /**
361 * Get query for bookings
362 *
363 * @return WP_Query
364 */
365 private function booking_query() {
366 $query = new \WP_Query(
367 [
368 'post_type' => 'timetics-booking',
369 'post_status' => 'completed',
370 // @codingStandardsIgnoreStart
371 'meta_key' => '_tt_booking_customer',
372 'meta_value' => $this->id,
373 // @codingStandardsIgnoreEnd
374 ]
375 );
376
377 return $query;
378 }
379
380 /**
381 * Get all customer
382 *
383 * @param array $args Customer
384 *
385 * @return array
386 */
387 public static function all( $args = [] ) {
388 $defaults = [
389 'role' => 'timetics-customer',
390 'number' => 20,
391 'paged' => 1,
392 ];
393
394 $args = wp_parse_args( $args, $defaults );
395
396 $users = new \WP_User_Query( $args );
397
398 return [
399 'total' => $users->get_total(),
400 'items' => $users->get_results(),
401 ];
402 }
403 }
404