• 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

78.67
/src/generators/schema/howto.php
1
<?php
2

3
namespace Yoast\WP\SEO\Generators\Schema;
4

5
use Yoast\WP\SEO\Config\Schema_IDs;
6

7
/**
8
 * Returns schema HowTo data.
9
 */
10
class HowTo extends Abstract_Schema_Piece {
11

12
        /**
13
         * Determines whether or not a piece should be added to the graph.
14
         *
15
         * @return bool
16
         */
17
        public function is_needed() {
2✔
18
                return ! empty( $this->context->blocks['yoast/how-to-block'] );
2✔
19
        }
20

21
        /**
22
         * Renders a list of questions, referencing them by ID.
23
         *
24
         * @return array Our Schema graph.
25
         */
26
        public function generate() {
14✔
27
                $graph = [];
14✔
28

29
                foreach ( $this->context->blocks['yoast/how-to-block'] as $index => $block ) {
14✔
30
                        $this->add_how_to( $graph, $block, $index );
14✔
31
                }
32

33
                return $graph;
14✔
34
        }
35

36
        /**
37
         * Adds the duration of the task to the Schema.
38
         *
39
         * @param array $data       Our How-To schema data.
40
         * @param array $attributes The block data attributes.
41
         *
42
         * @return void
43
         */
44
        private function add_duration( &$data, $attributes ) {
4✔
45
                if ( empty( $attributes['hasDuration'] ) ) {
4✔
46
                        return;
2✔
47
                }
48

49
                $days    = empty( $attributes['days'] ) ? 0 : $attributes['days'];
2✔
50
                $hours   = empty( $attributes['hours'] ) ? 0 : $attributes['hours'];
2✔
51
                $minutes = empty( $attributes['minutes'] ) ? 0 : $attributes['minutes'];
2✔
52

53
                if ( ( $days + $hours + $minutes ) > 0 ) {
2✔
54
                        $data['totalTime'] = \esc_attr( 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M' );
2✔
55
                }
56
        }
1✔
57

58
        /**
59
         * Adds the steps to our How-To output.
60
         *
61
         * @param array $data  Our How-To schema data.
62
         * @param array $steps Our How-To block's steps.
63
         *
64
         * @return void
65
         */
66
        private function add_steps( &$data, $steps ) {
10✔
67
                foreach ( $steps as $step ) {
10✔
68
                        $schema_id   = $this->context->canonical . '#' . \esc_attr( $step['id'] );
10✔
69
                        $schema_step = [
5✔
70
                                '@type' => 'HowToStep',
10✔
71
                                'url'   => $schema_id,
10✔
72
                        ];
5✔
73

74
                        if ( isset( $step['jsonText'] ) ) {
10✔
75
                                $json_text = $this->helpers->schema->html->sanitize( $step['jsonText'] );
6✔
76
                        }
77

78
                        if ( isset( $step['jsonName'] ) ) {
10✔
79
                                $json_name = $this->helpers->schema->html->smart_strip_tags( $step['jsonName'] );
8✔
80
                        }
81

82
                        if ( empty( $json_name ) ) {
10✔
83
                                if ( empty( $step['text'] ) ) {
4✔
84
                                        continue;
×
85
                                }
86

87
                                $schema_step['text'] = '';
4✔
88

89
                                $this->add_step_image( $schema_step, $step );
4✔
90

91
                                // If there is no text and no image, don't output the step.
92
                                if ( empty( $json_text ) && empty( $schema_step['image'] ) ) {
4✔
93
                                        continue;
2✔
94
                                }
95

96
                                if ( ! empty( $json_text ) ) {
2✔
97
                                        $schema_step['text'] = $json_text;
2✔
98
                                }
99
                        }
100

101
                        elseif ( empty( $json_text ) ) {
3✔
102
                                $schema_step['text'] = $json_name;
2✔
103
                        }
104
                        else {
105
                                $schema_step['name'] = $json_name;
4✔
106

107
                                $this->add_step_description( $schema_step, $json_text );
4✔
108
                                $this->add_step_image( $schema_step, $step );
4✔
109
                        }
110

111
                        $data['step'][] = $schema_step;
8✔
112
                }
113
        }
5✔
114

115
        /**
116
         * Checks if we have a step description, if we do, add it.
117
         *
118
         * @param array  $schema_step Our Schema output for the Step.
119
         * @param string $json_text   The step text.
120
         *
121
         * @return void
122
         */
123
        private function add_step_description( &$schema_step, $json_text ) {
2✔
124
                $schema_step['itemListElement'] = [
2✔
125
                        [
1✔
126
                                '@type' => 'HowToDirection',
2✔
127
                                'text'  => $json_text,
2✔
128
                        ],
1✔
129
                ];
1✔
130
        }
1✔
131

132
        /**
133
         * Checks if we have a step image, if we do, add it.
134
         *
135
         * @param array $schema_step Our Schema output for the Step.
136
         * @param array $step        The step block data.
137
         *
138
         * @return void
139
         */
140
        private function add_step_image( &$schema_step, $step ) {
2✔
141
                if ( isset( $step['text'] ) && \is_array( $step['text'] ) ) {
2✔
142
                        foreach ( $step['text'] as $line ) {
2✔
143
                                if ( \is_array( $line ) && isset( $line['type'] ) && $line['type'] === 'img' ) {
2✔
144
                                        $schema_step['image'] = $this->get_image_schema( \esc_url( $line['props']['src'] ) );
2✔
145
                                }
146
                        }
147
                }
148
        }
1✔
149

150
        /**
151
         * Generates the HowTo schema for a block.
152
         *
153
         * @param array $graph Our Schema data.
154
         * @param array $block The How-To block content.
155
         * @param int   $index The index of the current block.
156
         *
157
         * @return void
158
         */
159
        protected function add_how_to( &$graph, $block, $index ) {
×
160
                $data = [
161
                        '@type'            => 'HowTo',
×
162
                        '@id'              => $this->context->canonical . '#howto-' . ( $index + 1 ),
×
163
                        'name'             => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
×
164
                        'mainEntityOfPage' => [ '@id' => $this->context->main_schema_id ],
×
165
                        'description'      => '',
×
166
                ];
167

168
                if ( $this->context->has_article ) {
×
169
                        $data['mainEntityOfPage'] = [ '@id' => $this->context->main_schema_id . Schema_IDs::ARTICLE_HASH ];
×
170
                }
171

172
                if ( isset( $block['attrs']['jsonDescription'] ) ) {
×
173
                        $data['description'] = $this->helpers->schema->html->sanitize( $block['attrs']['jsonDescription'] );
×
174
                }
175

176
                $this->add_duration( $data, $block['attrs'] );
×
177

NEW
178
                if ( isset( $block['attrs']['steps'] ) ) {
×
NEW
179
                        $this->add_steps( $data, $block['attrs']['steps'] );
×
180
                }
181

182
                $data = $this->helpers->schema->language->add_piece_language( $data );
×
183

184
                $graph[] = $data;
×
185
        }
186

187
        /**
188
         * Generates the image schema from the attachment $url.
189
         *
190
         * @param string $url Attachment url.
191
         *
192
         * @return array Image schema.
193
         */
194
        protected function get_image_schema( $url ) {
2✔
195
                $schema_id = $this->context->canonical . '#schema-image-' . \md5( $url );
2✔
196

197
                return $this->helpers->schema->image->generate_from_url( $schema_id, $url );
2✔
198
        }
199
}
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

© 2026 Coveralls, Inc