<?php
|
namespace JVBase\managers\SEO\schemas\resolvers;
|
|
use JVBase\managers\SEO\schemas\SchemaDefinition;
|
use JVBase\managers\SEO\SchemaReferenceBuilder;
|
use JVBase\meta\Meta;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
/**
|
* Resolver for Person schema (artists, staff, authors).
|
*
|
* Auto-enrichment:
|
* - Adds `worksFor` from shop/organization association
|
* - Adds `makesOffer` / sample `workExample` from their content
|
* - Derives `knowsAbout` from their content's taxonomy terms
|
*/
|
class PersonResolver extends BaseResolver
|
{
|
public function getAutoFields(SchemaDefinition $definition): array
|
{
|
$fields = [];
|
|
if (!$definition->objectId) {
|
return $fields;
|
}
|
|
// For user-type Person schemas (author pages)
|
if ($definition->objectType === 'user') {
|
$worksFor = $this->buildWorksFor($definition->objectId);
|
if (!empty($worksFor)) {
|
$fields['worksFor'] = $worksFor;
|
}
|
|
$examples = $this->buildWorkExamples($definition->objectId);
|
if (!empty($examples)) {
|
$fields['workExample'] = $examples;
|
}
|
}
|
|
return $fields;
|
}
|
|
/**
|
* Find Organization/LocalBusiness the person works for.
|
*
|
* Checks taxonomy terms assigned to the user (e.g., shop terms).
|
*/
|
private function buildWorksFor(int $userId): ?array
|
{
|
if (!defined('JVB_TAXONOMY')) {
|
return null;
|
}
|
|
// Find taxonomies that represent organizations (is_content taxonomies)
|
foreach (JVB_TAXONOMY as $slug => $config) {
|
if (empty($config['is_content'])) {
|
continue;
|
}
|
|
$fullTax = BASE . $slug;
|
$terms = wp_get_object_terms($userId, $fullTax);
|
|
if (is_wp_error($terms) || empty($terms)) {
|
continue;
|
}
|
|
// Return first associated organization
|
$term = $terms[0];
|
|
return SchemaReferenceBuilder::build(
|
$term->term_id,
|
'term',
|
null
|
);
|
}
|
|
return null;
|
}
|
|
/**
|
* Build work examples from the person's authored content.
|
*/
|
private function buildWorkExamples(int $userId, int $limit = 5): array
|
{
|
if (!defined('JVB_CONTENT')) {
|
return [];
|
}
|
|
$examples = [];
|
|
foreach (JVB_CONTENT as $slug => $config) {
|
$fullType = BASE . $slug;
|
|
$posts = get_posts([
|
'post_type' => $fullType,
|
'author' => $userId,
|
'posts_per_page' => $limit,
|
'post_status' => 'publish',
|
'fields' => 'ids',
|
]);
|
|
if (empty($posts)) {
|
continue;
|
}
|
|
$refs = SchemaReferenceBuilder::buildMultiple($posts, 'post', null, true);
|
|
if (!empty($refs)) {
|
$examples = array_merge($examples, $refs);
|
}
|
|
// Cap total examples
|
if (count($examples) >= $limit) {
|
$examples = array_slice($examples, 0, $limit);
|
break;
|
}
|
}
|
|
return $examples;
|
}
|
}
|