From fa8a94d7215168113cd4f21749d7518f4cde519e Mon Sep 17 00:00:00 2001 From: Raymon Mens Date: Fri, 18 Oct 2024 21:56:45 +0200 Subject: [PATCH] Tekst TV 3.0 API Update (#120) --- README.md | 12 +- functions.php | 150 +---------------------- lib/teksttv.php | 315 ++++++++++++++++++++++++++++++++++++++++++++++++ style.css | 2 +- 4 files changed, 325 insertions(+), 154 deletions(-) create mode 100644 lib/teksttv.php diff --git a/README.md b/README.md index b7a6522..a2236c0 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,13 @@ # The Streekomroep WordPress Theme -This is a WordPress theme created for Streekomroep ZuidWest in the Netherlands. It utilizes Timber and Tailwind CSS, offering functionality for regional news, radio, and TV broadcasts. Use it with WordPress 6.6+ and PHP 8.2+. +This is a WordPress theme created for Streekomroep ZuidWest in the Netherlands. It utilizes Timber and Tailwind CSS, offering functionality for regional news, radio, and TV broadcasts. Use it with WordPress 6.6+ and PHP 8.2. -## How to build/install -It's not enough to download the theme and upload is to a server. The dependencies must be built first. Otherwise it will not work. +## How to install +Get the latest version from the [Releases tab](https://github.com/oszuidwest/streekomroep-wp/releases) and upload it as theme to your WordPress installation. Ensure to install all the hard dependencies too. + +## How to manually build +You can also build the theme yourself. Instructions for macOS: - Install Homebrew ([https://brew.sh](https://brew.sh)) @@ -14,6 +17,7 @@ Instructions for macOS: ```bash npm install +npx browserslist@latest --update-db NODE_ENV=production npx tailwindcss build assets/style.css -o dist/style.css --minify composer install --prefer-dist --no-dev --optimize-autoloader ``` @@ -23,7 +27,7 @@ For Linux users, use `apt` or `yum` instead of Homebrew. This theme has not been ### Hard dependencies Install these before activating the theme: -- Timber 2.2.x: [[Use composer](https://timber.github.io/docs/v2/installation/installation/)] +- Timber 2.2: [Bundled, if you build yourself [use composer](https://timber.github.io/docs/v2/installation/installation/)] - Advanced Custom Fields Pro 6.3.x: [[purchase](https://www.advancedcustomfields.com/pro/)] - Classic Editor 1.x: [[free download](https://wordpress.org/plugins/classic-editor/)] _(we are giving the block editor more time to stabilize)_ - Yoast SEO Premium 23.x: [[purchase](https://yoast.com/wordpress/plugins/seo/)] diff --git a/functions.php b/functions.php index f08cee7..3002f40 100644 --- a/functions.php +++ b/functions.php @@ -157,6 +157,7 @@ function zw_filter_pre_oembed_result($default, $url, $args) require_once 'lib/input_sanitizer.php'; require_once 'lib/push_adapter.php'; +require_once 'lib/teksttv.php'; // Use default class for all post types, except for pages. add_filter('timber/post/classmap', function ($base) { @@ -300,22 +301,6 @@ function zw_rest_api_init() ); } - register_rest_field( - 'post', - 'kabelkrant_text', - [ - 'get_callback' => function ($post_arr, $attr, $request, $object_type) { - $data = null; - $show = (bool)get_field('post_in_kabelkrant', $post_arr['id']); - if ($show) { - $data = get_field('post_kabelkrant_content', $post_arr['id']); - } - - return $data; - }, - ] - ); - register_rest_field( 'post', 'ranking', @@ -502,142 +487,9 @@ function zw_rest_api_init() }, $schedule->getTomorrow()->television), ]; - $commercials = []; - - if (!is_array($options['tv_reclame_slides'])) { - $options['tv_reclame_slides'] = []; - } - - $now = new DateTime('now', wp_timezone()); - foreach ($options['tv_reclame_slides'] as $slide) { - // Ignore slides with no image - if ($slide['tv_reclame_afbeelding'] === false) { - continue; - } - - $start = DateTime::createFromFormat('d/m/Y', $slide['tv_reclame_start'], wp_timezone()); - $start->setTime(0, 0); - // TODO: is end date inclusive? - $end = DateTime::createFromFormat('d/m/Y', $slide['tv_reclame_eind'], wp_timezone()); - $end->setTime(24, 0); - - if ($now >= $start && $now < $end) { - $commercials[] = $slide['tv_reclame_afbeelding']['url']; - } - } - - $response['commercials'] = $commercials; - return $response; } ]); - - register_rest_route('zw/v1', '/desking', [ - 'methods' => 'GET', - 'permission_callback' => '__return_true', - 'callback' => function (WP_REST_Request $request) { - $output = []; - $blocks = get_field('desking_blokken_voorpagina', 'option'); - - foreach ($blocks as $block) { - switch ($block['acf_fc_layout']) { - case 'blok_top_stories': - $output[] = [ - 'type' => 'topstories', - ]; - break; - - case 'blok_tv_gemist': - $output[] = [ - 'type' => 'recent_tv', - 'title' => trim($block['tekst_boven_videos']), - 'deduplicate' => $block['ontdubbel'], - 'count' => $block['aantal_videos'], - ]; - break; - - case 'blok_fm_gemist': - $output[] = [ - 'type' => 'recent_fm', - 'title' => trim($block['tekst_boven_audio']), - ]; - break; - - case 'blok_fragmenten_carrousel': - $title = $block['tekst_boven_fragmenten']; - if (empty($title)) { - $title = null; - } - $output[] = [ - 'type' => 'fragments', - 'title' => $title, - ]; - break; - - case 'blok_artikel_lijst': - $title = $block['tekst_boven_artikelen']; - if (empty($title)) { - $title = null; - } - $output[] = [ - 'type' => 'posts', - 'title' => $title, - 'count' => $block['aantal_artikelen'], - 'offset' => $block['offset'], - ]; - break; - - case 'blok_dossier': - $term = get_term($block['selecteer_dossier'], 'dossier'); - $title = $block['tekst_boven_dossier']; - if (empty($title)) { - $title = $term->name; - } - - $output[] = [ - 'type' => 'dossier', - 'title' => $title, - 'count' => $block['aantal_artikelen'], - 'offset' => $block['offset'], - 'term_id' => $term->term_id, - ]; - break; - - case 'blok_dossiers_carrousel': - $title = $block['tekst_boven_dossiers']; - if (empty($title)) { - $title = 'Dossiers'; - } - - $output[] = [ - 'type' => 'dossier_carousel', - 'title' => $title, - ]; - break; - - case 'blok_nu_op_fmtv': - $output[] = [ - 'type' => 'now_onair' - ]; - break; - - case 'blok_meest_gelezen': - $title = $block['tekst_boven_artikelen']; - if (empty($title)) { - $title = null; - } - - $output[] = [ - 'type' => 'popular', - 'title' => $title, - ]; - break; - } - } - - return $output; - } - ]); } function zw_bunny_parse_url($url) diff --git a/lib/teksttv.php b/lib/teksttv.php new file mode 100644 index 0000000..f877f9f --- /dev/null +++ b/lib/teksttv.php @@ -0,0 +1,315 @@ + 'GET', + 'callback' => [$this, 'get_slides'], + 'permission_callback' => '__return_true' + ]); + + register_rest_route('zw/v1', '/teksttv-ticker', [ + 'methods' => 'GET', + 'callback' => [$this, 'get_ticker_messages'], + 'permission_callback' => '__return_true' + ]); + } + + public function get_slides() + { + $slides = []; + + 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)); + break; + case 'blok_afbeelding': + $slides[] = $this->get_image_slide($block); + break; + case 'blok_reclame': + $slides = array_merge($slides, $this->get_ad_slides($block, $ad_campaigns)); + break; + case 'blok_fm_programmering': + // Implement FM programming slide if needed + break; + } + } + } + } + + return new WP_REST_Response($slides, 200); + } + + private function get_article_slides($block) + { + $slides = []; + $args = [ + 'post_type' => 'post', + 'posts_per_page' => $block['aantal_artikelen'], + 'meta_query' => [ + [ + 'key' => 'post_in_kabelkrant', + 'value' => '1', + 'compare' => '=' + ] + ] + ]; + + if (!empty($block['Regiofilter'])) { + $region_ids = array_map(function ($term) { + return $term->term_id; + }, $block['Regiofilter']); + + $args['tax_query'][] = [ + 'taxonomy' => 'regio', + 'field' => 'term_id', + 'terms' => $region_ids, + ]; + } + + if (!empty($block['categoriefilter'])) { + $category_ids = array_map(function ($term) { + return $term->term_id; + }, $block['categoriefilter']); + + $args['tax_query'][] = [ + 'taxonomy' => 'category', + 'field' => 'term_id', + 'terms' => $category_ids, + ]; + } + + + $query = new WP_Query($args); + + if ($query->have_posts()) { + while ($query->have_posts()) { + $p = $query->the_post(); + + // Check if the post should be displayed today + $display_days = get_field('post_kabelkrant_dagen', get_the_ID()); + $today = date('N'); + + if (!empty($display_days) && !in_array($today, $display_days)) { + 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')) { + continue; + } + + $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()); + + // Split content into pages + $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, + ]; + } + + + // Add extra images if any + $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'], + ]; + } + } + } + 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); + + if ($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'); + } + + + private function get_image_slide($block) + { + return [ + 'type' => 'image', + 'duration' => intval($block['seconden']) * 1000, + 'url' => $block['afbeelding']['url'], + ]; + } + + private function get_ad_campaigns() + { + $campaigns = []; + if (function_exists('get_field')) { + $all_campaigns = get_field('teksttv_reclame', 'option'); + if ($all_campaigns) { + $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; + + if ($current_timestamp >= $start_timestamp && $current_timestamp <= $end_timestamp) { + $campaigns[] = $campaign; + } + } + } + } + return $campaigns; + } + + private function get_ad_slides($block, $campaigns) + { + $slides = []; + $group = $block['groep']; + + foreach ($campaigns as $campaign) { + if (in_array($group, $campaign['campagne_groep'])) { + foreach ($campaign['campagne_slides'] as $slide) { + $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', + 'duration' => 5000, // 5 seconds, adjust as needed + 'url' => $block['afbeelding_in']['url'], + ]); + } + + if (!empty($block['afbeelding_uit'])) { + $slides[] = [ + 'type' => 'image', + 'duration' => 5000, // 5 seconds, adjust as needed + 'url' => $block['afbeelding_uit']['url'], + ]; + } + } + + return $slides; + } + + public function get_ticker_messages() + { + $ticker_messages = []; + + if (function_exists('get_field')) { + $ticker_content = get_field('teksttv_ticker', 'option'); + + if ($ticker_content) { + foreach ($ticker_content as $item) { + switch ($item['acf_fc_layout']) { + case 'ticker_nufm': + $message = $this->get_current_fm_program(); + if ($message) { + $ticker_messages[] = [ + 'message' => $message, + ]; + } + break; + + case 'ticker_straksfm': + $message = $this->get_next_fm_program(); + if ($message) { + $ticker_messages[] = [ + 'message' => $message, + ]; + } + break; + + case 'ticker_tekst': + if (!empty($item['ticker_tekst_tekst'])) { + $ticker_messages[] = [ + 'message' => $item['ticker_tekst_tekst'], + ]; + } + break; + } + } + } + } + + return new WP_REST_Response($ticker_messages, 200); + } + + private function get_current_fm_program() + { + $schedule = new \Streekomroep\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 \Streekomroep\BroadcastSchedule(); + + return 'Straks op Radio Rucphen: ' . $schedule->getNextRadioBroadcast()->getName(); + } +} + +$teksttv_api = new TekstTVAPI(); diff --git a/style.css b/style.css index 81f46ae..0fef19e 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-beta1 + * Version: 1.10.0-beta2 */