Extract date interval functions, add text if no streams are available

This commit is contained in:
Vencislav Atanasov 2024-10-30 22:05:30 +02:00
parent 1d83107b3a
commit bd6e489bcb
2 changed files with 46 additions and 6 deletions

View File

@ -13,13 +13,34 @@ if (!array_key_exists($blog_slug, $eventsConfig)) {
$now = new DateTimeImmutable('now');
$eventStart = new DateTimeImmutable($eventsConfig[$blog_slug]['startTime'] . ' Europe/Sofia');
$eventStartInterval = $now->diff($eventStart);
$isBeforeEvent = $eventStartInterval instanceof DateInterval && $eventStartInterval->invert === 0;
function interval_to_now(string $dateTimeStr) {
global $now;
return $now->diff(new DateTimeImmutable($dateTimeStr . ' Europe/Sofia'));
}
$eventEnd = new DateTimeImmutable($eventsConfig[$blog_slug]['endTime'] . ' Europe/Sofia');
$eventEndInterval = $now->diff($eventEnd);
$isAfterEvent = $eventEndInterval instanceof DateInterval && $eventEndInterval->invert === 1;
function is_before_interval(DateInterval $interval) {
return $interval->invert === 0;
}
function is_after_interval(DateInterval $interval) {
return $interval->invert === 1;
}
$eventStartInterval = interval_to_now($eventsConfig[$blog_slug]['startTime']);
$isBeforeEvent = is_before_interval($eventStartInterval);
$eventEndInterval = interval_to_now($eventsConfig[$blog_slug]['endTime']);
$isAfterEvent = is_after_interval($eventEndInterval);
$activeStreams = array_filter($eventsConfig[$blog_slug]['streams'], function($stream) {
$streamStartInterval = interval_to_now($stream['startTime']);
$streamEndInterval = interval_to_now($stream['endTime']);
return is_after_interval($streamStartInterval) && is_before_interval($streamEndInterval);
});
$activeStream = reset($activeStreams);
$noCurrentStreams = !$isBeforeEvent && !$isAfterEvent && $activeStream === false;
if ($isBeforeEvent) {
?>
@ -63,6 +84,24 @@ if ($isBeforeEvent) {
<?php
}
if ($activeStream) {
// TODO
}
if ($noCurrentStreams) {
?>
<style>
.no_current_streams {
text-align: center;
margin-top: 18px;
}
</style>
<div class="no_current_streams">
<?php e_('no_current_streams'); ?>
</div>
<?php
}
if ($isAfterEvent) {
?>
<style>

View File

@ -389,6 +389,7 @@ if (function_exists("pll_register_string")) {
pll_register_string('countdown_hours','countdown_hours');
pll_register_string('countdown_minutes','countdown_minutes');
pll_register_string('after_event','after_event');
pll_register_string('no_current_streams','no_current_streams');
}
add_filter( 'wp_title', 'wpdocs_hack_wp_title_for_home' );