Jake Vanderwerf
5 days ago 266aa37c48222993bf7bdad6834e31bd08736f5e
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
<?php
namespace JVBase\managers\SEO\render;
 
use JVBase\inc\managers\SEO\render\Thing\Intangible\ItemList\OfferCatalog;
use JVBase\managers\Cache;
use JVBase\managers\SEO\BreadcrumbManager;
use JVBase\managers\SEO\render\Thing\Intangible\Service;
use JVBase\managers\SEO\render\Thing\Organization\LocalBusiness\LocalBusiness;
use JVBase\registrar\config\seo\Resolver;
use JVBase\registrar\Registrar;
 
class SchemaOutput {
    protected array $types;
    public function __construct() {
        add_action('wp_head', [$this, 'outputSchema'], 0);
        $this->init();
    }
 
    protected function init(): void {
        $this->types = array_map(function ($type) { return jvbCheckBase($type); }, Registrar::getRegistered());
    }
    public function buildSchema():array
    {
        $schema = [];
 
 
        if (is_front_page()) {
            $schema[] = $this->buildWebsiteSchema(true);
            $test = $this->buildOrganizationSchema();
            if (!empty($test)) {
                $schema[] = $test;
            }
        }
        if (is_singular($this->types)) {
            $type = get_post_type();
            $registrar = Registrar::getInstance($type);
            if ($registrar) {
                $seo = $registrar->getSEO();
                $schema[] = $seo->schema()->outputSingularSchema();
            }
        }elseif (is_post_type_archive($this->types)) {
 
            $type = get_queried_object();
            $type = $type->name;
            $registrar = Registrar::getInstance($type);
 
            if ($registrar) {
                $seo = $registrar->getSEO();
                $schema[] =$seo->schema()->outputArchiveSchema();
            }
        } elseif (is_tax($this->types)) {
            $type = get_queried_object();
            $type = jvbNoBase($type->taxonomy);
            $registrar = Registrar::getInstance($type);
            if ($registrar ) {
                $seo = $registrar->getSEO();
                error_log('SEO: '.print_r($seo->schema(), true));
                $schema[] = $seo->schema()->outputArchiveSchema();
            }
        }
        $isContent = array_values(array_filter(array_map(function($item) {
            return intval(get_option(BASE.$item.'_archive', false));
        },Registrar::getFeatured('is_content', 'term'))));
 
        if (!empty($isContent) && is_page($isContent)){
            $type = get_post_meta(get_the_id(), BASE.'for_type', true);
//          error_log('Type: '.print_r($type, true));
            if (!empty($type)) {
                $registrar = Registrar::getInstance($type);
                if ($registrar) {
                    $schema[] = $registrar->getSEO()->schema()->outputContentTaxArchiveSchema();
                }
            }
 
        }
 
 
 
        $breadcrumbs = $this->buildBreadcrumbs();
        if ($breadcrumbs) {
            $schema[] = $breadcrumbs;
        }
 
        if (!empty($schema)) {
            if (!is_front_page()) {
                $website = JVB()->schemaHelper()::schema('website');
                if (!empty($website)) {
                    if (JVB_TESTING) {
                        Cache::for('websiteSchema')->flush();
                    }
                    $website = Cache::for('websiteSchema')->remember(
                        'schema',
                        function () {
                            return $this->websiteSchema();
                        }
                    );
                    array_unshift($schema, $website);
                }
            }
 
            $schema = [
                '@context'      => 'https://schema.org',
                '@graph'        => $schema
            ];
        }
        return $schema;
    }
 
    protected function websiteSchema():array
    {
        $stored = JVB()->schemaHelper()::schema('website');
        $website = JVB()->schemaHelper()::classFromConfig($stored);
 
        if (!$website->getUrl() || empty($website->getUrl())){
            $website->setUrl(get_home_url());
        }
 
        if (!$website->getName() || empty($website->getName())){
            $website->setName(get_bloginfo('name'));
        }
        if(!$website->getCreator() || empty($website->getCreator(true))) {
            $website->setCreator($this->getCreator(true));
        }
        return $website->outputSchema();
    }
 
    public function outputSchema():void
    {
        $schema = $this->buildSchema();
        if (empty($schema)) {
            return;
        }
//      $encoded =  wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
//      $encoded =  wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT, 1024);
        $encoded = wp_json_encode($schema, JSON_UNESCAPED_SLASHES, 512);
        if ($encoded === false) {
            error_log('wp_json_encode failed: ' . json_last_error_msg());
            return;
        }
        echo "\n<!-- SEO Schema by JakeVan -->\n";
        echo '<script type="application/ld+json">' . "\n";
 
        echo $encoded;
        echo "\n" . '</script>' . "\n";
        echo "\n" . '<!-- / SEO Schema by JakeVan -->'."\n";
    }
 
    public function buildBreadcrumbs():array
    {
        return BreadcrumbManager::getInstance()->toSchema();
    }
 
    public function buildWebsiteSchema(bool $full = true):array
    {
        if (JVB_TESTING){
            Cache::for('websiteSchema')->flush();
        }
        $storedWebsite = JVB()->schemaHelper()::schema('website');
 
        if (!$full) {
            return Cache::for('websiteSchema')->remember(
                'reference',
                function () use ($storedWebsite) {
                    $allowed = ['type','name', 'url', 'description', 'inLanguage'];
                    $storedWebsite = array_filter($storedWebsite, function ($key) use ($allowed) {
                        return in_array($key, $allowed);
                    }, ARRAY_FILTER_USE_KEY);
 
                    $website = JVB()->schemaHelper()::classFromConfig($storedWebsite);
                    if (!$website->getName() || empty($website->getName())) {
                        $website->setName(get_bloginfo('name'));
                    }
                    if (!$website->getUrl() || empty($website->getUrl())) {
                        $website->setUrl(get_home_url());
                    }
                    if (!$website->getId() || empty($website->getId())) {
                        $website->setId(get_home_url().'/#website');
                    }
                    if (!$website->getDescription() || empty($website->getDescription())) {
                        $website->setDescription(get_bloginfo('description'));
                    }
                    if (!$website->getInLanguage() || empty($website->getInLanguage())) {
                        $website->setInLanguage('en-CA');
                    }
                    if (!$website->getPublisher() || empty($website->getPublisher())) {
                        $publisher = $this->getOptionSchemaReference('organization');
                        if ($publisher){
                            $website->setPublisher($publisher);
                        }
                    }
 
                    $website->setCreator($this->getCreator(true));
 
                    return $website->outputSchema();
                }
            );
        }
        return Cache::for('websiteSchema')->remember(
            'full',
            function () use ($storedWebsite) {
                $website = JVB()->schemaHelper()::classFromConfig($storedWebsite);
                if (!$website->getName() || empty($website->getName())) {
                    $website->setName(get_bloginfo('name'));
                }
                if (!$website->getUrl() || empty($website->getUrl())) {
                    $website->setUrl(get_home_url());
                }
                if (!$website->getId() || empty($website->getId())) {
                    $website->setId(get_home_url().'/#website');
                }
                if (!$website->getDescription() || empty($website->getDescription())) {
                    $website->setDescription(get_bloginfo('description'));
                }
                if (!$website->getInLanguage() || empty($website->getInLanguage())) {
                    $website->setInLanguage('en-CA');
                }
                if (!$website->getPublisher() || empty($website->getPublisher())) {
                    $publisher = $this->getOptionSchemaReference('organization');
                    if ($publisher){
                        $website->setPublisher($publisher);
                    }
                }
 
                $website->setCreator($this->getCreator(true));
 
                return $website->outputSchema();
            }
        );
    }
 
 
    public function getCreator(bool $reference = false, bool $jakeVan = false):LocalBusiness
    {
        if (JVB_TESTING){
            Cache::for('JakeVanCreator')->flush();
        }
 
        if ($reference && $jakeVan) {
            return Cache::for('JakeVanCreator')->remember(
                'reference',
                function () {
                    $creator = new LocalBusiness();
                    $creator->setId('jakevan');
                    $creator->setName('JakeVan');
                    $creator->setUrl('https://jakevan.ca/');
                    $creator->setDescription('Let\'s bring your idea to life.');
                    return $creator;
                }
            );
        }
        if ($jakeVan) {
            return Cache::for('JakeVanCreator')->remember(
                'full',
                function () {
                    $creator = new LocalBusiness();
                    $creator->setName('JakeVan');
                    $creator->setId('https://jakevan.ca/#localbusiness');
                    $creator->setAlternateName(['Jake Vanderwerf', 'Rooted Romantic', 'Jacob Vanderwerf']);
                    $creator->setUrl('https://jakevan.ca');
                    $creator->setSameAs([
                        'https://bsky.app/profile/jakevan.ca',
                    ]);
                    $creator->setAreaServed(['Edmonton, Alberta', 'Alberta', 'Canada']);
                    $offers = new OfferCatalog();
                    $offers->setName('Services');
                    $offers->setUrl('https://jakevan.ca/services');
 
                    $graphicDesign = new Service();
                    $graphicDesign->setName('Graphic Design');
                    $graphicDesign->setUrl('https://jakevan.ca/services/design/');
                    $graphicDesign->setDescription('From print to digital design.');
                    $websiteDesign = new Service();
                    $websiteDesign->setName('Development');
                    $websiteDesign->setUrl('https://jakevan.ca/services/development/');
                    $websiteDesign->setDescription('From basic websites to custom functionality.');
                    $strategy = new Service();
                    $strategy->setName('Strategy');
                    $strategy->setUrl('https://jakevan.ca/services/strategy/');
                    $strategy->setDescription('From developing your business plan to SEO.');
                    $art = new Service();
                    $art->setName('Art');
                    $art->setUrl('https://jakevan.ca/services/art/');
                    $art->setDescription('From unique, custom, handmade pieces to small-scale wholesale.');
 
                    $offers->setItemListElement([
                        $graphicDesign,
                        $websiteDesign,
                        $strategy,
                        $art
                    ]);
 
 
                    $creator->setHasOfferCatalog($offers);
                    return $creator;
                }
            );
        }
        if ($reference) {
            return $this->getOptionSchemaReference('organization');
        }
        $config = JVB()->schemaHelper()::schema('organization');
 
        return JVB()->schemaHelper()::classFromConfig($config);
    }
 
