| | |
| | | <?php |
| | | namespace JVBase\meta; |
| | | |
| | | use JVBase\base\options; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\registrar\Registrar; |
| | | |
| | |
| | | * @var array of Registrar's defined fields, if any |
| | | */ |
| | | protected static array $registrars = []; |
| | | protected static ?options $options = null; |
| | | /** |
| | | * @var array Field values, stored in memory |
| | | */ |
| | |
| | | self::$registrars[$this->content] = $registrar->getFields(); |
| | | } |
| | | } |
| | | if ($this->objectType === 'options' && !isset(self::$options)) { |
| | | self::$options = options::getInstance(); |
| | | } |
| | | |
| | | if ($this->objectType === 'post' && function_exists('wc_get_product') && in_array($this->content, ['product', 'product_variation'], true)) { |
| | | $this->product = wc_get_product($this->ID) ?: null; |
| | |
| | | 'user' => get_user_meta($this->ID), |
| | | default => [] |
| | | }; |
| | | |
| | | $this->fields = array_map(fn($values) => is_array($values) ? $values[0] : $values, $this->fields); |
| | | |
| | | $this->fields = array_combine( |
| | | array_map('jvbNoBase', array_keys($this->fields)), |
| | | $this->fields |
| | |
| | | public function set(string $name, mixed $value): bool { |
| | | $field = $this->getFieldConfig($name); |
| | | if ($field) { |
| | | if ($field['type']??'' === 'repeater' && str_contains($name, ':')) { |
| | | return $this->setByPath($name, $value); |
| | | } elseif ($field['type']??'' === 'group' && str_contains($name, '|')) { |
| | | return $this->setGroup($name, $value); |
| | | if (array_key_exists('type', $field)) { |
| | | if ($field['type'] === 'repeater' && str_contains($name, ':')) { |
| | | $this->log('Setting by path in set'); |
| | | return $this->setByPath($name, $value); |
| | | } elseif ($field['type'] === 'group' && str_contains($name, '|')) { |
| | | return $this->setGroup($name, $value); |
| | | } |
| | | } |
| | | |
| | | if (!self::$validator->validate($value, $field)) { |
| | |
| | | * WOOCOMMERCE PRODUCT SPECIFIC |
| | | **************************************************************/ |
| | | protected function isProductField(string $name): bool { |
| | | return method_exists($this->product, 'get_'.$name) || method_exists($this->product, 'set_'.$name); |
| | | return $name === 'variations' || method_exists($this->product, 'get_'.$name) || method_exists($this->product, 'set_'.$name); |
| | | } |
| | | |
| | | protected function getProductField(string $name): mixed { |
| | |
| | | $variations = $this->product->get_children(); |
| | | $out = []; |
| | | foreach ($variations as $v) { |
| | | $out[] = self::forPost($v)->getAll(); |
| | | $out[] = array_merge(['id' => $v], self::forPost($v)->getAll()); |
| | | } |
| | | return $out; |
| | | } |
| | |
| | | return [ |
| | | 'price', |
| | | 'sku', |
| | | 'sale', |
| | | 'sale_price', |
| | | 'regular_price', |
| | | 'date_on_sale_from', |
| | | 'date_on_sale_to', |
| | | 'max_purchase_quantity', |
| | | 'min_purchase_quantity', |
| | | 'short_description', |
| | | 'description', |
| | | 'image_id', |
| | | 'gallery_image_ids', |
| | | 'stock', |
| | |
| | | } |
| | | protected function setByPath(string $path, mixed $value):bool |
| | | { |
| | | if (!str_contains($path, ':')) { |
| | | $this->log('setByPath called without appropriate string (:)'); |
| | | return $this->set($path, $value); |
| | | } |
| | | $parts = explode(':', $path, 3); |
| | | if (count($parts) !== 3) { |
| | | $this->log('Could not set value by path'); |
| | | $this->log('Could not set value by path: '.$path.', with value: '.print_r($value, true)); |
| | | return false; |
| | | } |
| | | [$repeaterName, $rowIndex, $subField] = $parts; |
| | |
| | | |
| | | protected function setGroup(string $name, mixed $value):bool |
| | | { |
| | | if (!str_contains($name, '|')) { |
| | | $this->log('setGroup called without appropriate key (|)'); |
| | | return $this->set($name, $value); |
| | | } |
| | | [$parent, $child] = explode('|', $name, 2); |
| | | $this->clearCache(); |
| | | return $this->group($parent)->set($child, $value); |
| | |
| | | |
| | | protected function hasRegistrar():bool |
| | | { |
| | | return array_key_exists($this->content, self::$registrars); |
| | | return !is_null($this->content) && array_key_exists($this->content, self::$registrars); |
| | | } |
| | | |
| | | public function getFieldConfig(string $name): ?array { |
| | | if ($this->objectType === 'options') return $this->getOptionsConfig($name); |
| | | if (!$this->hasRegistrar()) return null; |
| | | return self::$registrars[$this->content][$name] ?? null; |
| | | } |
| | | protected function getOptionsConfig(string $name):?array |
| | | { |
| | | return self::$options->getConfig($name); |
| | | } |
| | | |
| | | protected function getCoreDefault(string $name):string |
| | | { |
| | |
| | | return jvbCheckBase($name); |
| | | } |
| | | |
| | | /** |
| | | * Get item ID |
| | | */ |
| | | public function id(): int|string|null |
| | | { |
| | | return $this->ID; |
| | | } |
| | | |
| | | public function config(string $name):?array |
| | | { |
| | | return $this->getFieldConfig($name); |
| | | } |
| | | |
| | | protected function log(string $message, array $context = []):void |
| | | { |
| | | $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'] ?? ''; |