| | |
| | | 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']; |
| | | /** |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | /*************************************************************** |
| | |
| | | |
| | | $registrar = !is_null($this->slug) ? Registrar::getInstance($this->slug) : false; |
| | | $fields = $registrar ? $registrar->getFields() : []; |
| | | if ($this->type == 'options') { |
| | | if ($this->type == 'option') { |
| | | $options = Options::getInstance(); |
| | | $fields = $options->getFields(); |
| | | } |
| | |
| | | |
| | | 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)) { |
| | |
| | | if (str_contains($name, ':')) { |
| | | return $this->getByPath($name); |
| | | } |
| | | // 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 ''; |
| | |
| | | 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); |
| | |
| | | $this->save(); |
| | | } |
| | | |
| | | |
| | | if ($this->type === 'option') { |
| | | error_log('Value saved for: '.$name.': '.print_r($value, true)); |
| | | } |
| | | |
| | | return $this; |
| | | } |
| | | |
| | |
| | | 'post' => 'update_post_meta', |
| | | 'term' => 'update_term_meta', |
| | | 'user' => 'update_user_meta', |
| | | 'options', 'option' => 'update_option', |
| | | 'option' => 'update_option', |
| | | default => false, |
| | | }; |
| | | if (!$function) { |
| | |
| | | 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)); |
| | | } |
| | |
| | | '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 |
| | | }; |
| | | } |
| | |
| | | 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)); |
| | |
| | | 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 |
| | | // ───────────────────────────────────────────────────────────── |
| | |
| | | return $this->contentType; |
| | | } |
| | | |
| | | public function item():Item |
| | | { |
| | | return $this->item; |
| | | } |
| | | |
| | | |
| | | // ───────────────────────────────────────────────────────────── |
| | | // Protected Helpers |