    public function getOptionSchemaReference(string $option):mixed
    {
        if (!in_array(strtolower($option), array_merge(
            ['organization',
            'website'],
            Registrar::getRegistered()
        ))) {
            error_log('Attempted to get schema reference for: '.$option.', but it does not exist');
            return null;
        }
 
        $stored = JVB()->schemaHelper()::reference($option);
 
        if (empty($stored)){
            error_log('Attempted to get schema reference for: '.$option.', but defaults not set.');
            return null;
        }
        $type = $stored['type'];
        if (!class_exists($type)){
            error_log('Attempted to get schema reference, but class does not exist: '.$type);
            return null;
        }
        $class = new $type();
        unset($stored['type']);
        $minimal = apply_filters(BASE.'OrganizationReference', ['name', 'url', 'sameAs', 'logo']);
 
        foreach ($minimal as $property) {
            $method = 'set'.ucfirst($property);
            $value = array_key_exists($property, $stored) ? $stored[$property] : null;
            if (!$value) {continue;}
 
            if (str_contains($value, '{{')) {
                $meta = null;
                $value = Resolver::resolve($property, $value);
            }
            $class->$method($value);
        }
        return $class;
    }
 
    public function buildOrganizationSchema():array
    {
        $config = JVB()->schemaHelper()::schema('organization');
 
        $class = JVB()->schemaHelper()::classFromConfig($config);
        return ($class)? $class->outputSchema() : [];
    }
}