| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Generators\Schema; |
| 4 |
|
| 5 |
/** |
| 6 |
* Returns schema FAQ data. |
| 7 |
*/ |
| 8 |
class FAQ extends Abstract_Schema_Piece { |
| 9 |
|
| 10 |
/** |
| 11 |
* Determines whether a piece should be added to the graph. |
| 12 |
* |
| 13 |
* @return bool |
| 14 |
*/ |
| 15 |
public function is_needed() { |
| 16 |
if ( empty( $this->context->blocks['yoast/faq-block'] ) ) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! \is_array( $this->context->schema_page_type ) ) { |
| 21 |
$this->context->schema_page_type = [ $this->context->schema_page_type ]; |
| 22 |
} |
| 23 |
$this->context->schema_page_type[] = 'FAQPage'; |
| 24 |
$this->context->main_entity_of_page = $this->generate_ids(); |
| 25 |
|
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Generate the IDs so we can link to them in the main entity. |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
private function generate_ids() { |
| 35 |
$ids = []; |
| 36 |
foreach ( $this->context->blocks['yoast/faq-block'] as $block ) { |
| 37 |
if ( isset( $block['attrs']['questions'] ) ) { |
| 38 |
foreach ( $block['attrs']['questions'] as $question ) { |
| 39 |
if ( empty( $question['jsonAnswer'] ) ) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
$ids[] = [ '@id' => $this->context->canonical . '#' . \esc_attr( $question['id'] ) ]; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return $ids; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Render a list of questions, referencing them by ID. |
| 52 |
* |
| 53 |
* @return array Our Schema graph. |
| 54 |
*/ |
| 55 |
public function generate() { |
| 56 |
$graph = []; |
| 57 |
|
| 58 |
$questions = []; |
| 59 |
foreach ( $this->context->blocks['yoast/faq-block'] as $block ) { |
| 60 |
if ( isset( $block['attrs']['questions'] ) ) { |
| 61 |
$questions = \array_merge( $questions, $block['attrs']['questions'] ); |
| 62 |
} |
| 63 |
} |
| 64 |
foreach ( $questions as $index => $question ) { |
| 65 |
if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
$graph[] = $this->generate_question_block( $question, ( $index + 1 ) ); |
| 69 |
} |
| 70 |
|
| 71 |
return $graph; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Generate a Question piece. |
| 76 |
* |
| 77 |
* @param array $question The question to generate schema for. |
| 78 |
* @param int $position The position of the question. |
| 79 |
* |
| 80 |
* @return array Schema.org Question piece. |
| 81 |
*/ |
| 82 |
protected function generate_question_block( $question, $position ) { |
| 83 |
$url = $this->context->canonical . '#' . \esc_attr( $question['id'] ); |
| 84 |
|
| 85 |
$data = [ |
| 86 |
'@type' => 'Question', |
| 87 |
'@id' => $url, |
| 88 |
'position' => $position, |
| 89 |
'url' => $url, |
| 90 |
'name' => $this->helpers->schema->html->smart_strip_tags( $question['jsonQuestion'] ), |
| 91 |
'answerCount' => 1, |
| 92 |
'acceptedAnswer' => $this->add_accepted_answer_property( $question ), |
| 93 |
]; |
| 94 |
|
| 95 |
return $this->helpers->schema->language->add_piece_language( $data ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Adds the Questions `acceptedAnswer` property. |
| 100 |
* |
| 101 |
* @param array $question The question to add the acceptedAnswer to. |
| 102 |
* |
| 103 |
* @return array Schema.org Question piece. |
| 104 |
*/ |
| 105 |
protected function add_accepted_answer_property( $question ) { |
| 106 |
$data = [ |
| 107 |
'@type' => 'Answer', |
| 108 |
'text' => $this->helpers->schema->html->sanitize( $question['jsonAnswer'] ), |
| 109 |
]; |
| 110 |
|
| 111 |
return $this->helpers->schema->language->add_piece_language( $data ); |
| 112 |
} |
| 113 |
} |
| 114 |
|