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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace JVBase\registrar\config\seo;
 
use JVBase\managers\Cache;
use JVBase\managers\SEO\render\Thing\CreativeWork\MediaObject\ImageObject;
use JVBase\managers\SEO\render\Thing\Intangible\Quantity\Distance;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class Resolver {
    protected static ?Meta $meta;
 
    public static function resolve(string $template, ?Meta $meta): string
    {
        self::$meta = $meta;
        return preg_replace_callback(
            '/\{\{([^}]+)}}/',
            fn($matches) => self::resolveVariable($matches[1], $meta),
            $template
        );
    }
    protected static function resolveVariable(string $variable, ?Meta $meta = null): mixed
    {
        $variable = trim($variable);
        switch ($variable) {
            case 'CREATOR':
                return JVB()->seo()->getCreator();
                break;
        }
        if (str_contains($variable, '.')) {
            return self::resolveRelation($variable, $meta);
        }
        if ($meta && $variable === 'post_permalink') {
            return get_the_permalink($meta->id());
        }
        if (!$meta) {
            return '';
        }
        $config = $meta->config($variable);
        if (!$config) {
            error_log('[SEO]Meta Resolver. Could not find meta configuration for variable: '.$variable);
            return '';
        }
        return match($config['type']) {
            'upload', 'image', 'gallery' => self::resolveImage($variable, $meta),
            'selector', 'taxonomy', 'user', 'post' => self::resolveObject($variable, $meta),
            default => $meta->get($variable),
        };
    }
 
    protected static function resolveRelation(string $path, ?Meta $meta = null): string
    {
        $parts = explode('.', $path);
        $relation = array_shift($parts);
        $field = implode('.', $parts);
 
        //We need to:
            // 1) Get the id of the item we're fetching (meta value of the $relation)
        if ($relation === 'registrar') {
            if (count($parts) === 2) {
                return self::resolveRegistrar($parts[0], $parts[1]);
            } else {
                error_log('[Resolver]::resolveRelation: Registrar relation requires registrar.[slug].[property]');
                return '';
            }
 
        }
        $ID = $meta->get($relation);
        if (!$ID || $ID === '') {
            return '';
        }
            // 2) Create new Meta for that relation
        $config = $meta->config($relation);
        if (!$config) {
            return '';
        }
        $type = false;
        if ($config['type'] === 'taxonomy' || array_key_exists('taxonomy', $config)) {
            $type = 'taxonomy';
        } elseif ($config['type'] === 'user'){
            $type = 'user';
        } elseif ($config['type'] === 'post'){
            $type = 'post';
        }
        if (!$type) {
            error_log('[SEO]Meta Resolver. Could not find type for relation: '.$relation.': '.$field);
            return '';
        }
        $newMeta = new Meta($ID, $type);
            // 3) Pass to resolver
        return self::resolve($field, $newMeta);
    }
 
    protected static function resolveImage(string $variable, ?Meta $meta, bool $returnID = false): string
    {
        $imgID = $meta->get($variable);
        if (!$imgID || $imgID === '') {
            return '';
        }
        if ($returnID) {
            return $imgID;
        }
        $image = wp_get_attachment_image_src($imgID,'full');
        if (!$image) {
            return '';
        }
        return $image[0];
    }
    protected static function resolveObject(string $variable, ?Meta $meta): string
    {
        //Hmmm... this should already be handled by dot notation.
        return '';
    }
 
    /**
     * SCHEMA RESOLVING
     * We need to map the values to what schema.org expects.
     * Most are defined in the JVBase\managers\schema\render namespace.
     */
    public static function resolveForSchema(string $property, string $value, mixed $schema, ?Meta $meta = null):mixed
    {
        $check = 'resolve'.ucfirst($property).'Property';
        if (method_exists(self::class, $check)) {
            return self::$check($property, $value, $meta);
        }
 
        $checkType = self::checkPropertyType($property, $value, $schema, $meta);
        if ($checkType !== false) {
            return $checkType;
        }
 
        error_log('[SEO]Resolver - No method found for '.$property.' with value: '.print_r($value, true).'. Defaulting to base Resolver');
 
        return self::resolve($value, $meta);
    }
 
        public static function checkPropertyType(string $property, mixed $value, mixed $schema, ?Meta $meta):mixed
        {
            return match($property) {
                'logo', 'image', 'photo', 'primaryImageOfPage', 'thumbnail', 'associatedMedia' => self::resolveImageProperty($property, $value, $schema, $meta),
                'creator' => self::resolveCreator($property, $value, $schema, $meta),
                default => false
            };
        }
 
        public static function resolveImageProperty(string $property, mixed $value, mixed $schema, ?Meta $meta):?ImageObject
        {
            if (!$meta) {
                return null;
            }
            $value = str_replace('{{', '', str_replace('}}', '', $value));
            $imgID = $meta->get($value);
            error_log('Got image id: '.print_r($imgID, true));
            if (!$imgID || $imgID === '') {
                return null;
            }
            $img = wp_get_attachment_image_src($imgID,'full');
            if (!$img) {
                return null;
            }
            if (JVB_TESTING){
                Cache::for('imageSchemaObject')->flush();
            }
 
            return Cache::for('imageSchemaObject')->connect('post')->remember(
                $imgID,
                function () use ($imgID, $img) {
                    $imageObject = new ImageObject();
                    $imageObject->setContentUrl($img[0]);
                    $width = new Distance();
                    $width->setName($img[1].' px');
                    $imageObject->setWidth($width);
                    $height = new Distance();
                    $height->setName($img[2].' px');
                    $imageObject->setHeight($height);
 
                    $image_path = get_attached_file($imgID);
                    if ($image_path && file_exists($image_path)) {
                        $bytes = filesize($image_path);
                        $imageObject->setContentSize($bytes);
 
                        $fileType = wp_check_filetype($image_path);
                        if ($fileType['ext']) {
                            $imageObject->setEncodingFormat($fileType['ext']);
                        }
                    }
 
                    $caption = wp_get_attachment_caption($imgID)??get_post($imgID)->post_content??false;
                    if ($caption && $caption!== '') {
                        $imageObject->setCaption($caption);
                    }
                    return $imageObject;
                }
            );
 
        }
 
        public static function resolveCreator(string $type, mixed $value, mixed $schema, ?Meta $meta):mixed
        {
            if (is_numeric($value)) {
                //TODO generate from id
 
            } else if (str_contains($value, 'CREATOR')) {
                return JVB()->seo()->getCreator();
            }
            return '';
        }
 
        public static function resolveRegistrar(string $type, string $property):string
        {
            $registrar = Registrar::getInstance($type);
            if (!$registrar) {
                return '';
            }
            $method = 'get'.ucfirst($property);
            if (!method_exists($registrar, $method)) {
                error_log('[Resolver]::resolveRegistrar: Invalid property getter: '.$property);
                return '';
            }
            return $registrar->$method();
        }
}