PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / generators / schema / faq.php

faq.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/generators/schema/faq.php

114 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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