Jake Vanderwerf
2026-04-15 c4aa5cdb5e90ad4b420e22772797d16980232a2b
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
namespace JVBase\base;
use JVBase\meta\Meta;
use JVBase\registrar\config\seo\Resolver;
use JVBase\registrar\Registrar;
 
class SchemaHelper
{
    protected static array $allowedTypes;
    protected static array $allowedFormats = ['schema', 'archive', 'meta', 'reference'];
    protected static array $schemas = [];
    protected static array $metas = [];
    protected static array $archives = [];
    protected static array $references = [];
    public function __construct()
    {
        self::$allowedTypes = array_merge(['website', 'organization'], Registrar::getRegistered());
    }
    public static function checkType(string $type, string $reference = '[SchemaHelper] Invalid type'):string|false
    {
        $type = strtolower($type);
        if (!in_array($type, self::$allowedTypes)) {
            error_log($reference.': '.$type);
            return false;
        }
        return $type;
    }
    public static function checkFormat(string $format, string $reference = '[SchemaHelper] Invalid format'):string|false
    {
        $format = strtolower($format);
        if (!in_array($format, self::$allowedFormats)) {
            error_log($reference.': '.$format);
            return false;
        }
        return $format;
    }
    public static function getConfig(string $type, string $format):array
    {
        $reference = '[SchemaHelper]::getConfig';
        $type = self::checkType($type, $reference);
        $format = self::checkFormat($format, $reference);
        if (!$type || !$format) {
            return [];
        }
        return match($format) {
            'schema' => self::schema($type),
            'archive' => self::archive($type),
            'meta' => self::meta($type),
            'reference' => self::reference($type),
        };
    }
    public static function schema(string $type): array
    {
        $type = self::checkType($type, '[SchemaHelper]::schema');
        if (!$type) {
            return [];
        }
 
        if (!array_key_exists($type, self::$schemas)) {
            self::$schemas[$type] = get_option(BASE.ucfirst($type).'Schema', self::getDefault($type, 'schema'));
        }
        return self::$schemas[$type];
    }
    public static function meta(string $type): array
    {
        $type = self::checkType($type, '[SchemaHelper]::meta');
        if (!$type) {
            return [];
        }
        if (!array_key_exists($type, self::$metas)) {
            self::$metas[$type] = get_option(BASE.ucfirst($type).'Meta', self::getDefault($type, 'meta'));
        }
        return self::$metas[$type];
    }
    public static function archive(string $type): array
    {
        $type = self::checkType($type, '[SchemaHelper]::archive');
 
        if (!$type) {
            return [];
        }
 
        if (!array_key_exists($type, self::$archives)) {
            self::$archives[$type] = get_option(BASE.ucfirst($type).'Archive', self::getDefault($type, 'archive'));
        }
        return self::$archives[$type];
    }
    public static function reference(string $type): array
    {
        $type = self::checkType($type, '[SchemaHelper]::reference');
        if (!$type) {
            return [];
        }
        if (!array_key_exists($type, self::$references)) {
            self::$references[$type] = get_option(BASE.ucfirst($type).'Reference', self::getDefault($type, 'reference'));
        }
        return self::$references[$type];
    }
 
    public static function getDefault(string $type, string $format):array
    {
        $reference = '[SchemaHelper]::getDefault';
        $type = self::checkType($type, $reference);
        $format = self::checkFormat($format, $reference);
        if (!$type || !$format) {
            return [];
        }
 
        $defaults = match ($format) {
            'schema' => match ($type) {
                'website' => [
                    'type'  => 'JVBase\managers\SEO\render\Thing\CreativeWork\WebSite',
                    'name' => get_bloginfo('name'),
                    'url' => get_home_url(),
                    'id' => get_home_url() . '/#website',
                    'description' => get_bloginfo('description'),
                    'inLanguage' => 'en-CA'
                ],
                default => []
            },
            'archive' => [
                'type'  => 'JVBase\inc\managers\SEO\render\Thing\CreativeWork\WebPage\CollectionPage\CollectionPage',
                'name'  => '{{name}}'
            ],
            default => [],
        };
        error_log('Getting defaults for: '.BASE.ucfirst($type).ucfirst($format).'Default');
        $result = apply_filters(BASE.ucfirst($type).ucfirst($format).'Default', $defaults);
        if ($format === 'reference' && empty($result)) {
            $full = self::getDefault($type, 'schema');
            if (!empty($full)) {
                $result = [
                    'type'  => $full['type'],
                    'name'  => $full['name'],
                    'description'=> $full['description']
                ];
                $check = ['id', 'url'];
                foreach ($check as $ch) {
                    if (array_key_exists($ch, $full)) {
                        $result[$ch] = $full[$ch];
                    }
                }
            }
        }
        return $result;
    }
 
