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

358 lines 11.2 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 return $graph;
160 }
161
162 /**
163 * Adds schema graph pieces from Gutenberg blocks on the current page to
164 * the given schema graph.
165 *
166 * Think of blocks like the Yoast FAQ block or the How To block.
167 *
168 * @param array $graph The current schema graph.
169 * @param Meta_Tags_Context $context The meta tags context.
170 *
171 * @return array The graph with the schema blocks graph pieces added.
172 */
173 protected function add_schema_blocks_graph_pieces( $graph, $context ) {
174 foreach ( $context->blocks as $block_type => $blocks ) {
175 foreach ( $blocks as $block ) {
176 $block_type = \strtolower( $block['blockName'] );
177 /**
178 * Filter: 'wpseo_schema_block_<block-type>'.
179 * This filter is documented in the `generate_graph()` function in this class.
180 */
181 $graph = \apply_filters( 'wpseo_schema_block_' . $block_type, $graph, $block, $context );
182
183 if ( isset( $block['attrs']['yoast-schema'] ) ) {
184 $graph[] = $this->schema_replace_vars_helper->replace( $block['attrs']['yoast-schema'], $context->presentation );
185 }
186 }
187 }
188
189 return $graph;
190 }
191
192 /**
193 * Adapts the WebPage graph piece for password-protected posts.
194 *
195 * It should only have certain whitelisted properties.
196 * The type should always be WebPage.
197 *
198 * @param array $graph_piece The WebPage graph piece that should be adapted for password-protected posts.
199 *
200 * @return array The WebPage graph piece that has been adapted for password-protected posts.
201 */
202 public function protected_webpage_schema( $graph_piece ) {
203 $properties_to_show = \array_flip(
204 [
205 '@type',
206 '@id',
207 'url',
208 'name',
209 'isPartOf',
210 'inLanguage',
211 'datePublished',
212 'dateModified',
213 'breadcrumb',
214 ]
215 );
216
217 $graph_piece = \array_intersect_key( $graph_piece, $properties_to_show );
218 $graph_piece['@type'] = 'WebPage';
219
220 return $graph_piece;
221 }
222
223 /**
224 * Gets all the graph pieces we need.
225 *
226 * @param Meta_Tags_Context $context The meta tags context.
227 *
228 * @return Abstract_Schema_Piece[] A filtered array of graph pieces.
229 */
230 protected function get_graph_pieces( $context ) {
231 if ( $context->indexable->object_type === 'post' && \post_password_required( $context->post ) ) {
232 $schema_pieces = [
233 new Schema\Organization(),
234 new Schema\Website(),
235 new Schema\WebPage(),
236 ];
237
238 \add_filter( 'wpseo_schema_webpage', [ $this, 'protected_webpage_schema' ], 1 );
239 }
240 else {
241 $schema_pieces = [
242 new Schema\Organization(),
243 new Schema\Person(),
244 new Schema\Website(),
245 new Schema\Main_Image(),
246 new Schema\WebPage(),
247 new Schema\Breadcrumb(),
248 new Schema\Article(),
249 new Schema\Author(),
250 new Schema\FAQ(),
251 new Schema\HowTo(),
252 ];
253 }
254
255 /**
256 * Filter: 'wpseo_schema_graph_pieces' - Allows adding pieces to the graph.
257 *
258 * @param Meta_Tags_Context $context An object with context variables.
259 *
260 * @api array $pieces The schema pieces.
261 */
262 return \apply_filters( 'wpseo_schema_graph_pieces', $schema_pieces, $context );
263 }
264
265 /**
266 * Allows filtering the graph piece by its schema type.
267 *
268 * Note: We removed the Abstract_Schema_Piece type-hint from the $graph_piece_generator argument, because
269 * it caused conflicts with old code, Yoast SEO Video specifically.
270 *
271 * @param array $graph_piece The graph piece we're filtering.
272 * @param string $identifier The identifier of the graph piece that is being filtered.
273 * @param Meta_Tags_Context $context The meta tags context.
274 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
275 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
276 *
277 * @return array The filtered graph piece.
278 */
279 private function type_filter( $graph_piece, $identifier, Meta_Tags_Context $context, $graph_piece_generator, array $graph_piece_generators ) {
280 $types = $this->get_type_from_piece( $graph_piece );
281 foreach ( $types as $type ) {
282 $type = \strtolower( $type );
283
284 // Prevent running the same filter twice. This makes sure we run f/i. for 'author' and for 'person'.
285 if ( $type && $type !== $identifier ) {
286 /**
287 * Filter: 'wpseo_schema_<type>' - Allows changing graph piece output by @type.
288 *
289 * @api array $graph_piece The graph piece to filter.
290 *
291 * @param Meta_Tags_Context $context A value object with context variables.
292 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
293 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
294 */
295 $graph_piece = \apply_filters( 'wpseo_schema_' . $type, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
296 }
297 }
298
299 return $graph_piece;
300 }
301
302 /**
303 * Retrieves the type from a graph piece.
304 *
305 * @param array $piece The graph piece.
306 *
307 * @return array An array of the piece's types.
308 */
309 private function get_type_from_piece( $piece ) {
310 if ( isset( $piece['@type'] ) ) {
311 if ( \is_array( $piece['@type'] ) ) {
312 // Return as-is, but remove unusable values, like sub-arrays, objects, null.
313 return \array_filter( $piece['@type'], 'is_string' );
314 }
315
316 return [ $piece['@type'] ];
317 }
318
319 return [];
320 }
321
322 /**
323 * Validates a graph piece's type.
324 *
325 * When the type is an array:
326 * - Ensure the values are unique.
327 * - Only 1 value? Use that value without the array wrapping.
328 *
329 * @param array $piece The graph piece.
330 *
331 * @return array The graph piece.
332 */
333 private function validate_type( $piece ) {
334 if ( ! isset( $piece['@type'] ) ) {
335 // No type to validate.
336 return $piece;
337 }
338
339 // If it is not an array, we can return immediately.
340 if ( ! \is_array( $piece['@type'] ) ) {
341 return $piece;
342 }
343
344 /*
345 * Ensure the types are unique.
346 * Use array_values to reset the indices (e.g. no 0, 2 because 1 was a duplicate).
347 */
348 $piece['@type'] = \array_values( \array_unique( $piece['@type'] ) );
349
350 // Use the first value if there is only 1 type.
351 if ( \count( $piece['@type'] ) === 1 ) {
352 $piece['@type'] = \reset( $piece['@type'] );
353 }
354
355 return $piece;
356 }
357 }
358