| 1 |
<?php /** |
| 2 |
* @version 1.0 |
| 3 |
* @package Booking Calendar - JavaScript |
| 4 |
* @category Define JavaScript variables |
| 5 |
* @author wpdevelop |
| 6 |
* |
| 7 |
* @web-site https://wpbookingcalendar.com/ |
| 8 |
* @email info@wpbookingcalendar.com |
| 9 |
* |
| 10 |
* @modified 19.10.2015 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Get script string for localised variables. |
| 18 |
* |
| 19 |
* @return string |
| 20 |
*/ |
| 21 |
function wpbc_get_localized_js_vars() { |
| 22 |
|
| 23 |
// $script = 'var1 = '. wp_json_encode('var1') .'; '; // JSON.parse(wpbc_global_var); //. |
| 24 |
|
| 25 |
$script = ''; |
| 26 |
$script .= "_wpbc.set_other_param( 'locale_active', '" . esc_js( wpbc_get_maybe_reloaded_booking_locale() ) . "' ); "; |
| 27 |
|
| 28 |
// FixIn: 10.8.1.4. |
| 29 |
$gmt_time = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) ); |
| 30 |
|
| 31 |
$script .= "_wpbc.set_other_param( 'time_gmt_arr', [" . wpbc_datetime_localized__no_wp_timezone( $gmt_time, 'Y,m,d,H,i' ) . '] ); '; |
| 32 |
$script .= "_wpbc.set_other_param( 'time_local_arr', [" . wpbc_datetime_localized__use_wp_timezone( $gmt_time, 'Y,m,d,H,i' ) . '] ); '; |
| 33 |
|
| 34 |
$unavailable_time_from_today = get_bk_option( 'booking_unavailable_days_num_from_today' ); |
| 35 |
|
| 36 |
if ( ! empty( $unavailable_time_from_today ) ) { |
| 37 |
if ( 'm' === substr( $unavailable_time_from_today, - 1 ) ) { |
| 38 |
|
| 39 |
$gmt_time = gmdate( 'Y-m-d H:i:s', strtotime( '+' . ( intval( $unavailable_time_from_today ) - 1 ) . ' minutes' ) ); |
| 40 |
|
| 41 |
// Local timezone unavailable time. // FixIn: 10.9.6.3. |
| 42 |
$local_unavailable_datetime_ymdhis = wpbc_datetime_localized__use_wp_timezone( $gmt_time, 'Y-m-d H:i:s' ); // Local unavailable. |
| 43 |
// Local timezone Now time. |
| 44 |
$local_now_datetime_ymdhis = wpbc_datetime_localized__use_wp_timezone( strtotime( 'now' ), 'Y-m-d H:i:s' ); // Local Now. |
| 45 |
// Local timezone Today midnight time. |
| 46 |
$local_now_datetime_ymd000 = wpbc_datetime_localized__use_wp_timezone( strtotime( 'now' ), 'Y-m-d 00:00:00' ); // Local Midnight. |
| 47 |
|
| 48 |
// Check time from local MIDNIGHT to Unavailable. |
| 49 |
$days_number_shift = intval( ( strtotime( $local_unavailable_datetime_ymdhis ) - strtotime( $local_now_datetime_ymd000 ) ) / 86400 ); |
| 50 |
$unavailable_time_from_today = $days_number_shift; |
| 51 |
// $unavailable_time_from_today = '0'; // FixIn: 10.9.2.6. |
| 52 |
} |
| 53 |
} else { |
| 54 |
$unavailable_time_from_today = '0'; |
| 55 |
} |
| 56 |
|
| 57 |
$today_local = wpbc_datetime_localized__use_wp_timezone( $gmt_time, 'Y,m,d,H,i' ); |
| 58 |
|
| 59 |
$script .= "_wpbc.set_other_param( 'today_arr', [" . $today_local . "] ); "; |
| 60 |
|
| 61 |
$script .= "_wpbc.set_other_param( 'url_plugin', '" . esc_url( plugins_url( '', WPBC_FILE ) ) . "' ); "; |
| 62 |
|
| 63 |
$get_booking_hash = ( ( isset( $_GET['booking_hash'] ) ) ? sanitize_text_field( wp_unslash( $_GET['booking_hash'] ) ) : '' ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */ |
| 64 |
|
| 65 |
$script .= "_wpbc.set_other_param( 'this_page_booking_hash', '" . esc_js( $get_booking_hash ) . "' ); "; |
| 66 |
|
| 67 |
|
| 68 |
$script .= "_wpbc.set_other_param( 'calendars__on_this_page', [] ); "; |
| 69 |
|
| 70 |
$script .= "_wpbc.set_other_param( 'calendars__first_day', '" . intval( get_bk_option( 'booking_start_day_weeek' ) ) . "' ); "; //."\n"; |
| 71 |
$script .= "_wpbc.set_other_param( 'calendars__max_monthes_in_calendar', '" . esc_js( get_bk_option( 'booking_max_monthes_in_calendar' ) ) . "' ); "; |
| 72 |
// FixIn: 10.8.1.4. |
| 73 |
$script .= "_wpbc.set_other_param( 'availability__unavailable_from_today', '" . esc_js( $unavailable_time_from_today ) . "' ); "; //Default: 0 ' Old JS: block_some_dates_from_today' _wpbc.get_other_param( 'availability__unavailable_from_today' ) |
| 74 |
if ( class_exists( 'wpdev_bk_biz_m' ) ) { |
| 75 |
$script .= "_wpbc.set_other_param( 'availability__available_from_today', '" . esc_js( get_bk_option( 'booking_available_days_num_from_today' ) ) . "' ); "; //Default '' - all days. Old JS: 'wpbc_available_days_num_from_today' |
| 76 |
} |
| 77 |
$script .= "_wpbc.set_other_param( 'availability__week_days_unavailable', [" |
| 78 |
. ( ( get_bk_option( 'booking_unavailable_day0') == 'On' ) ? '0,' : '' ) |
| 79 |
. ( ( get_bk_option( 'booking_unavailable_day1') == 'On' ) ? '1,' : '' ) |
| 80 |
. ( ( get_bk_option( 'booking_unavailable_day2') == 'On' ) ? '2,' : '' ) |
| 81 |
. ( ( get_bk_option( 'booking_unavailable_day3') == 'On' ) ? '3,' : '' ) |
| 82 |
. ( ( get_bk_option( 'booking_unavailable_day4') == 'On' ) ? '4,' : '' ) |
| 83 |
. ( ( get_bk_option( 'booking_unavailable_day5') == 'On' ) ? '5,' : '' ) |
| 84 |
. ( ( get_bk_option( 'booking_unavailable_day6') == 'On' ) ? '6,' : '' ) |
| 85 |
. "999] ); "; // 999 - blank day, which will not impact to the checking of the week days. Required for correct creation of this array. |
| 86 |
// General days selection. |
| 87 |
$days_selection_arr = wpbc__calendar__js_params__get_days_selection_arr(); |
| 88 |
|
| 89 |
$script .= "_wpbc.set_other_param( 'calendars__days_select_mode', '" . $days_selection_arr['days_select_mode'] . "' ); "; |
| 90 |
$script .= "_wpbc.set_other_param( 'calendars__fixed__days_num', " . $days_selection_arr['fixed__days_num'] . " ); "; |
| 91 |
$script .= "_wpbc.set_other_param( 'calendars__fixed__week_days__start', [" . $days_selection_arr['fixed__week_days__start'] . "] ); "; |
| 92 |
$script .= "_wpbc.set_other_param( 'calendars__dynamic__days_min', " . $days_selection_arr['dynamic__days_min'] . " ); "; |
| 93 |
$script .= "_wpbc.set_other_param( 'calendars__dynamic__days_max', " . $days_selection_arr['dynamic__days_max'] . " ); "; |
| 94 |
$script .= "_wpbc.set_other_param( 'calendars__dynamic__days_specific', [" . $days_selection_arr['dynamic__days_specific'] . "] ); "; |
| 95 |
$script .= "_wpbc.set_other_param( 'calendars__dynamic__week_days__start', [" . $days_selection_arr['dynamic__week_days__start'] . "] ); "; |
| 96 |
|
| 97 |
// FixIn: 10.3.0.9. |
| 98 |
if ( false !== strpos( get_bk_option( 'booking_skin' ), 'light__24_8' ) ) { |
| 99 |
$script .= "_wpbc.set_other_param( 'calendars__days_selection__middle_days_opacity', '0.5' ); "; |
| 100 |
} else { |
| 101 |
$script .= "_wpbc.set_other_param( 'calendars__days_selection__middle_days_opacity', '0.75' ); "; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
// Defined in BS. |
| 106 |
$script .= "_wpbc.set_other_param( 'is_enabled_booking_recurrent_time', " . ( ( get_bk_option( 'booking_recurrent_time' ) !== 'On' ) ? 'false' : 'true' ) . " ); "; |
| 107 |
$script .= "_wpbc.set_other_param( 'is_allow_several_months_on_mobile', " . ( ( get_bk_option( 'booking_calendar_allow_several_months_on_mobile' ) !== 'On' ) ? 'false' : 'true' ) . " ); "; |
| 108 |
$script .= "_wpbc.set_other_param( 'is_enabled_change_over', " . ( |
| 109 |
( |
| 110 |
( function_exists( 'wpbc_is_booking_used_check_in_out_time' ) ) |
| 111 |
&& ( wpbc_is_booking_used_check_in_out_time() ) |
| 112 |
) ? 'true' : 'false' |
| 113 |
) . " ); "; |
| 114 |
if ( class_exists( 'wpdev_bk_biz_l' ) ) { |
| 115 |
$script .= "_wpbc.set_other_param( 'is_enabled_booking_search_results_days_select', '" . esc_js( get_bk_option( 'booking_search_results_days_select' ) ) . "' ); "; |
| 116 |
} |
| 117 |
|
| 118 |
$script .= "_wpbc.set_other_param( 'update', '" . esc_attr( WP_BK_VERSION_NUM ) . "' ); "; |
| 119 |
$script .= "_wpbc.set_other_param( 'version', '" . wpbc_get_version_type__and_mu() . "' ); "; |
| 120 |
|
| 121 |
// Warning Messages. |
| 122 |
$script .= "_wpbc.set_message( 'message_dates_times_unavailable', " . wp_json_encode( __( 'These dates and times in this calendar are already booked or unavailable.', 'booking' ) ) . " ); "; |
| 123 |
$script .= "_wpbc.set_message( 'message_choose_alternative_dates', " . wp_json_encode( __( 'Please choose alternative date(s), times, or adjust the number of slots booked.', 'booking' ) ) . " ); "; |
| 124 |
$script .= "_wpbc.set_message( 'message_cannot_save_in_one_resource', " . wp_json_encode( __( 'It is not possible to store this sequence of the dates into the one same resource.', 'booking' ) ) . " ); "; |
| 125 |
$script .= "_wpbc.set_message( 'message_check_required', " . wp_json_encode( __( 'This field is required', 'booking' ) ) . " ); "; |
| 126 |
$script .= "_wpbc.set_message( 'message_check_required_for_check_box', " . wp_json_encode( __( 'This checkbox must be checked', 'booking' ) ) . " ); "; |
| 127 |
$script .= "_wpbc.set_message( 'message_check_required_for_radio_box', " . wp_json_encode( __( 'At least one option must be selected', 'booking' ) ) . " ); "; |
| 128 |
$script .= "_wpbc.set_message( 'message_check_email', " . wp_json_encode( __( 'Incorrect email address', 'booking' ) ) . " ); "; |
| 129 |
$script .= "_wpbc.set_message( 'message_check_same_email', " . wp_json_encode( __( 'Your emails do not match', 'booking' ) ) . " ); "; |
| 130 |
$script .= "_wpbc.set_message( 'message_check_no_selected_dates', " . wp_json_encode( __( 'Please, select booking date(s) at Calendar.', 'booking' ) ) . " ); "; |
| 131 |
$script .= "_wpbc.set_message( 'message_processing', " . wp_json_encode( __( 'Processing', 'booking' ) ) . " ); "; |
| 132 |
$script .= "_wpbc.set_message( 'message_deleting', " . wp_json_encode( __( 'Deleting', 'booking' ) ) . " ); "; |
| 133 |
$script .= "_wpbc.set_message( 'message_updating', " . wp_json_encode( __( 'Updating', 'booking' ) ) . " ); "; |
| 134 |
$script .= "_wpbc.set_message( 'message_saving', " . wp_json_encode( __( 'Saving', 'booking' ) ) . " ); "; |
| 135 |
$script .= "_wpbc.set_message( 'message_error_check_in_out_time', " . wp_json_encode( __( 'Error! Please reset your check-in/check-out dates above.', 'booking' ) ) . " ); "; |
| 136 |
$script .= "_wpbc.set_message( 'message_error_start_time', " . wp_json_encode( __( 'Start Time is invalid. The date or time may be booked, or already in the past! Please choose another date or time.', 'booking' ) ) . " ); "; |
| 137 |
$script .= "_wpbc.set_message( 'message_error_end_time', " . wp_json_encode( __( 'End Time is invalid. The date or time may be booked, or already in the past. The End Time may also be earlier that the start time, if only 1 day was selected! Please choose another date or time.', 'booking' ) ) . " ); "; |
| 138 |
$script .= "_wpbc.set_message( 'message_error_range_time', " . wp_json_encode( __( 'The time(s) may be booked, or already in the past!', 'booking' ) ) . " ); "; |
| 139 |
$script .= "_wpbc.set_message( 'message_error_duration_time', " . wp_json_encode( __( 'The time(s) may be booked, or already in the past!', 'booking' ) ) . " ); "; |
| 140 |
|
| 141 |
$script .= "console.log( '== WPBC VARS " . esc_attr( WP_BK_VERSION_NUM ) . ' [' . wpbc_get_version_type__and_mu() . "] LOADED ==' );"; |
| 142 |
return $script; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Define General inline JavaScript variables for the Booking Calendar |
| 148 |
* This function run after ENQUEUE of WPBC JavaScript files |
| 149 |
* |
| 150 |
* @param string $where_to_load - 'both'. |
| 151 |
* |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
function wpbc_localize_js_vars( $where_to_load = 'both' ) { |
| 155 |
|
| 156 |
$script_before = 'var wpbc_url_ajax =' . wp_json_encode( admin_url( 'admin-ajax.php' ) ) . ';'; |
| 157 |
wp_add_inline_script( 'wpbc_all', $script_before, 'before' ); |
| 158 |
|
| 159 |
$script = wpbc_get_localized_js_vars(); |
| 160 |
|
| 161 |
// Load this _wpbc only after loading of all scripts // FixIn: 10.1.3.4. |
| 162 |
$script = ' function wpbc_init__head(){ ' . $script . ' } '; |
| 163 |
$script .= "( function() { if ( document.readyState === 'loading' ){ document.addEventListener( 'DOMContentLoaded', wpbc_init__head ); } else { wpbc_init__head(); } }() );"; |
| 164 |
|
| 165 |
wp_add_inline_script( 'wpbc_all', $script ); |
| 166 |
|
| 167 |
/** |
| 168 |
* Help info. Order of JS events: |
| 169 |
* |
| 170 |
* window.addEventListener("load", (event) => { log.textContent += "load\n"; }); -> window.onload (which is implemented even in old browsers), which fires when the entire page loads (images, styles, etc.) |
| 171 |
* document.addEventListener("readystatechange", (event) => { log.textContent += `readystate: ${document.readyState}\n`; }); |
| 172 |
* document.addEventListener("DOMContentLoaded", (event) => { log.textContent += "DOMContentLoaded\n"; }); -> newish event which fires when the document's DOM is loaded (which may be some time before the images, etc. are loaded); |
| 173 |
* :: |
| 174 |
* DOMContentLoaded |
| 175 |
* readystate: complete |
| 176 |
* load |
| 177 |
*/ |
| 178 |
} |
| 179 |
add_action( 'wpbc_enqueue_js_files', 'wpbc_localize_js_vars', 51 ); // Need to set here 51, because some JS has 50 priority, for example at WP Booking Calendar > Availability > Days Availability page -> This hook fired after ENQUEUE of WPBC JS - wp_enqueue_script |
| 180 |
|
| 181 |
|