From a9654f00c8fbc9934289d761843ddd071025013f Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 28 Aug 2026 17:20:28 +0000
Subject: [PATCH] =PHPStorm Code Inspection: fixed class reference errors (from getting rid of JVB() and replacing with direct class::getInstance; as well as general code sniffing, etc
---
inc/meta/Meta.php | 152 +++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 131 insertions(+), 21 deletions(-)
diff --git a/inc/meta/Meta.php b/inc/meta/Meta.php
index 796ebe9..ec00584 100644
--- a/inc/meta/Meta.php
+++ b/inc/meta/Meta.php
@@ -89,17 +89,17 @@
* 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),
@@ -115,10 +115,9 @@
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();
@@ -134,14 +133,137 @@
}
$meta = array_map(fn($value) => maybe_unserialize($value[0]), $meta);
+
+ $this->fields = [];
+
+ if (class_exists('WooCommerce') && $this->type === 'post' && $this->wpObject->post_type??'' === 'product') {
+ $fields['post_title'] = [
+ 'type' => 'text',
+ 'label' => 'Title',
+ ];
+ $fields['post_content'] = [
+ 'type' => 'text',
+ 'label' => 'Content',
+ ];
+ $fields['post_excerpt'] = [
+ 'type' => 'text',
+ 'label' => 'Short Description',
+ ];
+ $fields['post_id'] = [
+ 'type' => 'text',
+ 'label' => 'ID',
+ ];
+
+
+ $fields['_stock'] = [
+ 'type' => 'number',
+ 'label' => 'Stock',
+ 'default' => null
+ ];
+ $fields['_stock_status'] = [
+ 'type' => 'select',
+ 'label' => 'Stock Status',
+ 'default' => 'instock',
+ 'options' => [
+ 'instock' => 'In Stock',
+ 'outofstock' => 'Out of Stock',
+ 'onbackorder' => 'On Backorder'
+ ]
+ ];
+ $fields['_price'] = [
+ 'type' => 'text',
+ 'subtype' => 'number',
+ 'label' => 'Price'
+ ];
+
+
+ $product = wc_get_product($id);
+ $fields['sku'] = [
+ 'type' => 'text',
+ 'label' => 'SKU',
+ ];
+
+ $out = [];
+ if ($product && $product->is_type('variable')) {
+ $var = $product->get_children();
+ $var = array_filter(array_map(function($v) {
+ return wc_get_product($v);
+ }, $var));
+
+ $title= $product->get_title();
+ foreach ($var as $variation) {
+ $out[] = [
+ 'id' => $variation->get_id(),
+ 'sku' => $variation->get_sku(),
+ 'title' => str_replace($title.' - ', '', $variation->get_name()),
+ 'price' => $variation->get_price(),
+ 'description'=> $variation->get_short_description(),
+ 'sale' => $variation->get_sale_price(),
+ 'img' => $variation->get_image_id(),
+ 'stock' => $variation->get_stock_quantity(),
+ 'stock_status' => $variation->get_stock_status()
+ ];
+ }
+ }
+ if (!empty($out)) {
+ $this->fields['variations'] = new Field('variations', $out, [
+ 'type' => 'repeater',
+ 'label' => 'Variations',
+ 'fields' => [
+ 'id' => [
+ 'type' => 'text',
+ 'subtype' => 'number',
+ 'label' => 'Variation ID',
+ ],
+ 'sku' => [
+ 'type' => 'text',
+ 'label' => 'Stock Keeping Unit (SKU)'
+ ],
+ 'title' => [
+ 'type' => 'text',
+ 'label' => 'Variation Title'
+ ],
+ 'price' => [
+ 'type' => 'text',
+ 'subtype' => 'number',
+ 'label' => 'Variation Price'
+ ],
+ 'sale' => [
+ 'type' => 'text',
+ 'subtype' => 'number',
+ 'label' => 'Sale Price',
+ ],
+ 'image' => [
+ 'type' => 'text',
+ 'subtype' => 'number',
+ 'label' => 'Variation Image ID'
+ ],
+ 'stock' => [
+ 'type' => 'text',
+ 'label' => 'Stock Quantity'
+ ],
+ 'stock_status' => [
+ 'type' => 'text',
+ 'label' => 'Stock Status'
+ ]
+ ]
+ ]);
+ }
+
+
+ }
foreach ($fields as $fieldName => $config) {
$fieldName = jvbNoBase($fieldName);
+ $value = '';
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':
@@ -252,11 +374,6 @@
$this->save();
}
-
- if ($this->type === 'option') {
- error_log('Value saved for: '.$name.': '.print_r($value, true));
- }
-
return $this;
}
@@ -372,12 +489,6 @@
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);
}
@@ -450,7 +561,6 @@
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;
@@ -578,7 +688,7 @@
/**
* Get default value for a field type
*/
- protected function getDefaultValue(string $name): mixed
+ protected function getDefaultValue(string $name): string|int|bool|array
{
$config = $this->item->getFieldConfig($name);
$type = $config['type'] ?? 'text';
--
Gitblit v1.10.0