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

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

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

0.0
/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() {
18
                return ! empty( $this->context->blocks['yoast/how-to-block'] );
×
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() {
27
                $graph = [];
×
28

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

33
                return $graph;
×
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 ) {
43
                if ( empty( $attributes['hasDuration'] ) ) {
×
44
                        return;
×
45
                }
46

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

51
                if ( ( $days + $hours + $minutes ) > 0 ) {
×
52
                        $data['totalTime'] = \esc_attr( 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M' );
×
53
                }
54
        }
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 ) {
63
                foreach ( $steps as $step ) {
×
64
                        $schema_id   = $this->context->canonical . '#' . \esc_attr( $step['id'] );
×
65
                        $schema_step = [
×
66
                                '@type' => 'HowToStep',
×
67
                                'url'   => $schema_id,
×
68
                        ];
×
69

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

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

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

83
                                $schema_step['text'] = '';
×
84

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

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

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

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

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

107
                        $data['step'][] = $schema_step;
×
108
                }
109
        }
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 ) {
118
                $schema_step['itemListElement'] = [
×
119
                        [
×
120
                                '@type' => 'HowToDirection',
×
121
                                'text'  => $json_text,
×
122
                        ],
×
123
                ];
×
124
        }
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 ) {
133
                if ( isset( $step['text'] ) && \is_array( $step['text'] ) ) {
×
134
                        foreach ( $step['text'] as $line ) {
×
135
                                if ( \is_array( $line ) && isset( $line['type'] ) && $line['type'] === 'img' ) {
×
136
                                        $schema_step['image'] = $this->get_image_schema( \esc_url( $line['props']['src'] ) );
×
137
                                }
138
                        }
139
                }
140
        }
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 ) {
182
                $schema_id = $this->context->canonical . '#schema-image-' . \md5( $url );
×
183

184
                return $this->helpers->schema->image->generate_from_url( $schema_id, $url );
×
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