PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.2
Timetics – Appointment Booking Calendar & Scheduling v1.0.2
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 1.0.22 All 64 releases
timetics / core / bookings / booking.php

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

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