From 9f7ea9ffe80805d1be2873f334d32f0fa982566f Mon Sep 17 00:00:00 2001 From: rmens Date: Mon, 21 Oct 2024 08:43:05 +0200 Subject: [PATCH 1/4] Check if image exists --- lib/teksttv.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/teksttv.php b/lib/teksttv.php index db14f9d..0f922f2 100644 --- a/lib/teksttv.php +++ b/lib/teksttv.php @@ -170,11 +170,16 @@ private function get_primary_category_image($post_id) private function get_image_slide($block) { - return [ - 'type' => 'image', - 'duration' => \intval($block['seconden']) * 1000, - 'url' => $block['afbeelding']['url'], - ]; + // Check if the image exists + if (!empty($block['afbeelding']) && !empty($block['afbeelding']['url'])) { + return [ + 'type' => 'image', + 'duration' => \intval($block['seconden']) * 1000, + 'url' => $block['afbeelding']['url'], + ]; + } + // If no image exists, return null or an empty array, depending on your needs + return null; } private function get_ad_campaigns() From 6ad95a424abef123e9796fb1f63418b0c853718c Mon Sep 17 00:00:00 2001 From: rmens Date: Mon, 21 Oct 2024 08:50:34 +0200 Subject: [PATCH 2/4] Output broken image block if image doesn't exist --- lib/teksttv.php | 60 ++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/teksttv.php b/lib/teksttv.php index 0f922f2..e32a1f3 100644 --- a/lib/teksttv.php +++ b/lib/teksttv.php @@ -61,12 +61,12 @@ private function get_article_slides($block) { $slides = []; $args = [ - 'post_type' => 'post', + 'post_type' => 'post', 'posts_per_page' => $block['aantal_artikelen'], - 'meta_query' => [ + 'meta_query' => [ [ - 'key' => 'post_in_kabelkrant', - 'value' => '1', + 'key' => 'post_in_kabelkrant', + 'value' => '1', 'compare' => '=', ], ], @@ -78,19 +78,19 @@ private function get_article_slides($block) }, $block['Regiofilter']); $args['tax_query'][] = [ 'taxonomy' => 'regio', - 'field' => 'term_id', - 'terms' => $region_ids, + 'field' => 'term_id', + 'terms' => $region_ids, ]; } 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', - 'terms' => $category_ids, + 'field' => 'term_id', + 'terms' => $category_ids, ]; } @@ -102,7 +102,7 @@ private function get_article_slides($block) // Check if the post should be displayed today $display_days = \get_field('post_kabelkrant_dagen', \get_the_ID()); - $today = \date('N'); + $today = \date('N'); if (!empty($display_days) && !\in_array($today, $display_days)) { continue; @@ -127,11 +127,11 @@ private function get_article_slides($block) foreach ($pages as $index => $page_content) { $slides[] = [ - 'type' => 'text', + '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' => $slide_image, ]; } @@ -140,9 +140,9 @@ private function get_article_slides($block) if (!empty($extra_images)) { foreach ($extra_images as $image) { $slides[] = [ - 'type' => 'image', + 'type' => 'image', 'duration' => 7000, // 7 seconds, adjust as needed - 'url' => $image['url'], + 'url' => $image['url'], ]; } } @@ -170,16 +170,16 @@ private function get_primary_category_image($post_id) private function get_image_slide($block) { - // Check if the image exists + $slide = [ + 'type' => 'image', + 'duration' => \intval($block['seconden']) * 1000, + ]; + if (!empty($block['afbeelding']) && !empty($block['afbeelding']['url'])) { - return [ - 'type' => 'image', - 'duration' => \intval($block['seconden']) * 1000, - 'url' => $block['afbeelding']['url'], - ]; + $slide['url'] = $block['afbeelding']['url']; } - // If no image exists, return null or an empty array, depending on your needs - return null; + + return $slide; } private function get_ad_campaigns() @@ -217,9 +217,9 @@ private function get_ad_slides($block, $campaigns) if (\in_array($group, $campaign['campagne_groep'])) { foreach ($campaign['campagne_slides'] as $slide) { $slides[] = [ - 'type' => 'image', + 'type' => 'image', 'duration' => \intval($campaign['campagne_seconden']) * 1000, - 'url' => $slide['url'], + 'url' => $slide['url'], ]; } } @@ -228,17 +228,17 @@ private function get_ad_slides($block, $campaigns) if (!empty($slides)) { if (!empty($block['afbeelding_in'])) { \array_unshift($slides, [ - 'type' => 'image', + 'type' => 'image', 'duration' => 5000, // 5 seconds, adjust as needed - 'url' => $block['afbeelding_in']['url'], + 'url' => $block['afbeelding_in']['url'], ]); } if (!empty($block['afbeelding_uit'])) { $slides[] = [ - 'type' => 'image', + 'type' => 'image', 'duration' => 5000, // 5 seconds, adjust as needed - 'url' => $block['afbeelding_uit']['url'], + 'url' => $block['afbeelding_uit']['url'], ]; } } From 5e42931e8efaf78651b720f5a37e231eee5a8299 Mon Sep 17 00:00:00 2001 From: rmens Date: Mon, 21 Oct 2024 09:03:21 +0200 Subject: [PATCH 3/4] Does this work? --- lib/teksttv.php | 158 +++++++++++++++++++++++------------------------- 1 file changed, 76 insertions(+), 82 deletions(-) diff --git a/lib/teksttv.php b/lib/teksttv.php index e32a1f3..b2a2284 100644 --- a/lib/teksttv.php +++ b/lib/teksttv.php @@ -1,4 +1,5 @@ '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', ]); @@ -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 @@ -60,37 +64,39 @@ public function get_slides() private function get_article_slides($block) { $slides = []; - $args = [ - 'post_type' => 'post', + $args = [ + 'post_type' => 'post', 'posts_per_page' => $block['aantal_artikelen'], - 'meta_query' => [ + 'meta_query' => [ [ - 'key' => 'post_in_kabelkrant', - 'value' => '1', + 'key' => 'post_in_kabelkrant', + 'value' => '1', 'compare' => '=', ], ], ]; 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', - 'terms' => $region_ids, + 'field' => 'term_id', + 'terms' => $region_ids, ]; } 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', - 'terms' => $category_ids, + 'field' => 'term_id', + 'terms' => $category_ids, ]; } @@ -100,54 +106,51 @@ 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', + '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; @@ -155,49 +158,42 @@ private function get_article_slides($block) 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) { - $slide = [ - 'type' => 'image', - 'duration' => \intval($block['seconden']) * 1000, - ]; - if (!empty($block['afbeelding']) && !empty($block['afbeelding']['url'])) { - $slide['url'] = $block['afbeelding']['url']; + return [ + 'type' => 'image', + 'duration' => intval($block['seconden']) * 1000, + 'url' => $block['afbeelding']['url'], + ]; } - return $slide; + 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; @@ -214,31 +210,33 @@ 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, [ - 'type' => 'image', + 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'], + 'url' => $block['afbeelding_in']['url'], ]); } - if (!empty($block['afbeelding_uit'])) { + if (!empty($block['afbeelding_uit']) && !empty($block['afbeelding_uit']['url'])) { $slides[] = [ - 'type' => 'image', + 'type' => 'image', 'duration' => 5000, // 5 seconds, adjust as needed - 'url' => $block['afbeelding_uit']['url'], + 'url' => $block['afbeelding_uit']['url'], ]; } } @@ -250,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) { @@ -291,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(); } } From 89c17b712216b3b0db63ff8ac2a57a2d83a13831 Mon Sep 17 00:00:00 2001 From: Raymon Mens Date: Mon, 21 Oct 2024 09:08:38 +0200 Subject: [PATCH 4/4] Update style.css --- style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/style.css b/style.css index 6aa66c1..72216e1 100644 --- a/style.css +++ b/style.css @@ -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 */