PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.10
Timetics – Appointment Booking Calendar & Scheduling v1.0.10
1.0.64 1.0.62 1.0.63 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 All 65 releases
timetics / core / bookings / booking.php

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

793 lines 17.8 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
55 'post_status' => '',
56 'customer' => '',
57 'appointment' => '',
58 'staff' => '',
59 'location' => '',
60 'description' => '',
61 'location_type' => '',
62 'payment_method' => '',
63 'payment_status' => '',
64 'payment_details' => '',
65 'order_total' => '',
66 'start_date' => '',
67 'end_date' => '',
68 'start_time' => '',
69 'end_time' => '',
70 'calendar_event' => '',
71 'random_id' => '',
72 'seats' => '',
73 'zoom' => '',
74 'custom_form_data' => '',
75 'timezone' => '',
76 'recurrence' => [],
77 'parent' => 0
78 ];
79
80 /**
81 * Booking Constructor
82 *
83 * @return void
84 */
85 public function __construct( $booking = 0 ) {
86 if ( $booking instanceof self ) {
87 $this->set_id( $booking->get_id() );
88 } elseif ( ! empty( $booking->ID ) ) {
89 $this->set_id( $booking->ID );
90 } elseif ( is_numeric( $booking ) && $booking > 0 ) {
91 $this->set_id( $booking );
92 }
93 }
94
95 /**
96 * Get id
97 *
98 * @return integer
99 */
100 public function get_id() {
101 return $this->id;
102 }
103
104 /**
105 * Get customer id
106 *
107 * @return integer
108 */
109 public function get_customer_id() {
110 return $this->get_prop( 'customer' );
111 }
112
113 /**
114 * Get staff for the current booking
115 *
116 * @return integer
117 */
118 public function get_staff_id() {
119 return $this->get_prop( 'staff' );
120 }
121
122 /**
123 * Get customer
124 *
125 * @return User Object
126 */
127 public function get_customer() {
128 return get_userdata( $this->get_customer_id() );
129 }
130
131 /**
132 * Get seats of a booking
133 *
134 * @return array
135 */
136 public function get_seat() {
137 return $this->get_prop( 'seats' );
138 }
139
140 /**
141 * Get custom fields
142 *
143 * @return array
144 */
145 public function get_custom_fields() {
146 return $this->get_prop( 'custom_form_data' );
147 }
148
149 /**
150 * Get billing first name
151 *
152 * @return string
153 */
154 public function get_billing_first_name() {
155 return $this->get_prop( 'first_name' );
156 }
157
158 /**
159 * Get location
160 *
161 * @return string
162 */
163 public function get_location() {
164 return $this->get_prop( 'location' );
165 }
166
167 /**
168 * Get location type
169 *
170 * @return string
171 */
172 public function get_location_type() {
173 return $this->get_prop( 'location_type' );
174 }
175
176 /**
177 * Get last name
178 *
179 * @return string
180 */
181 public function get_billing_last_name() {
182 return $this->get_prop( 'last_name' );
183 }
184
185 /**
186 * Get email
187 *
188 * @return string
189 */
190 public function get_billing_email() {
191 return $this->get_prop( 'email' );
192 }
193
194 /**
195 * Get billing phone
196 *
197 * @return string
198 */
199 public function get_billing_phone() {
200 return $this->get_prop( 'phone' );
201 }
202
203 /**
204 * Get timezone
205 *
206 * @return string
207 */
208 public function get_timezone() {
209 return $this->get_prop( 'timezone' );
210 }
211
212 /**
213 * Get description
214 *
215 * @return string
216 */
217 public function get_description() {
218 return $this->get_prop( 'description' );
219 }
220
221 /**
222 * Get billing address
223 *
224 * @param string $adress_type Booking billing address
225 *
226 * @return string
227 */
228 public function get_billing_address( $adress_type = 'address_1' ) {
229 return $this->get_prop( $adress_type );
230 }
231
232 /**
233 * Get billing city
234 *
235 * @return string
236 */
237 public function get_billing_city() {
238 return $this->get_prop( 'city' );
239 }
240
241 /**
242 * Get billing state
243 *
244 * @return string
245 */
246 public function get_billing_state() {
247 return $this->get_prop( 'state' );
248 }
249
250 /**
251 * Get billing post code
252 *
253 * @return string
254 */
255 public function get_billing_post_code() {
256 return $this->get_prop( 'post_code' );
257 }
258
259 /**
260 * Get billing
261 *
262 * @return string
263 */
264 public function get_billing_country() {
265 return $this->get_prop( 'country' );
266 }
267
268 /**
269 * Get payment method
270 *
271 * @return string
272 */
273 public function get_payment_method() {
274 return $this->get_prop( 'payment_method' );
275 }
276
277 /**
278 * Get appointment
279 *
280 * @return Appointment
281 */
282 public function get_appointment() {
283 return $this->get_prop( 'appointment' );
284 }
285
286 /**
287 * Get calendar event
288 *
289 * @return array
290 */
291 public function get_event() {
292 return $this->get_prop( 'calendar_event' );
293 }
294
295 /**
296 * Get post status
297 *
298 * @return string
299 */
300 public function get_status() {
301 return get_post( $this->id )->post_status;
302 }
303
304 /**
305 * Get total
306 *
307 * @return integer
308 */
309 public function get_total() {
310 return $this->get_prop( 'order_total' );
311 }
312
313 /**
314 * Get start date
315 *
316 * @return string
317 */
318 public function get_start_date() {
319 return $this->get_prop( 'start_date' );
320 }
321 /**
322 * Get start date
323 *
324 * @return string
325 */
326 public function get_date() {
327 return $this->get_prop( 'date' );
328 }
329
330 /**
331 * Get end date
332 *
333 * @return string
334 */
335 public function get_end_date() {
336 return $this->get_prop( 'end_date' );
337 }
338
339 /**
340 * Get start time
341 *
342 * @return string
343 */
344 public function get_start_time() {
345 return $this->get_prop( 'start_time' );
346 }
347
348 /**
349 * Get end time
350 *
351 * @return string
352 */
353 public function get_end_time() {
354 return $this->get_prop( 'end_time' );
355 }
356
357 /**
358 * Get random id for the current booking
359 *
360 * @return string
361 */
362 public function get_random_id() {
363 return $this->get_prop( 'random_id' );
364 }
365
366 /**
367 * Get payment details
368 *
369 * @return array
370 */
371 public function get_payment_details() {
372 return $this->get_prop( 'payment_details' );
373 }
374
375 /**
376 * Get zoom meeting details
377 *
378 * @return array
379 */
380 public function get_zoom() {
381 return $this->get_prop( 'zoom' );
382 }
383
384 /**
385 * Get recurrences
386 *
387 * @return array
388 */
389 public function get_recurrence() {
390 return $this->get_prop( 'recurrence' );
391 }
392
393 /**
394 * Get parent
395 *
396 * @return integer
397 */
398 public function get_parent() {
399 return $this->get_prop( 'parent' );
400 }
401
402 /**
403 * Get booking data
404 *
405 * @param string $prop
406 *
407 * @return mixed
408 */
409 private function get_prop( $prop = '' ) {
410 return $this->get_metadata( $prop );
411 }
412
413 /**
414 * Get metadata
415 *
416 * @param string $prop
417 *
418 * @return mixed
419 */
420 private function get_metadata( $prop = '' ) {
421 $meta_key = $this->meta_prefix . $prop;
422
423 return get_post_meta( $this->id, $meta_key, true );
424 }
425
426 /**
427 * Generate random id
428 *
429 * @return string
430 */
431 private function generate_randon_id() {
432 return chr( rand( ord( 'A' ), ord( 'Z' ) ) ) . rand( 575639, 575639 + 400 );
433 }
434
435 // Setter.
436
437 /**
438 * Set booking id
439 *
440 * @param integer $id
441 *
442 * @return void
443 */
444 public function set_id( $id ) {
445 $this->id = $id;
446 }
447
448 /**
449 * Set data
450 *
451 * @param array $args Booking Args
452 *
453 * @return void
454 */
455 public function set_props( $args = [] ) {
456 $this->data = wp_parse_args( $args, $this->data );
457 $this->appointment = new Appointment( $this->data['appointment'] );
458 }
459
460 /**
461 * Save metada for booking
462 *
463 * @return void
464 */
465 public function save_metadata( $args = [] ) {
466 foreach ( $args as $key => $value ) {
467
468 // If a key not exists on data it will skip to save the data.
469 if ( ! array_key_exists( $key, $this->data ) ) {
470 continue;
471 }
472
473 // Prepare meta key.
474 $meta_key = $this->meta_prefix . $key;
475
476 if ( ! $value ) {
477 $value = $this->get_prop( $key );
478 }
479
480 // Update appointment meta data.
481 update_post_meta( $this->id, $meta_key, $value );
482 }
483 }
484
485 /**
486 * Save appointment
487 *
488 * @return void
489 */
490 public function save() {
491 $args = [
492 'post_title' => $this->appointment->get_name(),
493 'post_type' => $this->pos_type,
494 'post_status' => $this->data['post_status'],
495 'post_author' => get_current_user_id(),
496 ];
497
498 $updated = false;
499
500 if ( ! empty( $this->id ) ) {
501 $this->send_notification('update');
502 $args['ID'] = $this->id;
503 $updated = true;
504 }
505
506 // Insert or Update appointment.
507 $booking_id = wp_insert_post( $args );
508 $this->data['random_id'] = $this->generate_randon_id();
509
510 if ( ! is_wp_error( $booking_id ) ) {
511 $this->set_id( $booking_id );
512 $this->save_metadata( $this->data );
513 }
514
515 if ( ! $updated ) {
516 $this->send_notification('create');
517 }
518 }
519
520 /**
521 * Update booking
522 *
523 * @param array $args
524 *
525 * @return bool
526 */
527 public function update( $args = [] ) {
528 $post = get_post( $this->id )->to_array();
529 $args = wp_parse_args( $args, $post );
530
531 $updated = wp_update_post( $args );
532
533 if ( ! is_wp_error( $updated ) ) {
534 $this->save_metadata( $args );
535 }
536
537 return $updated;
538 }
539
540 /**
541 * Delete booking
542 *
543 * @return bool | WP_Error
544 */
545 public function delete() {
546 // Set action for sending notification.
547 $this->send_notification('delete');
548
549 return wp_delete_post( $this->id, true );
550 }
551
552 /**
553 * Check is a booking or not
554 *
555 * @return bool
556 */
557 public function is_booking() {
558 $post = get_post( $this->id );
559
560 if ( $post && $this->pos_type === $post->post_type ) {
561 return true;
562 }
563
564 return false;
565 }
566
567 /**
568 * Get all bookings
569 *
570 * @param array $args Bookings args
571 *
572 * @return array
573 */
574 public static function all( $args = [] ) {
575
576 $status = [
577 'approved',
578 'pending',
579 'cancel',
580 'completed',
581 ];
582 $defaults = [
583 'post_type' => 'timetics-booking',
584 'posts_per_page' => 20,
585 'post_status' => 'any',
586 'paged' => 1,
587 ];
588
589 $args = wp_parse_args( $args, $defaults );
590
591 if ( ! empty( $args['start_date'] ) ) {
592 //_tt_booking_start_date
593 $args['meta_query'] = [
594 [
595 'key' => '_tt_booking_start_date',
596 'value' => $args['start_date'],
597 'compare' => '>=',
598 'type' => 'DATE',
599 ],
600 ];
601 }
602
603 if ( ! empty( $args['meeting'] ) ) {
604 // @codingStandardsIgnoreStart
605 $args['meta_query'] = [
606 [
607 'key' => '_tt_booking_appointment',
608 'value' => $args['meeting'],
609 'compare' => '=',
610 ],
611 ];
612 // @codingStandardsIgnoreEnd
613 }
614
615 if ( ! empty( $args['staff'] ) ) {
616 // @codingStandardsIgnoreStart
617 $args['meta_query'] = [
618 [
619 'key' => '_tt_booking_staff',
620 'value' => $args['staff'],
621 'compare' => '=',
622 ],
623 ];
624 // @codingStandardsIgnoreEnd
625 }
626
627 $post = new WP_Query( $args );
628
629 return [
630 'total' => $post->found_posts,
631 'items' => $post->posts,
632 ];
633 }
634
635 /**
636 * Create event after creating a booking
637 *
638 * @return bool
639 */
640 public function create_event() {
641
642 if ( ! timetics_google_setup() ) {
643 return;
644 }
645
646 $data = [
647 'summary' => timetics_get_option( 'booking_created_customer_email_title' ),
648 'description' => timetics_get_option( 'booking_created_customer_email_body' ),
649 ];
650
651 $calendar = new Calendar();
652 $event_data = $this->prepare_event( $data );
653 $event = $calendar->create_event( $event_data );
654
655 update_post_meta( $this->id, $this->meta_prefix . 'calendar_event', $event );
656 }
657
658 /**
659 * Update calendar event
660 *
661 * @return bool
662 */
663 public function update_event() {
664 if ( ! timetics_google_setup() ) {
665 return;
666 }
667
668 $data = [
669 'summary' => timetics_get_option( 'booking_rescheduled_customer_email_title' ),
670 'description' => timetics_get_option( 'booking_rescheduled_customer_email_body' ),
671 ];
672
673 $calendar = new Calendar();
674 $event_data = $this->prepare_event( $data );
675 $calendar_event = $this->get_event();
676
677 if ( ! empty( $calendar_event['id'] ) ) {
678 // Update calendar event.
679 $event = $calendar->update_event( $calendar_event['id'], $event_data );
680
681 // update calendar event data.
682 update_post_meta( $this->id, $this->meta_prefix . 'calendar_event', $event );
683 }
684 }
685
686 /**
687 * Delete calendar event
688 *
689 * @return
690 */
691 public function delete_event() {
692 if ( ! timetics_google_setup() ) {
693 return;
694 }
695
696 $calendar = new Calendar();
697 $calendar_event = $this->get_event();
698 $access_token = timetics_get_google_access_token( $this->get_staff_id() );
699
700 if ( ! empty( $calendar_event['id'] ) ) {
701 // Update calendar event.
702 $event = $calendar->delete_event( $calendar_event['id'], $access_token );
703
704 // update calendar event data.
705 update_post_meta( $this->id, $this->meta_prefix . 'calendar_event', $event );
706 }
707 }
708
709 /**
710 * Prepare event data
711 *
712 * @return array
713 */
714 private function prepare_event( $data = [] ) {
715 $meeting_id = $this->get_appointment();
716 $staff_id = $this->get_staff_id();
717 $customer_id = $this->get_customer_id();
718 $location_type = $this->get_location_type();
719
720 $meeting = new Appointment( $meeting_id );
721 $staff = new Staff( $staff_id );
722 $customer = new Customer( $customer_id );
723
724 $time = gmdate( 'h:i a', strtotime( $this->get_start_time() ) );
725 $date = gmdate( 'd F Y', strtotime( $this->get_start_date() ) );
726 $location = $this->get_location();
727
728 $placeholders = [
729 '{%meeting_title%}' => $meeting->name,
730 '{%meeting_date%}' => $date,
731 '{%meeting_time%}' => $time,
732 '{%meeting_location%}' => $location,
733 '{%meeting_duration%}' => $meeting->duration,
734 '{%host_name%}' => $staff->get_display_name(),
735 '{%host_email%}' => $staff->get_email(),
736 '{%customer_name%}' => $customer->get_display_name(),
737 '{%customer_email%}' => $customer->get_email(),
738 ];
739
740 $summary = ! empty( $data['summary'] ) ? timetics_replace_placeholder( $data['summary'], $placeholders ) : $meeting->get_name();
741 $description = ! empty( $data['description'] ) ? timetics_replace_placeholder( $data['description'], $placeholders ) : $meeting->get_description();
742
743 $description = apply_filters( 'timetics_booking_event_data', $description, $summary, $this );
744
745 $event = [
746 'summary' => $summary,
747 'description' => $description,
748 'start' => [
749 'date' => $this->get_start_date(),
750 'time' => $this->get_start_time(),
751 ],
752 'end' => [
753 'date' => $this->get_end_date(),
754 'time' => $this->get_end_time(),
755 ],
756 'attendees' => [
757 ['email' => $staff->get_email()],
758 ['email' => $customer->get_email()],
759 ],
760 'google_meet' => 'google-meet' === $location_type,
761 'access_token' => timetics_get_google_access_token( $staff_id ),
762 ];
763
764 return $event;
765 }
766
767 /**
768 * Get all data of an object
769 *
770 * @return array
771 */
772 public function to_array() {
773 $data = [];
774
775 foreach( $this->data as $key => $value ) {
776
777 $data[$key] = $this->get_prop( $key );
778 }
779
780 return $data;
781 }
782
783 /**
784 * Send notification after booking
785 *
786 * @return void
787 */
788 private function send_notification( $action ) {
789
790 do_action( 'timetics_booking_notification', $this, $action );
791 }
792 }
793