From a9b3b28d001941921aa70d37fdc87c758a163a44 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 05 Jun 2026 16:47:03 +0000
Subject: [PATCH] =Some hefty changes to FeedBlock. Transitioning to loading first page in php to save on extra requests. Got a bit to do yet, but I have to work on Northeh for a bit here.

---
 inc/rest/routes/FeedRoutes.php |  236 ++++++++++++++++++++++++----------------------------------
 1 files changed, 98 insertions(+), 138 deletions(-)

diff --git a/inc/rest/routes/FeedRoutes.php b/inc/rest/routes/FeedRoutes.php
index 81d31d7..6abb894 100644
--- a/inc/rest/routes/FeedRoutes.php
+++ b/inc/rest/routes/FeedRoutes.php
@@ -2,11 +2,11 @@
 namespace JVBase\rest\routes;
 
 use JVBase\meta\Meta;
+use JVBase\registrar\Registrar;
 use JVBase\rest\Rest;
 use JVBase\integrations\Umami;
 use JVBase\rest\Route;
-use JVBase\utility\Checker;
-use JVBase\utility\Features;
+use JVBase\base\Site;
 use WP_Query;
 use WP_Post;
 use WP_Term;
@@ -21,7 +21,7 @@
 {
 	protected int $per_page = 36;
 	protected ?Umami $tracker = null;
-	protected ?Checker $checker = null;
+
 	protected ?array $fields = null;
 	protected ?array $timelineSharedFields = null;
 	protected ?array $timelineUniqueFields = null;
@@ -44,9 +44,7 @@
 
 	public function init():void
 	{
-		$this->checker = Checker::getInstance();
-
-		if (Features::hasIntegration('umami')) {
+		if (Site::hasIntegration('umami')) {
 			$this->tracker = JVB()->connect('umami');
 		}
 	}
@@ -70,7 +68,7 @@
 				'dateFrom' => 'string',
 				'dateTo' => 'string',
 				'context' => 'string',
-				'source' => 'string',
+				'contextId' => 'string',
 				'favourites' => 'boolean',
 				'user' => 'integer',
 				'highlight' => 'string',
@@ -89,7 +87,7 @@
 				'dateFrom' => 'string',
 				'dateTo' => 'string',
 				'context' => 'string',
-				'source' => 'string',
+				'contextId' => 'string',
 				'favourites' => 'boolean',
 				'user' => 'integer',
 				'highlight' => 'string',
@@ -133,40 +131,34 @@
 		return $this->cache->remember(
 			$postID,
 			function() use ($postID, $type, $metaType, $post, $skip) {
-				$config = null;
+				$registrar = null;
 				switch ($metaType) {
 					case 'post':
-						$config = JVB_CONTENT[$type];
-
+						$registrar = Registrar::getInstance($type);
 						$meta = Meta::forPost($postID);
-						if (!$skip && array_key_exists('is_timeline', $config) && $config['is_timeline']) {
+						if (!$skip && $registrar->isTimeline()) {
 							return $this->formatTimeline($postID, $post);
 						}
 						break;
 					case 'term':
 
 						$meta = Meta::forTerm($postID);
-						$config = JVB_TAXONOMY[$type];
+						$registrar = Registrar::getInstance($type);
 						break;
 					case 'user':
 						$meta = Meta::forUser($postID);
-						$config = JVB_USER[$type];
+						$registrar = Registrar::getInstance($type);
 						break;
+					default:
+						$meta = false;
 				}
-				if (!$config) {
+				if (!$registrar || !$meta) {
 					return [];
 				}
-				$fields = $config['fields'];
+				[$images, $fields] = $registrar->getFeedFields();
 
-				//Allow custom filtering for public fields
-				if (array_key_exists('feed', $config) && array_key_exists('fields', $config['feed'])) {
-					$fields = array_filter($fields, function($field) use ($config) {
-						return in_array($field, $config['feed']['fields']);
-					}, ARRAY_FILTER_USE_KEY);
-				}
-
-				$values = $meta->getAll(array_keys($fields));
-
+				$all = array_merge($images, $fields);
+				$values = $meta->getAll(array_map(function($f) { return $f['name']; }, $all));
 				$out = [
 					'fields' => $values,
 				];
@@ -176,23 +168,21 @@
 
 				//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['fields']) && $out['fields'][$key] !== '') {
-						$IDs = array_map('absint', explode(',',$out['fields'][$key]));
-						foreach ($IDs as $ID) {
-							$imgIDs[$ID] = jvbImageData($ID);
-						}
+				$imgs = $meta->getAll(array_map(function($f) {return $f['name'];}, $images));
+				foreach ($imgs as $img) {
+					$IDs = array_map('absint', explode(',',$img));
+					foreach ($IDs as $ID) {
+						$imgIDs[$ID] = jvbImageData($ID);
 					}
 				}
 				$out['images'] = $imgIDs;
 
 				$out['id'] = $postID;
 				$out['content'] = $type;
-				$out['icon'] = $config['icon']??jvbDefaultIcon();
+				$out['icon'] = $registrar->getIcon();
+				if ($out['icon'] === '') {
+					$out['icon'] = jvbDefaultIcon();
+				}
 
 				if ($this->tracker) {
 					$args = ($metaType === 'post') ? ['owner_id' => $post->post_author] : [];
@@ -203,7 +193,8 @@
 
 				switch ($metaType) {
 					case 'term':
-						$owner = (in_array($type, jvbContentTaxonomies()) ? $meta->getValue('owner') : null);
+
+						$owner = $registrar->hasFeature('is_content') ? $meta->get('owner') : null;
 						if (!is_null($owner)) {
 							$out['user_id'] = $owner;
 						}
@@ -211,8 +202,6 @@
 						$out['title'] = html_entity_decode($post->name);
 						break;
 					case 'post':
-						$out['date'] = $post->post_date;
-						$out['modified'] = $post->post_modified;
 						$out['user_id'] = (int)$post->post_author;
 						$out['url'] = get_the_permalink($postID);
 						$out['title']= get_the_title($postID);
@@ -227,12 +216,11 @@
 
 	protected function initTimelineFields(string $content):void
 	{
-		$content = jvbNoBase($content);
-		if (!Features::forContent($content)->has('is_timeline')){
+		$registrar = Registrar::getInstance($content);
+		if (!$registrar || !$registrar->hasFeature('is_timeline')){
 			return;
 		}
-		$config = Features::getConfig($content);
-		$this->fields = $config['fields'];
+		$this->fields = $registrar->getFields();
 
 		$this->timelineSharedFields = array_keys(array_filter($this->fields, function ($field) {
 			if (!array_key_exists('for_all', $field) || $field['for_all'] === false){
@@ -253,37 +241,30 @@
 
 	protected function formatTimeline(int $postID, WP_Post $post):array
 	{
-		if (!$this->timelineSharedFields || !$this->timelineUniqueFields){
-			$this->initTimelineFields($post->post_type);
-		}
-		$item = $this->formatItem($postID, 'post', true);
 		//Step 1: Get the fields that apply to all posts
-		$mainMeta = Meta::forPost($post->ID);
-		$item['fields'] = $mainMeta->getAll($this->timelineSharedFields);
+		$item = $this->formatItem($postID, 'post', true);
 
 		//Step 2: Get the fields for each individual posts
-		$children = get_children(['post_parent' => $post->ID, 'orderby' => 'date', 'order' => 'ASC', 'post_status' => ['publish'], 'fields'=> 'ids']);
-		array_unshift($children, $post->ID);
+		$children = jvbTimelinePoints($postID, $post->post_type);
+		$last = $children[array_key_last($children)];
+			$lastMeta = Meta::forPost($last);
+			$lastImg = $lastMeta->get('post_thumbnail');
 
-		$item['taxonomies'] = $this->extractTaxonomies($item['fields'], $postID, jvbNoBase($post->post_type));
+			$item['fields']['after'] = $lastImg;
+			$item['images'][$lastImg] = jvbImageData((int) $lastImg);
+			$item['fields']['post_modified'] = $lastMeta->get('post_date');
 
-		$subFields = [];
-		$images = [];
-		foreach ($children as $child) {
-			$meta = Meta::forPost($child);
-			$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']);
+			$count = count(array_filter($children, function ($child) {
+				$tmp = Meta::forPost($child);
+				return empty($tmp->get('is_update')) || $tmp->get('is_update') !==1;
+			}));
+
+		foreach($item['taxonomies'] as $tax => $items) {
+			$item[$tax] = array_keys($items);
 		}
-		$item['number'] = (int)get_post_meta($post->ID,BASE.'number', true);
-		$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;
-
+		$item['number'] = $count;
+		$item['fields']['before'] = $item['fields']['post_thumbnail'];
 
 		return $item;
 	}
@@ -293,14 +274,12 @@
 			if (empty($value)) {
 				continue;
 			}
-			if (!array_key_exists($key, JVB_TAXONOMY)) {
+
+			$registrar = Registrar::getInstance($key);
+			if (!$registrar || $registrar->registrar->public === false){
 				continue;
 			}
 
-			$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
 
@@ -340,13 +319,14 @@
 	protected function getAuthorData(WP_Post $post)
 	{
 		$author = $post->post_author;
-		$userLink = get_user_meta($author, BASE.'link', true);
+		$userLink = get_user_meta($author, BASE.'profile_link', true);
 		return $this->cache->remember(
 			$userLink,
 			function () use ($userLink, $author) {
 				$label = jvbUserRole($author);
-				if (array_key_exists($label, JVB_USER)) {
-					$label = JVB_USER[$label]['label'];
+				$registrar = Registrar::getInstance($label);
+				if ($registrar) {
+					$label = $registrar->getSingular();
 				} else {
 					$label = 'Artist';
 				}
@@ -363,16 +343,17 @@
 
 	protected function getTaxonomies(int $postID, string $content): array
 	{
-		$taxonomies = jvbTaxonomiesForContent($content);
+		$registrar = Registrar::getInstance($content)??false;
+		$taxonomies = $registrar->registrar->taxonomies;
 		$out = [];
 		foreach ($taxonomies as $tax) {
 			$terms = get_the_terms($postID, $tax);
 			$t = [];
 			if ($terms && !is_wp_error($terms)) {
-				$config = jvbNoBase($tax);
+				$config = Registrar::getInstance($tax);
 				$out[] = [
-					'icon' => $config,
-					'title' => JVB_TAXONOMY[$config]['plural'],
+					'icon' => $config->getIcon(),
+					'title' => $config->getPlural(),
 					'terms' => array_map(function ($term) use ($tax, $postID, $content) {
 						$item = $this->cache->remember(
 							$term->term_id,
@@ -402,8 +383,7 @@
 		$data = $request->get_params();
 		$args = [
 			'post_type' => (array_key_exists($data['content'], $this->buildFeedTypesConfig())) ?
-				BASE . $data['content'] :
-				BASE . array_key_first(JVB_CONTENT),
+				jvbCheckBase($data['content']) : null,
 			'paged' => intval($data['page'] ?? 1),
 			'posts_per_page' => $this->per_page,
 		];
@@ -411,7 +391,7 @@
 			$args = $this->applyContextFilters(
 				$args,
 				[
-					'id' => $data['source']??'0',
+					'id' => $data['contextId']??'0',
 					'type' => $data['context']
 				]
 			);
@@ -492,15 +472,8 @@
 		// Extract key and value
 		$key = array_keys($data['highlight'])[0] ?? false;
 		$value = array_values($data['highlight'])[0] ?? false;
-		error_log('Highlighted item: ' . $key);
-		error_log('Highlighted item: ' . $value);
-		error_log('No Single Content Types: ' . print_r(jvbNoSingleContentTypes(), true));
-		error_log('Page: ' . print_r($data['paged'], true));
-		if (in_array($key, jvbNoSingleContentTypes()) && $value && $data['paged'] === 1) {
-			error_log('Formatted Highlighted item: ' . print_r($this->formatItem($value), true));
-			error_log('Items: ' . print_r($items, true));
+		if ($key && $data['paged'] === 1) {
 			array_unshift($items, $this->formatItem($value));
-			error_log('Items after unshift: ' . print_r($items, true));
 		}
 		return $items;
 	}
@@ -517,44 +490,43 @@
 			return $args;
 		}
 
+		$registrar = Registrar::getInstance($context['type']);
 		switch (true) {
-			case contentIsJVBUserType($context['type']):
+			case $registrar->hasFeature('profile_link'):
 				$args['author'] = (int)get_post_meta($context['id'], BASE . 'link', true);
 				break;
-			case taxIsJVBContentTax($context['type']):
+			case $registrar->getType() === 'term' && $registrar->hasFeature('is_content'):
 				$args['post_type'] = is_array($args['post_type'])
 					? $args['post_type']
 					: explode(',', $args['post_type']);
 
 				// Check if filtering global feed content
-				if (in_array($context['type'], jvbGlobalFeedContentTaxonomies())) {
+				if (in_array(jvbNoBase($context['type']), Registrar::getFeatured('is_content', 'term'))) {
 					// Global: show posts from any content type with this taxonomy
-					$for_content = JVB_TAXONOMY[$context['type']]['for_content'] ?? [];
-					if (empty($for_content)) {
-						// Fall back to any content that has this taxonomy registered
-						$for_content = array_keys(
-							array_filter(
-								JVB_CONTENT,
-								fn($c) => in_array($context['type'], $c['taxonomies'] ?? [])
-							)
-						);
-					}
+					$for_content = Registrar::getInstance($context['type'])->registrar->for ?? [];
 
 					// Convert to full post types with BASE prefix
-					$post_types = array_map(fn($type) => BASE . $type, $for_content);
+					$post_types = array_map(fn($type) => jvbCheckBase($type), $for_content);
 
 					// Filter to only show_feed content types
-					$show_feed_types = Features::getTypesWithFeature('show_feed', 'content');
+					$show_feed_types = Registrar::getFeatured('show_feed', 'post');
 					$args['post_type'] = array_intersect(
 						$post_types,
-						array_map(fn($type) => BASE . $type, $show_feed_types)
+						array_map(fn($type) => jvbCheckBase($type), $show_feed_types)
 					);
 				}
 
 				// Add term to tax query
 				$args['tax_query'][] = [
-					'taxonomy' => jvbCheckBase($context['type']),
-					'field' => 'term_id',
+					'taxonomy' => $registrar->getBased(),
+//					'field' => 'term_id',
+					'terms' => [(int)$context['id']],
+				];
+				break;
+			case $registrar->getType() === 'term':
+				// Add term to tax query
+				$args['tax_query'][] = [
+					'taxonomy' => $registrar->getBased(),
 					'terms' => [(int)$context['id']],
 				];
 				break;
@@ -610,11 +582,11 @@
 	{
 		$postType = is_array($args['post_type']) ? $args['post_type'][0] : $args['post_type'];
 		$slug = jvbNoBase($postType);
-
-		if (Features::forContent($slug)->has('is_timeline')) {
+		$registrar = Registrar::getInstance($slug);
+		if ($registrar && $registrar->hasFeature('is_timeline')) {
 			$args['post_parent'] = 0;
 		}
-		if (in_array($slug, Features::getTypesWithFeature('is_content', 'taxonomy'))) {
+		if ($registrar && $registrar->hasFeature('is_content')) {
 			return $this->handleContentTaxonomies($args);
 		}
 		$args['fields'] = 'ids';
@@ -638,7 +610,7 @@
 
 	protected function handleContentTaxonomies(array $args): array
 	{
-
+		//TODO: This needs to be better. We have CustomTable now, maybe pass it along to that?
 		$taxonomy = jvbNoBase($args['post_type']);
 		global $wpdb;
 		$table = $wpdb->prefix . BASE . 'content_' . $taxonomy;
@@ -1141,15 +1113,6 @@
 		}
 	}
 
-	/**
-	 * Get custom table fields for a taxonomy
-	 * @param string $taxonomy Taxonomy type
-	 * @return array Field definitions
-	 */
-	protected function getCustomTableFields(string $taxonomy): array
-	{
-		return jvbContentTaxonomiesTableFields($taxonomy)['fields'] ?? [];
-	}
 
 	/**
 	 * Get available feed types (for block editor)
@@ -1178,35 +1141,32 @@
 	 */
 	protected function buildFeedTypesConfig(): array
 	{
-		if (!$this->checker) {
-			$this->checker = Checker::getInstance();
-		}
 		return $this->cache->remember(
 			'contentTypes',
 			function () {
 				$config = [];
 
 				// Get content types with show_feed
-				$contentTypes = Features::getTypesWithFeature('show_feed', 'content');
+				$contentTypes = Registrar::getFeatured('show_feed', 'post');
 				foreach ($contentTypes as $slug) {
 					$this->cache->tag('content:'.$slug);
-					$contentConfig = JVB_CONTENT[$slug] ?? null;
-					if (!$contentConfig) continue;
+					$registrar = Registrar::getInstance($slug);
+					if (!$registrar) continue;
 
 					$config[$slug] = [
 						'type' => 'content',
-						'singular' => $contentConfig['singular'] ?? ucfirst($slug),
-						'plural' => $contentConfig['plural'] ?? ucfirst($slug) . 's',
-						'icon' => $slug,
-						'taxonomies' => $this->checker->getTaxonomiesForContent($slug),
+						'singular' => $registrar->getSingular(),
+						'plural' => $registrar->getPlural(),
+						'icon' => $registrar->getIcon(),
+						'taxonomies' => $registrar->registrar->taxonomies,
 					];
 				}
 
 				// Get taxonomies with show_feed (content taxonomies)
-				$taxonomies = Features::getTypesWithFeature('show_feed', 'taxonomy');
+				$taxonomies = Registrar::getFeatured('show_feed', 'term');
 				foreach ($taxonomies as $slug) {
-					$taxConfig = JVB_TAXONOMY[$slug] ?? null;
-					if (!$taxConfig || !($taxConfig['is_content'] ?? false)) {
+					$registrar = Registrar::getInstance($slug);
+					if (!$registrar || !($registrar->hasFeature('is_content') ?? false)) {
 						continue;
 					}
 
@@ -1214,11 +1174,11 @@
 
 					$config[$slug] = [
 						'type' => 'taxonomy',
-						'singular' => $taxConfig['singular'] ?? ucfirst($slug),
-						'plural' => $taxConfig['plural'] ?? ucfirst($slug) . 's',
-						'icon' => $slug,
+						'singular' => $registrar->getSingular(),
+						'plural' => $registrar->getPlural(),
+						'icon' => $registrar->getIcon(),
 						'taxonomies' => [], // Content taxonomies don't have sub-taxonomies
-						'for_content' => $taxConfig['for_content'] ?? [],
+						'for_content' => $registrar->registrar->for ?? []
 					];
 				}
 

--
Gitblit v1.10.0