PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.1
Timetics – Appointment Booking Calendar & Scheduling v1.0.1
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.1, at core/bookings/booking.php

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