Jake Vanderwerf
3 days ago ba1e1ccf869b818f7a7a897264dfea05563a7796
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
<?php
 
namespace JVBase\registrar\config;
 
use JVBase\registrar\config\seo\Archive;
use JVBase\registrar\config\seo\Meta;
use JVBase\registrar\config\seo\Schema;
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
final class SEO extends Config
{
    protected string $slug;
    protected array $config;
    public Schema $schema;
    public Meta $meta;
    public Archive $archive;
 
 
    public function __construct(string $slug)
    {
        $this->slug = $slug;
        $action = ucFirst($slug);
        $this->config = [
            'schema'    => get_option(BASE.$action.'Schema', apply_filters(BASE.$action.'Schema', [])),
            'meta'      => get_option(BASE.$action.'Meta', apply_filters(BASE.$action.'Meta', [])),
            'archive'   => get_option(BASE.$action.'Archive', apply_filters(BASE.$action.'Archive', [])),
        ];
    }
 
    protected function initSchema():void
    {
        if (!array_key_exists('type', $this->config['schema'])) {
            $registrar = Registrar::getInstance($this->slug);
            if ($registrar) {
                switch ($registrar->getType()) {
                    case 'term':
                        $this->config['schema']['type'] = 'JVBase\inc\managers\SEO\render\Thing\CreativeWork\WebPage\CollectionPage\CollectionPage';
                        break;
                    case 'post':
                        $this->config['schema']['type'] = 'JVBase\managers\SEO\render\Thing\CreativeWork\CreativeWork';
                        break;
                    case 'user':
                        $this->config['schema']['type'] = 'JVBase\managers\SEO\render\Thing\CreativeWork\WebPage\ProfilePage';
                        break;
                }
 
            }
        }
        if (!array_key_exists('type', $this->config['schema'])) {
            error_log('Missing schema type');
            return;
        }
        if (!class_exists($this->config['schema']['type'])) {
            error_log('Could not find class: '.$this->config['schema']['type']);
            return;
        }
        $this->schema = new Schema($this->slug, $this->config['schema']['type']);
 
    }
    public function schema():Schema|false
    {
        if (!isset($this->schema)) {
            $this->initSchema();
        }
        return $this->schema??false;
    }
    public function meta():Meta
    {
        return $this->meta;
    }
    public function archive():Archive
    {
        return $this->archive;
    }
 
    public function getConfig():array
    {
        return  $this->config;
    }
}