From 8c6502de2f8ec2bd8382cd6945c327d7be400e14 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 28 Jan 2026 05:34:41 +0000
Subject: [PATCH] =Queue cleanup - seems to be working enough to get legacy before and after going!

---
 inc/meta/MetaManager.php |  101 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 85 insertions(+), 16 deletions(-)

diff --git a/inc/meta/MetaManager.php b/inc/meta/MetaManager.php
index 62e641d..9da898e 100644
--- a/inc/meta/MetaManager.php
+++ b/inc/meta/MetaManager.php
@@ -1,7 +1,10 @@
 <?php
 namespace JVBase\meta;
 
+use DateTime;
 use Exception;
+use JVBase\utility\Features;
+use WP_Post;
 
 if (!defined('ABSPATH')) {
     exit; // Exit if accessed directly
@@ -25,6 +28,7 @@
 	protected string|null $object_type;
 	protected int $max_file_size = 5242880;
 	protected ?string $content = null;
+	protected bool $isTimeline = false;
 
 	protected ?string $baseKey = null;
 	protected \wpdb $wpdb;
@@ -63,13 +67,17 @@
 			switch ($type) {
 				case 'post':
 					$this->data = get_post((string)$ID);
+					$this->content = jvbNoBase($this->data->post_type);
+					$this->isTimeline = Features::forContent($this->content)->has('is_timeline');
 					break;
 				case 'term':
 					$this->data = get_term($ID);
+					$this->content = jvbNoBase($this->data->taxonomy);
 					break;
 				case 'user':
 				case 'integrations':
 					$this->data = get_user($ID);
+					$this->content = jvbUserRole($ID);
 					break;
 				case 'options':
 					$this->baseKey = $ID;
@@ -80,7 +88,6 @@
 					break;
 			}
 		}
-		$this->content = $content;
 
 		$this->type_manager = new MetaTypeManager();
 		$this->validator = new MetaValidator();
@@ -254,7 +261,7 @@
 	 *
 	 * @return bool
 	 */
