From 56a9a1ccf764ff7a6af8f8a2292cb07443cb4aa7 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 28 May 2026 18:19:57 +0000
Subject: [PATCH] =New Gitbit setpu

---
 inc/registrar/config/seo/Resolver.php |  140 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 115 insertions(+), 25 deletions(-)

diff --git a/inc/registrar/config/seo/Resolver.php b/inc/registrar/config/seo/Resolver.php
index e754477..db40ea7 100644
--- a/inc/registrar/config/seo/Resolver.php
+++ b/inc/registrar/config/seo/Resolver.php
@@ -4,17 +4,26 @@
 use JVBase\managers\Cache;
 use JVBase\managers\SEO\render\Thing\CreativeWork\MediaObject\ImageObject;
 use JVBase\managers\SEO\render\Thing\Intangible\Quantity\Distance;
+use JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\QuantitativeValue;
 use JVBase\meta\Meta;
+use JVBase\registrar\Registrar;
 
 if (!defined('ABSPATH')) {
 	exit;
 }
 
 class Resolver {
-	protected static Meta $meta;
+	protected static ?Meta $meta;
 
-	public static function resolve(string $template, Meta $meta): string
+	public static function resolve(string $template, ?Meta $meta): string
 	{
+		if (!$meta) {
+			if (is_singular()){
+				$meta = Meta::forPost(get_the_ID());
+			}else if (is_tax()) {
+				$meta = Meta::forTerm(get_queried_object()->term_id);
+			}
+		}
 		self::$meta = $meta;
 		return preg_replace_callback(
 			'/\{\{([^}]+)}}/',
@@ -22,22 +31,40 @@
 			$template
 		);
 	}
-	protected static function resolveVariable(string $variable, Meta $meta): string
+	protected static function resolveVariable(string $variable, ?Meta $meta = null): mixed
 	{
 		$variable = trim($variable);
+		switch ($variable) {
+			case 'CREATOR':
+				return JVB()->seo()->getCreator();
+			case 'name':
+				if (!$meta && is_post_type_archive()) {
+					$obj = get_queried_object();
 
-		if (str_contains($variable, '.')) {
+					$registrar = Registrar::getInstance($obj->name);
+					return $registrar ? $registrar->getPlural() : $obj->label;
+				}
+				break;
+		}
+
+		if (str_contains($variable, '.') && $meta) {
 			return self::resolveRelation($variable, $meta);
 		}
-		if ($variable === 'post_permalink') {
+		if ($meta && $variable === 'post_permalink') {
 			return get_the_permalink($meta->id());
 		}
-
+		if (!$meta) {
+			return '';
+		}
 		$config = $meta->config($variable);
 		if (!$config) {
 			error_log('[SEO]Meta Resolver. Could not find meta configuration for variable: '.$variable);
 			return '';
 		}
+		if ($variable === 'post_content') {
+			return self::resolvePostContent($variable, $meta);
+		}
+
 		return match($config['type']) {
 			'upload', 'image', 'gallery' => self::resolveImage($variable, $meta),
 			'selector', 'taxonomy', 'user', 'post' => self::resolveObject($variable, $meta),
@@ -45,41 +72,67 @@
 		};
 	}
 
-	protected static function resolveRelation(string $path, Meta $meta): string
+	protected static function resolveRelation(string $path, ?Meta $meta = null): string
 	{
 		$parts = explode('.', $path);
 		$relation = array_shift($parts);
 		$field = implode('.', $parts);
 
+
 		//We need to:
 			// 1) Get the id of the item we're fetching (meta value of the $relation)
+		if ($relation === 'registrar') {
+			if (count($parts) === 2) {
+				return self::resolveRegistrar($parts[0], $parts[1]);
+			} else {
+				error_log('[Resolver]::resolveRelation: Registrar relation requires registrar.[slug].[property]');
+				return '';
+			}
+
+		}
+
+		if (!$meta) {
+			return'';
+		}
 		$ID = $meta->get($relation);
-		if (!$ID || $ID === '') {
+		if (empty($ID)) {
 			return '';
 		}
+		$IDs = explode(',',$ID);
 			// 2) Create new Meta for that relation
 		$config = $meta->config($relation);
 		if (!$config) {
 			return '';
 		}
+
 		$type = false;
 		if ($config['type'] === 'taxonomy' || array_key_exists('taxonomy', $config)) {
-			$type = 'taxonomy';
-		} elseif ($config['type'] === 'user'){
+			$type = 'term';
+		} elseif ($config['type'] === 'user' || array_key_exists('user', $config)){
 			$type = 'user';
-		} elseif ($config['type'] === 'post'){
+		} elseif ($config['type'] === 'post' || array_key_exists('post', $config)){
 			$type = 'post';
 		}
+
 		if (!$type) {
 			error_log('[SEO]Meta Resolver. Could not find type for relation: '.$relation.': '.$field);
 			return '';
 		}
-		$newMeta = new Meta($ID, $type);
+
+		$results = [];
+		foreach ($IDs as $ID) {
+			$newMeta = new Meta((int)$ID, $type);
+
 			// 3) Pass to resolver
-		return self::resolve($field, $newMeta);
+			$results[] =  self::resolveVariable($field, $newMeta);
+		}
+		return jvbCommaList($results);
 	}
 
-	protected static function resolveImage(string $variable, Meta $meta, bool $returnID = false): string
+	protected static function resolvePostContent(string $variable, ?Meta $meta):string{
+		return wp_strip_all_tags(str_replace("\n", '', $meta->get('post_content')));
+	}
+	protected static function resolveImage(string $variable, ?Meta $meta, bool $returnID = false): string
 	{
 		$imgID = $meta->get($variable);
 		if (!$imgID || $imgID === '') {
@@ -94,7 +147,7 @@
 		}
 		return $image[0];
 	}
-	protected static function resolveObject(string $variable, Meta $meta): string
+	protected static function resolveObject(string $variable, ?Meta $meta): string
 	{
 		//Hmmm... this should already be handled by dot notation.
 		return '';
@@ -105,7 +158,7 @@
 	 * We need to map the values to what schema.org expects.
 	 * Most are defined in the JVBase\managers\schema\render namespace.
 	 */
-	public static function resolveForSchema(string $property, string $value, mixed $schema, Meta $meta):mixed
+	public static function resolveForSchema(string $property, string $value, mixed $schema, ?Meta $meta = null):mixed
 	{
 		$check = 'resolve'.ucfirst($property).'Property';
 		if (method_exists(self::class, $check)) {
@@ -117,22 +170,29 @@
 			return $checkType;
 		}
 
-		error_log('[SEO]Resolver - No method found for '.$property.' with value: '.print_r($value, true).'. Defaulting to base Resolver');
+		$ignore = ['description', 'name'];
+		if (JVB_TESTING && !in_array($property, $ignore)) {
+			error_log('[SEO]Resolver - No method found for '.$property.' with value: '.print_r($value, true).'. Defaulting to base Resolver');
+		}
+
 
 		return self::resolve($value, $meta);
 	}
 
-		public static function checkPropertyType(string $property, mixed $value, mixed $schema, Meta $meta):mixed
+		public static function checkPropertyType(string $property, mixed $value, mixed $schema, ?Meta $meta):mixed
 		{
 			return match($property) {
 				'logo', 'image', 'photo', 'primaryImageOfPage', 'thumbnail', 'associatedMedia' => self::resolveImageProperty($property, $value, $schema, $meta),
+				'creator' => self::resolveCreator($property, $value, $schema, $meta),
 				default => false
 			};
-
 		}
 
-		public static function resolveImageProperty(string $property, mixed $value, mixed $schema, Meta $meta):?ImageObject
+		public static function resolveImageProperty(string $property, mixed $value, mixed $schema, ?Meta $meta):?ImageObject
 		{
+			if (!$meta) {
+				return null;
+			}
 			$value = str_replace('{{', '', str_replace('}}', '', $value));
 			$imgID = $meta->get($value);
 			error_log('Got image id: '.print_r($imgID, true));
@@ -143,17 +203,22 @@
 			if (!$img) {
 				return null;
 			}
-			Cache::for('imageSchemaObject')->flush();
+			if (JVB_TESTING){
+				Cache::for('imageSchemaObject')->flush();
+			}
+
 			return Cache::for('imageSchemaObject')->connect('post')->remember(
 				$imgID,
 				function () use ($imgID, $img) {
 					$imageObject = new ImageObject();
 					$imageObject->setContentUrl($img[0]);
-					$width = new Distance();
-					$width->setName($img[1].' px');
+					$width = new QuantitativeValue();
+					$width->setValue($img[1]);
+					$width->setUnitText('px');
 					$imageObject->setWidth($width);
-					$height = new Distance();
-					$height->setName($img[2].' px');
+					$height = new QuantitativeValue();
+					$height->setValue($img[2]);
+					$width->setUnitText('px');
 					$imageObject->setHeight($height);
 
 					$image_path = get_attached_file($imgID);
@@ -176,4 +241,29 @@
 			);
 
 		}
+
+		public static function resolveCreator(string $type, mixed $value, mixed $schema, ?Meta $meta):mixed
+		{
+			if (is_numeric($value)) {
+				//TODO generate from id
+
+			} else if (str_contains($value, 'CREATOR')) {
+				return JVB()->seo()->getCreator();
+			}
+			return '';
+		}
+
+		public static function resolveRegistrar(string $type, string $property):string
+		{
+			$registrar = Registrar::getInstance($type);
+			if (!$registrar) {
+				return '';
+			}
+			$method = 'get'.ucfirst($property);
+			if (!method_exists($registrar, $method)) {
+				error_log('[Resolver]::resolveRegistrar: Invalid property getter: '.$property);
+				return '';
+			}
+			return $registrar->$method();
+		}
 }

--
Gitblit v1.10.0