| | |
| | | <?php |
| | | |
| | | use JVBase\base\Options; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\meta\Meta; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | | } |
| | |
| | | |
| | | /** |
| | | * @param int $ID |
| | | * @param JVBase\Meta\MetaManager $meta |
| | | * @param string $type |
| | | * |
| | | * @return string |
| | | */ |
| | | function jvbRenderHours(int $ID, JVBase\Meta\MetaManager $meta):string |
| | | function jvbRenderHours(int $ID, string $type = ''):string |
| | | { |
| | | $cache = new JVBase\managers\CacheManager('hours-'.$ID, WEEK_IN_SECONDS); |
| | | $key = 'hours_display'; |
| | | $cached = $cache->get($key); |
| | | $cache = Cache::for('hours_display', WEEK_IN_SECONDS)->connect('taxonomy')->connect('post')->connect('user'); |
| | | |
| | | $cached = $cache->get($ID); |
| | | |
| | | if ($cached !== false) { |
| | | return $cached; |
| | | } |
| | | |
| | | $meta = match($type){ |
| | | 'post' => Meta::forPost($ID), |
| | | 'term' => Meta::forTerm($ID), |
| | | 'user' => Meta::forUser($ID), |
| | | default => false |
| | | }; |
| | | if (!$meta) { |
| | | if (term_exists((int)$ID)) { |
| | | $type = 'term'; |
| | | } elseif (get_post_status((int)$ID)) { |
| | | $type = 'post'; |
| | | } else { |
| | | $type = 'user'; |
| | | } |
| | | $meta = new JVBase\meta\MetaManager($ID, $type); |
| | | $meta = jvbGetMeta($ID); |
| | | } |
| | | if (!$meta) { |
| | | return ''; |
| | | } |
| | | |
| | | $hours = $meta->getValue('hours'); |
| | | $byAppt = $meta->getValue('by_appointment'); |
| | | $walkins = $meta->getValue('walkins'); |
| | | $hours = $meta->get('openingHours'); |
| | | $byAppt = $meta->get('by_appointment'); |
| | | $walkins = $meta->get('walkins'); |
| | | |
| | | $out = ''; |
| | | |
| | |
| | | $out .= '<p class="hours-notes"><small>' . implode(' • ', $notes) . '</small></p>'; |
| | | } |
| | | |
| | | $cache->set($key, $out); |
| | | $cache->set($ID, $out); |
| | | return $out; |
| | | } |
| | | |
| | |
| | | * @param string $timezone Timezone string (default: 'America/Edmonton') |
| | | * @return bool True if currently open, false if closed |
| | | */ |
| | | function jvbIsCurrentlyOpen($hours_data = null, $timezone = 'America/Edmonton') { |
| | | function jvbIsCurrentlyOpen(?array $hours_data = null, string $timezone = 'America/Edmonton'):bool |
| | | { |
| | | // Get hours data if not provided |
| | | if ($hours_data === null) { |
| | | $hours_data = get_option(BASE.'hours', []); |
| | | $hours_data = Options::get('openingHours'); |
| | | } |
| | | |
| | | // Return false if no hours data |
| | |
| | | $today_hours = $hours_data[$current_day] ?? []; |
| | | |
| | | // Return false if closed today |
| | | if (empty($today_hours) || !($today_hours['open'] ?? false)) { |
| | | if (empty($today_hours) || !($today_hours['isOpen'] ?? false)) { |
| | | return false; |
| | | } |
| | | |
| | | $open_time = $today_hours['time_opens'] ?? ''; |
| | | $close_time = $today_hours['time_closes'] ?? ''; |
| | | $open_time = $today_hours['opens'] ?? ''; |
| | | $close_time = $today_hours['closes'] ?? ''; |
| | | |
| | | if (!$open_time || !$close_time) { |
| | | return false; |
| | |
| | | /** |
| | | * Check if current time is between two specified times |
| | | * |
| | | * @param string $start_time Start time in H:i format (e.g., '10:00') |
| | | * @param string $end_time End time in H:i format (e.g., '15:15') |
| | | * @param ?string $start_time Start time in H:i format (e.g., '10:00') |
| | | * @param ?string $end_time End time in H:i format (e.g., '15:15') |
| | | * @param string $timezone Timezone string (default: 'America/Edmonton') |
| | | * @return bool True if current time is within range, false otherwise |
| | | */ |
| | | function jvbIsTimeBetween($start_time=null, $end_time=null, $timezone = 'America/Edmonton') { |
| | | function jvbIsTimeBetween(?string $start_time = null, ?string $end_time = null, string $timezone = 'America/Edmonton'):bool { |
| | | if (!$start_time && !$end_time) { |
| | | $hours = get_option(BASE.'today_hours'); |
| | | $hours = Options::get('today_hours'); |
| | | $start_time = $hours['time_start']; |
| | | $end_time = $hours['time_end']; |
| | | } |
| | |
| | | * |
| | | * @param array $hours_data Day-based hours data |
| | | * @param string $timezone Timezone string |
| | | * @return string|null Next opening time description or null if never opens |
| | | * @return string Next opening time description or empty string if never opens |
| | | * @throws DateInvalidTimeZoneException |
| | | */ |
| | | function jvbGetNextOpeningTime(array $hours_data, string $timezone = 'America/Edmonton'): ?string { |
| | | function jvbGetNextOpeningTime(array $hours_data, string $timezone = 'America/Edmonton'):string { |
| | | if (!jvbHasOperatingHours($hours_data)) { |
| | | return null; |
| | | return ''; |
| | | } |
| | | |
| | | $current_time = new DateTime('now', new DateTimeZone($timezone)); |
| | | $weekdays = jvbFullWeekdays(); |
| | | |
| | | // Check next 7 days |
| | | for ($i = 0; $i < 7; $i++) { |
| | |
| | | $day_name = strtolower($check_date->format('l')); |
| | | |
| | | $day_data = $hours_data[$day_name] ?? []; |
| | | if (empty($day_data) || !($day_data['open'] ?? false)) { |
| | | if (empty($day_data) || !($day_data['isOpen'] ?? false)) { |
| | | continue; |
| | | } |
| | | |
| | | $open_time = $day_data['time_opens'] ?? ''; |
| | | $open_time = $day_data['opens'] ?? ''; |
| | | if (!$open_time) { |
| | | continue; |
| | | } |
| | |
| | | |
| | | // Format the result |
| | | if ($i === 0) { |
| | | return 'Opens today at ' . jvbFormat12HourTime($open_time); |
| | | return ' Opens today at ' . jvbFormat12HourTime($open_time); |
| | | } elseif ($i === 1) { |
| | | return 'Opens tomorrow at ' . jvbFormat12HourTime($open_time); |
| | | return ' Opens tomorrow at ' . jvbFormat12HourTime($open_time); |
| | | } else { |
| | | $day_formatted = ucfirst($day_name); |
| | | return "Opens {$day_formatted} at " . jvbFormat12HourTime($open_time); |
| | | return " Opens {$day_formatted} at " . jvbFormat12HourTime($open_time); |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | return ''; |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | function jvbHasOperatingHours(array $hours_data): bool { |
| | | foreach ($hours_data as $day_data) { |
| | | if (!empty($day_data) && ($day_data['open'] ?? false)) { |
| | | if (!empty($day_data) && ($day_data['isOpen'] ?? false)) { |
| | | return true; |
| | | } |
| | | } |