    public static function updateHistory(string $type, string $format, array $newest):bool
    {
        $reference = '[SchemaHelper]::updateHistory';
        $type = self::checkType($type, $reference);
        $format = self::checkFormat($format, $reference);
        if (!$type || !$format) {
            return false;
        }
 
        $historyOption = BASE.ucfirst($type).ucfirst($format).'History';
        $history = get_option($historyOption, []);
        array_unshift($history, $newest);
        if (count($history) > 5) {
            array_pop($history);
        }
        return update_option($historyOption, $history);
    }
 
    public static function update(string $type, string $format, array $config, ?Meta $meta = null):bool
    {
        $reference = '[SchemaHelper]::update';
        $type = self::checkType($type, $reference);
        $format = self::checkFormat($format, $reference);
        if (!$type || !$format) {
            return false;
        }
        $method = 'update'.ucfirst($type);
        return self::$method($config, $meta);
    }
    public static function updateSchema(string $type, array $config):bool
    {
        $reference = '[SchemaHelper]::updateSchema';
        $type = self::checkType($type, $reference);
        if (!$type) {
            return false;
        }
        if (!class_exists($config['type'])){
            error_log('[SchemaHelper]::updateSchema Config must be a valid schema type: '.$config['type']);
            return false;
        }
        if (!in_array($type, self::$allowedTypes)) {
            error_log('[SchemaHelper]::updateSchema Config must have a schema type');
        }
 
        return self::updateClassConfig($type, 'schema', $config);
    }
 
    public static function updateClassConfig(string $type, string $format, array $config):bool
    {
        $reference = '[SchemaHelper]::updateClassConfig';
        $type = self::checkType($type, $reference);
        if (!$type) {
            return false;
        }
        if (!class_exists($config['type'])){
            error_log($reference.' Config must be a valid schema type: '.$config['type']);
            return false;
        }
        if (!in_array($type, self::$allowedTypes)) {
            error_log($reference.' Config must have a schema type');
        }
        //Merge stored config with updates
        $stored = self::schema($type);
        $update = array_merge_recursive($stored, $config);
 
        //Validate Properties
        $className = $update['type'];
        unset($update['type']);
        foreach ($update as $property => $value) {
            if (!property_exists($className, $property)) {
                error_log($reference.' invalid property attempted: '.$property.', with value: '.print_r($value, true).' for class: '.$className);
                unset($update[$property]);
            }
        }
        $update['type'] = $className;
 
        //Add changes to history (keeps last 5 changes)
        self::updateHistory($type, $format, $update);
        self::$schemas[$type] = $update;
        return update_option(BASE.ucfirst($type).ucfirst($format), $update);
    }
 
    public static function updateMeta(string $type, array $config):bool
    {
        $type = self::checkType($type, '[SchemaHelper]::updateMeta');
        $allowed = array_filter($config, function($key) {
            $allowed = in_array($key, ['name', 'description']);
            if (!$allowed) {
                error_log('[SchemaHelper]::updateMeta invalid property attempted: '.$key);
            }
            return $allowed;
        });
        if (empty($allowed)) {
            error_log('[SchemaHelper]::updateMeta Name or Description must be set');
            return false;
        }
        $config = array_map('sanitize_text_field', $config);
        self::updateHistory($type, 'meta', $config);
        self::$metas[$type] = $config;
        return update_option(BASE.ucfirst($type).'Meta', $config);
    }
 
    public static function updateArchive(string $type, array $config):bool
    {
        $reference = '[SchemaHelper]::updateArchive';
        $type = self::checkType($type, $reference);
        if (!$type) {
            return false;
        }
        if (!class_exists($config['type'])){
            error_log('[SchemaHelper]::updateSchema Config must be a valid schema type: '.$config['type']);
            return false;
        }
        if (!in_array($type, self::$allowedTypes)) {
            error_log('[SchemaHelper]::updateSchema Config must have a schema type');
        }
 
        return self::updateClassConfig($type, 'schema', $config);
    }
 
