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

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

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

0.0
/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(
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
                $graph              = $this->finalize_graph( $graph, $context );
×
79

80
                return [
×
81
                        '@context' => 'https://schema.org',
×
82
                        '@graph'   => $graph,
×
83
                ];
×
84
        }
85

86
        /**
87
         * Filters out any graph pieces that should not be generated.
88
         * (Using the `wpseo_schema_needs_<graph_piece_identifier>` series of filters).
89
         *
90
         * @param array $graph_pieces The current list of graph pieces that we want to generate.
91
         *
92
         * @return array The graph pieces to generate.
93
         */
94
        protected function filter_graph_pieces_to_generate( $graph_pieces ) {
95
                $pieces_to_generate = [];
×
96
                foreach ( $graph_pieces as $piece ) {
×
97
                        $identifier = \strtolower( \str_replace( 'Yoast\WP\SEO\Generators\Schema\\', '', \get_class( $piece ) ) );
×
98
                        if ( isset( $piece->identifier ) ) {
×
99
                                $identifier = $piece->identifier;
×
100
                        }
101

102
                        /**
103
                         * Filter: 'wpseo_schema_needs_<identifier>' - Allows changing which graph pieces we output.
104
                         *
105
                         * @api bool $is_needed Whether or not to show a graph piece.
106
                         */
107
                        $is_needed = \apply_filters( 'wpseo_schema_needs_' . $identifier, $piece->is_needed() );
×
108
                        if ( ! $is_needed ) {
×
109
                                continue;
×
110
                        }
111

112
                        $pieces_to_generate[ $identifier ] = $piece;
×
113
                }
114

115
                return $pieces_to_generate;
×
116
        }
117

118
        /**
119
         * Generates the schema graph.
120
         *
121
         * @param array             $graph_piece_generators The schema graph pieces to generate.
122
         * @param Meta_Tags_Context $context                The meta tags context to use.
123
         *
124
         * @return array The generated schema graph.
125
         */
126
        protected function generate_graph( $graph_piece_generators, $context ) {
127
                $graph = [];
×
128
                foreach ( $graph_piece_generators as $identifier => $graph_piece_generator ) {
×
129
                        $graph_pieces = $graph_piece_generator->generate();
×
130
                        // If only a single graph piece was returned.
131
                        if ( $graph_pieces !== false && \array_key_exists( '@type', $graph_pieces ) ) {
×
132
                                $graph_pieces = [ $graph_pieces ];
×
133
                        }
134

135
                        if ( ! \is_array( $graph_pieces ) ) {
×
136
                                continue;
×
137
                        }
138

139
                        foreach ( $graph_pieces as $graph_piece ) {
×
140
                                /**
141
                                 * Filter: 'wpseo_schema_<identifier>' - Allows changing graph piece output.
142
                                 * This filter can be called with either an identifier or a block type (see `add_schema_blocks_graph_pieces()`).
143
                                 *
144
                                 * @api array $graph_piece The graph piece to filter.
145
                                 *
146
                                 * @param Meta_Tags_Context $context A value object with context variables.
147
                                 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
148
                                 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
149
                                 */
150
                                $graph_piece = \apply_filters( 'wpseo_schema_' . $identifier, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
×
151
                                $graph_piece = $this->type_filter( $graph_piece, $identifier, $context, $graph_piece_generator, $graph_piece_generators );
×
152
                                $graph_piece = $this->validate_type( $graph_piece );
×
153

154
                                if ( \is_array( $graph_piece ) ) {
×
155
                                        $graph[] = $graph_piece;
×
156
                                }
157
                        }
158
                }
159

160
                /**
161
                 * Filter: 'wpseo_schema_graph' - Allows changing graph output.
162
                 *
163
                 * @api array $graph The graph to filter.
164
                 *
165
                 * @param Meta_Tags_Context $context A value object with context variables.
166
                 */
167
                $graph = \apply_filters( 'wpseo_schema_graph', $graph, $context );
×
168

169
                return $graph;
×
170
        }
171

172
        /**
173
         * Adds schema graph pieces from Gutenberg blocks on the current page to
174
         * the given schema graph.
175
         *
176
         * Think of blocks like the Yoast FAQ block or the How To block.
177
         *
178
         * @param array             $graph   The current schema graph.
179
         * @param Meta_Tags_Context $context The meta tags context.
180
         *
181
         * @return array The graph with the schema blocks graph pieces added.
182
         */
183
        protected function add_schema_blocks_graph_pieces( $graph, $context ) {
184
                foreach ( $context->blocks as $block_type => $blocks ) {
×
185
                        foreach ( $blocks as $block ) {
×
186
                                $block_type = \strtolower( $block['blockName'] );
×
187

188
                                /**
189
                                 * Filter: 'wpseo_schema_block_<block-type>'.
190
                                 * This filter is documented in the `generate_graph()` function in this class.
191
                                 */
192
                                $graph = \apply_filters( 'wpseo_schema_block_' . $block_type, $graph, $block, $context );
×
193

194
                                if ( isset( $block['attrs']['yoast-schema'] ) ) {
×
195
                                        $graph[] = $this->schema_replace_vars_helper->replace( $block['attrs']['yoast-schema'], $context->presentation );
×
196
                                }
197
                        }
198
                }
199

200
                return $graph;
×
201
        }
202

203
        /**
204
         * Finalizes the schema graph after all filtering is done.
205
         *
206
         * @param array             $graph   The current schema graph.
207
         * @param Meta_Tags_Context $context The meta tags context.
208
         *
209
         * @return array The schema graph.
210
         */
211
        protected function finalize_graph( $graph, $context ) {
212
                $graph = $this->remove_empty_breadcrumb( $graph, $context );
×
213

214
                return $graph;
×
215
        }
216

217
        /**
218
         * Removes the breadcrumb schema if empty.
219
         *
220
         * @param array             $graph   The current schema graph.
221
         * @param Meta_Tags_Context $context The meta tags context.
222
         *
223
         * @return array The schema graph with empty breadcrumbs taken out.
224
         */
225
        protected function remove_empty_breadcrumb( $graph, $context ) {
226
                if ( $this->helpers->current_page->is_home_static_page() || $this->helpers->current_page->is_home_posts_page() ) {
×
227
                        return $graph;
×
228
                }
229

230
                // Remove the breadcrumb piece, if it's empty.
231
                $index_to_remove = 0;
×
232
                foreach ( $graph as $key => $piece ) {
×
233
                        if ( \in_array( 'BreadcrumbList', $this->get_type_from_piece( $piece ), true ) ) {
×
234
                                if ( isset( $piece['itemListElement'] ) && \is_array( $piece['itemListElement'] ) && \count( $piece['itemListElement'] ) === 1 ) {
×
235
                                        $index_to_remove = $key;
×
236
                                        break;
×
237
                                }
238
                        }
239
                }
240

241
                // If the breadcrumb piece has been removed, we should remove its reference from the WebPage node.
242
                if ( $index_to_remove !== 0 ) {
×
243
                        \array_splice( $graph, $index_to_remove, 1 );
×
244

245
                        // Get the type of the WebPage node.
246
                        $webpage_types = \is_array( $context->schema_page_type ) ? $context->schema_page_type : [ $context->schema_page_type ];
×
247

248
                        foreach ( $graph as $key => $piece ) {
×
249
                                if ( ! empty( \array_intersect( $webpage_types, $this->get_type_from_piece( $piece ) ) ) && isset( $piece['breadcrumb'] ) ) {
×
250
                                        unset( $piece['breadcrumb'] );
×
251
                                        $graph[ $key ] = $piece;
×
252
                                }
253
                        }
254
                }
255

256
                return $graph;
×
257
        }
258

259
        /**
260
         * Adapts the WebPage graph piece for password-protected posts.
261
         *
262
         * It should only have certain whitelisted properties.
263
         * The type should always be WebPage.
264
         *
265
         * @param array $graph_piece The WebPage graph piece that should be adapted for password-protected posts.
266
         *
267
         * @return array The WebPage graph piece that has been adapted for password-protected posts.
268
         */
269
        public function protected_webpage_schema( $graph_piece ) {
270
                $properties_to_show = \array_flip(
×
271
                        [
×
272
                                '@type',
×
273
                                '@id',
×
274
                                'url',
×
275
                                'name',
×
276
                                'isPartOf',
×
277
                                'inLanguage',
×
278
                                'datePublished',
×
279
                                'dateModified',
×
280
                                'breadcrumb',
×
281
                        ]
×
282
                );
×
283

284
                $graph_piece          = \array_intersect_key( $graph_piece, $properties_to_show );
×
285
                $graph_piece['@type'] = 'WebPage';
×
286

287
                return $graph_piece;
×
288
        }
289

290
        /**
291
         * Gets all the graph pieces we need.
292
         *
293
         * @param Meta_Tags_Context $context The meta tags context.
294
         *
295
         * @return Abstract_Schema_Piece[] A filtered array of graph pieces.
296
         */
297
        protected function get_graph_pieces( $context ) {
298
                if ( $context->indexable->object_type === 'post' && \post_password_required( $context->post ) ) {
×
299
                        $schema_pieces = [
×
300
                                new Schema\WebPage(),
×
301
                                new Schema\Website(),
×
302
                                new Schema\Organization(),
×
303
                        ];
×
304

305
                        \add_filter( 'wpseo_schema_webpage', [ $this, 'protected_webpage_schema' ], 1 );
×
306
                }
307
                else {
308
                        $schema_pieces = [
×
309
                                new Schema\Article(),
×
310
                                new Schema\WebPage(),
×
311
                                new Schema\Main_Image(),
×
312
                                new Schema\Breadcrumb(),
×
313
                                new Schema\Website(),
×
314
                                new Schema\Organization(),
×
315
                                new Schema\Person(),
×
316
                                new Schema\Author(),
×
317
                                new Schema\FAQ(),
×
318
                                new Schema\HowTo(),
×
319
                        ];
×
320
                }
321

322
                /**
323
                 * Filter: 'wpseo_schema_graph_pieces' - Allows adding pieces to the graph.
324
                 *
325
                 * @param Meta_Tags_Context $context An object with context variables.
326
                 *
327
                 * @api array $pieces The schema pieces.
328
                 */
329
                return \apply_filters( 'wpseo_schema_graph_pieces', $schema_pieces, $context );
×
330
        }
331

332
        /**
333
         * Allows filtering the graph piece by its schema type.
334
         *
335
         * Note: We removed the Abstract_Schema_Piece type-hint from the $graph_piece_generator argument, because
336
         *       it caused conflicts with old code, Yoast SEO Video specifically.
337
         *
338
         * @param array                   $graph_piece            The graph piece we're filtering.
339
         * @param string                  $identifier             The identifier of the graph piece that is being filtered.
340
         * @param Meta_Tags_Context       $context                The meta tags context.
341
         * @param Abstract_Schema_Piece   $graph_piece_generator  A value object with context variables.
342
         * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
343
         *
344
         * @return array The filtered graph piece.
345
         */
346
        private function type_filter( $graph_piece, $identifier, Meta_Tags_Context $context, $graph_piece_generator, array $graph_piece_generators ) {
347
                $types = $this->get_type_from_piece( $graph_piece );
×
348
                foreach ( $types as $type ) {
×
349
                        $type = \strtolower( $type );
×
350

351
                        // Prevent running the same filter twice. This makes sure we run f/i. for 'author' and for 'person'.
352
                        if ( $type && $type !== $identifier ) {
×
353
                                /**
354
                                 * Filter: 'wpseo_schema_<type>' - Allows changing graph piece output by @type.
355
                                 *
356
                                 * @api array $graph_piece The graph piece to filter.
357
                                 *
358
                                 * @param Meta_Tags_Context $context A value object with context variables.
359
                                 * @param Abstract_Schema_Piece $graph_piece_generator A value object with context variables.
360
                                 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
361
                                 */
362
                                $graph_piece = \apply_filters( 'wpseo_schema_' . $type, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
×
363
                        }
364
                }
365

366
                return $graph_piece;
×
367
        }
368

369
        /**
370
         * Retrieves the type from a graph piece.
371
         *
372
         * @param array $piece The graph piece.
373
         *
374
         * @return array An array of the piece's types.
375
         */
376
        private function get_type_from_piece( $piece ) {
377
                if ( isset( $piece['@type'] ) ) {
×
378
                        if ( \is_array( $piece['@type'] ) ) {
×
379
                                // Return as-is, but remove unusable values, like sub-arrays, objects, null.
380
                                return \array_filter( $piece['@type'], 'is_string' );
×
381
                        }
382

383
                        return [ $piece['@type'] ];
×
384
                }
385

386
                return [];
×
387
        }
388

389
        /**
390
         * Validates a graph piece's type.
391
         *
392
         * When the type is an array:
393
         *   - Ensure the values are unique.
394
         *   - Only 1 value? Use that value without the array wrapping.
395
         *
396
         * @param array $piece The graph piece.
397
         *
398
         * @return array The graph piece.
399
         */
400
        private function validate_type( $piece ) {
401
                if ( ! isset( $piece['@type'] ) ) {
×
402
                        // No type to validate.
403
                        return $piece;
×
404
                }
405

406
                // If it is not an array, we can return immediately.
407
                if ( ! \is_array( $piece['@type'] ) ) {
×
408
                        return $piece;
×
409
                }
410

411
                /*
412
                 * Ensure the types are unique.
413
                 * Use array_values to reset the indices (e.g. no 0, 2 because 1 was a duplicate).
414
                 */
415
                $piece['@type'] = \array_values( \array_unique( $piece['@type'] ) );
×
416

417
                // Use the first value if there is only 1 type.
418
                if ( \count( $piece['@type'] ) === 1 ) {
×
419
                        $piece['@type'] = \reset( $piece['@type'] );
×
420
                }
421

422
                return $piece;
×
423
        }
424
}
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