Jake Vanderwerf
5 days ago 0dfe1d8afafc59c4a5559c498342668d5a58d6ef
inc/helpers/time.php
@@ -1,6 +1,8 @@
<?php
use JVBase\managers\CacheManager;
use JVBase\base\Options;
use JVBase\managers\Cache;
use JVBase\meta\Meta;
if (!defined('ABSPATH')) {
   exit;
@@ -135,34 +137,36 @@
/**
 * @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 = CacheManager::for('hours-'.$ID, WEEK_IN_SECONDS)->connectTo('taxonomy');
   $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 = '';
@@ -185,7 +189,7 @@
      $out .= '<p class="hours-notes"><small>' . implode(' • ', $notes) . '</small></p>';
   }
   $cache->set($key, $out);
   $cache->set($ID, $out);
   return $out;
}
@@ -308,10 +312,11 @@
 * @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
@@ -328,12 +333,12 @@
   $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;
@@ -356,14 +361,14 @@
/**
 * 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'];
   }
@@ -391,15 +396,15 @@
 *
 * @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++) {
@@ -408,11 +413,11 @@
      $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;
      }
@@ -433,16 +438,16 @@
      // Format the result
      if ($i === 0) {
         return 'Opens today at ' . jvbFormat12HourTime($open_time);
         return '&emsp;Opens today at ' . jvbFormat12HourTime($open_time);
      } elseif ($i === 1) {
         return 'Opens tomorrow at ' . jvbFormat12HourTime($open_time);
         return '&emsp;Opens tomorrow at ' . jvbFormat12HourTime($open_time);
      } else {
         $day_formatted = ucfirst($day_name);
         return "Opens {$day_formatted} at " . jvbFormat12HourTime($open_time);
         return "&emsp;Opens {$day_formatted} at " . jvbFormat12HourTime($open_time);
      }
   }
   return null;
   return '';
}
@@ -454,7 +459,7 @@
 */
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;
      }
   }