PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.0
Timetics – Appointment Booking Calendar & Scheduling v1.0.0
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 / bookings / booking.php

booking.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.0, at core/bookings/booking.php

629 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Booking Class
4 *
5 * @package Timetics
6 */
7
8 namespace Timetics\Core\Bookings;
9
10 use Timetics\Core\Appointments\Appointment;
11 use Timetics\Core\Customers\Customer;
12 use Timetics\Core\Integrations\Google\Service\Calendar;
13 use Timetics\Core\Staffs\Staff;
14 use WP_Query;
15
16 /**
17 * Class Booking
18 */
19 class Booking {
20 /**
21 * Store booking post type
22 *
23 * @var string
24 */
25 protected $pos_type = 'timetics-booking';
26
27 /**
28 * Store booking meta prefix
29 *
30 * @var string
31 */
32 protected $meta_prefix = '_tt_booking_';
33
34 /**
35 * Store booking id
36 *
37 * @var string
38 */
39 protected $id;
40
41 /**
42 * Store appointment object
43 *
44 * @var Object
45 */
46 protected $appointment;
47
48 /**
49 * Store booking data
50 *
51 * @var array
52 */
53 protected $data = [
54 'post_status' => '',
55 'customer' => '',
56 'appointment' => '',
57 'staff' => '',
58 'location' => '',
59 'location_type' => '',
60 'payment_method' => '',
61 'payment_status' => '',
62 'payment_details' => '',
63 'order_total' => '',
64 'start_date' => '',
65 'end_date' => '',
66 'start_time' => '',
67 'end_time' => '',
68 'calendar_event' => '',
69 'random_id' => '',
70 ];
71
72 /**
73 * Booking Constructor
74 *
75 * @return void
76 */
77 public function __construct( $booking = 0 ) {
78 if ( $booking instanceof self ) {
79 $this->set_id( $booking->get_id() );
80 } elseif ( ! empty( $booking->ID ) ) {
81 $this->set_id( $booking->ID );
82 } elseif ( is_numeric( $booking ) && $booking > 0 ) {
83 $this->set_id( $booking );
84 }
85 }
86
87 /**
88 * Get id
89 *
90 * @return integer
91 */
92 public function get_id() {
93 return $this->id;
94 }
95
96 /**
97 * Get customer id
98 *
99 * @return integer
100 */
101 public function get_customer_id() {
102 return $this->get_prop( 'customer' );
103 }
104
105 /**
106 * Get staff for the current booking
107 *
108 * @return integer
109 */
110 public function get_staff_id() {
111 return $this->get_prop( 'staff' );
112 }
113
114 /**
115 * Get customer
116 *
117 * @return User Object
118 */
119 public function get_customer() {
120 return get_userdata( $this->get_customer_id() );
121 }
122
123 /**
124 * Get billing first name
125 *
126 * @return string
127 */
128 public function get_billing_first_name() {
129 return $this->get_prop( 'first_name' );
130 }
131
132 /**
133 * Get location
134 *
135 * @return string
136 */
137 public function get_location() {
138 return $this->get_prop( 'location' );
139 }
140
141 /**
142 * Get location type
143 *
144 * @return string
145 */
146 public function get_location_type() {
147 return $this->get_prop( 'location_type' );
148 }
149
150 /**
151 * Get last name
152 *
153 * @return string
154 */
155 public function get_billing_last_name() {
156 return $this->get_prop( 'last_name' );
157 }
158
159 /**
160 * Get email
161 *
162 * @return string
163 */
164 public function get_billing_email() {
165 return $this->get_prop( 'email' );
166 }
167
168 /**
169 * Get billing phone
170 *
171 * @return string
172 */
173 public function get_billing_phone() {
174 return $this->get_prop( 'phone' );
175 }
176
177 /**
178 * Get billing address
179 *
180 * @param string $adress_type Booking billing address
181 *
182 * @return string
183 */
184 public function get_billing_address( $adress_type = 'address_1' ) {
185 return $this->get_prop( $adress_type );
186 }
187
188 /**
189 * Get billing city
190 *
191 * @return string
192 */
193 public function get_billing_city() {
194 return $this->get_prop( 'city' );
195 }
196
197 /**
198 * Get billing state
199 *
200 * @return string
201 */
202 public function get_billing_state() {
203 return $this->get_prop( 'state' );
204 }
205
206 /**
207 * Get billing post code
208 *
209 * @return string
210 */
211 public function get_billing_post_code() {
212 return $this->get_prop( 'post_code' );
213 }
214
215 /**
216 * Get billing
217 *
218 * @return string
219 */
220 public function get_billing_country() {
221 return $this->get_prop( 'country' );
222 }
223
224 /**
225 * Get payment method
226 *
227 * @return string
228 */
229 public function get_payment_method() {
230 return $this->get_prop( 'payment_method' );
231 }
232
233 /**
234 * Get appointment
235 *
236 * @return Appointment
237 */
238 public function get_appointment() {
239 return $this->get_prop( 'appointment' );
240 }
241
242 /**
243 * Get calendar event
244 *
245 * @return array
246 */
247 public function get_event() {
248 return $this->get_prop( 'calendar_event' );
249 }
250
251 /**
252 * Get post status
253 *
254 * @return string
255 */
256 public function get_status() {
257 return get_post( $this->id )->post_status;
258 }
259
260 /**
261 * Get total
262 *
263 * @return integer
264 */
265 public function get_total() {
266 return $this->get_prop( 'order_total' );
267 }
268
269 /**
270 * Get start date
271 *
272 * @return string
273 */
274 public function get_start_date() {
275 return $this->get_prop( 'start_date' );
276 }
277 /**
278 * Get start date
279 *
280 * @return string
281 */
282 public function get_date() {
283 return $this->get_prop( 'date' );
284 }
285
286 /**
287 * Get end date
288 *
289 * @return string
290 */
291 public function get_end_date() {
292 return $this->get_prop( 'end_date' );
293 }
294
295 /**
296 * Get start time
297 *
298 * @return string
299 */
300 public function get_start_time() {
301 return $this->get_prop( 'start_time' );
302 }
303
304 /**
305 * Get end time
306 *
307 * @return string
308 */
309 public function get_end_time() {
310 return $this->get_prop( 'end_time' );
311 }
312
313 /**
314 * Get random id for the current booking
315 *
316 * @return string
317 */
318 public function get_random_id() {
319 return $this->get_prop( 'random_id' );
320 }
321
322 /**
323 * Get booking data
324 *
325 * @param string $prop
326 *
327 * @return mixed
328 */
329 private function get_prop( $prop = '' ) {
330 return $this->get_metadata( $prop );
331 }
332
333 /**
334 * Get metadata
335 *
336 * @param string $prop
337 *
338 * @return mixed
339 */
340 private function get_metadata( $prop = '' ) {
341 $meta_key = $this->meta_prefix . $prop;
342
343 return get_post_meta( $this->id, $meta_key, true );
344 }
345
346 /**
347 * Generate random id
348 *
349 * @return string
350 */
351 private function generate_randon_id() {
352 return chr(rand(ord('A'), ord('Z'))) . rand(575639, 575639 + 400);
353 }
354
355 // Setter.
356
357 /**
358 * Set booking id
359 *
360 * @param integer $id
361 *
362 * @return void
363 */
364 public function set_id( $id ) {
365 $this->id = $id;
366 }
367
368 /**
369 * Set data
370 *
371 * @param array $args Booking Args
372 *
373 * @return void
374 */
375 public function set_props( $args = [] ) {
376 $this->data = wp_parse_args( $args, $this->data );
377 $this->appointment = new Appointment( $this->data['appointment'] );
378 }
379
380 /**
381 * Save metada for booking
382 *
383 * @return void
384 */
385 public function save_metadata( $args = [] ) {
386 foreach ( $args as $key => $value ) {
387
388 // If a key not exists on data it will skip to save the data.
389 if ( ! array_key_exists( $key, $this->data ) ) {
390 continue;
391 }
392
393 // Prepare meta key.
394 $meta_key = $this->meta_prefix . $key;
395
396 // Update appointment meta data.
397 update_post_meta( $this->id, $meta_key, $value );
398 }
399 }
400
401 /**
402 * Save appointment
403 *
404 * @return void
405 */
406 public function save() {
407 $args = [
408 'post_title' => $this->appointment->get_name(),
409 'post_type' => $this->pos_type,
410 'post_status' => $this->data['post_status'],
411 'post_author' => get_current_user_id(),
412 ];
413
414 if ( ! empty( $this->id ) ) {
415 $args['ID'] = $this->id;
416 }
417
418 // Insert or Update appointment.
419 $booking_id = wp_insert_post( $args );
420 $this->data['random_id'] = $this->generate_randon_id();
421
422 if ( ! is_wp_error( $booking_id ) ) {
423 $this->set_id( $booking_id );
424 $this->save_metadata( $this->data );
425 }
426 }
427
428 /**
429 * Update booking
430 *
431 * @param array $args
432 *
433 * @return bool
434 */
435 public function update( $args = [] ) {
436 $post = get_post( $this->id )->to_array();
437 $args = wp_parse_args( $args, $post );
438
439 $updated = wp_update_post( $args );
440
441 if ( ! is_wp_error( $updated ) ) {
442 $this->save_metadata( $args );
443 }
444
445 return $updated;
446 }
447
448 /**
449 * Delete booking
450 *
451 * @return bool | WP_Error
452 */
453 public function delete() {
454 return wp_delete_post( $this->id, true );
455 }
456
457 /**
458 * Check is a booking or not
459 *
460 * @return bool
461 */
462 public function is_booking() {
463 $post = get_post( $this->id );
464
465 if ( $post && $this->pos_type === $post->post_type ) {
466 return true;
467 }
468
469 return false;
470 }
471
472 /**
473 * Get all bookings
474 *
475 * @param array $args Bookings args
476 *
477 * @return array
478 */
479 public static function all( $args = [] ) {
480
481 $status = [
482 'approved',
483 'pending',
484 'cancel',
485 'completed',
486 ];
487 $defaults = [
488 'post_type' => 'timetics-booking',
489 'posts_per_page' => 20,
490 'post_status' => 'any',
491 'paged' => 1,
492 ];
493
494 $args = wp_parse_args( $args, $defaults );
495
496 if ( $args['meeting'] ) {
497 // @codingStandardsIgnoreStart
498 $args['meta_query'] = [
499 [
500 'key' => '_tt_booking_appointment',
501 'value' => $args['meeting'],
502 'compare' => '=',
503 ],
504 ];
505 // @codingStandardsIgnoreEnd
506 }
507
508 if ( $args['staff'] ) {
509 // @codingStandardsIgnoreStart
510 $args['meta_query'] = [
511 [
512 'key' => '_tt_booking_staff',
513 'value' => $args['staff'],
514 'compare' => '=',
515 ],
516 ];
517 // @codingStandardsIgnoreEnd
518 }
519
520 $post = new WP_Query( $args );
521
522 return [
523 'total' => $post->found_posts,
524 'items' => $post->posts,
525 ];
526 }
527
528 /**
529 * Create event after creating a booking
530 *
531 * @return bool
532 */
533 public function create_event() {
534
535 if ( ! timetics_google_setup() ) {
536 return;
537 }
538
539 $calendar = new Calendar();
540 $event_data = $this->prepare_event();
541 $event = $calendar->create_event( $event_data );
542
543 update_post_meta( $this->id, $this->meta_prefix . 'calendar_event', $event );
544 }
545
546 /**
547 * Update calendar event
548 *
549 * @return bool
550 */
551 public function update_event() {
552 if ( ! timetics_google_setup() ) {
553 return;
554 }
555
556 $calendar = new Calendar();
557 $event_data = $this->prepare_event();
558 $calendar_event = $this->get_event();
559
560 if ( ! empty( $calendar_event['id'] ) ) {
561 // Update calendar event.
562 $event = $calendar->update_event( $calendar_event['id'], $event_data );
563
564 // update calendar event data.
565 update_post_meta( $this->id, $this->meta_prefix . 'calendar_event', $event );
566 }
567 }
568
569 /**
570 * Delete calendar event
571 *
572 * @return
573 */
574 public function delete_event() {
575 if ( ! timetics_google_setup() ) {
576 return;
577 }
578
579 $calendar = new Calendar();
580 $calendar_event = $this->get_event();
581 $access_token = timetics_get_google_access_token( $this->get_staff_id() );
582
583 if ( ! empty( $calendar_event['id'] ) ) {
584 // Update calendar event.
585 $event = $calendar->delete_event( $calendar_event['id'], $access_token );
586
587 // update calendar event data.
588 update_post_meta( $this->id, $this->meta_prefix . 'calendar_event', $event );
589 }
590 }
591
592 /**
593 * Prepare event data
594 *
595 * @return array
596 */
597 private function prepare_event() {
598 $meeting_id = $this->get_appointment();
599 $staff_id = $this->get_staff_id();
600 $customer_id = $this->get_customer_id();
601 $location_type = $this->get_location_type();
602
603 $meeting = new Appointment( $meeting_id );
604 $staff = new Staff( $staff_id );
605 $customer = new Customer( $customer_id );
606
607 $event = [
608 'summary' => $meeting->get_name(),
609 'description' => $meeting->get_description(),
610 'start' => [
611 'date' => $this->get_start_date(),
612 'time' => $this->get_start_time(),
613 ],
614 'end' => [
615 'date' => $this->get_end_date(),
616 'time' => $this->get_end_time(),
617 ],
618 'attendees' => [
619 ['email' => $staff->get_email()],
620 ['email' => $customer->get_email()],
621 ],
622 'google_meet' => 'google-meet' === $location_type,
623 'access_token' => timetics_get_google_access_token( $staff_id ),
624 ];
625
626 return $event;
627 }
628 }
629