From c19264ac916707096fe294d996a1b7fb85206b34 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 08 Mar 2026 19:55:31 +0000
Subject: [PATCH] =Furthering along the complete refactor. Added some basic setup for is_content taxonomies, including a page for an archive of the terms.

---
 inc/registrar/Registrar.php |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/inc/registrar/Registrar.php b/inc/registrar/Registrar.php
index ad174ca..33a8687 100644
--- a/inc/registrar/Registrar.php
+++ b/inc/registrar/Registrar.php
@@ -1,8 +1,10 @@
 <?php
 namespace JVBase\registrar;
 
+use JVBase\managers\Cache;
 use JVBase\managers\CRUD;
 use JVBase\managers\IconsManager;
+use JVBase\meta\Meta;
 use JVBase\registrar\config\Breadcrumbs;
 use JVBase\registrar\config\Dashboard;
 use JVBase\registrar\config\Directory;
@@ -15,6 +17,7 @@
 use JVBase\registrar\helpers\MakeTrackChanges;
 use JVBase\registrar\helpers\MakeVerification;
 use JVBase\utility\Features;
+use WP_Query;
 
 if (!defined('ABSPATH')) {
 	exit;
@@ -36,6 +39,8 @@
 
 	protected ?string $upload_title = null;
 
+	protected int|false $page = false;
+
 	protected static array $allFlags = [
 		//Shared Flags
 		'favouritable', 'karma', 'show_feed', 'show_directory', 'approve_new', 'has_responses', 'invitable',
@@ -281,6 +286,11 @@
 		return $this->fields;
 	}
 
+	public function args():array
+	{
+		return $this->args;
+	}
+
 	public function setFields():void
 	{
 		$this->fields = new Fields($this->type, $this);
@@ -401,6 +411,11 @@
 		});
 		foreach ($flags as $flag) {
 			$this->$flag = true;
+			switch ($flag) {
+				case 'is_content':
+					add_action('init', [$this, 'setupContent'], 20);
+					break;
+			}
 		}
 		return $this;
 	}
@@ -639,4 +654,106 @@
 
 		return $content;
 	}
+
+	public function setupContent():void
+	{
+		if (!$this->is_content) return;
+		//We need a pseudo-archive page for this content taxonomy. We create a post with the plural name
+		$this->page = get_option(BASE.$this->slug.'_archive', false);
+		if (!$this->page || !(bool)get_post((int)$this->page)) {
+			$exists = new WP_Query([
+				'post_type'	=> 'page',
+				'title'=> $this->plural,
+				'posts_per_page'	=> 1,
+				'post_status'	=> 'publish',
+				'fields'	=> 'ids',
+			]);
+			if ($exists->have_posts()) {
+				$page = $exists->posts[0];
+			} else {
+				$page = wp_insert_post([
+					'post_type'		=> 'page',
+					'post_title'	=> $this->plural,
+					'post_content'	=> '',
+					'post_status'	=> 'publish',
+				]);
+			}
+
+			if ($page && !is_wp_error($page)) {
+				update_post_meta($page, BASE.'for_type', $this->slug);
+				update_option(BASE.$this->slug.'_archive', $page);
+				$this->page = $page;
+			}
+			wp_reset_postdata();
+		}
+
+		add_filter('jvb_post_content_output', [$this, 'renderContent'], 20, 2);
+	}
+	public function renderContent(string $content, array $block):string
+	{
+		if (!is_page($this->page)) {
+			return $content;
+		}
+		if ($block['blockName'] !== 'core/post-content') {
+			return $content;
+		}
+		if (JVB_TESTING) {
+			Cache::for($this->slug)->flush();
+		}
+
+		$out = Cache::for($this->slug)->remember(
+			get_the_ID(),
+			function() {
+
+				$items = get_terms([
+					'taxonomy'	=> jvbCheckBase($this->slug),
+//						'hide_empty' => true,
+					'fields'	=> 'ids',
+				]);
+				$out = [];
+				if ($items && !is_wp_error($items)) {
+					foreach ($items as $item) {
+						$meta = Meta::forTerm($item);
+						$slug = sanitize_title($meta->get('name'));
+						$item = sprintf(
+							'<li id="%s"><h3><a href="%s">%s</a></h3><p>%s</p><ul>',
+							$slug,
+							get_term_link($item, jvbCheckBase($this->slug))??'',
+							$meta->get('name'),
+							$meta->get('description')
+						);
+							$postTypes = array_map(function($type) { return jvbCheckBase($type);}, $this->registrar->for);
+							$posts = new WP_Query([
+								'post_type'		=> $postTypes,
+								'post_status'	=> 'publish',
+								'posts_per_page'	=> 3,
+								'fields'	=> 'ids',
+							]);
+							if ($posts->have_posts()) {
+								while($posts->have_posts()) {
+									$posts->the_post();
+									$ID = get_the_id();
+									$postMeta = Meta::forPost($ID);
+									$img = $postMeta->get('post_thumbnail');
+									$img = !empty($img) ? jvbFormatImage((int)$img, 'tiny', 'medium') : '';
+									$item .= sprintf(
+										'<li id="%s"><h4><a href="%s">%s</a></h4>%s</li>',
+										$slug.'-'.sanitize_title(get_the_title($ID)),
+										get_the_permalink($ID),
+										$postMeta->get('post_title'),
+										$img
+									);
+								}
+							}
+							wp_reset_postdata();
+						$item .= '</ul></li>';
+						$out[] = $item;
+					}
+				}
+				return empty($out) ? '' : '<ul class="content-term-list">'.implode('',$out).'</ul>';
+			}
+		);
+		error_log('Built the '.$this->slug.' page content.');
+		return $content . $out;
+	}
 }

--
Gitblit v1.10.0