<?php
|
namespace JVBase\managers\SEO\schemas\resolvers;
|
|
use JVBase\managers\SEO\schemas\SchemaDefinition;
|
use JVBase\meta\Meta;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
/**
|
* Resolver for FAQPage schema.
|
*
|
* Handles two contexts:
|
* - Single post: transforms question/answer into mainEntity Question structure
|
* - Archive/term: delegates to CollectionPageResolver for mainEntity from posts
|
*/
|
class FAQPageResolver extends BaseResolver
|
{
|
private CollectionPageResolver $collectionResolver;
|
|
public function __construct()
|
{
|
$this->collectionResolver = new CollectionPageResolver();
|
}
|
|
public function resolve(SchemaDefinition $definition, ?Meta $meta = null): ?array
|
{
|
// Single FAQ post: restructure question/answer into mainEntity
|
if ($definition->objectType === 'post') {
|
$this->transformQuestionAnswer($definition);
|
}
|
|
return parent::resolve($definition, $meta);
|
}
|
|
public function getAutoFields(SchemaDefinition $definition): array
|
{
|
// Archive/term context: delegate to CollectionPageResolver
|
if (in_array($definition->objectType, ['archive', 'term'])) {
|
return $this->collectionResolver->getAutoFields($definition);
|
}
|
|
return [];
|
}
|
|
/**
|
* Transform question/answer into proper mainEntity structure.
|
*
|
* Schema.org requires FAQPage Q&A nested as:
|
* mainEntity: [{ @type: Question, name: ..., acceptedAnswer: { @type: Answer, text: ... } }]
|
*/
|
private function transformQuestionAnswer(SchemaDefinition $definition): void
|
{
|
$question = $definition->config['question'] ?? null;
|
$answer = $definition->config['answer'] ?? null;
|
|
if (!$question && !$answer) {
|
return;
|
}
|
|
unset($definition->config['question'], $definition->config['answer']);
|
|
$questionEntity = ['@type' => 'Question', 'name' => $question ?? ''];
|
|
if ($answer) {
|
$questionEntity['acceptedAnswer'] = [
|
'@type' => 'Answer',
|
'text' => $answer,
|
];
|
}
|
|
$definition->config['mainEntity'] = [$questionEntity];
|
}
|
}
|