self::resolveVariable($matches[1], $meta), $template ); } protected static function resolveVariable(string $variable, Meta $meta): string { $variable = trim($variable); if (str_contains($variable, '.')) { return self::resolveRelation($variable, $meta); } if ($variable === 'post_permalink') { return get_the_permalink($meta->id()); } $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): 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) $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):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), default => false }; } public static function resolveImageProperty(string $property, mixed $value, mixed $schema, Meta $meta):?ImageObject { $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; } 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; } ); } }