• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / f44229b0911ab3251ebf3aec976097d0ed682dee

24 Mar 2025 06:25PM UTC coverage: 48.68% (-0.03%) from 48.71%
f44229b0911ab3251ebf3aec976097d0ed682dee

push

github

web-flow
Merge pull request #22139 from Yoast/JRF/CSQA/remove-unused-use-statements

CS/QA: remove unused import use statements

16083 of 33038 relevant lines covered (48.68%)

3.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.26
/src/generators/schema-generator.php
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 ) {
26✔
37
                $this->helpers                    = $helpers;
26✔
38
                $this->schema_replace_vars_helper = $schema_replace_vars_helper;
26✔
39
        }
13✔
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 ) {
24✔
49
                $pieces = $this->get_graph_pieces( $context );
24✔
50

51
                $this->schema_replace_vars_helper->register_replace_vars( $context );
24✔
52

53
                foreach ( \array_keys( $context->blocks ) as $block_type ) {
24✔
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 );
14✔
61
                }
62

63
                // Do a loop before everything else to inject the context and helpers.
64
                foreach ( $pieces as $piece ) {
24✔
65
                        if ( \is_a( $piece, Abstract_Schema_Piece::class ) ) {
20✔
66
                                $piece->context = $context;
20✔
67
                                $piece->helpers = $this->helpers;
20✔
68
                        }
69
                }
70

71
                $pieces_to_generate = $this->filter_graph_pieces_to_generate( $pieces );
24✔
72
                $graph              = $this->generate_graph( $pieces_to_generate, $context );
24✔
73
                $graph              = $this->add_schema_blocks_graph_pieces( $graph, $context );
24✔
74
                $graph              = $this->finalize_graph( $graph, $context );
24✔
75

76
                return [
12✔
77
                        '@context' => 'https://schema.org',
24✔
78
                        '@graph'   => $graph,
24✔
79
                ];
12✔
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 ) {
24✔
91
                $pieces_to_generate = [];
24✔
92
                foreach ( $graph_pieces as $piece ) {
24✔
93
                        $identifier = \strtolower( \str_replace( 'Yoast\WP\SEO\Generators\Schema\\', '', \get_class( $piece ) ) );
20✔
94
                        if ( isset( $piece->identifier ) ) {
20✔
95
                                $identifier = $piece->identifier;
2✔
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() );
20✔
104
                        if ( ! $is_needed ) {
20✔
105
                                continue;
18✔
106
                        }
107

108
                        $pieces_to_generate[ $identifier ] = $piece;
20✔
109
                }
110

111
                return $pieces_to_generate;
24✔
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 ) {
24✔
123
                $graph = [];
24✔
124
                foreach ( $graph_piece_generators as $identifier => $graph_piece_generator ) {
24✔
125
                        $graph_pieces = $graph_piece_generator->generate();
20✔
126
                        // If only a single graph piece was returned.
127
                        if ( $graph_pieces !== false && \array_key_exists( '@type', $graph_pieces ) ) {
20✔
128
                                $graph_pieces = [ $graph_pieces ];
18✔
129
                        }
130

131
                        if ( ! \is_array( $graph_pieces ) ) {
20✔
132
                                continue;
16✔
133
                        }
134

135
                        foreach ( $graph_pieces as $graph_piece ) {
20✔
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 );
20✔
146
                                $graph_piece = $this->type_filter( $graph_piece, $identifier, $context, $graph_piece_generator, $graph_piece_generators );
20✔
147
                                $graph_piece = $this->validate_type( $graph_piece );
20✔
148

149
                                if ( \is_array( $graph_piece ) ) {
20✔
150
                                        $graph[] = $graph_piece;
20✔
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 );
24✔
162

163
                return $graph;
24✔
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 ) {
24✔
178
                foreach ( $context->blocks as $block_type => $blocks ) {
24✔
179
                        foreach ( $blocks as $block ) {
14✔
180
                                $block_type = \strtolower( $block['blockName'] );
14✔
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 );
14✔
187

188
                                if ( isset( $block['attrs']['yoast-schema'] ) ) {
14✔
189
                                        $graph[] = $this->schema_replace_vars_helper->replace( $block['attrs']['yoast-schema'], $context->presentation );
2✔
190
                                }
191
                        }
192
                }
193

194
                return $graph;
24✔
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 ) {
24✔
206
                $graph = $this->remove_empty_breadcrumb( $graph, $context );
24✔
207

208
                return $graph;
24✔
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 ) {
24✔
220
                if ( $this->helpers->current_page->is_home_static_page() || $this->helpers->current_page->is_home_posts_page() ) {
24✔
221
                        return $graph;
×
222
                }
223

224
                // Remove the breadcrumb piece, if it's empty.
225
                $index_to_remove = 0;
24✔
226
                foreach ( $graph as $key => $piece ) {
24✔
227
                        if ( \in_array( 'BreadcrumbList', $this->get_type_from_piece( $piece ), true ) ) {
22✔
228
                                if ( isset( $piece['itemListElement'] ) && \is_array( $piece['itemListElement'] ) && \count( $piece['itemListElement'] ) === 1 ) {
4✔
229
                                        $index_to_remove = $key;
2✔
230
                                        break;
2✔
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 ) {
24✔
237
                        \array_splice( $graph, $index_to_remove, 1 );
2✔
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 ];
2✔
241

242
                        foreach ( $graph as $key => $piece ) {
2✔
243
                                if ( ! empty( \array_intersect( $webpage_types, $this->get_type_from_piece( $piece ) ) ) && isset( $piece['breadcrumb'] ) ) {
2✔
244
                                        unset( $piece['breadcrumb'] );
2✔
245
                                        $graph[ $key ] = $piece;
2✔
246
                                }
247
                        }
248
                }
249

250
                return $graph;
24✔
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 ) {
2✔
264
                $properties_to_show = \array_flip(
2✔
265
                        [
1✔
266
                                '@type',
2✔
267
                                '@id',
1✔
268
                                'url',
1✔
269
                                'name',
1✔
270
                                'isPartOf',
1✔
271
                                'inLanguage',
1✔
272
                                'datePublished',
1✔
273
                                'dateModified',
1✔
274
                                'breadcrumb',
1✔
275
                        ]
1✔
276
                );
1✔
277

278
                $graph_piece          = \array_intersect_key( $graph_piece, $properties_to_show );
2✔
279
                $graph_piece['@type'] = 'WebPage';
2✔
280

281
                return $graph_piece;
2✔
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 ) {
18✔
292
                if ( $context->indexable->object_type === 'post' && \post_password_required( $context->post ) ) {
18✔
293
                        $schema_pieces = [
1✔
294
                                new Schema\WebPage(),
2✔
295
                                new Schema\Website(),
2✔
296
                                new Schema\Organization(),
2✔
297
                        ];
1✔
298

299
                        \add_filter( 'wpseo_schema_webpage', [ $this, 'protected_webpage_schema' ], 1 );
2✔
300
                }
301
                else {
302
                        $schema_pieces = [
8✔
303
                                new Schema\Article(),
16✔
304
                                new Schema\WebPage(),
16✔
305
                                new Schema\Main_Image(),
16✔
306
                                new Schema\Breadcrumb(),
16✔
307
                                new Schema\Website(),
16✔
308
                                new Schema\Organization(),
16✔
309
                                new Schema\Person(),
16✔
310
                                new Schema\Author(),
16✔
311
                                new Schema\FAQ(),
16✔
312
                                new Schema\HowTo(),
16✔
313
                        ];
8✔
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 );
18✔
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 ) {
20✔
340
                $types = $this->get_type_from_piece( $graph_piece );
20✔
341
                foreach ( $types as $type ) {
20✔
342
                        $type = \strtolower( $type );
20✔
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 ) {
20✔
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 );
16✔
355
                        }
356
                }
357

358
                return $graph_piece;
20✔
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 ) {
22✔
369
                if ( isset( $piece['@type'] ) ) {
22✔
370
                        if ( \is_array( $piece['@type'] ) ) {
22✔
371
                                // Return as-is, but remove unusable values, like sub-arrays, objects, null.
372
                                return \array_filter( $piece['@type'], 'is_string' );
16✔
373
                        }
374

375
                        return [ $piece['@type'] ];
20✔
376
                }
377

378
                return [];
4✔
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 ) {
20✔
393
                if ( ! isset( $piece['@type'] ) ) {
20✔
394
                        // No type to validate.
395
                        return $piece;
4✔
396
                }
397

398
                // If it is not an array, we can return immediately.
399
                if ( ! \is_array( $piece['@type'] ) ) {
20✔
400
                        return $piece;
16✔
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'] ) );
16✔
408

409
                // Use the first value if there is only 1 type.
410
                if ( \count( $piece['@type'] ) === 1 ) {
16✔
411
                        $piece['@type'] = \reset( $piece['@type'] );
6✔
412
                }
413

414
                return $piece;
16✔
415
        }
416
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc