<?php
|
namespace JVBase\managers\SEO\schemas\resolvers;
|
|
use JVBase\managers\SEO\schemas\SchemaDefinition;
|
use JVBase\managers\SEO\SchemaFieldHelpers;
|
use JVBase\managers\SEO\SchemaReferenceBuilder;
|
use JVBase\meta\Meta;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
/**
|
* Resolver for VisualArtwork and its children (Tattoo, etc.)
|
*
|
* Auto-enrichment:
|
* - Derives `artform` from taxonomy terms (tattoo-style → "American Traditional")
|
* - Derives `artMedium` from taxonomy terms (tattoo-colour → "Black and Grey")
|
* - Adds `creator` as Person reference from post author
|
* - Adds `keywords` from associated taxonomy terms
|
*/
|
class VisualArtworkResolver extends BaseResolver
|
{
|
public function getAutoFields(SchemaDefinition $definition): array
|
{
|
$fields = [];
|
|
if (!$definition->objectId || $definition->objectType !== 'post') {
|
return $fields;
|
}
|
|
$contentConfig = $this->getContentConfig($definition->contentType);
|
|
if (empty($contentConfig)) {
|
return $fields;
|
}
|
|
// Derive artform from style taxonomy terms
|
$artform = $this->deriveFromTaxonomy($definition->objectId, $contentConfig, 'style');
|
if (!empty($artform)) {
|
$fields['artform'] = count($artform) === 1 ? $artform[0] : $artform;
|
}
|
|
// Derive artMedium from medium/colour taxonomy terms
|
$medium = $this->deriveFromTaxonomy($definition->objectId, $contentConfig, 'colour');
|
if (!empty($medium)) {
|
$fields['artMedium'] = count($medium) === 1 ? $medium[0] : $medium;
|
}
|
|
// Add creator from post author
|
$creator = $this->buildCreatorRef($definition->objectId);
|
if (!empty($creator)) {
|
$fields['creator'] = $creator;
|
}
|
|
// Add keywords from all associated taxonomy terms
|
$keywords = $this->buildKeywords($definition->objectId, $contentConfig);
|
if (!empty($keywords)) {
|
$fields['keywords'] = $keywords;
|
}
|
|
return $fields;
|
}
|
|
/**
|
* Derive values from taxonomy terms associated with this post.
|
*
|
* Looks for taxonomies whose slug contains $hint (e.g., 'style', 'colour').
|
*/
|
private function deriveFromTaxonomy(int $postId, array $contentConfig, string $hint): array
|
{
|
$values = [];
|
$taxonomies = $contentConfig['taxonomies'] ?? [];
|
|
foreach ($taxonomies as $taxSlug) {
|
$slug = is_array($taxSlug) ? ($taxSlug['taxonomy'] ?? '') : $taxSlug;
|
|
if (!str_contains($slug, $hint)) {
|
continue;
|
}
|
|
$fullTax = str_starts_with($slug, BASE) ? $slug : BASE . $slug;
|
$terms = wp_get_post_terms($postId, $fullTax, ['fields' => 'names']);
|
|
if (!is_wp_error($terms) && !empty($terms)) {
|
$values = array_merge($values, $terms);
|
}
|
}
|
|
return array_unique($values);
|
}
|
|
/**
|
* Build Person reference from the post author.
|
*/
|
private function buildCreatorRef(int $postId): ?array
|
{
|
$post = get_post($postId);
|
|
if (!$post || !$post->post_author) {
|
return null;
|
}
|
|
return SchemaReferenceBuilder::build(
|
'user',
|
$post->post_author,
|
'Person'
|
);
|
}
|
|
/**
|
* Collect all taxonomy term names as keywords.
|
*/
|
private function buildKeywords(int $postId, array $contentConfig): array
|
{
|
$keywords = [];
|
$taxonomies = $contentConfig['taxonomies'] ?? [];
|
|
foreach ($taxonomies as $taxSlug) {
|
$slug = is_array($taxSlug) ? ($taxSlug['taxonomy'] ?? '') : $taxSlug;
|
$fullTax = str_starts_with($slug, BASE) ? $slug : BASE . $slug;
|
|
$terms = wp_get_post_terms($postId, $fullTax, ['fields' => 'names']);
|
|
if (!is_wp_error($terms)) {
|
$keywords = array_merge($keywords, $terms);
|
}
|
}
|
|
return array_unique($keywords);
|
}
|
|
/**
|
* Get content config from JVB_CONTENT constant.
|
*/
|
private function getContentConfig(?string $contentType): array
|
{
|
if (!$contentType || !defined('JVB_CONTENT')) {
|
return [];
|
}
|
|
$slug = jvbNoBase($contentType);
|
|
return JVB_CONTENT[$slug] ?? [];
|
}
|
}
|