Jake Vanderwerf
2026-01-20 7a9054bb3f033c98067b3196378311dae54c5fbf
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace JVBase\registry;
 
if (!defined('ABSPATH')) {
    exit;
}
 
use JVBase\registry\providers\CalendarFieldProvider;
use JVBase\registry\providers\CommonFieldProvider;
use JVBase\registry\providers\FieldProviderInterface;
use JVBase\registry\providers\HelcimFieldProvider;
use JVBase\registry\providers\IntegrationFieldProvider;
 
class FieldRegistry
{
    private static ?self $instance = null;
    private array $fieldCache = [];
    private array $fieldProviders = [];
 
    /**
     * Get singleton instance
     */
    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
 
    private function __construct()
    {
        $this->registerFieldProviders();
    }
 
    /**
     * Register all field providers
     */
    private function registerFieldProviders(): void
    {
        // Core field providers
        $this->addFieldProvider('common', new CommonFieldProvider());
        $this->addFieldProvider('calendar', new CalendarFieldProvider());
        $this->addFieldProvider('integration', new IntegrationFieldProvider());
//      if (jvbSiteUsesHelcim()) {
//          $this->addFieldProvider('helcim', new HelcimFieldProvider());
//      }
 
 
 
        // Allow extensions to add providers
        do_action(BASE . 'register_field_providers', $this);
    }
 
    /**
     * Add a field provider
     */
    public function addFieldProvider(string $key, FieldProviderInterface $provider): void
    {
        $this->fieldProviders[$key] = $provider;
    }
 
    /**
     * Get fields for a specific type
     */
    public function getFields(string $type, ?string $object_type = null): array
    {
        $type = $this->normalizeType($type);
 
        if (!$this->isValidType($type)) {
            return [];
        }
 
        if (!$object_type) {
            $object_type = $this->getObjectType($type);
        }
 
        $cacheKey = "{$type}_{$object_type}";
        // Check cache first
        if (isset($this->fieldCache[$cacheKey]) && !JVB_TESTING) {
            return $this->fieldCache[$cacheKey];
        }
        $key = BASE.$cacheKey.'_fields';
        $fields = get_option($key, false);
        if ($fields) {
            return $fields;
        }
 
        // Build fields
        $fields = $this->buildFields($type, $object_type);
 
        // Cache the result
        $this->fieldCache[$cacheKey] = $fields;
        update_option($key, $fields);
 
        return $fields;
    }
 
    /**
     * Build fields for a type
     */
    private function buildFields(string $type, string $object_type): array
    {
        $config = $this->getConfig($type, $object_type);
 
        if (!$config) {
            return [];
        }
 
        $fields = $config['fields'] ?? [];
 
        // Process common fields
        if (array_key_exists('common', $fields) && !empty($fields['common'])) {
            $fields = $this->processCommonFields($fields, $fields['common']);
            unset($fields['common']);
        }
 
 
        // Apply integration fields
        $fields = $this->applyIntegrationFields($fields, $config, $type);
 
        // Apply filters for extensibility
        $fields = apply_filters(BASE . 'fields', $fields, $type, $object_type);
        return apply_filters(BASE . "{$type}_fields", $fields, $object_type);
    }
 
    /**
     * Process common fields
     */
    private function processCommonFields(array $fields, array $common): array
    {
        if (!isset($this->fieldProviders['common'])) {
            return $fields;
        }
 
        $provider = $this->fieldProviders['common'];
 
        foreach ($common as $field => $config) {
            if (!is_numeric($field)) {
                $commonFields = $provider->getFields($field, $config);
            } else {
                $commonFields = $provider->getFields($config);
            }
            $fields = array_merge($fields, $commonFields);
        }
 
        return $fields;
    }
 
    /**
     * Apply integration fields based on configuration
     */
    private function applyIntegrationFields(array $fields, array $config, string $type): array
    {
        if (array_key_exists('integrations', $config)) {
            if (isset($this->fieldProviders['integration'])) {
                $fields = array_merge($fields, $this->fieldProviders['integration']->getFields($config));
            }
        }
 
        // Calendar fields
        if (jvbCheck('is_calendar', $config)) {
            if (isset($this->fieldProviders['calendar'])) {
                $fields = array_merge($fields, $this->fieldProviders['calendar']->getFields());
            }
        }
 
        return $fields;
    }
 
    /**
     * Get configuration for a type
     */
    private function getConfig(string $type, string $object_type): ?array
    {
        switch ($object_type) {
            case 'post':
                return JVB_CONTENT[$type] ?? null;
            case 'term':
                return JVB_TAXONOMY[$type] ?? null;
            case 'user':
                return JVB_USER[$type] ?? null;
            case 'options':
                return JVB_OPTIONS;
            default:
                return null;
        }
    }
 
    /**
     * Initialize fields on plugin load
     */
    public function initializeFields(): void
    {
        // Pre-populate cache for all registered types
        foreach (JVB_CONTENT as $slug => $config) {
            $this->getFields($slug, 'post');
        }
 
        foreach (JVB_TAXONOMY as $slug => $config) {
            $this->getFields($slug, 'term');
        }
 
        foreach (JVB_USER as $slug => $config) {
            $this->getFields($slug, 'user');
        }
 
        if (!empty(JVB_OPTIONS)) {
            $this->getFields('options', 'options');
        }
    }
 
    private function normalizeType(string $type): string
    {
        return str_replace('-', '_', jvbNoBase($type));
    }
 
    private function isValidType(string $type): bool
    {
        return array_key_exists($type, JVB_CONTENT) ||
            array_key_exists($type, JVB_TAXONOMY) ||
            array_key_exists($type, JVB_USER) ||
            $type === 'options';
    }
 
    private function getObjectType(string $type): string
    {
        if (array_key_exists($type, JVB_CONTENT)) {
            return 'post';
        } elseif (array_key_exists($type, JVB_TAXONOMY)) {
            return 'term';
        } elseif (array_key_exists($type, JVB_USER)) {
            return 'user';
        } elseif ($type === 'options') {
            return 'options';
        }
 
        return '';
    }
}