Skip to content

Commit

Permalink
Poging 7
Browse files Browse the repository at this point in the history
Alles in dezelfde namespace
  • Loading branch information
rmens committed Nov 2, 2024
1 parent 92574d2 commit e3c468b
Showing 1 changed file with 46 additions and 85 deletions.
131 changes: 46 additions & 85 deletions lib/teksttv.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
<?php

namespace ZW\TekstTV;

if (!defined('ABSPATH')) {
exit;
}
namespace Streekomroep;

use DateTime;
use DateTimeZone;
use WP_Query;
use WP_REST_Response;
use ZW\Radio\BroadcastSchedule;

/**
* Text TV API handler - manages content for cable TV display system
*/
class TekstTVAPI
{
private static $instance = null;


private $wp_timezone;

// Slide durations in milliseconds
private const SLIDE_DURATIONS = [
'text' => 20000, // 20 seconds for text slides
'image' => 7000, // 7 seconds for image slides
'ad_transition' => 5000 // 5 seconds for ad transitions
'text' => 20000,
'image' => 7000,
'ad_transition' => 5000
];

/**
* Singleton pattern implementatie
*/
// Singleton pattern implementation
public static function get_instance()
{
if (null === self::$instance) {
Expand All @@ -35,18 +33,14 @@ public static function get_instance()
return self::$instance;
}

/**
* Constructor - initialiseert de API
*/
// Initialize the API and register hooks
private function __construct()
{
$this->wp_timezone = new DateTimeZone(wp_timezone_string());
add_action('rest_api_init', [$this, 'register_api_routes']);
}

/**
* Registreert de API endpoints
*/
// Register REST API endpoints
public function register_api_routes()
{
register_rest_route('zw/v1', '/teksttv-slides', [
Expand All @@ -62,15 +56,12 @@ public function register_api_routes()
]);
}

/**
* Haalt alle slides op voor de TekstTV weergave
*/
// Retrieve all slides for text TV display
public function get_slides()
{
if (!function_exists('get_field')) {
return new WP_REST_Response([
'slides' => [],
'_debug' => $this->get_debug_info(),
'error' => 'ACF plugin is niet actief'
], 200);
}
Expand All @@ -79,14 +70,11 @@ public function get_slides()
$slides = $this->process_blocks($blocks);

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

/**
* Verwerkt verschillende type content blokken naar slides
*/
// Process different types of content blocks into slides
private function process_blocks($blocks)
{
$slides = [];
Expand Down Expand Up @@ -114,9 +102,7 @@ private function process_blocks($blocks)
return array_filter($slides);
}

/**
* Genereert slides van artikelen
*/
// Generate slides from articles with filter support
private function get_article_slides($block)
{
$slides = [];
Expand All @@ -132,7 +118,7 @@ private function get_article_slides($block)
]
];

// Add region filters if specified
// Add region filter if specified
if (!empty($block['Regiofilter'])) {
$args['tax_query'][] = [
'taxonomy' => 'regio',
Expand All @@ -143,7 +129,7 @@ private function get_article_slides($block)
];
}

// Add category filters if specified
// Add category filter if specified
if (!empty($block['categoriefilter'])) {
$args['tax_query'][] = [
'taxonomy' => 'category',
Expand Down Expand Up @@ -178,7 +164,7 @@ private function get_article_slides($block)
}
}

// Get content and images
// Create slides from content and images
$content = get_field('post_kabelkrant_content', $post_id);
$slide_image = $this->get_primary_category_image($post_id);

Expand All @@ -195,7 +181,7 @@ private function get_article_slides($block)
}
}

// Add extra images
// Add extra images as separate slides
$extra_images = get_field('post_kabelkrant_extra_afbeeldingen', $post_id);
if (!empty($extra_images)) {
foreach ($extra_images as $image) {
Expand All @@ -215,9 +201,7 @@ private function get_article_slides($block)
return $slides;
}

/**
* Haalt de primaire categorie afbeelding op
*/
// Get primary category image or fallback to post thumbnail
private function get_primary_category_image($post_id)
{
$primary_term_id = get_post_meta($post_id, '_yoast_wpseo_primary_category', true);
Expand All @@ -232,21 +216,20 @@ private function get_primary_category_image($post_id)
return get_the_post_thumbnail_url($post_id, 'large');
}

/**
* Genereert een image slide
*/
// Generate image slide with date range and day restrictions
private function get_image_slide($block)
{
$current_date = new DateTime('now', $this->wp_timezone);

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

// Check end date if set
if (!empty($block['datum_uit'])) {
$end_date = DateTime::createFromFormat('d/m/Y', trim($block['datum_uit']), $this->wp_timezone);
if ($end_date && $current_date > $end_date) {
Expand All @@ -273,9 +256,7 @@ private function get_image_slide($block)
return null;
}

/**
* Haalt actieve advertentie campagnes op
*/
// Retrieve active advertising campaigns
private function get_ad_campaigns()
{
$campaigns = [];
Expand All @@ -294,9 +275,7 @@ private function get_ad_campaigns()
return $campaigns;
}

/**
* Controleert of een campagne actief is
*/
// Check if campaign is currently active based on date range
private function is_campaign_active($campaign, $current_date)
{
if (!empty($campaign['campagne_datum_in'])) {
Expand All @@ -316,9 +295,7 @@ private function is_campaign_active($campaign, $current_date)
return true;
}

/**
* Genereert advertentie slides
*/
// Generate advertisement slides with intro and outro transitions
private function get_ad_slides($block, $campaigns)
{
$slides = [];
Expand All @@ -338,7 +315,7 @@ private function get_ad_slides($block, $campaigns)
}
}

// Add intro and outro images
// Add transition slides if main slides exist
if (!empty($slides)) {
if (!empty($block['afbeelding_in']) && !empty($block['afbeelding_in']['url'])) {
array_unshift($slides, [
Expand All @@ -360,13 +337,10 @@ private function get_ad_slides($block, $campaigns)
return $slides;
}

/**
* Haalt ticker berichten op
*/
// Retrieve ticker messages including radio program info
public function get_ticker_messages()
{
$messages = [];
$debug_info = $this->get_debug_info();

if (function_exists('get_field')) {
$ticker_content = get_field('teksttv_ticker', 'option');
Expand Down Expand Up @@ -399,48 +373,35 @@ public function get_ticker_messages()
}

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

/**
* Haalt het huidige FM programma op
*/
// Get current radio program name
private function get_current_fm_program()
{
$schedule = new BroadcastSchedule();
return 'Nu op Radio Rucphen: ' . $schedule->getCurrentRadioBroadcast()->getName();
try {
$schedule = new BroadcastSchedule();
return 'Nu op Radio Rucphen: ' . $schedule->getCurrentRadioBroadcast()->getName();
} catch (\Exception $e) {
return 'Radio Rucphen - Altijd nieuws, altijd muziek';
return 'Radio Rucphen';
}
}

/**
* Haalt het volgende FM programma op
*/
// Get next radio program name
private function get_next_fm_program()
{
$schedule = new BroadcastSchedule();
return 'Straks op Radio Rucphen: ' . $schedule->getNextRadioBroadcast()->getName();
}

/**
* Verzamelt debug informatie
*/
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')
]
];
try {
$schedule = new BroadcastSchedule();
return 'Straks op Radio Rucphen: ' . $schedule->getNextRadioBroadcast()->getName();
} catch (\Exception $e) {
return '';
}
}
}

// Initialiseer de plugin
// Initialize the TekstTV API
add_action('init', function () {
TekstTVAPI::get_instance();
});

0 comments on commit e3c468b

Please sign in to comment.