PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 19.0, at src/generators/schema-generator.php

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