← All changes
|
includes/page-availability-timeslots/ajax/availability_timeslots__front.php
+119
-16
11.0
→
11.9
View file →
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Time slots availability helpers and public read endpoint. |
| 4 | 4 | * |
| @@ -470,23 +470,30 @@ | ||
| 470 | 470 | $was_day_unavailable = ! empty( $availability_obj->is_day_unavailable ); |
| 471 | 471 | $previous_day_status = $availability_obj->_day_status; |
| 472 | 472 | |
| 473 | 473 | foreach ( $blocked_intervals as $blocked_interval ) { |
| 474 | + $source_type = isset( $blocked_interval['source_type'] ) ? sanitize_key( $blocked_interval['source_type'] ) : 'availability_timeslot'; | |
| 475 | + $is_working_time = ( 'working_time' === $source_type ); | |
| 476 | + $timeslot_id = isset( $blocked_interval['timeslot_id'] ) ? (int) $blocked_interval['timeslot_id'] : 0; | |
| 474 | 477 | $time_range = array( |
| 475 | 478 | wpbc_availability_timeslots__sanitize_second( $blocked_interval['start_second'] ), |
| 476 | 479 | wpbc_availability_timeslots__sanitize_second( $blocked_interval['end_second'] ), |
| 477 | 480 | ); |
| 481 | + $details_id = $is_working_time | |
| 482 | + ? 'working_time_' . ( isset( $blocked_interval['resource_id'] ) ? intval( $blocked_interval['resource_id'] ) : 0 ) . '_' . ( isset( $blocked_interval['date'] ) ? preg_replace( '/[^0-9\-]/', '', $blocked_interval['date'] ) : '' ) . '_' . $time_range[0] . '_' . $time_range[1] | |
| 483 | + : 'availability_timeslot_' . $timeslot_id; | |
| 484 | + $status_title = $is_working_time ? __( 'Outside working time', 'booking' ) : __( 'Unavailable time-slot', 'booking' ); | |
| 478 | 485 | |
| 479 | 486 | if ( $time_range[0] >= $time_range[1] ) { |
| 480 | 487 | continue; |
| 481 | 488 | } |
| 482 | 489 | |
| 483 | - if ( ! $was_day_unavailable ) { | |
| 490 | + if ( ! $was_day_unavailable && ! $is_working_time ) { | |
| 484 | 491 | $availability_obj->_day_status = 'time_slots_booking'; |
| 485 | 492 | } |
| 486 | 493 | $availability_obj->booked_time_slots['in_seconds'][] = $time_range; |
| 487 | 494 | $availability_obj->booked_time_slots['readable24h'][] = wpbc_availability_timeslots__interval_to_readable( $time_range ); |
| 488 | - if ( ! $was_day_unavailable ) { | |
| 495 | + if ( ! $was_day_unavailable && ! $is_working_time ) { | |
| 489 | 496 | $availability_obj->pending_approved[ $time_range[0] ] = 'approved'; |
| 490 | 497 | } |
| 491 | 498 | |
| 492 | 499 | $time_range_key = $time_range[0] . ' - ' . $time_range[1]; |
| @@ -495,14 +502,14 @@ | ||
| 495 | 502 | } |
| 496 | 503 | |
| 497 | 504 | $availability_obj->__booked__times__details[ $time_range_key ] = (object) array( |
| 498 | 505 | 'approved' => '1', |
| 499 | - 'booking_id' => 'availability_timeslot_' . (int) $blocked_interval['timeslot_id'], | |
| 500 | - 'unavailable_timeslot_id' => (int) $blocked_interval['timeslot_id'], | |
| 501 | - 'availability_timeslot_id' => (int) $blocked_interval['timeslot_id'], | |
| 502 | - 'source_type' => 'availability_timeslot', | |
| 506 | + 'booking_id' => $details_id, | |
| 507 | + 'unavailable_timeslot_id' => $is_working_time ? 0 : $timeslot_id, | |
| 508 | + 'availability_timeslot_id' => $is_working_time ? 0 : $timeslot_id, | |
| 509 | + 'source_type' => $source_type, | |
| 503 | 510 | 'time_slot_status' => 'unavailable', |
| 504 | - 'time_slot_status_title' => __( 'Unavailable time-slot', 'booking' ), | |
| 511 | + 'time_slot_status_title' => $status_title, | |
| 505 | 512 | 'parent' => '0', |
| 506 | 513 | 'prioritet' => '0', |
| 507 | 514 | 'type' => isset( $blocked_interval['resource_id'] ) ? (string) intval( $blocked_interval['resource_id'] ) : '0', |
| 508 | 515 | 'date_res_type' => null, |
| @@ -509,10 +516,10 @@ | ||
| 509 | 516 | 'block_date' => isset( $blocked_interval['date'] ) ? $blocked_interval['date'] : '', |
| 510 | 517 | 'readable_time' => wpbc_transform_timerange__seconds_arr__in__formated_hours( $time_range ), |
| 511 | 518 | 'group_uid' => isset( $blocked_interval['group_uid'] ) ? (string) $blocked_interval['group_uid'] : '', |
| 512 | 519 | '__summary__booking' => (object) array( |
| 513 | - 'source_type' => 'availability_timeslot', | |
| 514 | - 'unavailable_timeslot_id' => (int) $blocked_interval['timeslot_id'], | |
| 520 | + 'source_type' => $source_type, | |
| 521 | + 'unavailable_timeslot_id' => $is_working_time ? 0 : $timeslot_id, | |
| 515 | 522 | 'time_slot_status' => 'unavailable', |
| 516 | 523 | ), |
| 517 | 524 | ); |
| 518 | 525 | |
| @@ -518,10 +525,10 @@ | ||
| 518 | 525 | |
| 519 | 526 | if ( 'On' === get_bk_option( 'booking_is_show_booked_data_in_tooltips' ) ) { |
| 520 | 527 | $tooltip_text = ''; |
| 521 | 528 | $tooltip_text .= '<div class="wpbc_tooltip_item tooltip_booked_time">' . wpbc_transform_timerange__seconds_arr__in__formated_hours( $time_range ) . '</div>'; |
| 522 | - $tooltip_text .= '<div class="wpbc_tooltip_item">' . esc_html__( 'Unavailable time-slot', 'booking' ) . '</div>'; | |
| 523 | - $availability_obj->tooltips['details'][ 'availability_timeslot_' . (int) $blocked_interval['timeslot_id'] ] = $tooltip_text; | |
| 529 | + $tooltip_text .= '<div class="wpbc_tooltip_item">' . esc_html( $status_title ) . '</div>'; | |
| 530 | + $availability_obj->tooltips['details'][ $details_id ] = $tooltip_text; | |
| 524 | 531 | } |
| 525 | 532 | } |
| 526 | 533 | |
| 527 | 534 | $merged_seconds_arr = wpbc_merge_intersected_intervals( $availability_obj->booked_time_slots['in_seconds'] ); |
| @@ -590,11 +597,14 @@ | ||
| 590 | 597 | |
| 591 | 598 | case 'weekday_unavailable': |
| 592 | 599 | case 'from_today_unavailable': |
| 593 | 600 | case 'limit_available_from_today': |
| 594 | - return function_exists( 'wpbc_get_general_availability_url' ) | |
| 601 | + case 'working_time': | |
| 602 | + $url = function_exists( 'wpbc_get_general_availability_url' ) | |
| 595 | 603 | ? esc_url_raw( wpbc_get_general_availability_url( true, false ) ) |
| 596 | 604 | : esc_url_raw( admin_url( 'admin.php?page=wpbc-availability&tab=general_availability' ) ); |
| 605 | + $url = add_query_arg( 'wpbc_ag_open', 'working_time', $url ); | |
| 606 | + break; | |
| 597 | 607 | |
| 598 | 608 | default: |
| 599 | 609 | return ''; |
| 600 | 610 | } |
| @@ -616,9 +626,9 @@ | ||
| 616 | 626 | return array( |
| 617 | 627 | 'resource_availability' => array( |
| 618 | 628 | 'source_type' => 'days_availability', |
| 619 | 629 | 'title' => __( 'Unavailable day', 'booking' ), |
| 620 | - 'source' => __( 'Days Availability', 'booking' ), | |
| 630 | + 'source' => __( 'Block Dates', 'booking' ), | |
| 621 | 631 | ), |
| 622 | 632 | 'weekday_unavailable' => array( |
| 623 | 633 | 'source_type' => 'weekday_unavailable', |
| 624 | 634 | 'title' => __( 'Unavailable weekday', 'booking' ), |
| @@ -626,9 +636,9 @@ | ||
| 626 | 636 | ), |
| 627 | 637 | 'season_filter' => array( |
| 628 | 638 | 'source_type' => 'season_filter', |
| 629 | 639 | 'title' => __( 'Season unavailable', 'booking' ), |
| 630 | - 'source' => __( 'Season Availability', 'booking' ), | |
| 640 | + 'source' => __( 'Seasonal Availability', 'booking' ), | |
| 631 | 641 | ), |
| 632 | 642 | 'from_today_unavailable' => array( |
| 633 | 643 | 'source_type' => 'from_today_unavailable', |
| 634 | 644 | 'title' => __( 'Unavailable from today', 'booking' ), |
| @@ -735,9 +745,10 @@ | ||
| 735 | 745 | } |
| 736 | 746 | |
| 737 | 747 | foreach ( $date_obj[ $resource_id ]->__booked__times__details as $time_key => $details ) { |
| 738 | 748 | $seconds = wpbc_availability_timeslots__time_key_to_seconds( $time_key ); |
| 739 | - $source_type = ( isset( $details->source_type ) && 'availability_timeslot' === $details->source_type ) ? 'availability_timeslot' : 'booking'; | |
| 749 | + $source_type = isset( $details->source_type ) ? sanitize_key( $details->source_type ) : 'booking'; | |
| 750 | + $source_type = in_array( $source_type, array( 'availability_timeslot', 'working_time' ), true ) ? $source_type : 'booking'; | |
| 740 | 751 | $time_text = wpbc_transform_timerange__seconds_arr__in__formated_hours( $seconds ); |
| 741 | 752 | $booking_id = isset( $details->booking_id ) ? (string) $details->booking_id : ''; |
| 742 | 753 | $booking_url = ''; |
| 743 | 754 | $status_text = ''; |
| @@ -754,8 +765,16 @@ | ||
| 754 | 765 | $booking_id, |
| 755 | 766 | $status_text, |
| 756 | 767 | $time_text |
| 757 | 768 | ); |
| 769 | + } elseif ( 'working_time' === $source_type ) { | |
| 770 | + $status_text = __( 'Outside working time', 'booking' ); | |
| 771 | + $tooltip = sprintf( | |
| 772 | + /* translators: 1: unavailable time, 2: rule source. */ | |
| 773 | + __( '%1$s | Source: %2$s', 'booking' ), | |
| 774 | + $time_text, | |
| 775 | + __( 'Working Time', 'booking' ) | |
| 776 | + ); | |
| 758 | 777 | } else { |
| 759 | 778 | $status_text = __( 'Unavailable time-slot', 'booking' ); |
| 760 | 779 | $tooltip = sprintf( |
| 761 | 780 | /* translators: 1: unavailable time-slot id, 2: unavailable time. */ |
| @@ -765,9 +784,9 @@ | ||
| 765 | 784 | ); |
| 766 | 785 | } |
| 767 | 786 | |
| 768 | 787 | $bars[ $date ][ $resource_id ][] = array( |
| 769 | - 'type' => ( 'availability_timeslot' === $source_type ) ? 'blocked' : 'booked', | |
| 788 | + 'type' => ( 'working_time' === $source_type ) ? 'working_time' : ( ( 'availability_timeslot' === $source_type ) ? 'blocked' : 'booked' ), | |
| 770 | 789 | 'source_type' => $source_type, |
| 771 | 790 | 'editable' => ( 'availability_timeslot' === $source_type ), |
| 772 | 791 | 'start_second' => (int) $seconds[0], |
| 773 | 792 | 'end_second' => (int) $seconds[1], |
| @@ -774,8 +793,10 @@ | ||
| 774 | 793 | 'start_minute' => (int) floor( $seconds[0] / MINUTE_IN_SECONDS ), |
| 775 | 794 | 'end_minute' => (int) ceil( $seconds[1] / MINUTE_IN_SECONDS ), |
| 776 | 795 | 'booking_id' => $booking_id, |
| 777 | 796 | 'booking_url' => $booking_url, |
| 797 | + 'rule_url' => ( 'working_time' === $source_type ) ? wpbc_availability_timeslots__get_unavailable_rule_url( 'working_time', $resource_id ) : '', | |
| 798 | + 'rule_source' => ( 'working_time' === $source_type ) ? __( 'Working Time', 'booking' ) : '', | |
| 778 | 799 | 'unavailable_timeslot_id' => isset( $details->unavailable_timeslot_id ) ? (int) $details->unavailable_timeslot_id : 0, |
| 779 | 800 | 'approved' => isset( $details->approved ) ? (string) $details->approved : '', |
| 780 | 801 | 'status_title' => $status_text, |
| 781 | 802 | 'time_text' => $time_text, |
| @@ -788,8 +809,89 @@ | ||
| 788 | 809 | return $bars; |
| 789 | 810 | } |
| 790 | 811 | |
| 791 | 812 | /** |
| 813 | + * Add read-only bars for time outside configured Working Time. | |
| 814 | + * | |
| 815 | + * @param array $bars Timeline bars. | |
| 816 | + * @param array $dates Dates to check. | |
| 817 | + * @param array $resource_ids Resource IDs. | |
| 818 | + * | |
| 819 | + * @return array | |
| 820 | + */ | |
| 821 | +function wpbc_availability_timeslots__add_working_time_bars( $bars, $dates, $resource_ids ) { | |
| 822 | + | |
| 823 | + if ( ! function_exists( 'wpbc_working_time__get_non_working_intervals_for_date' ) ) { | |
| 824 | + return $bars; | |
| 825 | + } | |
| 826 | + | |
| 827 | + $dates = array_values( array_filter( array_map( 'wpbc_availability_timeslots__sanitize_date', $dates ) ) ); | |
| 828 | + $resource_ids = array_values( array_filter( array_map( 'intval', $resource_ids ) ) ); | |
| 829 | + | |
| 830 | + foreach ( $dates as $date ) { | |
| 831 | + foreach ( $resource_ids as $resource_id ) { | |
| 832 | + if ( ! empty( $bars[ $date ][ $resource_id ] ) ) { | |
| 833 | + foreach ( $bars[ $date ][ $resource_id ] as $existing_bar ) { | |
| 834 | + if ( 'unavailable_day' === ( isset( $existing_bar['type'] ) ? $existing_bar['type'] : '' ) ) { | |
| 835 | + continue 2; | |
| 836 | + } | |
| 837 | + } | |
| 838 | + } | |
| 839 | + | |
| 840 | + $intervals = wpbc_working_time__get_non_working_intervals_for_date( $resource_id, $date ); | |
| 841 | + | |
| 842 | + if ( empty( $intervals ) ) { | |
| 843 | + continue; | |
| 844 | + } | |
| 845 | + | |
| 846 | + if ( ! isset( $bars[ $date ] ) ) { | |
| 847 | + $bars[ $date ] = array(); | |
| 848 | + } | |
| 849 | + if ( ! isset( $bars[ $date ][ $resource_id ] ) ) { | |
| 850 | + $bars[ $date ][ $resource_id ] = array(); | |
| 851 | + } | |
| 852 | + | |
| 853 | + foreach ( $intervals as $interval ) { | |
| 854 | + $start_second = wpbc_availability_timeslots__sanitize_second( $interval['start_second'] ); | |
| 855 | + $end_second = wpbc_availability_timeslots__sanitize_second( $interval['end_second'] ); | |
| 856 | + | |
| 857 | + if ( $start_second >= $end_second ) { | |
| 858 | + continue; | |
| 859 | + } | |
| 860 | + | |
| 861 | + $time_text = wpbc_transform_timerange__seconds_arr__in__formated_hours( array( $start_second, $end_second ) ); | |
| 862 | + | |
| 863 | + $bars[ $date ][ $resource_id ][] = array( | |
| 864 | + 'type' => 'working_time', | |
| 865 | + 'source_type' => 'working_time', | |
| 866 | + 'editable' => false, | |
| 867 | + 'start_second' => $start_second, | |
| 868 | + 'end_second' => $end_second, | |
| 869 | + 'start_minute' => (int) floor( $start_second / MINUTE_IN_SECONDS ), | |
| 870 | + 'end_minute' => (int) ceil( $end_second / MINUTE_IN_SECONDS ), | |
| 871 | + 'booking_id' => '', | |
| 872 | + 'booking_url' => '', | |
| 873 | + 'rule_url' => wpbc_availability_timeslots__get_unavailable_rule_url( 'working_time', $resource_id ), | |
| 874 | + 'rule_source' => __( 'Working Time', 'booking' ), | |
| 875 | + 'unavailable_timeslot_id' => 0, | |
| 876 | + 'approved' => '', | |
| 877 | + 'status_title' => __( 'Outside working time', 'booking' ), | |
| 878 | + 'time_text' => $time_text, | |
| 879 | + 'tooltip' => sprintf( | |
| 880 | + /* translators: 1: unavailable time, 2: rule source. */ | |
| 881 | + __( '%1$s | Source: %2$s', 'booking' ), | |
| 882 | + $time_text, | |
| 883 | + __( 'Working Time', 'booking' ) | |
| 884 | + ), | |
| 885 | + ); | |
| 886 | + } | |
| 887 | + } | |
| 888 | + } | |
| 889 | + | |
| 890 | + return $bars; | |
| 891 | +} | |
| 892 | + | |
| 893 | +/** | |
| 792 | 894 | * AJAX: read blocked time slots for a calendar/frontend request. |
| 793 | 895 | * |
| 794 | 896 | * @return void |
| 795 | 897 | */ |
| @@ -816,8 +918,9 @@ | ||
| 816 | 918 | ) |
| 817 | 919 | ); |
| 818 | 920 | $resource_ids = isset( $availability['resources_id_arr__in_dates'] ) ? $availability['resources_id_arr__in_dates'] : array( $resource_id ); |
| 819 | 921 | $timeline_bars = wpbc_availability_timeslots__availability_to_bars( $availability['dates'], $resource_ids ); |
| 922 | + $timeline_bars = wpbc_availability_timeslots__add_working_time_bars( $timeline_bars, $dates_to_check, $resource_ids ); | |
| 820 | 923 | |
| 821 | 924 | wp_send_json_success( |
| 822 | 925 | array( |
| 823 | 926 | 'resource_id' => $resource_id, |