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

Yoast / wordpress-seo / 7257244093

19 Dec 2023 04:09AM UTC coverage: 49.388% (-0.002%) from 49.39%
7257244093

push

github

web-flow
Merge pull request #20987 from Yoast/JRF/docs/more-fixes

Docs: more fixes

15425 of 31232 relevant lines covered (49.39%)

4.07 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 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 );
12✔
65
                }
66

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

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

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

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

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

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

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

139
                        foreach ( $graph_pieces as $graph_piece ) {
18✔
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
                                 * @param array                   $graph_piece            The graph piece to filter.
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 );
18✔
150
                                $graph_piece = $this->type_filter( $graph_piece, $identifier, $context, $graph_piece_generator, $graph_piece_generators );
18✔
151
                                $graph_piece = $this->validate_type( $graph_piece );
18✔
152

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

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

167
                return $graph;
22✔
168
        }
169

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

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 );
12✔
191

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

198
                return $graph;
22✔
199
        }
200

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

212
                return $graph;
22✔
213
        }
214

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

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

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

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

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

254
                return $graph;
22✔
255
        }
256

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

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

285
                return $graph_piece;
2✔
286
        }
287

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

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

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

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

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

362
                return $graph_piece;
18✔
363
        }
364

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

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

382
                return [];
4✔
383
        }
384

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

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

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

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

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