PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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 18.7, 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 or not 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 foreach ( $block['attrs']['questions'] as $index => $question ) {
38 if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
39 continue;
40 }
41 $ids[] = [ '@id' => $this->context->canonical . '#' . \esc_attr( $question['id'] ) ];
42 }
43 }
44
45 return $ids;
46 }
47
48 /**
49 * Render a list of questions, referencing them by ID.
50 *
51 * @return array Our Schema graph.
52 */
53 public function generate() {
54 $graph = [];
55
56 $questions = [];
57 foreach ( $this->context->blocks['yoast/faq-block'] as $index => $block ) {
58 $questions = \array_merge( $questions, $block['attrs']['questions'] );
59 }
60 foreach ( $questions as $index => $question ) {
61 if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
62 continue;
63 }
64 $graph[] = $this->generate_question_block( $question, ( $index + 1 ) );
65 }
66
67 return $graph;
68 }
69
70 /**
71 * Generate a Question piece.
72 *
73 * @param array $question The question to generate schema for.
74 * @param int $position The position of the question.
75 *
76 * @return array Schema.org Question piece.
77 */
78 protected function generate_question_block( $question, $position ) {
79 $url = $this->context->canonical . '#' . \esc_attr( $question['id'] );
80
81 $data = [
82 '@type' => 'Question',
83 '@id' => $url,
84 'position' => $position,
85 'url' => $url,
86 'name' => $this->helpers->schema->html->smart_strip_tags( $question['jsonQuestion'] ),
87 'answerCount' => 1,
88 'acceptedAnswer' => $this->add_accepted_answer_property( $question ),
89 ];
90
91 $data = $this->helpers->schema->language->add_piece_language( $data );
92
93 return $data;
94 }
95
96 /**
97 * Adds the Questions `acceptedAnswer` property.
98 *
99 * @param array $question The question to add the acceptedAnswer to.
100 *
101 * @return array Schema.org Question piece.
102 */
103 protected function add_accepted_answer_property( $question ) {
104 $data = [
105 '@type' => 'Answer',
106 'text' => $this->helpers->schema->html->sanitize( $question['jsonAnswer'] ),
107 ];
108
109 $data = $this->helpers->schema->language->add_piece_language( $data );
110
111 return $data;
112 }
113 }
114