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/registrar/helpers/AddCustomFields.php |  118 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 101 insertions(+), 17 deletions(-)

diff --git a/inc/registrar/helpers/AddCustomFields.php b/inc/registrar/helpers/AddCustomFields.php
index 7894f18..1cccca9 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'], 10, 2);
+				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']);
 	}
 
@@ -35,7 +50,7 @@
 
 		wp_enqueue_script(
 			'jvb-editor-fields',
-			JVB_URL.'/build/_meta/fields.js',
+			JVB_URL.'build/_meta/fields.js',
 			$asset['dependencies'],
 			$asset['version'],
 			true
@@ -44,11 +59,24 @@
 		wp_add_inline_script(
 			'jvb-editor-fields',
 			'window.jvbFieldConfig = '.wp_json_encode([
-				'postType' => $this->registrar->getBased(),
 				'fields'   => array_map(function($f) {
 					$f['name'] = jvbCheckBase($f['name']);
+					$f['id'] = jvbCheckBase($f['name']);
+					if ($f['type'] === 'selector') {
+						$f['taxonomy'] = jvbCheckBase($f['taxonomy']);
+					}
+					if (array_key_exists('condition', $f)) {
+						$f['condition']['field'] = jvbCheckBase($f['condition']['field']);
+					}
 					return $f;
 				},array_values($this->registrar->getFields(false))),
+				'sections'  => array_map(function ($s) {
+					$s['id'] = jvbCheckBase($s['slug']);
+					$s['fields'] = array_map(function ($f) {
+						return jvbCheckBase($f);
+					}, $s['fields']);
+					return $s;
+				},$this->registrar->getSections()),
 			]).';',
 			'before'
 		);
@@ -60,16 +88,28 @@
 		foreach ($this->registrar->getFields(false) as $name => $config) {
 			$metaType = $this->getSchemaType($config);
 
-			$schema = $metaType === 'array' ? $this->determineSchema($config) : true;
+			$rawSchema = in_array($metaType, ['array', 'object'], true)
+				? $this->determineSchema($config)
+				: true;
+
+			//Allow for null
+			if ($metaType === 'object') {
+				$rawSchema['type'] = ['object', 'null'];
+			}
+
+			$schema = is_array($rawSchema) ? $this->schema($rawSchema) : $rawSchema;
+
+			$default = $config['type'] === 'repeater' ?
+				$this->determineDefault($schema['schema']['properties']??[]) :
+				$config['default'] ?? MetaTypeManager::getType($config['type'])['default'] ?? '';
 
 			register_post_meta($this->registrar->getBased(), jvbCheckBase($name), [
 				'single'            => true,
 				'type'              => $metaType,
-				'default'           => $config['default'] ?? MetaTypeManager::getType($config['type'])['default'] ?? '',
+				'default'           => $default,
 				'sanitize_callback' => fn($value) => (Sanitizer::sanitize($value, $config)),
 				'auth_callback'     => fn($allowed, $key, $postID) => current_user_can('edit_post', $postID),
 				'show_in_rest'      => $schema,
-				'revisions_enabled' => true,
 			]);
 		}
 	}
@@ -150,16 +190,16 @@
 				$required[] = $name;
 			}
 
-			$schema = $this->determineSchema($c);
-			if (is_array($schema)) {
-				if (array_key_exists('default', $schema)) {
-					$args['default'] = $schema['default'];
+			$tmp = $this->determineSchema($c);
+			if (is_array($tmp)) {
+				if (array_key_exists('default', $tmp)) {
+					$args['default'] = $tmp['default'];
 				}
-				if (array_key_exists('enum', $schema)) {
-					$args['enum'] = $schema['enum'];
+				if (array_key_exists('enum', $tmp)) {
+					$args['enum'] = $tmp['enum'];
 				}
-				if (array_key_exists('format', $schema)) {
-					$args['format'] = $schema['format'];
+				if (array_key_exists('format', $tmp)) {
+					$args['format'] = $tmp['format'];
 				}
 			}
 			if (array_key_exists('default', $c)) {
@@ -188,7 +228,7 @@
 	protected function schemaLocation(array $config):array|true
 	{
 		return [
-			'type'          => 'object',
+			'type'          => ['object', 'null'],
 			'properties'    => [
 				'address'   => [
 					'type'  => 'string'
@@ -217,4 +257,48 @@
 			]
 		];
 	}
+
+	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
+	{
+
+	}
+
+	protected function determineDefault(array $properties):array
+	{
+		$out = [];
+		foreach ($properties as $prop => $value) {
+			$out[$prop] = match ($value) {
+				'object'	=> [],
+				default		=> ''
+			};
+		}
+		return $out;
+	}
 }

--
Gitblit v1.10.0