<?php
|
namespace JVBase\managers\SEO\schemas\resolvers;
|
|
use JVBase\managers\SEO\schemas\SchemaDefinition;
|
use JVBase\managers\SEO\SchemaReferenceBuilder;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
/**
|
* Resolver for archive/collection page schemas.
|
*
|
* Handles: CollectionPage, FAQPage, DefinedTermSet, ItemList
|
*
|
* Auto-enrichment:
|
* - Builds `mainEntity` from archive posts or term's associated posts
|
* - Adds `numberOfItems` for ItemList
|
*/
|
class CollectionPageResolver extends BaseResolver
|
{
|
/** Schema types that should get mainEntity from their posts */
|
private const ENTITY_TYPES = ['FAQPage', 'CollectionPage', 'ItemList', 'DefinedTermSet'];
|
|
public function getAutoFields(SchemaDefinition $definition): array
|
{
|
$fields = [];
|
|
if (!in_array($definition->schemaType, self::ENTITY_TYPES)) {
|
return $fields;
|
}
|
|
$mainEntity = $this->buildMainEntity($definition);
|
|
if (!empty($mainEntity)) {
|
$fields['mainEntity'] = $mainEntity;
|
}
|
|
return $fields;
|
}
|
|
/**
|
* Build mainEntity from the archive's posts.
|
*/
|
private function buildMainEntity(SchemaDefinition $definition): ?array
|
{
|
// Term archive (e.g., /tattoo-style/american-traditional/)
|
if ($definition->objectType === 'term' && $definition->objectId) {
|
return $this->buildFromTerm($definition);
|
}
|
|
// Post type archive (e.g., /tattoos/)
|
if ($definition->objectType === 'archive' && $definition->contentType) {
|
return SchemaReferenceBuilder::buildFromArchive($definition->contentType);
|
}
|
|
return null;
|
}
|
|
/**
|
* Build mainEntity from a taxonomy term's associated posts.
|
*/
|
private function buildFromTerm(SchemaDefinition $definition): ?array
|
{
|
if (!defined('JVB_TAXONOMY') || !$definition->contentType) {
|
return null;
|
}
|
|
$slug = jvbNoBase($definition->contentType);
|
$taxConfig = JVB_TAXONOMY[$slug] ?? [];
|
|
if (empty($taxConfig['for_content'])) {
|
return null;
|
}
|
|
// Use the first associated post type
|
$postType = $taxConfig['for_content'][0];
|
$fullType = str_starts_with($postType, BASE) ? $postType : BASE . $postType;
|
|
return SchemaReferenceBuilder::buildFromTerm(
|
$definition->objectId,
|
$fullType,
|
10,
|
null,
|
true
|
);
|
}
|
}
|