<?php
|
namespace JVBase\managers\SEO\schemas\resolvers;
|
|
use JVBase\managers\SEO\schemas\SchemaDefinition;
|
use JVBase\managers\SEO\SchemaReferenceBuilder;
|
use JVBase\meta\Meta;
|
use JVBase\registrar\Registrar;
|
|
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
|
{
|
$contentTypes = Registrar::getFeatured('is_content', 'term');
|
if (empty($contentTypes)) {
|
return null;
|
}
|
|
foreach ($contentTypes as $content) {
|
$fullTax = jvbCheckBase($content);
|
$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->term_id
|
);
|
}
|
|
return null;
|
}
|
|
/**
|
* Build work examples from the person's authored content.
|
*/
|
private function buildWorkExamples(int $userId, int $limit = 5): array
|
{
|
$role = jvbUserRole($userId);
|
$registrar = Registrar::getInstance($role);
|
|
if (!$registrar){
|
return [];
|
}
|
$types = $registrar->getCreatable();
|
if (empty($types)){
|
return [];
|
}
|
|
$examples = [];
|
|
foreach ($types as $slug) {
|
$type = jvbCheckBase($slug);
|
|
$posts = get_posts([
|
'post_type' => $type,
|
'author' => $userId,
|
'posts_per_page' => $limit,
|
'post_status' => 'publish',
|
'fields' => 'ids',
|
]);
|
|
if (empty($posts)) {
|
continue;
|
}
|
|
$refs = SchemaReferenceBuilder::buildMultiple('post', $posts, 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;
|
}
|
}
|