namespace Yoast\WP\SEO\Generators\Schema;
* Returns schema FAQ data.
class FAQ extends Abstract_Schema_Piece {
* Determines whether or not a piece should be added to the graph.
public function is_needed() {
if ( empty( $this->context->blocks['yoast/faq-block'] ) ) {
if ( ! \is_array( $this->context->schema_page_type ) ) {
$this->context->schema_page_type = [ $this->context->schema_page_type ];
$this->context->schema_page_type[] = 'FAQPage';
$this->context->main_entity_of_page = $this->generate_ids();
* Generate the IDs so we can link to them in the main entity.
private function generate_ids() {
foreach ( $this->context->blocks['yoast/faq-block'] as $block ) {
foreach ( $block['attrs']['questions'] as $index => $question ) {
if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
$ids[] = [ '@id' => $this->context->canonical . '#' . \esc_attr( $question['id'] ) ];
* Render a list of questions, referencing them by ID.
* @return array Our Schema graph.
public function generate() {
foreach ( $this->context->blocks['yoast/faq-block'] as $index => $block ) {
$questions = \array_merge( $questions, $block['attrs']['questions'] );
foreach ( $questions as $index => $question ) {
if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
$graph[] = $this->generate_question_block( $question, ( $index + 1 ) );
* Generate a Question piece.
* @param array $question The question to generate schema for.
* @param int $position The position of the question.
* @return array Schema.org Question piece.
protected function generate_question_block( $question, $position ) {
$url = $this->context->canonical . '#' . \esc_attr( $question['id'] );
'name' => $this->helpers->schema->html->smart_strip_tags( $question['jsonQuestion'] ),
'acceptedAnswer' => $this->add_accepted_answer_property( $question ),
$data = $this->helpers->schema->language->add_piece_language( $data );
* Adds the Questions `acceptedAnswer` property.
* @param array $question The question to add the acceptedAnswer to.
* @return array Schema.org Question piece.
protected function add_accepted_answer_property( $question ) {
'text' => $this->helpers->schema->html->sanitize( $question['jsonAnswer'] ),
$data = $this->helpers->schema->language->add_piece_language( $data );