From bad59c66549eb601fed963ed013f9b79305ca003 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 07 Jan 2026 20:09:14 +0000
Subject: [PATCH] =Feedblock integrated with refactored taxonomy selector

---
 inc/rest/routes/FeedRoutes.php |  136 +++++++++++++++++++++++++--------------------
 1 files changed, 76 insertions(+), 60 deletions(-)

diff --git a/inc/rest/routes/FeedRoutes.php b/inc/rest/routes/FeedRoutes.php
index daadec6..de35a83 100644
--- a/inc/rest/routes/FeedRoutes.php
+++ b/inc/rest/routes/FeedRoutes.php
@@ -32,17 +32,17 @@
 		$this->cache_name = 'feed';
 		$this->cache_ttl = 86400;
 		parent::__construct();
+		$this->cache->clear();
 	}
 
 	public function init():void
 	{
+		$this->cache->clear();
 		$this->checker = Checker::getInstance();
 
-		if (jvbSiteUsesUmami()) {
+		if (Features::hasIntegration('umami')) {
 			$this->tracker = JVB()->connect('umami');
 		}
-
-		$this->cache->clear();
 		$this->setupCacheConnections();
 	}
 
@@ -111,9 +111,9 @@
 		if (!$post || is_wp_error($post)) {
 			return [];
 		}
-
-		return $cache->remember($postID,
-			function() use ($postID, $type, $metaType, $post, $skip) {
+//
+//		return $cache->remember($postID,
+//			function() use ($postID, $type, $metaType, $post, $skip) {
 				$config = null;
 				switch ($metaType) {
 					case 'post':
@@ -146,42 +146,17 @@
 				];
 
 				//Format Taxonomies
-				$temp = array_filter($fields, function($field) {
-					return $field['type'] === 'taxonomy';
-				});
-				foreach ($temp as $key => $config) {
-					if (array_key_exists($key, $out) && $out[$key] !== '') {
-						$IDs = array_map('absint', explode(',', $out[$key]));
-						$data = [];
-						$icon = JVB_TAXONOMY[$config['taxonomy']]['icon']??'triangle';
-						foreach ($IDs as $ID) {
-							$term = get_term($ID, jvbCheckBase($config['taxonomy']));
-							if ($term && !is_wp_error($term)) {
-								$data[$ID] = [
-									'id' 	=> $ID,
-									'icon'	=> $icon,
-									'name'	=> $term->name,
-									'url'	=> get_term_link($ID, jvbCheckBase($config['taxonomy'])),
-								];
-								if ($this->tracker) {
-									$data[$ID]['umami_click'] = $this->tracker->trackClick($ID, $config['taxonomy'], ['from' => $type.'_'.$postID]);
-								}
-							}
-						}
-						if (!empty($data)) {
-							$out['fields'][$key] = $data;
-						}
-					}
-				}
+				$out['taxonomies'] = $this->extractTaxonomies($values, $postID, $type);
 
 				//Add images
 				$imgIDs = [];
 				$temp = array_filter($fields, function($field) {
 					return in_array($field['type'], [ 'upload', 'image', 'gallery']);
 				});
+
 				foreach ($temp as $key => $config) {
-					if (array_key_exists($key, $out) && $out[$key] !== '') {
-						$IDs = array_map('absint', explode(',',$out[$key]));
+					if (array_key_exists($key, $out['fields']) && $out['fields'][$key] !== '') {
+						$IDs = array_map('absint', explode(',',$out['fields'][$key]));
 						foreach ($IDs as $ID) {
 							$imgIDs[$ID] = jvbImageData($ID);
 						}
@@ -191,7 +166,7 @@
 
 				$out['id'] = $postID;
 				$out['content'] = $type;
-				$out['icon'] = $config['icon']??'triangle';
+				$out['icon'] = $config['icon']??jvbDefaultIcon();
 
 				if ($this->tracker) {
 					$args = ($metaType === 'post') ? ['owner_id' => $post->post_author] : [];
@@ -214,12 +189,14 @@
 						$out['url'] = get_the_permalink($postID);
 						break;
 				}
-				return $out;
-			}
-		);
-
+//				return $out;
+//			}
+//		);
+		return $out;
 	}
 
+
+
 	protected function initTimelineFields(string $content):void
 	{
 		$content = jvbNoBase($content);
@@ -260,6 +237,8 @@
 		$children = get_children(['post_parent' => $post->ID, 'orderby' => 'date', 'order' => 'ASC', 'post_status' => ['publish'], 'fields'=> 'ids']);
 		array_unshift($children, $post->ID);
 
+		$item['taxonomies'] = $this->extractTaxonomies($item['fields'], $postID, jvbNoBase($post->post_type));
+
 		$subFields = [];
 		$images = [];
 		foreach ($children as $child) {
@@ -267,25 +246,67 @@
 			$f = $meta->getAll($this->timelineUniqueFields);
 			$f =  ['id' => $child] + $f;
 			$subFields[] = $f;
-
+			$item['taxonomies'] = array_merge($item['taxonomies'], $this->extractTaxonomies($f, $postID, jvbNoBase($post->post_type)));
 			$images[$f['post_thumbnail']] = jvbImageData((int) $f['post_thumbnail']);
 		}
-		$item['fields']['order'] = $subFields;
+		$item['fields']['number'] = count($children);
+		$item['fields']['before'] = get_post_thumbnail_id($children[0]);
+		$item['fields']['after'] = get_post_thumbnail_id($children[array_key_last($children)]);
+
+		$item['fields']['timeline'] = $subFields;
 		$item['images'] = $item['images'] + $images;
 
 		return $item;
 	}
+	protected function extractTaxonomies(array $fields, int $postID, string $content):array {
+		$taxonomies = [];
+		foreach ($fields as $key => $value) {
+			if (empty($value)) {
+				continue;
+			}
+			if (!array_key_exists($key, JVB_TAXONOMY)) {
+				continue;
+			}
 
-	protected function formatTaxonomy(WP_Term $term, int $postID, string $type)
+			$taxConfig = JVB_TAXONOMY[$key];
+			if (isset($taxConfig['public']) && $taxConfig['public'] === false) {
+				continue;
+			}
+			$terms = array_map('absint', explode(',', $value));
+			$terms = array_filter($terms); // Remove 0 values
+
+			if (empty($terms)) {
+				continue;
+			}
+			foreach($terms as $termID) {
+				$term = get_term($termID, jvbCheckBase($key));
+				if ($term && !is_wp_error($term)) {
+					$taxonomies[$key][$termID] = $this->formatTaxonomy($term, $postID, $content);
+				}
+			}
+		}
+		return $taxonomies;
+	}
+
+	protected function formatTaxonomy(WP_Term|int $term, int $postID, string $type)
 	{
-		return [
-			'ID' => $term->term_id,
-			'title' => htmlspecialchars_decode($term->name),
-			'url' => get_term_link($term->term_id, $term->taxonomy),
-			'umami_click' => $this->tracker->trackTaxonomyClick($term->term_id, $term->taxonomy, [
-				'from' => $type . '_' . $postID
-			])
-		];
+		$cache = CacheManager::for(jvbNoBase($term->taxonomy));
+		return $cache->remember(
+			'feed_link_'.$term->term_id,
+			function () use ($term, $postID, $type) {
+				$base = [
+					'ID' => $term->term_id,
+					'title' => htmlspecialchars_decode($term->name),
+					'url' => get_term_link($term->term_id, $term->taxonomy),
+				];
+				if ($this->tracker) {
+					$base['umami_click'] =$this->tracker->trackTaxonomyClick($term->term_id, $term->taxonomy, [
+						'from' => $type . '_' . $postID
+					]);
+				}
+				return $base;
+			}
+		);
 	}
 
 	protected function getAuthorData(WP_Post $post)
@@ -336,7 +357,6 @@
 	protected function buildRequestArgs(WP_REST_Request $request): array
 	{
 		$data = $request->get_params();
-		error_log('Feed Request: ' . print_r($data, true));
 		$args = [
 			'post_type' => (array_key_exists($data['content'], $this->buildFeedTypesConfig())) ?
 				BASE . $data['content'] :
@@ -353,13 +373,15 @@
 				]
 			);
 		}
+		if (array_key_exists('taxonomy', $data) && is_string($data['taxonomy'])) {
+			$data['taxonomy'] = json_decode($data['taxonomy'], true);
+		}
 		$args = $this->applyContextFilters($args, $data);
 		$args = $this->applyTaxonomyFilters($args, $data);
 		$args = $this->applyOrderFilters($args, $data);
 		$args = $this->applyDateFilters($args, $data);
 
 		$args = $this->applyFavouritesFilter($args, $data);
-		error_log('Final Args: '.print_r($args, true));
 		return $args;
 	}
 
@@ -420,7 +442,7 @@
 		if ($cache_check) {
 			return $cache_check; // Returns 304 Not Modified
 		}
-		error_log('Feed Request Args: '.print_r($args, true));
+
 		$key = $this->cache->generateKey($args);
 		$cached = $this->cache->get($key);
 		if ($cached) {
@@ -435,9 +457,6 @@
 		// Fetch and format items
 		$items = $this->fetchFeedItems($args);
 
-		error_log('Feed Got items: ' .print_r($items, true));
-
-
 		$ttl = (str_contains($args['orderby'], 'RAND')) ? 1800 : $this->cache_ttl;
 		$this->cache->set($key, $items, $ttl);
 
@@ -533,7 +552,6 @@
 	 */
 	protected function processHighlightedItem(array $items, array $data): array
 	{
-		error_log('Data passed to processHighlightedItem:' . print_r($data, true));
 		if (empty($data['highlight'] ?? null)) {
 			return $items;
 		}
@@ -624,7 +642,6 @@
 		if (!array_key_exists('favourites', $filters)) {
 			return $args;
 		}
-		error_log('Proceeding to check for favourites:');
 		global $wpdb;
 
 		// Get post types for the current filter
@@ -634,7 +651,6 @@
 
 		$favourites_table = $wpdb->prefix . BASE . 'favourites';
 		$placeholders = implode(',', array_fill(0, count($post_types), '%s'));
-		error_log('CurrentUser ID: ' . print_r(get_current_user_id(), true));
 		$favourited_ids = $wpdb->get_col($wpdb->prepare(
 			"SELECT target_id FROM {$favourites_table}
             WHERE user_id = %d AND type IN ($placeholders)",

--
Gitblit v1.10.0