1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Frontend\Schema */
/** * Returns schema FAQ data. * * @since 11.5 */ class WPSEO_Schema_HowTo implements WPSEO_Graph_Piece { /** * Determine whether this graph piece is needed or not. * * Always false, because this graph piece adds itself using the filter API. * * @var bool */ private $is_needed = false;
/** * The FAQ blocks count on the current page. * * @var int */ private $counter;
/** * A value object with context variables. * * @var WPSEO_Schema_Context */ private $context;
/** * WPSEO_Schema_FAQ constructor. * * @param WPSEO_Schema_Context $context A value object with context variables. * * @codeCoverageIgnore */ public function __construct( WPSEO_Schema_Context $context ) { $this->counter = 0; $this->context = $context;
add_filter( 'wpseo_schema_block_yoast/how-to-block', array( $this, 'render' ), 10, 2 ); }
/** * Renders a list of questions, referencing them by ID. * * @return array $data Our Schema graph. */ public function generate() { return array(); }
/** * Renders the How-To block into our graph. * * @param array $graph Our Schema data. * @param array $block The How-To block content. * * @return mixed */ public function render( $graph, $block ) { $this->counter++; $data = array( '@type' => 'HowTo', '@id' => $this->context->canonical . '#howto-' . $this->counter, 'name' => $this->context->title, 'mainEntityOfPage' => array( '@id' => $this->get_main_schema_id() ), 'description' => '', );
if ( isset( $block['attrs']['jsonDescription'] ) ) { $data['description'] = $block['attrs']['jsonDescription']; }
$this->add_duration( $data, $block['attrs'] ); $this->add_steps( $data, $block['attrs']['steps'] );
$graph[] = $data;
return $graph; }
/** * Adds the duration of the task to the Schema. * * @param array $data Our How-To schema data. * @param array $attributes The block data attributes. * * @return array $data Our schema data. */ private function add_duration( &$data, $attributes ) { if ( ! empty( $attributes['hasDuration'] ) && $attributes['hasDuration'] ) { $days = empty( $attributes['days'] ) ? 0 : $attributes['days']; $hours = empty( $attributes['hours'] ) ? 0 : $attributes['hours']; $minutes = empty( $attributes['minutes'] ) ? 0 : $attributes['minutes'];
if ( ( $days + $hours + $minutes ) > 0 ) { $data['totalTime'] = 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M'; } }
return $data; }
/** * Determines whether we're part of an article or a webpage. * * @return string A reference URL. */ protected function get_main_schema_id() { if ( $this->context->site_represents !== false && WPSEO_Schema_Article::is_article_post_type() ) { return $this->context->canonical . WPSEO_Schema_IDs::ARTICLE_HASH; }
return $this->context->canonical . WPSEO_Schema_IDs::WEBPAGE_HASH; }
/** * Determines whether or not a piece should be added to the graph. * * @return bool */ public function is_needed() { return $this->is_needed; }
/** * Adds the steps to our How-To output. * * @param array $data Our How-To schema data. * @param array $steps Our How-To block's steps. */ private function add_steps( &$data, $steps ) { foreach ( $steps as $step ) { $schema_id = $this->context->canonical . '#' . $step['id']; $schema_step = array( '@type' => 'HowToStep', 'url' => $schema_id, );
if ( empty( $step['jsonName'] ) ) { if ( empty( $step['text'] ) ) { continue; }
$schema_step['text'] = '';
$this->add_step_image( $schema_step, $step );
// If there is no text and no image, don't output the step. if ( empty( $step['jsonText'] ) && empty( $schema_step['image'] ) ) { continue; }
if ( ! empty( $step['jsonText'] ) ) { $schema_step['text'] = $step['jsonText']; } } else if ( empty( $step['jsonText'] ) ) { $schema_step['text'] = $step['jsonName']; } else { $schema_step['name'] = $step['jsonName'];
$this->add_step_description( $schema_step, $step ); $this->add_step_image( $schema_step, $step ); }
$data['step'][] = $schema_step; } }
/** * Checks if we have a step description, if we do, add it. * * @param array $schema_step Our Schema output for the Step. * @param array $step The step block data. */ private function add_step_description( &$schema_step, $step ) { if ( empty( $step['jsonText'] ) ) { return; }
$schema_step['itemListElement'] = array();
$schema_step['itemListElement'][] = array( '@type' => 'HowToDirection', 'text' => $step['jsonText'], ); }
/** * Checks if we have a step image, if we do, add it. * * @param array $schema_step Our Schema output for the Step. * @param array $step The step block data. */ private function add_step_image( &$schema_step, $step ) { foreach ( $step['text'] as $line ) { if ( is_array( $line ) && isset( $line['type'] ) && $line['type'] === 'img' ) { $schema_step['image'] = $this->get_image_schema( $line['props']['src'] ); } } }
/** * Generates the image schema from the attachment $url. * * @param string $url Attachment url. * * @return array Image schema. * * @codeCoverageIgnore */ protected function get_image_schema( $url ) { $image = new WPSEO_Schema_Image( $this->context->canonical . '#schema-image-' . md5( $url ) );
return $image->generate_from_url( $url ); } }
|