Jake Vanderwerf
2026-03-08 c19264ac916707096fe294d996a1b7fb85206b34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace JVBase\meta;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Data container for a single WordPress object (post, term, user, or options)
 * Holds the WP object reference and a collection of Field instances
 */
final class Item
{
    public int|string|null $id;
    public string $objectType;      // post, term, user, options
    public ?string $contentType;    // tattoo, artist, style (without BASE prefix)
    public ?object $wpObject;       // WP_Post, WP_Term, WP_User
 
    /** @var array<string, Field> Loaded fields */
    public array $fields = [];
 
    /** @var array<string, array> Raw field configs from registry */
    public array $fieldConfigs = [];
 
    /** @var string|null Base key for options storage */
    public ?string $baseKey = null;
 
    /**
     * WordPress default fields by object type
     */
    public const WP_DEFAULTS = [
        'post' => [
            'post_title',
            'post_excerpt',
            'post_content',
            'post_date',
            'post_status',
            'post_modified',
            'post_thumbnail',
            'menu_order'
        ],
        'user' => [
            'first_name',
            'last_name',
            'display_name',
            'description',
            'user_email',
        ],
        'term' => [
            'name',
            'description'
        ]
    ];
 
    public function __construct(
        int|string|null $id,
        string $objectType,
        ?string $contentType = null
    ) {
        $this->id = $id;
        $this->objectType = $objectType;
        $this->contentType = $contentType;
    }
 
    /**
     * Check if field exists in configs
     */
    public function hasField(string $name): bool
    {
        return isset($this->fields[$name]) || isset($this->fieldConfigs[$name]);
    }
 
    /**
     * Get loaded field instance
     */
    public function getField(string $name): ?Field
    {
        return $this->fields[$name] ?? null;
    }
 
    /**
     * Set/add a field instance
     */
    public function setField(Field $field): self
    {
        $this->fields[$field->name] = $field;
        return $this;
    }
 
    /**
     * Remove a field instance
     */
    public function removeField(string $name): self
    {
        unset($this->fields[$name]);
        return $this;
    }
 
    /**
     * Get field configuration
     */
    public function getFieldConfig(string $name): ?array
    {
        if (isset($this->fieldConfigs[$name])) {
            return $this->fieldConfigs[$name];
        }
 
        // Search nested fields (repeaters, groups)
        foreach ($this->fieldConfigs as $config) {
            if (isset($config['fields'][$name])) {
                return $config['fields'][$name];
            }
        }
 
        return null;
    }
 
    /**
     * Check if field is a WordPress default
     */
    public function isWpDefault(string $name): bool
    {
        $defaults = self::WP_DEFAULTS[$this->objectType] ?? [];
        return in_array($name, $defaults, true);
    }
 
    /**
     * Get all dirty (changed) fields
     * @return array<string, Field>
     */
    public function getDirtyFields(): array
    {
        return array_filter($this->fields, fn(Field $f) => $f->isDirty);
    }
 
    /**
     * Get all invalid fields
     * @return array<string, Field>
     */
    public function getInvalidFields(): array
    {
        return array_filter($this->fields, fn(Field $f) => !$f->isValid);
    }
 
    /**
     * Mark all loaded fields as clean
     */
    public function markAllClean(): self
    {
        foreach ($this->fields as $field) {
            $field->markClean();
        }
        return $this;
    }
 
    /**
     * Reset all fields to original values
     */
    public function resetAll(): self
    {
        foreach ($this->fields as $field) {
            $field->reset();
        }
        return $this;
    }
 
    /**
     * Check if any fields are dirty
     */
    public function hasDirtyFields(): bool
    {
        return !empty($this->getDirtyFields());
    }
 
    /**
     * Check if all fields are valid
     */
    public function isValid(): bool
    {
        return empty($this->getInvalidFields());
    }
 
    /**
     * Get all field names from configs
     */
    public function getFieldNames(): array
    {
        return array_keys($this->fieldConfigs);
    }
 
    /**
     * Get loaded field values as array
     */
    public function toArray(): array
    {
        $data = [];
        foreach ($this->fields as $name => $field) {
            $data[$name] = $field->value;
        }
        return $data;
    }
}