Derive meta-description hours from Hours & Visit data instead of hardcoding
The <meta description>, og:description, and twitter:description tags all had "Open Thursday through Saturday, 4–10pm" typed in by hand, so editing hours in the admin panel silently left the page's search/social preview copy wrong. Added hours_meta_summary() to inc/hours.php, built from the same group_hours_into_ranges() grouping the Hours & Visit card and footer already use, so all three now read the live pub_hours data and stay in sync with whatever's set under Hours & Visit. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bbe84e8614
commit
e0904b368c
2 files changed
+80
-3
No files matched your search
@@ -263,6 +263,82 @@ function day_abbr_range(array $days, string $dash = ' – '): string {
|
||||
return HOURS_DAY_ABBR[$days[0]] . $dash . HOURS_DAY_ABBR[end($days)];
|
||||
}
|
||||
|
||||
/**
|
||||
* "4pm" / "4:30pm" — tight, lowercase, no leading-zero-minute clutter.
|
||||
* Used only by format_time_range_compact() below for the meta-description
|
||||
* summary's condensed style; the site's on-page hours use format_time_12h
|
||||
* instead, which keeps a space and uppercase AM/PM.
|
||||
*/
|
||||
function format_hour_compact(int $hour, int $minute): string {
|
||||
$period = $hour >= 12 ? 'pm' : 'am';
|
||||
$hour12 = $hour % 12;
|
||||
if ($hour12 === 0) {
|
||||
$hour12 = 12;
|
||||
}
|
||||
if ($minute === 0) {
|
||||
return "{$hour12}{$period}";
|
||||
}
|
||||
return sprintf('%d:%02d%s', $hour12, $minute, $period);
|
||||
}
|
||||
|
||||
/**
|
||||
* "4–10pm" (or "11am–2pm" when the two ends straddle noon) — same single
|
||||
* trailing-meridiem rule as format_time_range(), but compact/lowercase to
|
||||
* match the site's existing meta-description copy style.
|
||||
*/
|
||||
function format_time_range_compact(string $openTime, string $closeTime): string {
|
||||
[$openHour, $openMin] = array_map('intval', explode(':', $openTime));
|
||||
[$closeHour, $closeMin] = array_map('intval', explode(':', $closeTime));
|
||||
$openPeriod = $openHour >= 12 ? 'pm' : 'am';
|
||||
$closePeriod = $closeHour >= 12 ? 'pm' : 'am';
|
||||
|
||||
$openLabel = format_hour_compact($openHour, $openMin);
|
||||
$closeLabel = format_hour_compact($closeHour, $closeMin);
|
||||
|
||||
if ($openPeriod === $closePeriod) {
|
||||
$openLabel = substr($openLabel, 0, -strlen($openPeriod));
|
||||
}
|
||||
|
||||
return "{$openLabel}–{$closeLabel}";
|
||||
}
|
||||
|
||||
/**
|
||||
* "Thursday" for a single day, "Thursday through Saturday" for a run —
|
||||
* the full-name equivalent of day_abbr_range(), for prose summaries.
|
||||
*/
|
||||
function day_name_range(array $days): string {
|
||||
if (count($days) === 1) {
|
||||
return HOURS_DAY_NAMES[$days[0]];
|
||||
}
|
||||
return HOURS_DAY_NAMES[$days[0]] . ' through ' . HOURS_DAY_NAMES[end($days)];
|
||||
}
|
||||
|
||||
/**
|
||||
* "Open Thursday through Saturday, 4–10pm." — the one-line summary used in
|
||||
* <meta name="description">, og:description and twitter:description, built
|
||||
* from the same day groupings as the rest of this file so the blurb search
|
||||
* engines and social previews show always matches whatever's actually set
|
||||
* in Hours & Visit / the admin hours page, instead of hand-typed copy that
|
||||
* can silently drift out of date.
|
||||
*/
|
||||
function hours_meta_summary(array $hoursByDay): string {
|
||||
$openGroups = array_values(array_filter(
|
||||
group_hours_into_ranges($hoursByDay),
|
||||
fn(array $group): bool => $group['is_open']
|
||||
));
|
||||
|
||||
if (empty($openGroups)) {
|
||||
return 'Check our Facebook page for current hours.';
|
||||
}
|
||||
|
||||
$parts = [];
|
||||
foreach ($openGroups as $group) {
|
||||
$parts[] = day_name_range($group['days']) . ', ' . format_time_range_compact($group['data']['open_time'], $group['data']['close_time']);
|
||||
}
|
||||
|
||||
return 'Open ' . implode('; ', $parts) . '.';
|
||||
}
|
||||
|
||||
/**
|
||||
* "Kitchen closes at 9 PM" — or, if open days don't all share one kitchen
|
||||
* closing time, one clause per distinct time naming which days it applies
|
||||
|
||||
Reference in new issue
Block a user