From a6413da67a4579866f0fd0323ddb23a7b6b9654a Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 18 Sep 2026 17:00:53 +0000
Subject: [PATCH] =Cleaning up the Integrations setup a bit, started the Helcim integration work too

---
 inc/meta/Storage.php |   89 ++++++++++++++++++++++++++++----------------
 1 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/inc/meta/Storage.php b/inc/meta/Storage.php
index df1089c..732a9bb 100644
--- a/inc/meta/Storage.php
+++ b/inc/meta/Storage.php
@@ -2,6 +2,10 @@
 namespace JVBase\meta;
 
 use Exception;
+use JVBase\inc\managers\ErrorHelper;
+use JVBase\managers\Cache;
+use JVBase\managers\Errors;
+use wpdb;
 
 if (!defined('ABSPATH')) {
 	exit;
@@ -9,11 +13,11 @@
 
 /**
  * Handles persistence of meta values to WordPress
- * Pure storage layer - no validation or sanitization
  */
 class Storage
 {
-	protected \wpdb $wpdb;
+	use ErrorHelper;
+	protected wpdb $wpdb;
 
 	public function __construct()
 	{
@@ -76,7 +80,7 @@
 				&& (
 					($config['type'] ?? '') === 'taxonomy'
 					|| (($config['type']??'') === 'selector' && ($config['subtype']??'') === 'taxonomy')
-				) && !isset($config['taxonomy_type'])) {
+				) && (!isset($config['taxonomy_type']) || !isset($config['isReference']))) {
 				$taxonomyFields[$name] = $config;
 			} else {
 				$metaFields[] = $name;
@@ -122,18 +126,21 @@
 		}
 
 		if ($field->isTaxonomy()) {
+			error_log('Saving Taxonomy field with set_object_terms');
 			return $this->saveTaxonomyField($item, $field);
 		}
 
 		$metaKey = BASE . $field->name;
-
-		return match ($item->objectType) {
-			'post' => update_post_meta($item->id, $metaKey, $field->value) !== false,
-			'term' => update_term_meta($item->id, $metaKey, $field->value) !== false,
-			'user', 'integrations' => update_user_meta($item->id, $metaKey, $field->value) !== false,
+		$result =  match ($item->objectType) {
+			'post' => (bool)update_post_meta($item->id, $metaKey, $field->value),
+			'term' => (bool)update_term_meta($item->id, $metaKey, $field->value),
+			'user', 'integrations' => (bool)update_user_meta($item->id, $metaKey, $field->value),
 			'options' => $this->saveOption($item, $field),
 			default => false
 		};
+
+		error_log('Result: '.print_r($result, true));
+		return $result;
 	}
 
 	/**
@@ -148,10 +155,10 @@
 		}
 
 		$this->wpdb->query('START TRANSACTION');
-
 		try {
 			foreach ($dirty as $field) {
 				if (!$this->saveField($item, $field)) {
+					error_log("Could not save field: {$field->name}");
 					throw new Exception("Failed to save field: {$field->name}");
 				}
 				$field->markClean();
@@ -160,21 +167,14 @@
 			$this->wpdb->query('COMMIT');
 
 			// Update post modified timestamp
-			if ($updateTimestamp && $item->objectType === 'post' && $item->id) {
-				wp_update_post(['ID' => $item->id]);
-			}
-
 			$this->clearCache($item);
 
 			return true;
 
 		} catch (Exception $e) {
 			$this->wpdb->query('ROLLBACK');
-			JVB()->error()->log('meta_storage', $e->getMessage(), [
-				'item_id' => $item->id,
-				'object_type' => $item->objectType,
-				'fields' => array_keys($dirty)
-			], 'error');
+			$this->logError($e->getMessage(), 'save');
+
 			return false;
 		}
 	}
@@ -188,7 +188,7 @@
 		$config = $item->getFieldConfig($name);
 		if ($config && ($config['type'] ?? '') === 'taxonomy' && !isset($config['taxonomy_type'])) {
 			$taxonomy = jvbCheckBase($config['taxonomy']);
-			wp_set_object_terms($item->id, [], $taxonomy, false);
+			wp_set_object_terms($item->id, [], $taxonomy);
 			return true;
 		}
 
@@ -326,7 +326,7 @@
 				$results[$meta->item()->id ?? 'options'] = false;
 			}
 
-			JVB()->error()->log('meta_storage', 'Bulk save failed: ' . $e->getMessage(), [], 'error');
+			self::staticLogError($e->getMessage(), 'save');
 
 			return $results;
 		}
@@ -433,7 +433,7 @@
 	{
 		return match ($name) {
 			'post_title' => get_the_title($item->id),
-			'post_excerpt' => get_the_excerpt($item->id),
+			'post_excerpt' => has_excerpt($item->id) ? get_the_excerpt($item->id):'',
 			'post_content' => get_post_field('post_content', $item->id),
 			default => $item->wpObject->$name ?? ''
 		};
@@ -442,7 +442,7 @@
 	protected function getTermField(Item $item, string $name): mixed
 	{
 		return match ($name) {
-			'term_name' => get_term_field('name', $item->id),
+			'name' => get_term_field('name', $item->id),
 			'description' => get_term_field('description', $item->id),
 			default => ''
 		};
@@ -465,6 +465,9 @@
 		$value = $field->value;
 
 		if (in_array($name, ['featured_image', 'post_thumbnail'])) {
+			if (empty($value)) {
+				return delete_post_thumbnail($item->id);
+			}
 			return set_post_thumbnail($item->id, $value) !== false;
 		}
 
@@ -537,14 +540,13 @@
 	{
 		$taxonomy = jvbCheckBase($field->config['taxonomy']);
 		$value = $field->value;
-
 		if (empty(trim((string)$value))) {
-			wp_set_object_terms($item->id, [], $taxonomy, false);
+			wp_set_object_terms($item->id, [], $taxonomy);
 			return true;
 		}
 
 		$termIds = array_map('intval', array_filter(explode(',', $value)));
-		$result = wp_set_object_terms($item->id, $termIds, $taxonomy, false);
+		$result = wp_set_object_terms($item->id, $termIds, $taxonomy);
 
 		return !is_wp_error($result);
 	}
@@ -607,12 +609,21 @@
 
 	public function clearCache(Item $item): void
 	{
-		match ($item->objectType) {
-			'post' => clean_post_cache($item->id),
-			'term' => clean_term_cache($item->id),
-			'user', 'integrations' => clean_user_cache($item->id),
-			default => null
-		};
+		switch ($item->objectType) {
+			case 'post':
+				Cache::clearPost($item->id);
+				clean_post_cache($item->id);
+				break;
+			case 'term':
+				Cache::clearTerm($item->id);
+				clean_term_cache($item->id);
+				break;
+			case 'user':
+			case 'integrations':
+				Cache::clearUser($item->id);
+				clean_user_cache($item->id);
+				break;
+		}
 	}
 
 	// ─────────────────────────────────────────────────────────────
@@ -722,7 +733,7 @@
 				? []
 				: array_map('intval', array_filter(explode(',', $update['value'])));
 
-			wp_set_object_terms($update['object_id'], $termIds, $update['taxonomy'], false);
+			wp_set_object_terms($update['object_id'], $termIds, $update['taxonomy']);
 		}
 	}
 
@@ -737,8 +748,20 @@
 				set_post_thumbnail($id, $fields['post_thumbnail']);
 				unset($fields['post_thumbnail']);
 			}
+			if (isset($fields['post_thumbnail'])) {
+				if (empty($fields['post_thumbnail'])) {
+					delete_post_thumbnail($id);
+				} else {
+					set_post_thumbnail($id, $fields['post_thumbnail']);
+				}
+				unset($fields['post_thumbnail']);
+			}
 			if (isset($fields['featured_image'])) {
-				set_post_thumbnail($id, $fields['featured_image']);
+				if (empty($fields['featured_image'])) {
+					delete_post_thumbnail($id);
+				} else {
+					set_post_thumbnail($id, $fields['featured_image']);
+				}
 				unset($fields['featured_image']);
 			}
 

--
Gitblit v1.10.0