From f16cb88a3218ac7bb32e43f0e0a2542d35c7a01b Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 17 Jun 2026 00:28:16 +0000
Subject: [PATCH] =Working on the Options Meta still. Group fields also needed some changes
---
inc/meta/Meta.php | 104 ++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 85 insertions(+), 19 deletions(-)
diff --git a/inc/meta/Meta.php b/inc/meta/Meta.php
index fcf262a..796ebe9 100644
--- a/inc/meta/Meta.php
+++ b/inc/meta/Meta.php
@@ -1,6 +1,7 @@
<?php
namespace JVBase\meta;
+use JVBase\base\Options;
use JVBase\registrar\Registrar;
use WP_Post;
use WP_Term;
@@ -30,7 +31,7 @@
protected WP_Post|WP_Term|WP_User|false|null $wpObject;
protected int|string $ID;
protected MetaTypeManager $typeManager;
- protected static array $instances = ['post' => [],'term' => [], 'user'=>[],'options'=>[]];
+ protected static array $instances = ['post' => [],'term' => [], 'user'=>[],'option'=>[]];
protected array $defaults = ['post_thumbnail'];
/**
@@ -75,13 +76,13 @@
/**
* Create Meta instance for options
*/
- public static function forOptions(?string $baseKey = 'ajv'): self
+ public static function forOptions(?string $baseKey = BASE): self
{
- if (array_key_exists($baseKey, self::$instances['options'])) {
- return self::$instances['options'][$baseKey];
+ if (array_key_exists($baseKey, self::$instances['option'])) {
+ return self::$instances['option'][$baseKey];
}
- $new = new self($baseKey, 'options');
- self::$instances['options'][$baseKey] = $new;
+ $new = new self($baseKey, 'option');
+ self::$instances['option'][$baseKey] = $new;
return $new;
}
/***************************************************************
@@ -118,17 +119,26 @@
$registrar = !is_null($this->slug) ? Registrar::getInstance($this->slug) : false;
$fields = $registrar ? $registrar->getFields() : [];
+ if ($this->type == 'option') {
+ $options = Options::getInstance();
+ $fields = $options->getFields();
+ }
$meta = match($type) {
'post' => get_post_meta($id),
'term' => get_term_meta($id),
'user' => get_user_meta($id),
default => []
};
+ if (!$meta) {
+ $meta = [];
+ }
$meta = array_map(fn($value) => maybe_unserialize($value[0]), $meta);
foreach ($fields as $fieldName => $config) {
$fieldName = jvbNoBase($fieldName);
- if ($this->wpObject && property_exists($this->wpObject, $fieldName)) {
+ if ($this->type === 'option') {
+ $value = get_option(BASE . $fieldName, $config['default'] ?? '');
+ } else if ($this->wpObject && property_exists($this->wpObject, $fieldName)) {
$config['wp'] = true;
$value = $this->wpObject->$fieldName;
} else if (in_array($fieldName, $this->defaults)) {
@@ -192,8 +202,20 @@
if (str_contains($name, ':')) {
return $this->getByPath($name);
}
-
- return $this->fields[$name]->get();
+ // Group shortcut path
+ if (str_contains($name, '|')) {
+ [$parent, $child] = explode('|', $name, 2);
+ return $this->group($parent)->get($child);
+ }
+ if (!array_key_exists($name, $this->fields)) {
+ error_log('[Meta]::get Attempted to get unregistered field: '.$name);
+ return '';
+ }
+ if (is_null($this->fields[$name])) {
+ error_log('[Meta]::get Field does not seem to be setup yet: '.$name);
+ return '';
+ }
+ return $this->fields[$name]->get()??'';
}
/**
@@ -206,6 +228,11 @@
return $this->setByPath($name, $value);
}
+ // Group shortcut
+ if (str_contains($name, '|')) {
+ return $this->setGroup($name, $value, $autosave);
+ }
+
$field = $this->fields[$name]??false;
if (!$field) {
error_log('No config found for field: '.$name);
@@ -225,6 +252,11 @@
$this->save();
}
+
+ if ($this->type === 'option') {
+ error_log('Value saved for: '.$name.': '.print_r($value, true));
+ }
+
return $this;
}
@@ -312,12 +344,12 @@
}
break;
case 'term':
- $result = wp_update_term($this->ID, $this->slug, $defaults);
+ $termDefaults = array_map(fn($field) => $field->value, $defaults);
+ $result = wp_update_term($this->ID, $this->slug, $termDefaults);
break;
case 'user':
- $data = array_merge([
- 'ID' => $this->ID
- ], $defaults);
+ $userDefaults = array_map(fn($field) => $field->value, $defaults);
+ $data = array_merge(['ID' => $this->ID], $userDefaults);
$result = wp_update_user($data);
break;
}
@@ -330,7 +362,7 @@
'post' => 'update_post_meta',
'term' => 'update_term_meta',
'user' => 'update_user_meta',
- 'options', 'option' => 'update_option',
+ 'option' => 'update_option',
default => false,
};
if (!$function) {
@@ -338,14 +370,26 @@
return false;
}
foreach ($custom as $field) {
- $result = $function($this->ID, BASE.$field->name, $field->value);
+ if ($this->type === 'option') {
+ $result = update_option(BASE.$field->name, $field->value);
+ if ($result) {
+ error_log('Option '.$field->name.' updated');
+ } else {
+ error_log('Option '.$field->name.' not updated');
+ }
+ error_log('Update option '.$field->name.' result: '.print_r($result, true));
+ } else {
+ $result = $function($this->ID, BASE.$field->name, $field->value);
+ }
+
if (!$result) {
error_log('Problem saving field: '.$field->name.' with value: '.print_r($field->value, true));
}
}
- if ($this->type === 'term' && Registrar::getInstance($this->slug)->hasFeature('is_content')) {
- update_term_meta($this->ID, BASE.'date_modified', date('Y-m-d H:i:s'));
- }
+ //Now handled directly from Registrar
+// if ($this->type === 'term' && Registrar::getInstance($this->slug)->hasFeature('is_content')) {
+// update_term_meta($this->ID, BASE.'date_modified', date('Y-m-d H:i:s'));
+// }
}
@@ -372,7 +416,7 @@
'post' => delete_post_meta($this->ID, BASE.$name),
'term' => delete_term_meta($this->ID, BASE.$name),
'user', 'integrations' => delete_user_meta($this->ID, BASE.$name),
- 'options' => delete_option(BASE.$name),
+ 'option' => delete_option(BASE.$name),
default => false
};
}
@@ -403,6 +447,7 @@
return new Repeater($this, $name);
}
+
protected function setByPath(string $path, mixed $value): self
{
error_log('Setting by path: '.$path.', with value: '.print_r($value, true));
@@ -428,6 +473,22 @@
return $this->repeater($repeaterName)->field((int) $rowIndex, $subField);
}
+
+
+ public function group(string $name): Group
+ {
+ return new Group($this, $name);
+ }
+
+ protected function setGroup(string $name, mixed $value, bool $autosave = true): self
+ {
+ $parent = strtok($name, '|');
+ $child = str_replace($parent . '|', '', $name);
+
+ $this->group($parent)->set($child, $value, $autosave);
+ return $this;
+ }
+
// ─────────────────────────────────────────────────────────────
// Utility Methods
// ─────────────────────────────────────────────────────────────
@@ -476,6 +537,11 @@
return $this->contentType;
}
+ public function item():Item
+ {
+ return $this->item;
+ }
+
// ─────────────────────────────────────────────────────────────
// Protected Helpers
--
Gitblit v1.10.0