    public static function classFromConfig(array $config, ?Meta $meta = null):mixed
    {
        if (!array_key_exists('type', $config)) {
            error_log('[SchemaHelper]::classFromConfig No class defined in config: '.print_r($config, true));
            return false;
        }
        $className = $config['type'];
        unset($config['type']);
        $class = new $className();
 
 
        foreach ($config as $property => $value) {
            if (is_array($value) && array_key_exists('type', $value)) {
                $value = self::classFromConfig($value, $meta);
            }
            $method = 'set'.ucfirst($property);
            if (!method_exists($class, $method)) {
                error_log('[SchemaHelper]::classFromConfig - method: '.$method.' does not exist in class: '.$className);
                continue;
            }
            if (is_string($value) && str_contains($value, '{{')) {
                $value = Resolver::resolveForSchema($property, $value, $config, $meta);
            }
            if (!empty($value)) {
                $class->$method($value);
            }
        }
        return $class;
    }
}
 
 
 
 
 
 
/**
 * JVB_SCHEMA: Site-wide schema configuration
 *
 * Structure:
 *   - business: LocalBusiness/Organization for home page
 *   - website: WebSite schema configuration
 *   - actions: PotentialAction definitions
 *   - attribution: Developer/maintainer info
 */
 
 
/**
JVB_CONTENT['artwork'] = [
    'singular' => 'Artwork',
    'plural' => 'Artworks',
    // ... other config
 
    'seo' => [
        'meta' => [
            'title' => '{{post_title}} by {{linked_user.display_name}} | {{site_title}}',
            'description' => '{{style.primary.name}} artwork by {{linked_user.display_name}}. {{short_bio|default:View this piece and more.}}',
            'archive_title' => 'Artwork Gallery',
            'archive_description' => 'Browse our collection of tattoo artwork and designs.',
        ],
        'schema' => [
            'type' => 'VisualArtwork',
            'mappings' => [
                'artform' => 'style.primary',           // DefinedTerm from taxonomy
                'creator' => 'linked_user',             // Person from linked user
                'image' => 'featured_image',            // ImageObject
                'artMedium' => 'medium.names:3',        // Comma-separated term names
            ],
            'overrides' => [
                'inLanguage' => 'en',
            ]
        ]
    ]
];
 
JVB_CONTENT['artist'] = [
    'singular' => 'Artist',
    'plural' => 'Artists',
 
    'seo' => [
        'meta' => [
            'title' => '{{post_title}} - {{artist_type|default:Tattoo Artist}} in {{city.primary.name|default:Edmonton}}',
            'description' => '{{short_bio|truncate:155}}',
        ],
        'schema' => [
            'type' => 'Person',
            'mappings' => [
                'image' => 'image_portrait',
                'jobTitle' => 'artist_type',
                'worksFor' => 'shop.primary',           // LocalBusiness reference
                'knowsAbout' => 'style.names',          // Array of style names
                'areaServed' => 'city.primary.name',
            ]
        ]
    ]
];
 
JVB_TAXONOMY['shop'] = [
    'singular' => 'Shop',
    'plural' => 'Shops',
 
    'seo' => [
        'meta' => [
            'title' => '{{term_name}} - Tattoo Shop in {{city.primary.name|default:Edmonton}}',
            'description' => '{{tagline|default:Visit}} {{term_name}}. {{short_bio|truncate:120}}',
        ],
        'schema' => [
            'type' => 'TattooParlor',  // or LocalBusiness
            'mappings' => [
                'address' => 'location',
                'telephone' => 'phone',
                'email' => 'email',
                'openingHoursSpecification' => 'hours',
                'image' => 'image',
                'priceRange' => 'price_range',
                'paymentAccepted' => 'payment_accepted',
            ],
            'overrides' => [
                'additionalTypeTrait' => 'https://schema.org/TattooParlor',
            ]
        ]
    ]
];
 
JVB_TAXONOMY['style'] = [
    'singular' => 'Style',
    'plural' => 'Styles',
 
    'seo' => [
        'meta' => [
            'title' => '{{term_name}} Tattoos in Edmonton | {{site_title}}',
            'description' => '{{tagline|default:Explore}} {{term_name}} tattoo artists and designs. {{characteristics|strip|truncate:100}}',
        ],
        'schema' => [
            'type' => 'DefinedTerm',
            'mappings' => [
                'alternateName' => 'alternate_name',
            ]
        ]
    ]
];
 
**/