PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / generators / schema-generator.php

schema-generator.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/generators/schema-generator.php

417 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Generators;
4
5 use WP_Block_Parser_Block;
6 use Yoast\WP\SEO\Context\Meta_Tags_Context;
7 use Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece;
8 use Yoast\WP\SEO\Helpers\Schema\Replace_Vars_Helper;
9 use Yoast\WP\SEO\Surfaces\Helpers_Surface;
10
11 /**
12 * Class Schema_Generator.
13 */
14 class Schema_Generator implements Generator_Interface {
15
16 /**
17 * The helpers surface.
18 *
19 * @var Helpers_Surface
20 */
21 protected $helpers;
22
23 /**
24 * The Schema replace vars helper.
25 *
26 * @var Replace_Vars_Helper
27 */
28 protected $schema_replace_vars_helper;
29
30 /**
31 * Generator constructor.
32 *
33 * @param Helpers_Surface $helpers The helpers surface.
34 * @param Replace_Vars_Helper $schema_replace_vars_helper The replace vars helper.
35 */
36 public function __construct( Helpers_Surface $helpers, Replace_Vars_Helper $schema_replace_vars_helper ) {
37 $this->helpers = $helpers;
38 $this->schema_replace_vars_helper = $schema_replace_vars_helper;
39 }
40
41 /**
42 * Returns a Schema graph array.
43 *
44 * @param Meta_Tags_Context $context The meta tags context.
45 *
46 * @return array The graph.
47 */
48 public function generate( Meta_Tags_Context $context ) {
49 $pieces = $this->get_graph_pieces( $context );
50
51 $this->schema_replace_vars_helper->register_replace_vars( $context );
52
53 foreach ( \array_keys( $context->blocks ) as $block_type ) {
54 /**
55 * Filter: 'wpseo_pre_schema_block_type_<block-type>' - Allows hooking things to change graph output based on the blocks on the page.
56 *
57 * @param WP_Block_Parser_Block[] $blocks All the blocks of this block type.
58 * @param Meta_Tags_Context $context A value object with context variables.
59 */
60 \do_action( 'wpseo_pre_schema_block_type_' . $block_type, $context->blocks[ $block_type ], $context );
61 }
62
63 // Do a loop before everything else to inject the context and helpers.
64 foreach ( $pieces as $piece ) {
65 if ( \is_a( $piece, Abstract_Schema_Piece::class ) ) {
66 $piece->context = $context;
67 $piece->helpers = $this->helpers;
68 }
69 }
70
71 $pieces_to_generate = $this->filter_graph_pieces_to_generate( $pieces );
72 $graph = $this->generate_graph( $pieces_to_generate, $context );
73 $graph = $this->add_schema_blocks_graph_pieces( $graph, $context );
74 $graph = $this->finalize_graph( $graph, $context );
75
76 return [
77 '@context' => 'https://schema.org',
78 '@graph' => $graph,
79 ];
80 }
81
82 /**
83 * Filters out any graph pieces that should not be generated.
84 * (Using the `wpseo_schema_needs_<graph_piece_identifier>` series of filters).
85 *
86 * @param array $graph_pieces The current list of graph pieces that we want to generate.
87 *
88 * @return array The graph pieces to generate.
89 */
90 protected function filter_graph_pieces_to_generate( $graph_pieces ) {
91 $pieces_to_generate = [];
92 foreach ( $graph_pieces as $piece ) {
93 $identifier = \strtolower( \str_replace( 'Yoast\WP\SEO\Generators\Schema\\', '', \get_class( $piece ) ) );
94 if ( isset( $piece->identifier ) ) {
95 $identifier = $piece->identifier;
96 }
97
98 /**
99 * Filter: 'wpseo_schema_needs_<identifier>' - Allows changing which graph pieces we output.
100 *
101 * @param bool $is_needed Whether or not to show a graph piece.
102 */
103 $is_needed = \apply_filters( 'wpseo_schema_needs_' . $identifier, $piece->is_needed() );
104 if ( ! $is_needed ) {
105 continue;
106 }
107
108 $pieces_to_generate[ $identifier ] = $piece;
109 }
110
111 return $pieces_to_generate;
112 }
113
114 /**
115 * Generates the schema graph.
116 *
117 * @param array $graph_piece_generators The schema graph pieces to generate.
118 * @param Meta_Tags_Context $context The meta tags context to use.
119 *
120 * @return array The generated schema graph.
121 */
122 protected function generate_graph( $graph_piece_generators, $context ) {
123 $graph = [];
124 foreach ( $graph_piece_generators as $identifier => $graph_piece_generator ) {
125 $graph_pieces = $graph_piece_generator->generate();
126 // If only a single graph piece was returned.
127 if ( $graph_pieces !== false && \array_key_exists( '@type', $graph_pieces ) ) {
128 $graph_pieces = [ $graph_pieces ];
129 }
130
131 if ( ! \is_array( $graph_pieces ) ) {
132 continue;
133 }
134
135 foreach ( $graph_pieces as $graph_piece ) {
136 /**
137 * Filter: 'wpseo_schema_<identifier>' - Allows changing graph piece output.
138 * This filter can be called with either an identifier or a block type (see `add_schema_blocks_graph_pieces()`).
139 *
140 * @param array $graph_piece The graph piece to filter.
141 * @param Meta_Tags_Context $context A value object with context variables.
142 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
143 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
144 */
145 $graph_piece = \apply_filters( 'wpseo_schema_' . $identifier, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
146 $graph_piece = $this->type_filter( $graph_piece, $identifier, $context, $graph_piece_generator, $graph_piece_generators );
147 $graph_piece = $this->validate_type( $graph_piece );
148
149 if ( \is_array( $graph_piece ) ) {
150 $graph[] = $graph_piece;
151 }
152 }
153 }
154
155 /**
156 * Filter: 'wpseo_schema_graph' - Allows changing graph output.
157 *
158 * @param array $graph The graph to filter.
159 * @param Meta_Tags_Context $context A value object with context variables.
160 */
161 $graph = \apply_filters( 'wpseo_schema_graph', $graph, $context );
162
163 return $graph;
164 }
165
166 /**
167 * Adds schema graph pieces from Gutenberg blocks on the current page to
168 * the given schema graph.
169 *
170 * Think of blocks like the Yoast FAQ block or the How To block.
171 *
172 * @param array $graph The current schema graph.
173 * @param Meta_Tags_Context $context The meta tags context.
174 *
175 * @return array The graph with the schema blocks graph pieces added.
176 */
177 protected function add_schema_blocks_graph_pieces( $graph, $context ) {
178 foreach ( $context->blocks as $block_type => $blocks ) {
179 foreach ( $blocks as $block ) {
180 $block_type = \strtolower( $block['blockName'] );
181
182 /**
183 * Filter: 'wpseo_schema_block_<block-type>'.
184 * This filter is documented in the `generate_graph()` function in this class.
185 */
186 $graph = \apply_filters( 'wpseo_schema_block_' . $block_type, $graph, $block, $context );
187
188 if ( isset( $block['attrs']['yoast-schema'] ) ) {
189 $graph[] = $this->schema_replace_vars_helper->replace( $block['attrs']['yoast-schema'], $context->presentation );
190 }
191 }
192 }
193
194 return $graph;
195 }
196
197 /**
198 * Finalizes the schema graph after all filtering is done.
199 *
200 * @param array $graph The current schema graph.
201 * @param Meta_Tags_Context $context The meta tags context.
202 *
203 * @return array The schema graph.
204 */
205 protected function finalize_graph( $graph, $context ) {
206 $graph = $this->remove_empty_breadcrumb( $graph, $context );
207
208 return $graph;
209 }
210
211 /**
212 * Removes the breadcrumb schema if empty.
213 *
214 * @param array $graph The current schema graph.
215 * @param Meta_Tags_Context $context The meta tags context.
216 *
217 * @return array The schema graph with empty breadcrumbs taken out.
218 */
219 protected function remove_empty_breadcrumb( $graph, $context ) {
220 if ( $this->helpers->current_page->is_home_static_page() || $this->helpers->current_page->is_home_posts_page() ) {
221 return $graph;
222 }
223
224 // Remove the breadcrumb piece, if it's empty.
225 $index_to_remove = 0;
226 foreach ( $graph as $key => $piece ) {
227 if ( \in_array( 'BreadcrumbList', $this->get_type_from_piece( $piece ), true ) ) {
228 if ( isset( $piece['itemListElement'] ) && \is_array( $piece['itemListElement'] ) && \count( $piece['itemListElement'] ) === 1 ) {
229 $index_to_remove = $key;
230 break;
231 }
232 }
233 }
234
235 // If the breadcrumb piece has been removed, we should remove its reference from the WebPage node.
236 if ( $index_to_remove !== 0 ) {
237 \array_splice( $graph, $index_to_remove, 1 );
238
239 // Get the type of the WebPage node.
240 $webpage_types = \is_array( $context->schema_page_type ) ? $context->schema_page_type : [ $context->schema_page_type ];
241
242 foreach ( $graph as $key => $piece ) {
243 if ( ! empty( \array_intersect( $webpage_types, $this->get_type_from_piece( $piece ) ) ) && isset( $piece['breadcrumb'] ) ) {
244 unset( $piece['breadcrumb'] );
245 $graph[ $key ] = $piece;
246 }
247 }
248 }
249
250 return $graph;
251 }
252
253 /**
254 * Adapts the WebPage graph piece for password-protected posts.
255 *
256 * It should only have certain whitelisted properties.
257 * The type should always be WebPage.
258 *
259 * @param array $graph_piece The WebPage graph piece that should be adapted for password-protected posts.
260 *
261 * @return array The WebPage graph piece that has been adapted for password-protected posts.
262 */
263 public function protected_webpage_schema( $graph_piece ) {
264 $properties_to_show = \array_flip(
265 [
266 '@type',
267 '@id',
268 'url',
269 'name',
270 'isPartOf',
271 'inLanguage',
272 'datePublished',
273 'dateModified',
274 'breadcrumb',
275 ],
276 );
277
278 $graph_piece = \array_intersect_key( $graph_piece, $properties_to_show );
279 $graph_piece['@type'] = 'WebPage';
280
281 return $graph_piece;
282 }
283
284 /**
285 * Gets all the graph pieces we need.
286 *
287 * @param Meta_Tags_Context $context The meta tags context.
288 *
289 * @return Abstract_Schema_Piece[] A filtered array of graph pieces.
290 */
291 protected function get_graph_pieces( $context ) {
292 if ( $context->indexable->object_type === 'post' && \post_password_required( $context->post ) ) {
293 $schema_pieces = [
294 new Schema\WebPage(),
295 new Schema\Website(),
296 new Schema\Organization(),
297 ];
298
299 \add_filter( 'wpseo_schema_webpage', [ $this, 'protected_webpage_schema' ], 1 );
300 }
301 else {
302 $schema_pieces = [
303 new Schema\Article(),
304 new Schema\WebPage(),
305 new Schema\Main_Image(),
306 new Schema\Breadcrumb(),
307 new Schema\Website(),
308 new Schema\Organization(),
309 new Schema\Person(),
310 new Schema\Author(),
311 new Schema\FAQ(),
312 new Schema\HowTo(),
313 ];
314 }
315
316 /**
317 * Filter: 'wpseo_schema_graph_pieces' - Allows adding pieces to the graph.
318 *
319 * @param array $pieces The schema pieces.
320 * @param Meta_Tags_Context $context An object with context variables.
321 */
322 return \apply_filters( 'wpseo_schema_graph_pieces', $schema_pieces, $context );
323 }
324
325 /**
326 * Allows filtering the graph piece by its schema type.
327 *
328 * Note: We removed the Abstract_Schema_Piece type-hint from the $graph_piece_generator argument, because
329 * it caused conflicts with old code, Yoast SEO Video specifically.
330 *
331 * @param array $graph_piece The graph piece we're filtering.
332 * @param string $identifier The identifier of the graph piece that is being filtered.
333 * @param Meta_Tags_Context $context The meta tags context.
334 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
335 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
336 *
337 * @return array The filtered graph piece.
338 */
339 private function type_filter( $graph_piece, $identifier, Meta_Tags_Context $context, $graph_piece_generator, array $graph_piece_generators ) {
340 $types = $this->get_type_from_piece( $graph_piece );
341 foreach ( $types as $type ) {
342 $type = \strtolower( $type );
343
344 // Prevent running the same filter twice. This makes sure we run f/i. for 'author' and for 'person'.
345 if ( $type && $type !== $identifier ) {
346 /**
347 * Filter: 'wpseo_schema_<type>' - Allows changing graph piece output by @type.
348 *
349 * @param array $graph_piece The graph piece to filter.
350 * @param Meta_Tags_Context $context A value object with context variables.
351 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
352 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
353 */
354 $graph_piece = \apply_filters( 'wpseo_schema_' . $type, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
355 }
356 }
357
358 return $graph_piece;
359 }
360
361 /**
362 * Retrieves the type from a graph piece.
363 *
364 * @param array $piece The graph piece.
365 *
366 * @return array An array of the piece's types.
367 */
368 private function get_type_from_piece( $piece ) {
369 if ( isset( $piece['@type'] ) ) {
370 if ( \is_array( $piece['@type'] ) ) {
371 // Return as-is, but remove unusable values, like sub-arrays, objects, null.
372 return \array_filter( $piece['@type'], 'is_string' );
373 }
374
375 return [ $piece['@type'] ];
376 }
377
378 return [];
379 }
380
381 /**
382 * Validates a graph piece's type.
383 *
384 * When the type is an array:
385 * - Ensure the values are unique.
386 * - Only 1 value? Use that value without the array wrapping.
387 *
388 * @param array $piece The graph piece.
389 *
390 * @return array The graph piece.
391 */
392 private function validate_type( $piece ) {
393 if ( ! isset( $piece['@type'] ) ) {
394 // No type to validate.
395 return $piece;
396 }
397
398 // If it is not an array, we can return immediately.
399 if ( ! \is_array( $piece['@type'] ) ) {
400 return $piece;
401 }
402
403 /*
404 * Ensure the types are unique.
405 * Use array_values to reset the indices (e.g. no 0, 2 because 1 was a duplicate).
406 */
407 $piece['@type'] = \array_values( \array_unique( $piece['@type'] ) );
408
409 // Use the first value if there is only 1 type.
410 if ( \count( $piece['@type'] ) === 1 ) {
411 $piece['@type'] = \reset( $piece['@type'] );
412 }
413
414 return $piece;
415 }
416 }
417