| | |
| | | * Constructor |
| | | ***************************************************************/ |
| | | |
| | | public function __construct(int|string|null $id, string $type) |
| | | public function __construct(int|string|null $id, string $type, array $fields = []) |
| | | { |
| | | $this->validator = new Validator(); |
| | | $this->sanitizer = new Sanitizer(); |
| | | $this->typeManager = new MetaTypeManager(); |
| | | $this->type = $type; |
| | | $this->ID = $id; |
| | | $this->buildData($id, $type); |
| | | $this->buildData($id, $type, $fields); |
| | | } |
| | | |
| | | protected function buildData(int|string|null $id, string $type):void |
| | | protected function buildData(int|string|null $id, string $type, array $fields):void |
| | | { |
| | | $this->wpObject = match($type) { |
| | | 'post' => get_post($id), |
| | |
| | | default => null |
| | | }; |
| | | |
| | | |
| | | |
| | | $registrar = !is_null($this->slug) ? Registrar::getInstance($this->slug) : false; |
| | | $fields = $registrar ? $registrar->getFields() : []; |
| | | $fields = $registrar ? $registrar->getFields() : $fields; |
| | | |
| | | if ($this->type == 'option') { |
| | | $options = Options::getInstance(); |
| | | $fields = $options->getFields(); |
| | |
| | | $meta = []; |
| | | } |
| | | $meta = array_map(fn($value) => maybe_unserialize($value[0]), $meta); |
| | | |
| | | $this->fields = []; |
| | | foreach ($fields as $fieldName => $config) { |
| | | $fieldName = jvbNoBase($fieldName); |
| | | if ($this->type === 'option') { |
| | | $value = get_option(BASE . $fieldName, $config['default'] ?? ''); |
| | | } else if ($this->wpObject && property_exists($this->wpObject, $fieldName)) { |
| | | } else if ( |
| | | $this->wpObject && property_exists($this->wpObject, $fieldName) |
| | | || $type === 'user' && $this->wpObject && property_exists($this->wpObject->data, $fieldName)) { |
| | | $config['wp'] = true; |
| | | $value = $this->wpObject->$fieldName; |
| | | } else if (in_array($fieldName, $this->defaults)) { |
| | | |
| | | } else if (in_array($fieldName, $this->defaults)) { |
| | | $config['wp'] = true; |
| | | switch ($fieldName) { |
| | | case 'post_thumbnail': |
| | |
| | | $this->save(); |
| | | } |
| | | |
| | | |
| | | if ($this->type === 'option') { |
| | | error_log('Value saved for: '.$name.': '.print_r($value, true)); |
| | | } |
| | | |
| | | return $this; |
| | | } |
| | | |
| | |
| | | foreach ($custom as $field) { |
| | | 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); |
| | | } |
| | |
| | | |
| | | protected function setByPath(string $path, mixed $value): self |
| | | { |
| | | error_log('Setting by path: '.$path.', with value: '.print_r($value, true)); |
| | | $parts = explode(':', $path, 3); |
| | | if (count($parts) !== 3) { |
| | | return $this; |