-	public function updateValue(string $name, mixed $value): bool
+	public function updateValue(string $name, mixed $value, bool $updatePost = true): bool
 	{
 		try {
 			// Get field definition
@@ -268,7 +275,6 @@
 			$field_config['name'] = $name;
 			// Validate value
 			if (!$this->validator->validate($value, $field_config)) {
-				error_log('Validation unsuccessful');
 				throw new Exception("Validation failed for {$name}");
 			}
 
@@ -293,7 +299,8 @@
 					if ($old !== $sanitized) {
 						switch ($this->object_type) {
 							case 'post':
-								$ID = wp_update_post([
+
+								$ID = jvb_update_post([
 									'ID' => $this->object_id,
 									$name => $sanitized
 								]);
@@ -317,7 +324,7 @@
 								if ($name === 'display_name') {
 									$link = get_user_meta($this->object_id, BASE.'link', true);
 									if ($link !== '') {
-										wp_update_post([
+										jvb_update_post([
 											'ID'	=> $link,
 											'post_title'	=> $sanitized
 										]);
@@ -333,7 +340,13 @@
 
 
 			if ($field_config['type'] == 'taxonomy' && (!array_key_exists('taxonomy_type', $field_config))) {
-				$set = wp_set_post_terms($this->object_id, $sanitized, jvbCheckBase($field_config['taxonomy']), false);
+				if (empty(trim($sanitized))) {
+					// Clear all terms when value is empty
+					wp_set_object_terms($this->object_id, [], jvbCheckBase($field_config['taxonomy']), false);
+				} else {
+					$term_ids = array_map('intval', array_filter(explode(',', $sanitized)));
+					wp_set_object_terms($this->object_id, $term_ids, jvbCheckBase($field_config['taxonomy']), false);
+				}
 			}
 			if ($field_config['type'] === 'location' && empty($sanitized)) {
 				$this->addMeta('has_map', false);
@@ -364,6 +377,12 @@
 				throw new Exception("Failed to update meta value for {$name}");
 			}
 
+			if ($updatePost && $this->object_type === 'post') {
+				//Flush the cache for this post.
+				jvb_update_post([
+					'ID'	=> $this->object_id,
+				]);
+			}
 			return true;
 
 		} catch (Exception $e) {
@@ -523,7 +542,8 @@
 			return [];
 		}
 
-		return jvbGetFields($type, $this->object_type);
+		$this->fields = jvbGetFields($type, $this->object_type);
+		return $this->fields;
 	}
 
 	protected function getObjectType(): string|false
@@ -930,7 +950,7 @@
 		return false;
 	}
 
-	public function setAll(array $fields):bool
+	public function setAll(array $fields, bool $updatePost = true):bool
 	{
 		if (empty($fields) || !$this->object_type) {
 			return false;
@@ -940,6 +960,7 @@
 			return false;
 		}
 
+
 		// Determine table based on object type
 		$check = array_key_exists($this->object_type, $this->wpDefaults) ? $this->wpDefaults[$this->object_type] : [];
 		switch ($this->object_type) {
@@ -983,7 +1004,7 @@
 		$temp = [];
 		foreach ($setFields as $f) {
 			$temp[$f] = $fields[$f];
-			unset($fields[array_search($f, $fields)]);
+			unset($fields[$f]);
 		}
 		$setFields = $temp;
 
@@ -999,12 +1020,18 @@
 					// Sanitize value
 					$sanitized = $this->sanitizer->sanitize($value, $field_config);
 					if ($this->checkOverrides($field, $sanitized, $field_config)) {
-						return true;
+						continue;
 					}
 
-					if ($field_config['type'] === 'taxonomy' && !array_key_exists('taxonomy_type', $field_config)){
-						$term_ids = array_map('intval', explode(',', trim($sanitized)));
-						$set = wp_set_post_terms($this->object_id, $term_ids, jvbCheckBase($field_config['taxonomy']), false);
+
+					if ($field_config['type'] == 'taxonomy' && (!array_key_exists('taxonomy_type', $field_config))) {
+						if (empty(trim($sanitized))) {
+							// Clear all terms when value is empty
+							wp_set_object_terms($this->object_id, [], jvbCheckBase($field_config['taxonomy']), false);
+						} else {
+							$term_ids = array_map('intval', array_filter(explode(',', $sanitized)));
+							wp_set_object_terms($this->object_id, $term_ids, jvbCheckBase($field_config['taxonomy']), false);
+						}
 					}
 
 					if ($field_config['type'] === 'location' && empty($sanitized)) {
@@ -1062,6 +1089,25 @@
 			}
 
 			if (!empty($setFields)) {
+				foreach ($setFields as $field => $value) {
+					$field_config = $this->getFieldConfig($field);
+					if ($field_config) {
+						$setFields[$field] = $this->sanitizer->sanitize($value, $field_config);
+					}
+
+					if ($field === 'post_date') {
+						$datetime = strtotime($setFields[$field]);
+						if ($datetime !== false) {
+							$setFields[$field] = date('Y-m-d H:i:s', $datetime);
+						} else {
+							$setFields[$field] = date('Y-m-d H:i:s', time());
+						}
+
+						$setFields['post_date_gmt'] = get_gmt_from_date($setFields[$field]);
+						$setFields['edit_date'] = true;
+					}
+				}
+
 				switch ($this->object_type) {
 					case 'post':
 						if (array_key_exists('post_thumbnail', $setFields)) {
@@ -1070,7 +1116,7 @@
 						}
 
 						if (!empty($setFields)) {
-							$result = wp_update_post(array_merge(['ID' => $this->object_id], $setFields), true);
+							$result = jvb_update_post(array_merge(['ID' => $this->object_id], $setFields));
 						}
 						break;
 					case 'user':
@@ -1081,9 +1127,9 @@
 						wp_update_term($this->object_id, $this->data->taxonomy, $setFields);
 						break;
 				}
-			} elseif ($this->object_type === 'post' && !empty($this->object_id)) {
+			} elseif ($updatePost && $this->object_type === 'post' && !empty($this->object_id)) {
 				//Update the 'post modified' date with meta updates, for filtering
-				wp_update_post(['ID' => $this->object_id]);
+				jvb_update_post(['ID' => $this->object_id]);
 			}
 
 		} catch (Exception $e) {
@@ -1377,4 +1423,27 @@
 			}
 		}
 	}
+
+
+
+	private function getOrCreateTerm(string $termName, string $taxonomy):?int
+	{
+		$taxonomy = jvbCheckBase($taxonomy);
+		$term = get_term_by('name', $termName, $taxonomy);
+
+		if (!$term) {
+			$result = wp_insert_term($termName, $taxonomy);
+			if (is_wp_error($result)) {
+				return null;
+			}
+			$termID = $result['term_id'];
+		} else {
+			$termID = $term->term_id;
+		}
+
+		if ($termID) {
+			return $termID;
+		}
+		return null;
+	}
 }

--
Gitblit v1.10.0