| | |
| | | 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; |
| | | protected static ?Meta $meta; |
| | | |
| | | public static function resolve(string $template, Meta $meta): string |
| | | public static function resolve(string $template, ?Meta $meta): string |
| | | { |
| | | self::$meta = $meta; |
| | | return preg_replace_callback( |
| | |
| | | $template |
| | | ); |
| | | } |
| | | protected static function resolveVariable(string $variable, Meta $meta): string |
| | | 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 ($variable === 'post_permalink') { |
| | | 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); |
| | |
| | | }; |
| | | } |
| | | |
| | | protected static function resolveRelation(string $path, Meta $meta): string |
| | | protected static function resolveRelation(string $path, ?Meta $meta = null): string |
| | | { |
| | | $parts = explode('.', $path); |
| | | $relation = array_shift($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 ''; |
| | |
| | | return self::resolve($field, $newMeta); |
| | | } |
| | | |
| | | protected static function resolveImage(string $variable, Meta $meta, bool $returnID = false): string |
| | | protected static function resolveImage(string $variable, ?Meta $meta, bool $returnID = false): string |
| | | { |
| | | $imgID = $meta->get($variable); |
| | | if (!$imgID || $imgID === '') { |
| | |
| | | } |
| | | return $image[0]; |
| | | } |
| | | protected static function resolveObject(string $variable, Meta $meta): string |
| | | protected static function resolveObject(string $variable, ?Meta $meta): string |
| | | { |
| | | //Hmmm... this should already be handled by dot notation. |
| | | return ''; |
| | |
| | | * 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):mixed |
| | | 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::resolve($value, $meta); |
| | | } |
| | | |
| | | public static function checkPropertyType(string $property, mixed $value, mixed $schema, Meta $meta):mixed |
| | | 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 |
| | | 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 (!$img) { |
| | | return null; |
| | | } |
| | | Cache::for('imageSchemaObject')->flush(); |
| | | if (JVB_TESTING){ |
| | | Cache::for('imageSchemaObject')->flush(); |
| | | } |
| | | |
| | | return Cache::for('imageSchemaObject')->connect('post')->remember( |
| | | $imgID, |
| | | function () use ($imgID, $img) { |
| | |
| | | ); |
| | | |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | } |