From 20306459e336840da928aec1f9f7c30225e8479a Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 10 Sep 2026 19:10:37 +0000
Subject: [PATCH] =ensured that meta fields set object terms after save_post through AddCustomFields->checkPostFields

---
 inc/registrar/helpers/AddCustomFields.php |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/inc/registrar/helpers/AddCustomFields.php b/inc/registrar/helpers/AddCustomFields.php
index c9709ff..87be897 100644
--- a/inc/registrar/helpers/AddCustomFields.php
+++ b/inc/registrar/helpers/AddCustomFields.php
@@ -1,9 +1,12 @@
 <?php
 
 namespace JVBase\inc\registrar\helpers;
+use JVBase\meta\Meta;
 use JVBase\meta\MetaTypeManager;
 use JVBase\meta\Sanitizer;
 use JVBase\registrar\Registrar;
+use WP_Post;
+use WP_User;
 
 if (!defined('ABSPATH')) {
 	exit;
@@ -14,10 +17,22 @@
 	public function __construct(Registrar $registrar) {
 		$this->registrar = $registrar;
 
-		if ($registrar->getType() === 'post') {
-			add_action('enqueue_block_editor_assets', [$this, 'enqueue']);
+		$this->registerHooks();
+	}
+	protected function registerHooks():void
+	{
+		switch ($this->registrar->getType()) {
+			case 'post':
+				add_action('save_post', [$this, 'checkPostFields']);
+				add_action('enqueue_block_editor_assets', [$this, 'enqueue']);
+				break;
+			case 'term':
+				add_action('saved_term', [$this, 'checkTermFields']);
+				break;
+			case 'user':
+				add_action('profile_update', [$this, 'checkUserFields']);
+				break;
 		}
-
 		add_action('init', [$this, 'registerMeta']);
 	}
 
@@ -237,4 +252,36 @@
 			]
 		];
 	}
+
+	public function checkPostFields(int $ID, WP_Post $post):void
+	{
+		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
+		if (wp_is_post_revision($ID) || wp_is_post_autosave($ID)) return;
+
+		if ($post->post_type !== $this->registrar->getBased()) {
+			return;
+		}
+		$taxFields = array_filter($this->registrar->getFields(false), function ($field) {
+			return array_key_exists('type', $field) && $field['type'] === 'selector' && (!array_key_exists('isReference', $field) || !$field['isReference']);
+		});
+		if (empty($taxFields)) {
+			return;
+		}
+		foreach ($taxFields as $name => $config) {
+			$tax = jvbCheckBase($config['taxonomy']);
+
+			$value = get_post_meta($ID, BASE.$name, true);
+			$termIDs = array_map('intval', array_filter(explode(',', $value)));
+			$result = wp_set_object_terms($ID, $termIDs, $tax);
+		}
+	}
+
+	public function checkTermFields(int $ID, int $ttID, string $taxonomy):void
+	{
+
+	}
+	public function checkUserFields(int $ID, WP_User $oldUser, array $userData):void
+	{
+
+	}
 }

--
Gitblit v1.10.0