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

Yoast / wordpress-seo / baea08ee244233b9b0667839b599ef84b1377f59

22 Dec 2023 11:33PM UTC coverage: 53.107%. Remained the same
baea08ee244233b9b0667839b599ef84b1377f59

push

github

web-flow
Merge pull request #21010 from Yoast/JRF/php-8.2/prevent-dynamic-property-deprecations

PHP 8.2: prevent dynamic property deprecations

7549 of 13844 branches covered (0.0%)

Branch coverage included in aggregate %.

28964 of 54910 relevant lines covered (52.75%)

40302.26 hits per line

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

99.27
/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(
24✔
37
                Helpers_Surface $helpers,
38
                Replace_Vars_Helper $schema_replace_vars_helper
39
        ) {
12✔
40
                $this->helpers                    = $helpers;
24✔
41
                $this->schema_replace_vars_helper = $schema_replace_vars_helper;
24✔
42
        }
12✔
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 ) {
22✔
52
                $pieces = $this->get_graph_pieces( $context );
22✔
53

54
                $this->schema_replace_vars_helper->register_replace_vars( $context );
22✔
55

56
                foreach ( \array_keys( $context->blocks ) as $block_type ) {
22✔
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 WP_Block_Parser_Block[] $blocks     All the blocks of this block type.
61
                         * @param Meta_Tags_Context       $context    A value object with context variables.
62
                         */
63
                        \do_action( 'wpseo_pre_schema_block_type_' . $block_type, $context->blocks[ $block_type ], $context );
12✔
64
                }
65

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

74
                $pieces_to_generate = $this->filter_graph_pieces_to_generate( $pieces );
22✔
75
                $graph              = $this->generate_graph( $pieces_to_generate, $context );
22✔
76
                $graph              = $this->add_schema_blocks_graph_pieces( $graph, $context );
22✔
77
                $graph              = $this->finalize_graph( $graph, $context );
22✔
78

79
                return [
11✔
80
                        '@context' => 'https://schema.org',
22✔
81
                        '@graph'   => $graph,
22✔
82
                ];
11✔
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 ) {
22✔
94
                $pieces_to_generate = [];
22✔
95
                foreach ( $graph_pieces as $piece ) {
22✔
96
                        $identifier = \strtolower( \str_replace( 'Yoast\WP\SEO\Generators\Schema\\', '', \get_class( $piece ) ) );
18✔
97
                        if ( isset( $piece->identifier ) ) {
18✔
98
                                $identifier = $piece->identifier;
2✔
99
                        }
100

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

111
                        $pieces_to_generate[ $identifier ] = $piece;
18✔
112
                }
113

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

134
                        if ( ! \is_array( $graph_pieces ) ) {
18✔
135
                                continue;
14✔
136
                        }
137

138
                        foreach ( $graph_pieces as $graph_piece ) {
18✔
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
                                 * @param array                   $graph_piece            The graph piece to filter.
144
                                 * @param Meta_Tags_Context       $context                A value object with context variables.
145
                                 * @param Abstract_Schema_Piece   $graph_piece_generator  A value object with context variables.
146
                                 * @param Abstract_Schema_Piece[] $graph_piece_generators A value object with context variables.
147
                                 */
148
                                $graph_piece = \apply_filters( 'wpseo_schema_' . $identifier, $graph_piece, $context, $graph_piece_generator, $graph_piece_generators );
18✔
149
                                $graph_piece = $this->type_filter( $graph_piece, $identifier, $context, $graph_piece_generator, $graph_piece_generators );
18✔
150
                                $graph_piece = $this->validate_type( $graph_piece );
18✔
151

152
                                if ( \is_array( $graph_piece ) ) {
18✔
153
                                        $graph[] = $graph_piece;
18✔
154
                                }
155
                        }
156
                }
157

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

166
                return $graph;
22✔
167
        }
168

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

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

191
                                if ( isset( $block['attrs']['yoast-schema'] ) ) {
12✔
192
                                        $graph[] = $this->schema_replace_vars_helper->replace( $block['attrs']['yoast-schema'], $context->presentation );
2✔
193
                                }
194
                        }
195
                }
196

197
                return $graph;
22✔
198
        }
199

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

211
                return $graph;
22✔
212
        }
213

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

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

238
                // If the breadcrumb piece has been removed, we should remove its reference from the WebPage node.
239
                if ( $index_to_remove !== 0 ) {
22✔
240
                        \array_splice( $graph, $index_to_remove, 1 );
2✔
241

242
                        // Get the type of the WebPage node.
243
                        $webpage_types = \is_array( $context->schema_page_type ) ? $context->schema_page_type : [ $context->schema_page_type ];
2✔
244

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

253
                return $graph;
22✔
254
        }
255

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

281
                $graph_piece          = \array_intersect_key( $graph_piece, $properties_to_show );
2✔
282
                $graph_piece['@type'] = 'WebPage';
2✔
283

284
                return $graph_piece;
2✔
285
        }
286

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

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

319
                /**
320
                 * Filter: 'wpseo_schema_graph_pieces' - Allows adding pieces to the graph.
321
                 *
322
                 * @param array             $pieces  The schema pieces.
323
                 * @param Meta_Tags_Context $context An object with context variables.
324
                 */
325
                return \apply_filters( 'wpseo_schema_graph_pieces', $schema_pieces, $context );
16✔
326
        }
327

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

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

361
                return $graph_piece;
18✔
362
        }
363

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

378
                        return [ $piece['@type'] ];
18✔
379
                }
380

381
                return [];
4✔
382
        }
383

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

401
                // If it is not an array, we can return immediately.
402
                if ( ! \is_array( $piece['@type'] ) ) {
18✔
403
                        return $piece;
14✔
404
                }
405

406
                /*
407
                 * Ensure the types are unique.
408
                 * Use array_values to reset the indices (e.g. no 0, 2 because 1 was a duplicate).
409
                 */
410
                $piece['@type'] = \array_values( \array_unique( $piece['@type'] ) );
14✔
411

412
                // Use the first value if there is only 1 type.
413
                if ( \count( $piece['@type'] ) === 1 ) {
14✔
414
                        $piece['@type'] = \reset( $piece['@type'] );
6✔
415
                }
416

417
                return $piece;
14✔
418
        }
419
}
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