-
Notifications
You must be signed in to change notification settings - Fork 882
/
howto.php
199 lines (167 loc) · 5.09 KB
/
howto.php
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
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema HowTo data.
*/
class HowTo extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
return ! empty( $this->context->blocks['yoast/how-to-block'] );
}
/**
* Renders a list of questions, referencing them by ID.
*
* @return array Our Schema graph.
*/
public function generate() {
$graph = [];
foreach ( $this->context->blocks['yoast/how-to-block'] as $index => $block ) {
$this->add_how_to( $graph, $block, $index );
}
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 void
*/
private function add_duration( &$data, $attributes ) {
if ( empty( $attributes['hasDuration'] ) ) {
return;
}
$days = \intval( ( $attributes['days'] ?? 0 ) );
$hours = \intval( ( $attributes['hours'] ?? 0 ) );
$minutes = \intval( ( $attributes['minutes'] ?? 0 ) );
if ( ( $days + $hours + $minutes ) > 0 ) {
$data['totalTime'] = \esc_attr( 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M' );
}
}
/**
* 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.
*
* @return void
*/
private function add_steps( &$data, $steps ) {
foreach ( $steps as $step ) {
$schema_id = $this->context->canonical . '#' . \esc_attr( $step['id'] );
$schema_step = [
'@type' => 'HowToStep',
'url' => $schema_id,
];
if ( isset( $step['jsonText'] ) ) {
$json_text = $this->helpers->schema->html->sanitize( $step['jsonText'] );
}
if ( isset( $step['jsonName'] ) ) {
$json_name = $this->helpers->schema->html->smart_strip_tags( $step['jsonName'] );
}
if ( empty( $json_name ) ) {
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( $json_text ) && empty( $schema_step['image'] ) ) {
continue;
}
if ( ! empty( $json_text ) ) {
$schema_step['text'] = $json_text;
}
}
elseif ( empty( $json_text ) ) {
$schema_step['text'] = $json_name;
}
else {
$schema_step['name'] = $json_name;
$this->add_step_description( $schema_step, $json_text );
$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 string $json_text The step text.
*
* @return void
*/
private function add_step_description( &$schema_step, $json_text ) {
$schema_step['itemListElement'] = [
[
'@type' => 'HowToDirection',
'text' => $json_text,
],
];
}
/**
* 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.
*
* @return void
*/
private function add_step_image( &$schema_step, $step ) {
if ( isset( $step['text'] ) && \is_array( $step['text'] ) ) {
foreach ( $step['text'] as $line ) {
if ( \is_array( $line ) && isset( $line['type'] ) && $line['type'] === 'img' ) {
$schema_step['image'] = $this->get_image_schema( \esc_url( $line['props']['src'] ) );
}
}
}
}
/**
* Generates the HowTo schema for a block.
*
* @param array $graph Our Schema data.
* @param array $block The How-To block content.
* @param int $index The index of the current block.
*
* @return void
*/
protected function add_how_to( &$graph, $block, $index ) {
$data = [
'@type' => 'HowTo',
'@id' => $this->context->canonical . '#howto-' . ( $index + 1 ),
'name' => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
'mainEntityOfPage' => [ '@id' => $this->context->main_schema_id ],
'description' => '',
];
if ( $this->context->has_article ) {
$data['mainEntityOfPage'] = [ '@id' => $this->context->main_schema_id . Schema_IDs::ARTICLE_HASH ];
}
if ( isset( $block['attrs']['jsonDescription'] ) ) {
$data['description'] = $this->helpers->schema->html->sanitize( $block['attrs']['jsonDescription'] );
}
$this->add_duration( $data, $block['attrs'] );
if ( isset( $block['attrs']['steps'] ) ) {
$this->add_steps( $data, $block['attrs']['steps'] );
}
$data = $this->helpers->schema->language->add_piece_language( $data );
$graph[] = $data;
}
/**
* Generates the image schema from the attachment $url.
*
* @param string $url Attachment url.
*
* @return array Image schema.
*/
protected function get_image_schema( $url ) {
$schema_id = $this->context->canonical . '#schema-image-' . \md5( $url );
return $this->helpers->schema->image->generate_from_url( $schema_id, $url );
}
}