Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API robustness #122

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 66 additions & 67 deletions lib/teksttv.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Streekomroep;

use WP_Query;
Expand All @@ -14,13 +15,13 @@ public function __construct()
public function register_api_routes()
{
register_rest_route('zw/v1', '/teksttv-slides', [
'methods' => 'GET',
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'get_slides'],
'permission_callback' => '__return_true',
]);

register_rest_route('zw/v1', '/teksttv-ticker', [
'methods' => 'GET',
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'get_ticker_messages'],
'permission_callback' => '__return_true',
]);
Expand All @@ -30,21 +31,24 @@ public function get_slides()
{
$slides = [];

if (\function_exists('get_field')) {
$blocks = \get_field('teksttv_blokken', 'option');
if (function_exists('get_field')) {
$blocks = get_field('teksttv_blokken', 'option');
$ad_campaigns = $this->get_ad_campaigns();

if ($blocks) {
foreach ($blocks as $block) {
switch ($block['acf_fc_layout']) {
case 'blok_artikelen':
$slides = \array_merge($slides, $this->get_article_slides($block));
$slides = array_merge($slides, $this->get_article_slides($block));
break;
case 'blok_afbeelding':
$slides[] = $this->get_image_slide($block);
$image_slide = $this->get_image_slide($block);
if ($image_slide) {
$slides[] = $image_slide;
}
break;
case 'blok_reclame':
$slides = \array_merge($slides, $this->get_ad_slides($block, $ad_campaigns));
$slides = array_merge($slides, $this->get_ad_slides($block, $ad_campaigns));
break;
case 'blok_fm_programmering':
// Implement FM programming slide if needed
Expand All @@ -60,7 +64,7 @@ public function get_slides()
private function get_article_slides($block)
{
$slides = [];
$args = [
$args = [
'post_type' => 'post',
'posts_per_page' => $block['aantal_artikelen'],
'meta_query' => [
Expand All @@ -73,9 +77,10 @@ private function get_article_slides($block)
];

if (!empty($block['Regiofilter'])) {
$region_ids = \array_map(function ($term) {
$region_ids = array_map(function ($term) {
return $term->term_id;
}, $block['Regiofilter']);

$args['tax_query'][] = [
'taxonomy' => 'regio',
'field' => 'term_id',
Expand All @@ -84,9 +89,10 @@ private function get_article_slides($block)
}

if (!empty($block['categoriefilter'])) {
$category_ids = \array_map(function ($term) {
$category_ids = array_map(function ($term) {
return $term->term_id;
}, $block['categoriefilter']);

$args['tax_query'][] = [
'taxonomy' => 'category',
'field' => 'term_id',
Expand All @@ -100,99 +106,94 @@ private function get_article_slides($block)
while ($query->have_posts()) {
$query->the_post();

// Check if the post should be displayed today
$display_days = \get_field('post_kabelkrant_dagen', \get_the_ID());
$today = \date('N');
$display_days = get_field('post_kabelkrant_dagen', get_the_ID());
$today = date('N');

if (!empty($display_days) && !\in_array($today, $display_days)) {
if (!empty($display_days) && !in_array($today, $display_days, true)) {
continue;
}

// Check if the post is still valid based on end date
$end_date = \get_field('post_kabelkrant_datum_uit', \get_the_ID());
if (!empty($end_date) && \strtotime($end_date) < \current_time('timestamp')) {
$end_date = get_field('post_kabelkrant_datum_uit', get_the_ID());
if (!empty($end_date) && strtotime($end_date) < current_time('timestamp')) {
continue;
}

$kabelkrant_content = \get_field('post_kabelkrant_content', \get_the_ID());
$kabelkrant_content = get_field('post_kabelkrant_content', get_the_ID());
if (empty($kabelkrant_content)) {
continue;
}

// Get the image based on Yoast primary category
$slide_image = $this->get_primary_category_image(\get_the_ID());
$slide_image = $this->get_primary_category_image(get_the_ID());

// Split content into pages
$pages = \preg_split('/\n*-{3,}\n*/', $kabelkrant_content);
$pages = preg_split('/\n*-{3,}\n*/', $kabelkrant_content);

foreach ($pages as $index => $page_content) {
$slides[] = [
'type' => 'text',
'duration' => 15000, // 15 seconds, adjust as needed
'title' => \get_the_title(),
'body' => \wpautop(\trim($page_content)),
'image' => $slide_image,
'title' => get_the_title(),
'body' => wpautop(trim($page_content)),
'image' => !empty($slide_image) ? $slide_image : null,
];
}

// Add extra images if any
$extra_images = \get_field('post_kabelkrant_extra_afbeeldingen', \get_the_ID());
$extra_images = get_field('post_kabelkrant_extra_afbeeldingen', get_the_ID());
if (!empty($extra_images)) {
foreach ($extra_images as $image) {
$slides[] = [
'type' => 'image',
'duration' => 7000, // 7 seconds, adjust as needed
'url' => $image['url'],
];
if (!empty($image['url'])) {
$slides[] = [
'type' => 'image',
'duration' => 7000, // 7 seconds, adjust as needed
'url' => $image['url'],
];
}
}
}
}
\wp_reset_postdata();
wp_reset_postdata();
}

return $slides;
}

private function get_primary_category_image($post_id)
{
$primary_term_id = \get_post_meta($post_id, '_yoast_wpseo_primary_category', true);
$primary_term_id = get_post_meta($post_id, '_yoast_wpseo_primary_category', true);

if ($primary_term_id) {
$term_image = \get_field('teksttv_categorie_afbeelding', 'category_' . $primary_term_id);
$term_image = get_field('teksttv_categorie_afbeelding', 'category_' . $primary_term_id);
if ($term_image) {
return $term_image['url'];
}
}

// Fallback to post thumbnail if no primary category image
return \get_the_post_thumbnail_url($post_id, 'medium');
return get_the_post_thumbnail_url($post_id, 'large');
}

private function get_image_slide($block)
{
return [
'type' => 'image',
'duration' => \intval($block['seconden']) * 1000,
'url' => $block['afbeelding']['url'],
];
if (!empty($block['afbeelding']) && !empty($block['afbeelding']['url'])) {
return [
'type' => 'image',
'duration' => intval($block['seconden']) * 1000,
'url' => $block['afbeelding']['url'],
];
}

return null;
}

private function get_ad_campaigns()
{
$campaigns = [];
if (\function_exists('get_field')) {
$all_campaigns = \get_field('teksttv_reclame', 'option');
if (function_exists('get_field')) {
$all_campaigns = get_field('teksttv_reclame', 'option');
if ($all_campaigns) {
$current_timestamp = \current_time('timestamp');
$current_timestamp = current_time('timestamp');
foreach ($all_campaigns as $campaign) {
$start_date = $campaign['campagne_datum_in'];
$end_date = $campaign['campagne_datum_uit'];

// Convert start date to timestamp (beginning of the day)
$start_timestamp = $start_date ? \strtotime($start_date . ' 00:00:00') : 0;

// Convert end date to timestamp (end of the day)
$end_timestamp = $end_date ? \strtotime($end_date . ' 23:59:59') : PHP_INT_MAX;
$start_timestamp = !empty($campaign['campagne_datum_in']) ? strtotime($campaign['campagne_datum_in'] . ' 00:00:00') : 0;
$end_timestamp = !empty($campaign['campagne_datum_uit']) ? strtotime($campaign['campagne_datum_uit'] . ' 23:59:59') : PHP_INT_MAX;

if ($current_timestamp >= $start_timestamp && $current_timestamp <= $end_timestamp) {
$campaigns[] = $campaign;
Expand All @@ -209,27 +210,29 @@ private function get_ad_slides($block, $campaigns)
$group = $block['groep'];

foreach ($campaigns as $campaign) {
if (\in_array($group, $campaign['campagne_groep'])) {
if (in_array($group, $campaign['campagne_groep'], true)) {
foreach ($campaign['campagne_slides'] as $slide) {
$slides[] = [
'type' => 'image',
'duration' => \intval($campaign['campagne_seconden']) * 1000,
'url' => $slide['url'],
];
if (!empty($slide['url'])) {
$slides[] = [
'type' => 'image',
'duration' => intval($campaign['campagne_seconden']) * 1000,
'url' => $slide['url'],
];
}
}
}
}

if (!empty($slides)) {
if (!empty($block['afbeelding_in'])) {
\array_unshift($slides, [
if (!empty($block['afbeelding_in']) && !empty($block['afbeelding_in']['url'])) {
array_unshift($slides, [
'type' => 'image',
'duration' => 5000, // 5 seconds, adjust as needed
'url' => $block['afbeelding_in']['url'],
]);
}

if (!empty($block['afbeelding_uit'])) {
if (!empty($block['afbeelding_uit']) && !empty($block['afbeelding_uit']['url'])) {
$slides[] = [
'type' => 'image',
'duration' => 5000, // 5 seconds, adjust as needed
Expand All @@ -245,8 +248,8 @@ public function get_ticker_messages()
{
$ticker_messages = [];

if (\function_exists('get_field')) {
$ticker_content = \get_field('teksttv_ticker', 'option');
if (function_exists('get_field')) {
$ticker_content = get_field('teksttv_ticker', 'option');

if ($ticker_content) {
foreach ($ticker_content as $item) {
Expand Down Expand Up @@ -286,17 +289,13 @@ public function get_ticker_messages()

private function get_current_fm_program()
{
// Assuming BroadcastSchedule is in the same namespace
$schedule = new BroadcastSchedule();
// Implement logic to get current FM program
// This is a placeholder and should be replaced with actual logic
return 'Nu op Radio Rucphen: ' . $schedule->getCurrentRadioBroadcast()->getName();
}

private function get_next_fm_program()
{
$schedule = new BroadcastSchedule();

return 'Straks op Radio Rucphen: ' . $schedule->getNextRadioBroadcast()->getName();
}
}
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-beta3
* Version: 1.10.0-beta4
*/