| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. |
| 3 |
add_action('wp_enqueue_scripts', 'eshb_wp_enqueue_scripts', 999); |
| 4 |
function eshb_wp_enqueue_scripts (){ |
| 5 |
|
| 6 |
$css_version = filemtime( ESHB_PL_PATH . 'public/assets/css/public.css' ); |
| 7 |
|
| 8 |
wp_enqueue_style( 'dashicons' ); |
| 9 |
wp_enqueue_style( 'eshb-daterangepicker-style', ESHB_PL_URL . 'public/assets/css/date-range-picker.css', array(), $css_version ); |
| 10 |
wp_enqueue_style( 'eshb-style', ESHB_PL_URL . 'public/assets/css/public.css', array(), $css_version ); |
| 11 |
// Show a formatted, translated date on top of the machine (Y-m-d) date input. |
| 12 |
// The machine input stays in normal flow (so the calendar opens exactly as before); |
| 13 |
// only its text is hidden. The display overlay is click-through (pointer-events:none). |
| 14 |
wp_add_inline_style( 'eshb-style', '.eshb-date-field{position:relative;display:block;width:100%}.eshb-date-field .eshb-date-machine{color:transparent;-webkit-text-fill-color:transparent;caret-color:transparent}.eshb-date-field .eshb-date-machine::selection{background:transparent}.eshb-date-field .eshb-date-display{position:absolute;top:0;left:0;width:100%;height:100%;margin:0;pointer-events:none;background:transparent;border-color:transparent;box-shadow:none}.daterangepicker td.active.start-date{pointer-events:none;cursor:not-allowed}' ); |
| 15 |
wp_enqueue_style( 'eshb-fontawesome-style', ESHB_PL_URL . 'public/assets/css/fontawesome.all.min.css', array(), '7.2.0', 'all' ); |
| 16 |
wp_enqueue_style( 'swiper', ESHB_PL_URL . 'public/assets/css/swiper-bundle.min.css', array(), $css_version, 'all' ); |
| 17 |
wp_enqueue_script( 'jquery' ); |
| 18 |
wp_enqueue_script( 'moment' ); |
| 19 |
wp_enqueue_script( 'eshb-date-range-picker-js', ESHB_PL_URL . 'public/assets/js/date-range-picker.js', array('jquery'),'3.1',true ); |
| 20 |
wp_enqueue_script( 'eshb-swiper', ESHB_PL_URL . 'public/assets/js/swiper-bundle.min.js', array(),'12.1.4',false ); |
| 21 |
wp_enqueue_script( 'eshb-public-script', ESHB_PL_URL . 'public/assets/js/public.js', array(),'1.0.0',true ); |
| 22 |
wp_enqueue_script( 'eshb-booking-script', ESHB_PL_URL . 'public/assets/js/booking.js', array(), ESHB_VERSION, true ); |
| 23 |
|
| 24 |
|
| 25 |
// Get WordPress current locale |
| 26 |
$locale = get_locale(); |
| 27 |
$eshb_settings = get_option('eshb_settings'); |
| 28 |
$apply_text_default = isset($eshb_settings['string_apply']) && !empty($eshb_settings['string_apply']) ? $eshb_settings['string_apply'] : ''; |
| 29 |
$cancel_text_default = isset($eshb_settings['string_cancel']) && !empty($eshb_settings['string_cancel']) ? $eshb_settings['string_cancel'] : ''; |
| 30 |
|
| 31 |
ESHB_Helper::eshb_set_accomodation_localize(); |
| 32 |
|
| 33 |
// Cart blocking notice config for JS injection |
| 34 |
$eshb_notice_msg = ! empty( $eshb_settings['cart-blocking-notice-msg'] ) |
| 35 |
? esc_html( $eshb_settings['cart-blocking-notice-msg'] ) |
| 36 |
: esc_html__( 'Your reservation is held for', 'easy-hotel' ); |
| 37 |
wp_localize_script( 'eshb-booking-script', 'eshb_cart_notice', [ |
| 38 |
'enabled' => ! empty( $eshb_settings['cart-blocking-switcher'] ) ? '1' : '0', |
| 39 |
'msg' => $eshb_notice_msg, |
| 40 |
] ); |
| 41 |
|
| 42 |
wp_localize_script('eshb-public-script', 'eshb_rest', [ |
| 43 |
'root' => esc_url(rest_url()), |
| 44 |
'nonce' => wp_create_nonce('wp_rest') |
| 45 |
]); |
| 46 |
|
| 47 |
// Convert the WordPress "date_format" (Settings > General) into a moment.js format |
| 48 |
// so the calendar can display dates exactly as WordPress does, per the site locale. |
| 49 |
// The conversion has to walk the format token by token rather than character by |
| 50 |
// character: locales escape literal words with a backslash — Spanish ships |
| 51 |
// "j \d\e F \d\e Y" — and a plain strtr() rewrites the "de" inside those escapes |
| 52 |
// into date tokens, printing "4 DDe agosto DDe 2026". |
| 53 |
$eshb_moment_format = ESHB_Helper::eshb_date_format_to_moment( get_option( 'date_format', 'F j, Y' ) ); |
| 54 |
|
| 55 |
// Translated month + short weekday names (from WordPress translations, so they are |
| 56 |
// guaranteed localized even when moment.js has no bundled locale on the page). |
| 57 |
// Each sample instant is built at local noon in the site timezone: wp_date() renders |
| 58 |
// in the site timezone, so a UTC-midnight sample would roll back to the previous |
| 59 |
// day/month on any timezone behind UTC and shift the whole array by one. |
| 60 |
$eshb_tz = wp_timezone(); |
| 61 |
|
| 62 |
$eshb_months = array(); |
| 63 |
for ( $m = 1; $m <= 12; $m++ ) { |
| 64 |
$eshb_month_sample = new DateTime( sprintf( '2025-%02d-15 12:00:00', $m ), $eshb_tz ); |
| 65 |
$eshb_months[] = wp_date( 'F', $eshb_month_sample->getTimestamp() ); |
| 66 |
} |
| 67 |
|
| 68 |
$eshb_weekdays_short = array(); // Sunday-first to match moment.js weekdaysShort() |
| 69 |
$eshb_weekday_sample = new DateTime( '2025-01-05 12:00:00', $eshb_tz ); // a Sunday |
| 70 |
for ( $d = 0; $d < 7; $d++ ) { |
| 71 |
$eshb_weekdays_short[] = wp_date( 'D', $eshb_weekday_sample->getTimestamp() ); |
| 72 |
$eshb_weekday_sample->modify( '+1 day' ); |
| 73 |
} |
| 74 |
|
| 75 |
// moment.monthsShort() drives the calendar header; without it the header falls back |
| 76 |
// to moment's built-in English names regardless of the site locale. |
| 77 |
$eshb_months_short = array(); |
| 78 |
for ( $m = 1; $m <= 12; $m++ ) { |
| 79 |
$eshb_month_sample = new DateTime( sprintf( '2025-%02d-15 12:00:00', $m ), $eshb_tz ); |
| 80 |
$eshb_months_short[] = wp_date( 'M', $eshb_month_sample->getTimestamp() ); |
| 81 |
} |
| 82 |
|
| 83 |
// Prepare translations dynamically based on current locale |
| 84 |
$translations = [ |
| 85 |
'displayFormat' => $eshb_moment_format, |
| 86 |
'locale' => str_replace( '_', '-', strtolower( $locale ) ), |
| 87 |
'months' => $eshb_months, |
| 88 |
'monthsShort' => $eshb_months_short, |
| 89 |
'weekdaysShort' => $eshb_weekdays_short, |
| 90 |
'applyLabel' => !empty($apply_text_default) ? eshb_get_translated_string($apply_text_default) : __('Apply', 'easy-hotel'), |
| 91 |
'cancelLabel' => !empty($cancel_text_default) ? eshb_get_translated_string($cancel_text_default) : __('Cancel', 'easy-hotel'), |
| 92 |
'BookedTooltip' => __('Already Booked!', 'easy-hotel'), |
| 93 |
'holidayTooltip' => __('This is Holiday!', 'easy-hotel'), |
| 94 |
'fromLabel' => __('From', 'easy-hotel'), |
| 95 |
'toLabel' => __('To', 'easy-hotel'), |
| 96 |
'customRangeLabel' => __('Custom Range', 'easy-hotel'), |
| 97 |
'weekLabel' => __('W', 'easy-hotel'), |
| 98 |
'firstDay' => get_option('start_of_week'), // Get WordPress's start of the week setting |
| 99 |
'currentLocale' => $locale, // Pass current locale for debugging or further use |
| 100 |
]; |
| 101 |
|
| 102 |
// Pass translations to JavaScript |
| 103 |
wp_localize_script('eshb-date-range-picker-js', 'eshb_daterangepicker_i18n', $translations); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|