PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.64
Timetics – Appointment Booking Calendar & Scheduling v1.0.64
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
← All changes | core/appointments/appointment.php +484 -58 1.0.2 → 1.0.64 View file →
@@ -7,8 +7,11 @@
7 7 * @package Timetics
8 8 */
9 9 namespace Timetics\Core\Appointments;
10 10
11 +defined( 'ABSPATH' ) || exit;
12 +
13 +use DateTime;
11 14 use Timetics\Base\PostModel;
12 15 use Timetics\Core\Bookings\Booking_Entry;
13 16 use Timetics\Core\Staffs\Staff;
14 17 use WP_Query;
@@ -34,9 +37,9 @@
34 37 * @since 1.0.0
35 38 *
36 39 * @var string $taxonomy
37 40 */
38 - protected $taxonomy = 'tt_category';
41 + protected $taxonomy = 'timetics-meeting-category';
39 42
40 43 /**
41 44 * Store id
42 45 *
@@ -53,25 +56,41 @@
53 56 *
54 57 * @var array $data
55 58 */
56 59 public $data = [
57 - 'name' => '',
58 - 'type' => '',
59 - 'description' => '',
60 - 'staff' => '',
61 - 'locations' => '',
62 - 'duration' => '',
63 - 'schedule' => '',
64 - 'price' => '',
65 - 'buffer_time' => '',
66 - 'timezone' => '',
67 - 'availability' => '',
68 - 'visibility' => '',
69 - 'capacity' => '',
70 - 'seat_plan' => '',
71 - 'duplicate' => 0,
72 - 'seat_plan_settings' => '',
73 - 'notifications' => [
60 + 'name' => '',
61 + 'type' => '',
62 + 'description' => '',
63 + 'staff' => '',
64 + 'locations' => '',
65 + 'duration' => '',
66 + 'schedule' => '',
67 + 'blocked_schedule' => '',
68 + 'price' => '',
69 + 'buffer_time' => '',
70 + 'buffer_time_before_value'=> '',
71 + 'buffer_time_before_unit' => '',
72 + 'buffer_time_after_value' => '',
73 + 'buffer_time_after_unit' => '',
74 + 'timezone' => '',
75 + 'availability' => '',
76 + 'visibility' => '',
77 + 'capacity' => '',
78 + 'seat_plan' => '',
79 + 'duplicate' => 0,
80 + 'custom_fields' => [],
81 + 'wc_product_id' => 0,
82 + 'seat_plan_settings' => '',
83 + 'fleunt_crm_webhook' => '',
84 + 'fluent_hook_overwrite' => '',
85 + 'pabbly_hook_overwrite' => '',
86 + 'zapier_hook_overwrite' => '',
87 + 'pabbly_webook' => '',
88 + 'zapier_webook' => '',
89 + 'flowmattic_hook_overwrite' => '',
90 + 'flowmattic_webhook' => '',
91 + 'min_notice_time' => '',
92 + 'notifications' => [
74 93 'booking_created_email_form' => '',
75 94 'booking_created_email_title' => '',
76 95 'booking_created_email_body' => '',
77 96 'booking_canceled_email_form' => '',
@@ -84,8 +103,15 @@
84 103 'booking_reminder_email_title' => '',
85 104 'booking_reminder_email_body' => '',
86 105 'booking_reminder_time' => 30,
87 106 ],
107 + 'permalink' => '',
108 + 'recurring' => false,
109 + 'recurring_limit' => 0,
110 + 'recurring_interval' => 1,
111 + 'recurring_interval_name' => 'day',
112 + 'guest_enabled' => false,
113 + 'guest_limit' => 1,
88 114 ];
89 115
90 116 /**
91 117 * Store meta key prefix
@@ -125,8 +151,32 @@
125 151 return $this->id;
126 152 }
127 153
128 154 /**
155 + * Get appointment slug
156 + *
157 + * @return mixed
158 + */
159 + public function get_slug() {
160 + $post = get_post( $this->get_id() );
161 +
162 + if ( $post ) {
163 + return $post->post_name;
164 + }
165 +
166 + return false;
167 + }
168 +
169 + /**
170 + * Get woocommerce product id
171 + *
172 + * @return integer
173 + */
174 + public function get_wc_product_id() {
175 + return $this->get_prop( 'wc_product_id' );
176 + }
177 +
178 + /**
129 179 * Get name.
130 180 *
131 181 * @since 1.0.0
132 182 *
@@ -166,9 +216,11 @@
166 216 *
167 217 * @return array
168 218 */
169 219 public function get_staff_ids() {
170 - return $this->get_prop( 'staff' );
220 + $ids = $this->get_prop( 'staff' );
221 +
222 + return is_array( $ids ) ? array_values( $ids ) : [];
171 223 }
172 224
173 225 /**
174 226 * Get duplicate number
@@ -175,9 +227,9 @@
175 227 *
176 228 * @return integer
177 229 */
178 230 public function get_duplicate_nuber() {
179 - return $this->get_prop('duplicate');
231 + return $this->get_prop( 'duplicate' );
180 232 }
181 233
182 234 /**
183 235 * Get capacity
@@ -188,8 +240,25 @@
188 240 return $this->get_prop( 'capacity' );
189 241 }
190 242
191 243 /**
244 + * Get the effective booking capacity used for availability checks.
245 + *
246 + * One-to-One meetings always allow a single booking per slot regardless of
247 + * the stored capacity meta (which is only meaningful for group meetings),
248 + * matching the one-to-one handling used elsewhere in the booking flow.
249 + *
250 + * @return integer
251 + */
252 + public function get_effective_capacity() {
253 + if ( 'one-to-one' === strtolower( (string) $this->get_type() ) ) {
254 + return 1;
255 + }
256 +
257 + return intval( $this->get_capacity() );
258 + }
259 +
260 + /**
192 261 * Get location
193 262 *
194 263 * @since 1.0.0
195 264 *
@@ -199,8 +268,19 @@
199 268 return $this->get_prop( 'locations' );
200 269 }
201 270
202 271 /**
272 + * Get Permalink
273 + *
274 + * @since 1.0.0
275 + *
276 + * @return string
277 + */
278 + public function get_appointment_permalink() {
279 + return get_post_permalink( $this->id );
280 + }
281 +
282 + /**
203 283 * Get duration
204 284 *
205 285 * @since 1.0.0
206 286 *
@@ -217,10 +297,10 @@
217 297 */
218 298 public function get_interval() {
219 299 $duration = $this->get_duration();
220 300 if ( $duration ) {
221 - $duration = explode(' ', $duration);
222 - $duration = 'hr' === $duration[1] ? intval($duration[0]) * 60 * 60 : intval($duration[0]) * 60;
301 + $duration = explode( ' ', $duration );
302 + $duration = 'hr' === $duration[1] ? intval( $duration[0] ) * 60 * 60 : intval( $duration[0] ) * 60;
223 303 }
224 304
225 305 return $duration;
226 306 }
@@ -236,8 +316,19 @@
236 316 return $this->get_prop( 'schedule' );
237 317 }
238 318
239 319 /**
320 + * Get blocked schedule
321 + *
322 + * @since 1.0.0
323 + *
324 + * @return array
325 + */
326 + public function get_blocked_schedule() {
327 + return $this->get_prop( 'blocked_schedule' );
328 + }
329 +
330 + /**
240 331 * Get price
241 332 *
242 333 * @since 1.0.0
243 334 *
@@ -272,8 +363,44 @@
272 363 public function get_buffer_time() {
273 364 return $this->get_prop( 'buffer_time' );
274 365 }
275 366
367 + /**
368 + * Returns the amount of buffer time before the appointment (without unit like minute or hour)
369 + *
370 + * @return integer
371 + */
372 + public function get_buffer_time_before_value() {
373 + return $this->get_prop( 'buffer_time_before_value' );
374 + }
375 +
376 + /**
377 + * Returns the unit of buffer time before the appointment (like minute or hour)
378 + *
379 + * @return string
380 + */
381 + public function get_buffer_time_before_unit() {
382 + return $this->get_prop( 'buffer_time_before_unit' );
383 + }
384 +
385 + /**
386 + * Returns the amount of buffer time after the appointment (without unit like minute or hour)
387 + *
388 + * @return integer
389 + */
390 + public function get_buffer_time_after_value() {
391 + return $this->get_prop( 'buffer_time_after_value' );
392 + }
393 +
394 + /**
395 + * Returns the unit of buffer time after the appointment (like minute or hour)
396 + *
397 + * @return string
398 + */
399 + public function get_buffer_time_after_unit() {
400 + return $this->get_prop( 'buffer_time_after_unit' );
401 + }
402 +
276 403 public function get_timezone() {
277 404 return $this->get_prop( 'timezone' );
278 405 }
279 406
@@ -283,10 +410,52 @@
283 410
284 411 public function get_visibility() {
285 412 return $this->get_prop( 'visibility' );
286 413 }
414 + public function get_fleunt_crm_webhook() {
415 + return $this->get_prop( 'fleunt_crm_webhook' );
416 + }
417 + public function get_fluent_hook_overwrite() {
418 + return $this->get_prop( 'fluent_hook_overwrite' );
419 + }
420 + public function get_pabbly_webook() {
421 + return $this->get_prop( 'pabbly_webook' );
422 + }
423 + public function get_pabbly_hook_overwrite() {
424 + return $this->get_prop( 'pabbly_hook_overwrite' );
425 + }
426 + public function get_zapier_webook() {
427 + return $this->get_prop( 'zapier_webook' );
428 + }
429 + public function get_zapier_hook_overwrite() {
430 + return $this->get_prop( 'zapier_hook_overwrite' );
431 + }
432 + public function get_flowmattic_webhook() {
433 + return $this->get_prop( 'flowmattic_webhook' );
434 + }
435 + public function get_flowmattic_hook_overwrite() {
436 + return $this->get_prop( 'flowmattic_hook_overwrite' );
437 + }
438 + public function get_min_notice_time() {
439 + return $this->get_prop( 'min_notice_time' );
440 + }
441 + public function get_guest_enabled() {
442 + return $this->get_prop( 'guest_enabled' );
443 + }
444 + public function get_guest_limit() {
445 + return $this->get_prop( 'guest_limit' );
446 + }
287 447
288 448 /**
449 + * Get Custom fields
450 + *
451 + * @return array
452 + */
453 + public function get_custom_fields() {
454 + return $this->get_prop( 'custom_fields' );
455 + }
456 +
457 + /**
289 458 * Get notifications
290 459 *
291 460 * @return array
292 461 */
@@ -294,8 +463,38 @@
294 463 return $this->get_prop( 'notifications' );
295 464 }
296 465
297 466 /**
467 + * Returns the buffer time before the appointment in seconds
468 + *
469 + * @return integer seconds
470 + */
471 + public function get_buffer_time_before_in_seconds() {
472 + $buffer_time_value = (int) $this->get_buffer_time_before_value() ?? 0;
473 +
474 + if ( 'hr' === $this->get_buffer_time_before_unit() ) {
475 + return $buffer_time_value * 60 * 60;
476 + }
477 +
478 + return $buffer_time_value * 60;
479 + }
480 +
481 + /**
482 + * Returns the buffer time after the appointment in seconds
483 + *
484 + * @return integer seconds
485 + */
486 + public function get_buffer_time_after_in_seconds() {
487 + $buffer_time_value = (int) $this->get_buffer_time_after_value() ?? 0;
488 +
489 + if ( 'hr' === $this->get_buffer_time_after_unit() ) {
490 + return $buffer_time_value * 60 * 60;
491 + }
492 +
493 + return $buffer_time_value * 60;
494 + }
495 +
496 + /**
298 497 * Get appointment categories
299 498 *
300 499 * @since 1.0.0
301 500 *
@@ -311,8 +510,25 @@
311 510 return $categories;
312 511 }
313 512
314 513 /**
514 + * Get category ids
515 + *
516 + * @return array
517 + */
518 + public function get_category_ids() {
519 + $categories = $this->get_categories();
520 +
521 + $ids = [];
522 +
523 + foreach ( $categories as $category ) {
524 + $ids[] = $category->term_id;
525 + }
526 +
527 + return $ids;
528 + }
529 +
530 + /**
315 531 * Get post thumbnail.
316 532 *
317 533 * @param string $size Image size
318 534 *
@@ -340,8 +556,19 @@
340 556 return $this->get_prop( 'seat_plan' );
341 557 }
342 558
343 559 /**
560 + * Get appointment author
561 + *
562 + * @return integer
563 + */
564 + public function get_author() {
565 + $post = get_post( $this->id );
566 +
567 + return $post ? (int) $post->post_author : 0;
568 + }
569 +
570 + /**
344 571 * Get seat plan settings
345 572 *
346 573 * @return array
347 574 */
@@ -348,9 +575,47 @@
348 575 public function get_seat_settings() {
349 576 return $this->get_prop( 'seat_plan_settings' );
350 577 }
351 578
579 + // Recurring options.
580 +
352 581 /**
582 + * Check it is recurring or not
583 + *
584 + * @return bool
585 + */
586 + public function is_recurring() {
587 + return boolval( $this->get_prop( 'recurring' ) );
588 + }
589 +
590 + /**
591 + * Get recurring limit
592 + *
593 + * @return integer
594 + */
595 + public function get_recurring_limit() {
596 + return intval( $this->get_prop( 'recurring_limit' ) );
597 + }
598 +
599 + /**
600 + * Get recurring interval
601 + *
602 + * @return integer
603 + */
604 + public function get_recurring_interval() {
605 + return intval( $this->get_prop( 'recurring_interval' ) );
606 + }
607 +
608 + /**
609 + * Get recurring interval name
610 + *
611 + * @return string
612 + */
613 + public function get_recurring_interval_name() {
614 + return $this->get_prop( 'recurring_interval_name' );
615 + }
616 +
617 + /**
353 618 * Get appointment data
354 619 *
355 620 * @param string $prop
356 621 *
@@ -394,9 +659,9 @@
394 659 *
395 660 * @return void
396 661 */
397 662 public function set_props( $data = [] ) {
398 - $this->data = wp_parse_args( $data, $this->data );
663 + $this->data = $data;
399 664 }
400 665
401 666 /**
402 667 * Save appointment
@@ -410,13 +675,15 @@
410 675 $args = [
411 676 'post_title' => $this->data['name'],
412 677 'post_type' => $this->post_type,
413 678 'post_status' => 'publish',
414 - 'post_author' => 1,
415 679 ];
416 680
417 681 if ( ! empty( $this->id ) ) {
418 682 $args['ID'] = $this->id;
683 + } else {
684 + // Only on creation — setting this on update reassigns post_author to whoever edits it.
685 + $args['post_author'] = get_current_user_id();
419 686 }
420 687
421 688 // Insert or Update appointment.
422 689 $appoint_id = wp_insert_post( $args );
@@ -498,9 +765,9 @@
498 765 * @return array
499 766 */
500 767 public static function all( $args = [] ) {
501 768 $defaults = [
502 - 'post_type' => (new self)->post_type,
769 + 'post_type' => ( new self )->post_type,
503 770 'post_status' => 'publish',
504 771 'posts_per_page' => 20,
505 772 'paged' => 1,
506 773 'orderby' => 'ID',
@@ -508,9 +775,10 @@
508 775 ];
509 776
510 777 $args = wp_parse_args( $args, $defaults );
511 778
512 - if ( ! empty( $args['staff'] ) ) {
779 + // isset()/'' check, not empty() — empty(0) skipped this filter, leaking all staff data to guests.
780 + if ( isset( $args['staff'] ) && '' !== $args['staff'] ) {
513 781 $args['meta_query'][] = [
514 782 'key' => '_tt_apointment_staff',
515 783 'value' => $args['staff'],
516 784 'compare' => 'LIKE',
@@ -532,8 +800,16 @@
532 800 'compare' => 'LIKE',
533 801 ];
534 802 }
535 803
804 + if ( ! empty( $args['category'] ) ) {
805 + $args['tax_query'][] = [
806 + 'taxonomy' => 'timetics-meeting-category',
807 + 'terms' => $args['category'],
808 + 'include_children' => false,
809 + ];
810 + }
811 +
536 812 $post = new WP_Query( $args );
537 813
538 814 return [
539 815 'total' => $post->found_posts,
@@ -549,24 +825,35 @@
549 825 * @return void
550 826 */
551 827 public function duplicate() {
552 828 $this->set_props( [
553 - 'name' => $this->get_name(),
554 - 'description' => $this->get_description(),
555 - 'type' => $this->get_type(),
556 - 'locations' => $this->get_locations(),
557 - 'staff' => $this->get_staff_ids(),
558 - 'duplicate' => intval( $this->get_duplicate_nuber() ) + 1,
559 - 'duration' => $this->get_duration(),
560 - 'price' => $this->get_price(),
561 - 'capacity' => $this->get_capacity(),
562 - 'schedule' => $this->get_schedule(),
563 - 'categories' => $this->get_categories(),
564 - 'timezone' => $this->get_timezone(),
565 - 'availability' => $this->get_availability(),
566 - 'visibility' => $this->get_visibility(),
567 - 'buffer_time' => $this->get_buffer_time(),
568 - 'notifications' => $this->get_notifications(),
829 + 'name' => $this->get_name(),
830 + 'description' => $this->get_description(),
831 + 'type' => $this->get_type(),
832 + 'locations' => $this->get_locations(),
833 + 'staff' => $this->get_staff_ids(),
834 + 'duplicate' => intval( $this->get_duplicate_nuber() ) + 1,
835 + 'duration' => $this->get_duration(),
836 + 'price' => $this->get_price(),
837 + 'capacity' => $this->get_capacity(),
838 + 'schedule' => $this->get_schedule(),
839 + 'categories' => $this->get_categories(),
840 + 'timezone' => $this->get_timezone(),
841 + 'availability' => $this->get_availability(),
842 + 'visibility' => $this->get_visibility(),
843 + 'buffer_time' => $this->get_buffer_time(),
844 + 'notifications' => $this->get_notifications(),
845 + 'fluent_hook_overwrite' => $this->get_fluent_hook_overwrite(),
846 + 'fleunt_crm_webhook' => $this->get_fleunt_crm_webhook(),
847 + 'pabbly_hook_overwrite' => $this->get_pabbly_hook_overwrite(),
848 + 'pabbly_webook' => $this->get_pabbly_webook(),
849 + 'zapier_webook' => $this->get_zapier_webook(),
850 + 'flowmattic_webhook' => $this->get_flowmattic_webhook(),
851 + 'flowmattic_hook_overwrite' => $this->get_flowmattic_hook_overwrite(),
852 + 'min_notice_time' => $this->get_min_notice_time(),
853 + 'zapier_hook_overwrite' => $this->get_zapier_hook_overwrite(),
854 + 'guest_enabled' => $this->get_guest_enabled(),
855 + 'guest_limit' => $this->get_guest_limit(),
569 856 ] );
570 857
571 858 $this->set_id( 0 );
572 859 $this->save();
@@ -580,9 +867,9 @@
580 867 * @param integer $staff_id
581 868 *
582 869 * @return array
583 870 */
584 - public function prepare_schedule( $start, $end, $staff_id ) {
871 + public function prepare_schedule( $start, $end, $staff_id, $timze_zone = '' ) {
585 872 $start = new \DateTime( $start );
586 873 $end = new \DateTime( $end );
587 874 $schedule = [];
588 875
@@ -587,9 +874,9 @@
587 874 $schedule = [];
588 875
589 876 // Prepare schedule for the current meeting
590 877 for ( $date = $start; $date <= $end; $date->modify( '+1 day' ) ) {
591 - $schedule[] = $this->get_schedule_by_date( $date->format( 'Y-m-d' ), $staff_id );
878 + $schedule[] = $this->get_schedule_by_date( $date->format( 'Y-m-d' ), $staff_id, $timze_zone );
592 879 }
593 880
594 881 return $schedule;
595 882 }
@@ -598,31 +885,42 @@
598 885 * Get meeting schedule by date
599 886 *
600 887 * @param string $date [$date description]
601 888 * @param integer $staff_id [$staff_id description]
889 + * @param string $timze_zone [$timze_zone description]
602 890 *
603 891 * @return array
604 892 */
605 - private function get_schedule_by_date( $date, $staff_id ) {
606 - $day_name = ucfirst( date( 'D', strtotime( $date ) ) );
607 - $schedule = $this->get_schedule();
893 + private function get_schedule_by_date( $date, $staff_id, $time_zone = '' ) {
894 + $day_name = ucfirst( gmdate( 'D', strtotime( $date ) ) );
895 + $schedule = $this->get_schedule();
608 896 $interval = $this->get_interval();
609 - $tartget_schedule = [];
897 + $target_schedule = [];
610 898
611 899 $slots = [];
612 900
613 901 if ( ! empty( $schedule[$staff_id] ) ) {
614 - $tartget_schedule = $schedule[$staff_id][$day_name];
902 + $target_schedule = $schedule[$staff_id][$day_name];
615 903 }
616 904
617 - foreach ( $tartget_schedule as $day ) {
905 + foreach ( $target_schedule as $day ) {
618 906 $start = strtotime( $day['start'] );
619 907 $end = strtotime( $day['end'] );
620 908
621 - for ( $time = $start; $time <= $end; $time += $interval ) {
909 + // Get buffer times in seconds
910 + $buffer_before = $this->get_buffer_time_before_in_seconds();
911 + $buffer_after = $this->get_buffer_time_after_in_seconds();
912 +
913 + // Adjust the start time to include buffer before
914 + $adjusted_start = $start + $buffer_before;
915 +
916 + // Adjust the end time to ensure we have enough time for the slot + buffer after
917 + $min_slot_duration = $interval + $buffer_after;
918 +
919 + for ( $time = $adjusted_start; $time + $min_slot_duration <= $end; $time += $interval + $buffer_after + $buffer_before ) {
622 920 $booked_entry = $this->get_booking_entries( $date, $time, $staff_id );
623 921 $status = 'available';
624 - $capacity = $this->get_capacity();
922 + $capacity = $this->get_effective_capacity();
625 923 $booked = 0;
626 924
627 925 if ( $booked_entry ) {
628 926 if ( $booked_entry->get_booked() >= $capacity ) {
@@ -631,11 +929,15 @@
631 929
632 930 $booked = $booked_entry->get_booked();
633 931 }
634 932
933 + // $time is only a clock reading, so pass the slot's own date or
934 + // today's DST offset gets applied to a date in the other season.
935 + $datetime = $this->convert_timezone( $date . ' ' . gmdate( 'g:ia', $time ), $time_zone );
936 +
635 937 $slot = [
636 938 'status' => $status,
637 - 'start_time' => gmdate( 'g:ia', $time ),
939 + 'start_time' => $datetime->format( 'g:ia' ),
638 940 'booked' => $booked,
639 941 ];
640 942
641 943 $slot = apply_filters( 'timetics_booking_slot', $slot, $this, $booked_entry );
@@ -660,21 +962,145 @@
660 962 *
661 963 * @return bool
662 964 */
663 965 private function get_booking_entries( $date, $time, $staff_id ) {
664 - $time = gmdate( 'H:i', $time );
966 + $time = gmdate( 'h:i a', $time );
665 967 $booking_entries = new Booking_Entry();
666 968 $entries = $booking_entries->find( [
667 - 'meeting_id' => $this->id,
668 969 'staff_id' => $staff_id,
669 - 'date' => $date,
970 + 'date' => $this->convert_timezone( $date, $this->get_timezone())->format('Y-m-d'),
670 971 ] );
671 972
672 973 foreach ( $entries as $entry ) {
673 - if ( gmdate( 'H:i', strtotime( $entry->get_start() ) ) == $time ) {
674 - return $entry;
974 + $start_time = $entry->get_start();
975 + $end_time = $entry->get_end();
976 +
977 + // Check if current time slot falls within the booking duration
978 + if ( ! $this->is_time_in_range( $time, $start_time, $end_time ) ) {
979 + continue;
675 980 }
981 +
982 + if ( $this->is_orphan_entry( $entry ) ) {
983 + continue;
984 + }
985 +
986 + return $entry;
676 987 }
677 988
678 989 return false;
990 + }
991 +
992 + /**
993 + * Whether an entry outlived its booking and should stop blocking the slot.
994 + *
995 + * Only single-booking entries qualify: a shared entry names just its first
996 + * booker in `booking_id`, and entries with none belong to events/imports.
997 + *
998 + * @param Booking_Entry $entry
999 + *
1000 + * @return bool
1001 + */
1002 + private function is_orphan_entry( $entry ) {
1003 + $booking_id = $entry->get_booking_id();
1004 +
1005 + if ( ! $booking_id || intval( $entry->get_booked() ) > 1 ) {
1006 + return false;
1007 + }
1008 +
1009 + $booking = get_post( $booking_id );
1010 +
1011 + return ! $booking || 'timetics-booking' !== $booking->post_type;
1012 + }
1013 +
1014 + /**
1015 + * Check if a time falls within a given time range
1016 + *
1017 + * @param string $current_time Current time in h:i a format
1018 + * @param string $start_time Start time in h:i a format
1019 + * @param string $end_time End time in h:i a format
1020 + *
1021 + * @return bool
1022 + */
1023 + private function is_time_in_range( $current_time, $start_time, $end_time ) {
1024 + $current = strtotime( $current_time );
1025 + $start = strtotime( $start_time );
1026 + $end = strtotime( $end_time );
1027 +
1028 + return $current >= $start && $current < $end;
1029 + }
1030 +
1031 + /**
1032 + * Convert timestamp in target timezone
1033 + *
1034 + * @param integer $timestamp
1035 + * @param string $timze_zone
1036 + *
1037 + * @return DateTime
1038 + */
1039 + public function convert_timezone( $date, $timze_zone ) {
1040 + $date = is_int( $date ) ? gmdate( 'Y-m-d g:ia', $date ) : $date;
1041 +
1042 + // Create a DateTime object with the provided timestamp and time zone
1043 + $datetime = new \DateTime( $date, new \DateTimeZone( $this->get_timezone() ) );
1044 +
1045 + // Convert the time zone
1046 + $datetime->setTimezone( new \DateTimeZone( $timze_zone ) );
1047 +
1048 + // Get the converted timestamp
1049 + return $datetime;
1050 + }
1051 +
1052 + /**
1053 + * Get appointmentt ids by author id
1054 + *
1055 + * @param integer $author_id Appointment author id
1056 + *
1057 + * @return array Appointment ids
1058 + */
1059 + public static function get_appointment_ids_by_author( $author_id ) {
1060 + $query = new WP_Query([
1061 + 'post_type' => 'timetics-appointment',
1062 + 'post_status' => 'any',
1063 + 'author' => $author_id,
1064 + 'posts_per_page' => -1,
1065 + 'fields' => 'ids'
1066 + ]);
1067 +
1068 + return $query->posts;
1069 + }
1070 +
1071 + /**
1072 + * Get meeting available time slot
1073 + *
1074 + * @param string $date Start date
1075 + * @param integer $staff_id Meeting assigned staff id
1076 + * @param string $timze_zone Booking timezone
1077 + *
1078 + * @return array Available timeslots
1079 + */
1080 + public function get_avilable_timeslots( $date, $staff_id, $time_zone = '' ) {
1081 + $day_name = ucfirst( gmdate( 'D', strtotime( $date ) ) );
1082 + $schedule = $this->get_schedule();
1083 + $interval = $this->get_interval();
1084 + $tartget_schedule = [];
1085 +
1086 + $slots = [];
1087 +
1088 + if ( isset($schedule[$staff_id]) && ! empty(array_filter($schedule[$staff_id])) ) {
1089 + $tartget_schedule = $schedule[$staff_id][$day_name];
1090 + }
1091 +
1092 + foreach ( $tartget_schedule as $day ) {
1093 + $start = strtotime( $day['start'] );
1094 + $end = strtotime( $day['end'] );
1095 +
1096 + for ( $time = $start; $time <= $end; $time += $interval ) {
1097 + // Slot's own date, see get_schedule_by_date().
1098 + $datetime = $this->convert_timezone( $date . ' ' . gmdate( 'g:ia', $time ), $time_zone );
1099 + $slot = $datetime->format( 'g:ia' );
1100 + $slots[] = $slot;
1101 + }
1102 + }
1103 +
1104 + return $slots;
679 1105 }
680 1106 }