Skip to content

Commit

Permalink
Date and day filter for images
Browse files Browse the repository at this point in the history
  • Loading branch information
rmens committed Oct 24, 2024
1 parent db406d8 commit 1d094c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
43 changes: 39 additions & 4 deletions lib/teksttv.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WP_Query;
use WP_REST_Response;
use DateTime;

class TekstTVAPI
{
Expand Down Expand Up @@ -126,8 +127,11 @@ private function get_article_slides($block)

// Skip if the post's expiration date has passed
$end_date = get_field('post_kabelkrant_datum_uit', get_the_ID());
if (!empty($end_date) && strtotime($end_date) < current_time('timestamp')) {
continue;
if (!empty($end_date)) {
$end_date_obj = DateTime::createFromFormat('d/m/Y', $end_date);
if ($end_date_obj && $end_date_obj->getTimestamp() < current_time('timestamp')) {
continue;
}
}

// Skip if this post isn't for TekstTV
Expand Down Expand Up @@ -190,13 +194,44 @@ private function get_primary_category_image($post_id)
return get_the_post_thumbnail_url($post_id, 'large');
}

// Generate an image slide from a block
// Generate an image slide from a block with day and date filtering
private function get_image_slide($block)
{
$current_timestamp = current_time('timestamp');

// Check date range if 'datum_in' or 'datum_uit' is set
if (!empty($block['datum_in'])) {
$start_date = DateTime::createFromFormat('d/m/Y', $block['datum_in']);
if ($start_date && $current_timestamp < $start_date->getTimestamp()) {
// Current date is before the start date
return null;
}
}

if (!empty($block['datum_uit'])) {
$end_date = DateTime::createFromFormat('d/m/Y', $block['datum_uit']);
if ($end_date && $current_timestamp > $end_date->getTimestamp()) {
// Current date is after the end date
return null;
}
}

// Check day filter if 'dagen' is set
if (!empty($block['dagen'])) {
$today = date('N'); // 1 (Monday) to 7 (Sunday)
$allowed_days = $block['dagen']; // Array of strings ["1", "2", ..., "7"]

if (!in_array((string)$today, $allowed_days, true)) {
// Today is not in the allowed days
return null;
}
}

// If all checks pass, return the image slide
if (!empty($block['afbeelding']) && !empty($block['afbeelding']['url'])) {
return [
'type' => 'image',
'duration' => intval($block['seconden']) * 1000,
'duration' => intval($block['seconden']) * 1000, // Convert to milliseconds
'url' => $block['afbeelding']['url'],
];
}
Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
* Theme Name: Streekomroep
* Description: This is a WordPress theme made for Streekomroep ZuidWest in the Netherlands. It's made using Timber and Tailwind CSS and provides functionality for regional news, radio and television broadcasts.
* Author: Streekomroep ZuidWest
* Version: 1.10.0-beta5
* Version: 1.10.0-beta6
*/

0 comments on commit 1d094c9

Please sign in to comment.