• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 7004843404

27 Nov 2023 11:48AM UTC coverage: 49.206% (-0.03%) from 49.232%
7004843404

push

github

web-flow
Merge pull request #20858 from Yoast/improve-copy-in-the-ftc-57

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.61
/src/generators/schema/faq.php
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() {
4✔
16
                if ( empty( $this->context->blocks['yoast/faq-block'] ) ) {
4✔
17
                        return false;
2✔
18
                }
19

20
                if ( ! \is_array( $this->context->schema_page_type ) ) {
2✔
21
                        $this->context->schema_page_type = [ $this->context->schema_page_type ];
2✔
22
                }
23
                $this->context->schema_page_type[]  = 'FAQPage';
2✔
24
                $this->context->main_entity_of_page = $this->generate_ids();
2✔
25

26
                return true;
2✔
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 $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() {
4✔
54
                $graph = [];
4✔
55

56
                $questions = [];
4✔
57
                foreach ( $this->context->blocks['yoast/faq-block'] as $index => $block ) {
4✔
58
                        $questions = \array_merge( $questions, $block['attrs']['questions'] );
4✔
59
                }
60
                foreach ( $questions as $index => $question ) {
4✔
61
                        if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
4✔
62
                                continue;
2✔
63
                        }
64
                        $graph[] = $this->generate_question_block( $question, ( $index + 1 ) );
4✔
65
                }
66

67
                return $graph;
4✔
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 ) {
4✔
79
                $url = $this->context->canonical . '#' . \esc_attr( $question['id'] );
4✔
80

81
                $data = [
2✔
82
                        '@type'          => 'Question',
4✔
83
                        '@id'            => $url,
4✔
84
                        'position'       => $position,
4✔
85
                        'url'            => $url,
4✔
86
                        'name'           => $this->helpers->schema->html->smart_strip_tags( $question['jsonQuestion'] ),
4✔
87
                        'answerCount'    => 1,
4✔
88
                        'acceptedAnswer' => $this->add_accepted_answer_property( $question ),
4✔
89
                ];
2✔
90

91
                $data = $this->helpers->schema->language->add_piece_language( $data );
4✔
92

93
                return $data;
4✔
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 ) {
4✔
104
                $data = [
2✔
105
                        '@type' => 'Answer',
4✔
106
                        'text'  => $this->helpers->schema->html->sanitize( $question['jsonAnswer'] ),
4✔
107
                ];
2✔
108

109
                $data = $this->helpers->schema->language->add_piece_language( $data );
4✔
110

111
                return $data;
4✔
112
        }
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc