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

Yoast / wordpress-seo / 6c877bac739efa616037d2daac5a30c63873bb5b

26 Jan 2024 07:58AM UTC coverage: 53.098% (+0.02%) from 53.074%
6c877bac739efa616037d2daac5a30c63873bb5b

Pull #21053

github

web-flow
Merge fc6aa301f into a80a04504
Pull Request #21053: Adds defensive programming for FAQ and How-To blocks

7563 of 13902 branches covered (0.0%)

Branch coverage included in aggregate %.

5 of 16 new or added lines in 3 files covered. (31.25%)

20 existing lines in 3 files now uncovered.

28996 of 54950 relevant lines covered (52.77%)

40272.96 hits per line

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

80.43
/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 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 ) {
×
NEW
37
                        if ( isset( $block['attrs']['questions'] ) ) {
×
NEW
38
                                foreach ( $block['attrs']['questions'] as $question ) {
×
NEW
39
                                        if ( empty( $question['jsonAnswer'] ) ) {
×
NEW
40
                                                continue;
×
41
                                        }
NEW
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() {
6✔
56
                $graph = [];
6✔
57

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

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

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

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

111
                return $this->helpers->schema->language->add_piece_language( $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