Skip to content

Commit

Permalink
Poging 6
Browse files Browse the repository at this point in the history
  • Loading branch information
rmens committed Nov 2, 2024
1 parent 4c91f43 commit 82775e2
Showing 1 changed file with 55 additions and 40 deletions.
95 changes: 55 additions & 40 deletions lib/teksttv.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
class TekstTVAPI
{
// Constructor to hook the API routes into WordPress' REST API initialization

public function __construct()
{
add_action('rest_api_init', [$this, 'register_api_routes']);
$this->wp_timezone = new DateTimeZone(wp_timezone_string());
}

// Register custom API routes for TekstTV slides and ticker messages
Expand All @@ -35,6 +37,7 @@ public function register_api_routes()
public function get_slides()
{
$slides = [];
$debug_info = $this->get_debug_info();

// Check if Advanced Custom Fields (ACF) plugin is active and the 'teksttv_blokken' field exists
if (function_exists('get_field')) {
Expand Down Expand Up @@ -65,7 +68,24 @@ public function get_slides()
}
}

return new WP_REST_Response($slides, 200);
return new WP_REST_Response([
'slides' => $slides,
'_debug' => $debug_info
], 200);
}

private function get_debug_info()
{
$current_date = new DateTime('now', $this->wp_timezone);
return [
'current_time' => $current_date->format('Y-m-d H:i:s'),
'timezone' => [
'wp_timezone_string' => wp_timezone_string(),
'wp_timezone_offset' => get_option('gmt_offset'),
'php_timezone' => date_default_timezone_get(),
'server_time' => date('Y-m-d H:i:s'),
],
];
}

// Generate slides based on posts filtered by regions and categories
Expand Down Expand Up @@ -121,9 +141,11 @@ private function get_article_slides($block)

// Check if the post should be displayed on this day of the week
$display_days = get_field('post_kabelkrant_dagen', $post_id);
$today = date('N');
$current_date = new DateTime('now', $this->wp_timezone);
$today = $current_date->format('N');

// Skip if today is not a display day
$display_days = get_field('post_kabelkrant_dagen', $post_id);
if (!empty($display_days) && !in_array($today, $display_days, true)) {
continue;
}
Expand All @@ -132,39 +154,29 @@ private function get_article_slides($block)
$end_date = get_field('post_kabelkrant_datum_uit', $post_id);
if (!empty($end_date)) {
$end_date = trim($end_date);
$end_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', $end_date, $this->wp_timezone);

$wp_timezone = new DateTimeZone(wp_timezone_string());
$end_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', $end_date, $wp_timezone);

if ($end_date_obj) {
$end_timestamp = $end_date_obj->getTimestamp();
$current_timestamp = current_time('timestamp');

if ($end_timestamp < $current_timestamp) {
continue;
}
} else {
// Date parsing failed, skip the post or handle the error
if ($end_date_obj && $current_date > $end_date_obj) {
continue;
}
}

// Get the primary category image for the post
$slide_image = $this->get_primary_category_image($post_id);

// Check for content or extra images
$kabelkrant_content = get_field('post_kabelkrant_content', $post_id);
$extra_images = get_field('post_kabelkrant_extra_afbeeldingen', $post_id);

if (!empty($kabelkrant_content)) {
// Split content by pages using "---" as a delimiter
$pages = preg_split('/\n*-{3,}\n*/', $kabelkrant_content);

// Generate a slide for each page of content
foreach ($pages as $index => $page_content) {
$slides[] = [
'type' => 'text',
'duration' => 20000, // 20 seconds per slide, adjust as needed
'duration' => 20000,
'title' => get_the_title(),
'body' => wpautop(trim($page_content)),
'image' => !empty($slide_image) ? $slide_image : null,
Expand All @@ -179,6 +191,7 @@ private function get_article_slides($block)
$slides[] = [
'type' => 'image',
'duration' => 7000, // 7 seconds per image, adjust as needed
'duration' => 7000,
'url' => $image['url'],
];
}
Expand Down Expand Up @@ -211,32 +224,29 @@ private function get_primary_category_image($post_id)
// Generate an image slide from a block with day and date filtering
private function get_image_slide($block)
{
$current_timestamp = current_time('timestamp');
$wp_timezone = new DateTimeZone(wp_timezone_string());
$current_date = new DateTime('now', $this->wp_timezone);

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

if (!empty($block['datum_uit'])) {
$end_date = trim($block['datum_uit']);
$end_date_obj = DateTime::createFromFormat('d/m/Y', $end_date, $wp_timezone);
if ($end_date_obj && $current_timestamp > $end_date_obj->getTimestamp()) {
// Current date is after the end date
$end_date_obj = DateTime::createFromFormat('d/m/Y', $end_date, $this->wp_timezone);
if ($end_date_obj && $current_date > $end_date_obj) {
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"]
$today = $current_date->format('N');
$allowed_days = $block['dagen'];

if (!in_array((string)$today, $allowed_days, true)) {
// Today is not in the allowed days
Expand All @@ -249,6 +259,7 @@ private function get_image_slide($block)
return [
'type' => 'image',
'duration' => intval($block['seconden']) * 1000, // Convert to milliseconds
'duration' => intval($block['seconden']) * 1000,
'url' => $block['afbeelding']['url'],
];
}
Expand All @@ -263,31 +274,29 @@ private function get_ad_campaigns()
if (function_exists('get_field')) {
$all_campaigns = get_field('teksttv_reclame', 'option');
if ($all_campaigns) {
$current_timestamp = current_time('timestamp');
$wp_timezone = new DateTimeZone(wp_timezone_string());
$current_date = new DateTime('now', $this->wp_timezone);

foreach ($all_campaigns as $campaign) {
// Get start and end timestamps for the campaign
$start_timestamp = 0;
$is_active = true;

if (!empty($campaign['campagne_datum_in'])) {
$start_date = trim($campaign['campagne_datum_in']);
$start_date_obj = DateTime::createFromFormat('d/m/Y', $start_date, $wp_timezone);
if ($start_date_obj) {
$start_timestamp = $start_date_obj->getTimestamp();
$start_date_obj = DateTime::createFromFormat('d/m/Y', $start_date, $this->wp_timezone);
if ($start_date_obj && $current_date < $start_date_obj) {
$is_active = false;
}
}

$end_timestamp = PHP_INT_MAX;
if (!empty($campaign['campagne_datum_uit'])) {
if ($is_active && !empty($campaign['campagne_datum_uit'])) {
$end_date = trim($campaign['campagne_datum_uit']);
$end_date_obj = DateTime::createFromFormat('d/m/Y', $end_date, $wp_timezone);
if ($end_date_obj) {
$end_timestamp = $end_date_obj->getTimestamp();
$end_date_obj = DateTime::createFromFormat('d/m/Y', $end_date, $this->wp_timezone);
if ($end_date_obj && $current_date > $end_date_obj) {
$is_active = false;
}
}

// Check if the campaign is active
if ($current_timestamp >= $start_timestamp && $current_timestamp <= $end_timestamp) {
if ($is_active) {
$campaigns[] = $campaign;
}
}
Expand Down Expand Up @@ -323,6 +332,7 @@ private function get_ad_slides($block, $campaigns)
array_unshift($slides, [
'type' => 'image',
'duration' => 5000, // 5 seconds, adjust as needed
'duration' => 5000,
'url' => $block['afbeelding_in']['url'],
]);
}
Expand All @@ -331,6 +341,7 @@ private function get_ad_slides($block, $campaigns)
$slides[] = [
'type' => 'image',
'duration' => 5000, // 5 seconds, adjust as needed
'duration' => 5000,
'url' => $block['afbeelding_uit']['url'],
];
}
Expand All @@ -343,6 +354,7 @@ private function get_ad_slides($block, $campaigns)
public function get_ticker_messages()
{
$ticker_messages = [];
$debug_info = $this->get_debug_info();

// Check if ACF is active and 'teksttv_ticker' field exists
if (function_exists('get_field')) {
Expand Down Expand Up @@ -382,7 +394,10 @@ public function get_ticker_messages()
}
}

return new WP_REST_Response($ticker_messages, 200);
return new WP_REST_Response([
'messages' => $ticker_messages,
'_debug' => $debug_info
], 200);
}

// Get the current FM program
Expand All @@ -401,4 +416,4 @@ private function get_next_fm_program()
}

// Instantiate the API class
$teksttv_api = new TekstTVAPI();
$teksttv_api = new TekstTVAPI();

Check failure on line 419 in lib/teksttv.php

View workflow job for this annotation

GitHub Actions / PHPCS

File must end with a newline character

Check failure on line 419 in lib/teksttv.php

View workflow job for this annotation

GitHub Actions / PHPCS

Expected 1 newline at end of file; 0 found

Check failure on line 419 in lib/teksttv.php

View workflow job for this annotation

GitHub Actions / PHPCS

File must end with a newline character

Check failure on line 419 in lib/teksttv.php

View workflow job for this annotation

GitHub Actions / PHPCS

Expected 1 newline at end of file; 0 found

0 comments on commit 82775e2

Please sign in to comment.