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

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

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

79.73
/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() {
12✔
27
                $graph = [];
12✔
28

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

33
                return $graph;
12✔
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
        private function add_duration( &$data, $attributes ) {
4✔
43
                if ( empty( $attributes['hasDuration'] ) ) {
4✔
44
                        return;
2✔
45
                }
46

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

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

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

70
                        if ( isset( $step['jsonText'] ) ) {
10✔
71
                                $json_text = $this->helpers->schema->html->sanitize( $step['jsonText'] );
6✔
72
                        }
73

74
                        if ( isset( $step['jsonName'] ) ) {
10✔
75
                                $json_name = $this->helpers->schema->html->smart_strip_tags( $step['jsonName'] );
8✔
76
                        }
77

78
                        if ( empty( $json_name ) ) {
10✔
79
                                if ( empty( $step['text'] ) ) {
4✔
80
                                        continue;
×
81
                                }
82

83
                                $schema_step['text'] = '';
4✔
84

85
                                $this->add_step_image( $schema_step, $step );
4✔
86

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

92
                                if ( ! empty( $json_text ) ) {
2✔
93
                                        $schema_step['text'] = $json_text;
2✔
94
                                }
95
                        }
96

97
                        elseif ( empty( $json_text ) ) {
3✔
98
                                $schema_step['text'] = $json_name;
2✔
99
                        }
100
                        else {
101
                                $schema_step['name'] = $json_name;
4✔
102

103
                                $this->add_step_description( $schema_step, $json_text );
4✔
104
                                $this->add_step_image( $schema_step, $step );
4✔
105
                        }
106

107
                        $data['step'][] = $schema_step;
8✔
108
                }
109
        }
5✔
110

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

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

142
        /**
143
         * Generates the HowTo schema for a block.
144
         *
145
         * @param array $graph Our Schema data.
146
         * @param array $block The How-To block content.
147
         * @param int   $index The index of the current block.
148
         */
149
        protected function add_how_to( &$graph, $block, $index ) {
×
150
                $data = [
151
                        '@type'            => 'HowTo',
×
152
                        '@id'              => $this->context->canonical . '#howto-' . ( $index + 1 ),
×
153
                        'name'             => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
×
154
                        'mainEntityOfPage' => [ '@id' => $this->context->main_schema_id ],
×
155
                        'description'      => '',
×
156
                ];
157

158
                if ( $this->context->has_article ) {
×
159
                        $data['mainEntityOfPage'] = [ '@id' => $this->context->main_schema_id . Schema_IDs::ARTICLE_HASH ];
×
160
                }
161

162
                if ( isset( $block['attrs']['jsonDescription'] ) ) {
×
163
                        $data['description'] = $this->helpers->schema->html->sanitize( $block['attrs']['jsonDescription'] );
×
164
                }
165

166
                $this->add_duration( $data, $block['attrs'] );
×
167
                $this->add_steps( $data, $block['attrs']['steps'] );
×
168

169
                $data = $this->helpers->schema->language->add_piece_language( $data );
×
170

171
                $graph[] = $data;
×
172
        }
173

174
        /**
175
         * Generates the image schema from the attachment $url.
176
         *
177
         * @param string $url Attachment url.
178
         *
179
         * @return array Image schema.
180
         */
181
        protected function get_image_schema( $url ) {
2✔
182
                $schema_id = $this->context->canonical . '#schema-image-' . \md5( $url );
2✔
183

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