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

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

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

77.27
/src/integrations/front-end-integration.php
1
<?php
2

3
namespace Yoast\WP\SEO\Integrations;
4

5
use WP_HTML_Tag_Processor;
6
use WPSEO_Replace_Vars;
7
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
8
use Yoast\WP\SEO\Context\Meta_Tags_Context;
9
use Yoast\WP\SEO\Helpers\Options_Helper;
10
use Yoast\WP\SEO\Helpers\Request_Helper;
11
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
12
use Yoast\WP\SEO\Presenters\Abstract_Indexable_Presenter;
13
use Yoast\WP\SEO\Presenters\Debug\Marker_Close_Presenter;
14
use Yoast\WP\SEO\Presenters\Debug\Marker_Open_Presenter;
15
use Yoast\WP\SEO\Presenters\Title_Presenter;
16
use Yoast\WP\SEO\Surfaces\Helpers_Surface;
17
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
18

19
/**
20
 * Class Front_End_Integration.
21
 */
22
class Front_End_Integration implements Integration_Interface {
23

24
        /**
25
         * The memoizer for the meta tags context.
26
         *
27
         * @var Meta_Tags_Context_Memoizer
28
         */
29
        private $context_memoizer;
30

31
        /**
32
         * The container.
33
         *
34
         * @var ContainerInterface
35
         */
36
        protected $container;
37

38
        /**
39
         * Represents the options helper.
40
         *
41
         * @var Options_Helper
42
         */
43
        protected $options;
44

45
        /**
46
         * Represents the request helper.
47
         *
48
         * @var Request_Helper
49
         */
50
        protected $request;
51

52
        /**
53
         * The helpers surface.
54
         *
55
         * @var Helpers_Surface
56
         */
57
        protected $helpers;
58

59
        /**
60
         * The replace vars helper.
61
         *
62
         * @var WPSEO_Replace_Vars
63
         */
64
        protected $replace_vars;
65

66
        /**
67
         * The presenters we loop through on each page load.
68
         *
69
         * @var string[]
70
         */
71
        protected $base_presenters = [
72
                'Title',
73
                'Meta_Description',
74
                'Robots',
75
        ];
76

77
        /**
78
         * The presenters we loop through on each page load.
79
         *
80
         * @var string[]
81
         */
82
        protected $indexing_directive_presenters = [
83
                'Canonical',
84
                'Rel_Prev',
85
                'Rel_Next',
86
        ];
87

88
        /**
89
         * The Open Graph specific presenters.
90
         *
91
         * @var string[]
92
         */
93
        protected $open_graph_presenters = [
94
                'Open_Graph\Locale',
95
                'Open_Graph\Type',
96
                'Open_Graph\Title',
97
                'Open_Graph\Description',
98
                'Open_Graph\Url',
99
                'Open_Graph\Site_Name',
100
                'Open_Graph\Article_Publisher',
101
                'Open_Graph\Article_Author',
102
                'Open_Graph\Article_Published_Time',
103
                'Open_Graph\Article_Modified_Time',
104
                'Open_Graph\Image',
105
                'Meta_Author',
106
        ];
107

108
        /**
109
         * The Open Graph specific presenters that should be output on error pages.
110
         *
111
         * @var array<string>
112
         */
113
        protected $open_graph_error_presenters = [
114
                'Open_Graph\Locale',
115
                'Open_Graph\Title',
116
                'Open_Graph\Site_Name',
117
        ];
118

119
        /**
120
         * The Twitter card specific presenters.
121
         *
122
         * @var array<string>
123
         */
124
        protected $twitter_card_presenters = [
125
                'Twitter\Card',
126
                'Twitter\Title',
127
                'Twitter\Description',
128
                'Twitter\Image',
129
                'Twitter\Creator',
130
                'Twitter\Site',
131
        ];
132

133
        /**
134
         * The Slack specific presenters.
135
         *
136
         * @var array<string>
137
         */
138
        protected $slack_presenters = [
139
                'Slack\Enhanced_Data',
140
        ];
141

142
        /**
143
         * The Webmaster verification specific presenters.
144
         *
145
         * @var array<string>
146
         */
147
        protected $webmaster_verification_presenters = [
148
                'Webmaster\Baidu',
149
                'Webmaster\Bing',
150
                'Webmaster\Google',
151
                'Webmaster\Pinterest',
152
                'Webmaster\Yandex',
153
        ];
154

155
        /**
156
         * Presenters that are only needed on singular pages.
157
         *
158
         * @var array<string>
159
         */
160
        protected $singular_presenters = [
161
                'Meta_Author',
162
                'Open_Graph\Article_Author',
163
                'Open_Graph\Article_Publisher',
164
                'Open_Graph\Article_Published_Time',
165
                'Open_Graph\Article_Modified_Time',
166
                'Twitter\Creator',
167
                'Slack\Enhanced_Data',
168
        ];
169

170
        /**
171
         * The presenters we want to be last in our output.
172
         *
173
         * @var array<string>
174
         */
175
        protected $closing_presenters = [
176
                'Schema',
177
        ];
178

179
        /**
180
         * The next output.
181
         *
182
         * @var string
183
         */
184
        protected $next;
185

186
        /**
187
         * The prev output.
188
         *
189
         * @var string
190
         */
191
        protected $prev;
192

193
        /**
194
         * Returns the conditionals based on which this loadable should be active.
195
         *
196
         * @return array<string> The conditionals.
197
         */
198
        public static function get_conditionals() {
2✔
199
                return [ Front_End_Conditional::class ];
2✔
200
        }
201

202
        /**
203
         * Front_End_Integration constructor.
204
         *
205
         * @codeCoverageIgnore It sets dependencies.
206
         *
207
         * @param Meta_Tags_Context_Memoizer $context_memoizer  The meta tags context memoizer.
208
         * @param ContainerInterface         $service_container The DI container.
209
         * @param Options_Helper             $options           The options helper.
210
         * @param Request_Helper             $request           The request helper.
211
         * @param Helpers_Surface            $helpers           The helpers surface.
212
         * @param WPSEO_Replace_Vars         $replace_vars      The replace vars helper.
213
         */
214
        public function __construct(
215
                Meta_Tags_Context_Memoizer $context_memoizer,
216
                ContainerInterface $service_container,
217
                Options_Helper $options,
218
                Request_Helper $request,
219
                Helpers_Surface $helpers,
220
                WPSEO_Replace_Vars $replace_vars
221
        ) {
222
                $this->container        = $service_container;
223
                $this->context_memoizer = $context_memoizer;
224
                $this->options          = $options;
225
                $this->request          = $request;
226
                $this->helpers          = $helpers;
227
                $this->replace_vars     = $replace_vars;
228
        }
229

230
        /**
231
         * Registers the appropriate hooks to show the SEO metadata on the frontend.
232
         *
233
         * Removes some actions to remove metadata that WordPress shows on the frontend,
234
         * to avoid duplicate and/or mismatched metadata.
235
         *
236
         * @return void
237
         */
238
        public function register_hooks() {
2✔
239
                \add_filter( 'render_block', [ $this, 'query_loop_next_prev' ], 1, 2 );
2✔
240

241
                \add_action( 'wp_head', [ $this, 'call_wpseo_head' ], 1 );
2✔
242
                // Filter the title for compatibility with other plugins and themes.
243
                \add_filter( 'wp_title', [ $this, 'filter_title' ], 15 );
2✔
244
                // Filter the title for compatibility with block-based themes.
245
                \add_filter( 'pre_get_document_title', [ $this, 'filter_title' ], 15 );
2✔
246

247
                // Removes our robots presenter from the list when wp_robots is handling this.
248
                \add_filter( 'wpseo_frontend_presenter_classes', [ $this, 'filter_robots_presenter' ] );
2✔
249

250
                \add_action( 'wpseo_head', [ $this, 'present_head' ], -9999 );
2✔
251

252
                \remove_action( 'wp_head', 'rel_canonical' );
2✔
253
                \remove_action( 'wp_head', 'index_rel_link' );
2✔
254
                \remove_action( 'wp_head', 'start_post_rel_link' );
2✔
255
                \remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
2✔
256
                \remove_action( 'wp_head', 'noindex', 1 );
2✔
257
                \remove_action( 'wp_head', '_wp_render_title_tag', 1 );
2✔
258
                \remove_action( 'wp_head', '_block_template_render_title_tag', 1 );
2✔
259
                \remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
2✔
260
        }
1✔
261

262
        /**
263
         * Filters the title, mainly used for compatibility reasons.
264
         *
265
         * @return string
266
         */
267
        public function filter_title() {
×
268
                $context = $this->context_memoizer->for_current_page();
×
269

270
                $title_presenter = new Title_Presenter();
×
271

272
                /** This filter is documented in src/integrations/front-end-integration.php */
273
                $title_presenter->presentation = \apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context );
×
274
                $title_presenter->replace_vars = $this->replace_vars;
×
275
                $title_presenter->helpers      = $this->helpers;
×
276

277
                \remove_filter( 'pre_get_document_title', [ $this, 'filter_title' ], 15 );
×
278
                $title = \esc_html( $title_presenter->get() );
×
279
                \add_filter( 'pre_get_document_title', [ $this, 'filter_title' ], 15 );
×
280

281
                return $title;
×
282
        }
283

284
        /**
285
         * Filters the next and prev links in the query loop block.
286
         *
287
         * @param string                   $html  The HTML output.
288
         * @param array<string|array|null> $block The block.
289
         * @return string The filtered HTML output.
290
         */
291
        public function query_loop_next_prev( $html, $block ) {
6✔
292
                if ( $block['blockName'] === 'core/query' ) {
6✔
293
                        // Check that the query does not inherit the main query.
294
                        if ( isset( $block['attrs']['query']['inherit'] ) && ! $block['attrs']['query']['inherit'] ) {
2✔
295
                                \add_filter( 'wpseo_adjacent_rel_url', [ $this, 'adjacent_rel_url' ], 1, 3 );
2✔
296
                        }
297
                }
298

299
                if ( $block['blockName'] === 'core/query-pagination-next' ) {
6✔
300
                        $this->next = $html;
2✔
301
                }
302

303
                if ( $block['blockName'] === 'core/query-pagination-previous' ) {
6✔
304
                        $this->prev = $html;
2✔
305
                }
306

307
                return $html;
6✔
308
        }
309

310
        /**
311
         * Returns correct adjacent pages when Query loop block does not inherit query from template.
312
         *
313
         * @param string                      $link         The current link.
314
         * @param string                      $rel          Link relationship, prev or next.
315
         * @param Indexable_Presentation|null $presentation The indexable presentation.
316
         *
317
         * @return string The correct link.
318
         */
UNCOV
319
        public function adjacent_rel_url( $link, $rel, $presentation = null ) {
×
UNCOV
320
                if ( $link === \home_url( '/' ) ) {
×
321
                        return $link;
×
322
                }
323

UNCOV
324
                if ( ( $rel === 'next' || $rel === 'prev' ) && ( ! \is_null( $this->$rel ) ) ) {
×
325
                        // Reconstruct url if it's relative.
UNCOV
326
                        if ( \class_exists( WP_HTML_Tag_Processor::class ) ) {
×
UNCOV
327
                                $processor = new WP_HTML_Tag_Processor( $this->$rel );
×
UNCOV
328
                                while ( $processor->next_tag( [ 'tag_name' => 'a' ] ) ) {
×
UNCOV
329
                                        $href = $processor->get_attribute( 'href' );
×
UNCOV
330
                                        if ( $href && \strpos( $href, '/' ) === 0 ) {
×
UNCOV
331
                                                return $presentation->permalink . \substr( $href, 1 );
×
332
                                        }
333
                                }
334
                        }
335
                }
336

UNCOV
337
                return $link;
×
338
        }
339

340
        /**
341
         * Filters our robots presenter, but only when wp_robots is attached to the wp_head action.
342
         *
343
         * @param array<string> $presenters The presenters for current page.
344
         *
345
         * @return array<string> The filtered presenters.
346
         */
347
        public function filter_robots_presenter( $presenters ) {
8✔
348
                if ( ! \function_exists( 'wp_robots' ) ) {
8✔
349
                        return $presenters;
×
350
                }
351

352
                if ( ! \has_action( 'wp_head', 'wp_robots' ) ) {
8✔
353
                        return $presenters;
4✔
354
                }
355

356
                if ( $this->request->is_rest_request() ) {
4✔
357
                        return $presenters;
2✔
358
                }
359

360
                return \array_diff( $presenters, [ 'Yoast\\WP\\SEO\\Presenters\\Robots_Presenter' ] );
2✔
361
        }
362

363
        /**
364
         * Presents the head in the front-end. Resets wp_query if it's not the main query.
365
         *
366
         * @codeCoverageIgnore It just calls a WordPress function.
367
         *
368
         * @return void
369
         */
370
        public function call_wpseo_head() {
371
                global $wp_query;
372

373
                $old_wp_query = $wp_query;
374
                // phpcs:ignore WordPress.WP.DiscouragedFunctions.wp_reset_query_wp_reset_query -- Reason: The recommended function, wp_reset_postdata, doesn't reset wp_query.
375
                \wp_reset_query();
376

377
                \do_action( 'wpseo_head' );
378

379
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Reason: we have to restore the query.
380
                $GLOBALS['wp_query'] = $old_wp_query;
381
        }
382

383
        /**
384
         * Echoes all applicable presenters for a page.
385
         *
386
         * @return void
387
         */
388
        public function present_head() {
2✔
389
                $context    = $this->context_memoizer->for_current_page();
2✔
390
                $presenters = $this->get_presenters( $context->page_type, $context );
2✔
391

392
                /**
393
                 * Filter 'wpseo_frontend_presentation' - Allow filtering the presentation used to output our meta values.
394
                 *
395
                 * @param Indexable_Presention $presentation The indexable presentation.
396
                 * @param Meta_Tags_Context    $context      The meta tags context for the current page.
397
                 */
398
                $presentation = \apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context );
2✔
399

400
                echo \PHP_EOL;
2✔
401
                foreach ( $presenters as $presenter ) {
2✔
402
                        $presenter->presentation = $presentation;
2✔
403
                        $presenter->helpers      = $this->helpers;
2✔
404
                        $presenter->replace_vars = $this->replace_vars;
2✔
405

406
                        $output = $presenter->present();
2✔
407
                        if ( ! empty( $output ) ) {
2✔
408
                                // phpcs:ignore WordPress.Security.EscapeOutput -- Presenters are responsible for correctly escaping their output.
409
                                echo "\t" . $output . \PHP_EOL;
2✔
410
                        }
411
                }
412
                echo \PHP_EOL . \PHP_EOL;
2✔
413
        }
1✔
414

415
        /**
416
         * Returns all presenters for this page.
417
         *
418
         * @param string                 $page_type The page type.
419
         * @param Meta_Tags_Context|null $context   The meta tags context for the current page.
420
         *
421
         * @return Abstract_Indexable_Presenter[] The presenters.
422
         */
423
        public function get_presenters( $page_type, $context = null ) {
14✔
424
                if ( \is_null( $context ) ) {
14✔
425
                        $context = $this->context_memoizer->for_current_page();
14✔
426
                }
427

428
                $needed_presenters = $this->get_needed_presenters( $page_type );
14✔
429

430
                $callback   = static function ( $presenter ) {
7✔
431
                        if ( ! \class_exists( $presenter ) ) {
14✔
432
                                return null;
×
433
                        }
434
                        return new $presenter();
14✔
435
                };
14✔
436
                $presenters = \array_filter( \array_map( $callback, $needed_presenters ) );
14✔
437

438
                /**
439
                 * Filter 'wpseo_frontend_presenters' - Allow filtering the presenter instances in or out of the request.
440
                 *
441
                 * @param Abstract_Indexable_Presenter[] $presenters List of presenter instances.
442
                 * @param Meta_Tags_Context              $context    The meta tags context for the current page.
443
                 */
444
                $presenter_instances = \apply_filters( 'wpseo_frontend_presenters', $presenters, $context );
14✔
445

446
                if ( ! \is_array( $presenter_instances ) ) {
14✔
447
                        $presenter_instances = $presenters;
×
448
                }
449

450
                $is_presenter_callback = static function ( $presenter_instance ) {
7✔
451
                        return $presenter_instance instanceof Abstract_Indexable_Presenter;
14✔
452
                };
14✔
453
                $presenter_instances   = \array_filter( $presenter_instances, $is_presenter_callback );
14✔
454

455
                return \array_merge(
14✔
456
                        [ new Marker_Open_Presenter() ],
14✔
457
                        $presenter_instances,
14✔
458
                        [ new Marker_Close_Presenter() ]
14✔
459
                );
7✔
460
        }
461

462
        /**
463
         * Generate the array of presenters we need for the current request.
464
         *
465
         * @param string $page_type The page type we're retrieving presenters for.
466
         *
467
         * @return string[] The presenters.
468
         */
469
        private function get_needed_presenters( $page_type ) {
14✔
470
                $presenters = $this->get_presenters_for_page_type( $page_type );
14✔
471

472
                $presenters = $this->maybe_remove_title_presenter( $presenters );
14✔
473

474
                $callback   = static function ( $presenter ) {
7✔
475
                        return "Yoast\WP\SEO\Presenters\\{$presenter}_Presenter";
14✔
476
                };
14✔
477
                $presenters = \array_map( $callback, $presenters );
14✔
478

479
                /**
480
                 * Filter 'wpseo_frontend_presenter_classes' - Allow filtering presenters in or out of the request.
481
                 *
482
                 * @param array  $presenters List of presenters.
483
                 * @param string $page_type  The current page type.
484
                 */
485
                $presenters = \apply_filters( 'wpseo_frontend_presenter_classes', $presenters, $page_type );
14✔
486

487
                return $presenters;
14✔
488
        }
489

490
        /**
491
         * Filters the presenters based on the page type.
492
         *
493
         * @param string $page_type The page type.
494
         *
495
         * @return string[] The presenters.
496
         */
497
        private function get_presenters_for_page_type( $page_type ) {
14✔
498
                if ( $page_type === 'Error_Page' ) {
14✔
499
                        $presenters = $this->base_presenters;
8✔
500
                        if ( $this->options->get( 'opengraph' ) === true ) {
8✔
501
                                $presenters = \array_merge( $presenters, $this->open_graph_error_presenters );
8✔
502
                        }
503
                        return \array_merge( $presenters, $this->closing_presenters );
8✔
504
                }
505

506
                $presenters = $this->get_all_presenters();
6✔
507
                if ( \in_array( $page_type, [ 'Static_Home_Page', 'Home_Page' ], true ) ) {
6✔
508
                        $presenters = \array_merge( $presenters, $this->webmaster_verification_presenters );
2✔
509
                }
510

511
                // Filter out the presenters only needed for singular pages on non-singular pages.
512
                if ( ! \in_array( $page_type, [ 'Post_Type', 'Static_Home_Page' ], true ) ) {
6✔
513
                        $presenters = \array_diff( $presenters, $this->singular_presenters );
2✔
514
                }
515

516
                // Filter out `twitter:data` presenters for static home pages.
517
                if ( $page_type === 'Static_Home_Page' ) {
6✔
518
                        $presenters = \array_diff( $presenters, $this->slack_presenters );
2✔
519
                }
520

521
                return $presenters;
6✔
522
        }
523

524
        /**
525
         * Returns a list of all available presenters based on settings.
526
         *
527
         * @return string[] The presenters.
528
         */
529
        private function get_all_presenters() {
6✔
530
                $presenters = \array_merge( $this->base_presenters, $this->indexing_directive_presenters );
6✔
531
                if ( $this->options->get( 'opengraph' ) === true ) {
6✔
532
                        $presenters = \array_merge( $presenters, $this->open_graph_presenters );
6✔
533
                }
534
                if ( $this->options->get( 'twitter' ) === true && \apply_filters( 'wpseo_output_twitter_card', true ) !== false ) {
6✔
535
                        $presenters = \array_merge( $presenters, $this->twitter_card_presenters );
6✔
536
                }
537
                if ( $this->options->get( 'enable_enhanced_slack_sharing' ) === true && \apply_filters( 'wpseo_output_enhanced_slack_data', true ) !== false ) {
6✔
538
                        $presenters = \array_merge( $presenters, $this->slack_presenters );
6✔
539
                }
540

541
                return \array_merge( $presenters, $this->closing_presenters );
6✔
542
        }
543

544
        /**
545
         * Whether the title presenter should be removed.
546
         *
547
         * @return bool True when the title presenter should be removed, false otherwise.
548
         */
549
        public function should_title_presenter_be_removed() {
2✔
550
                return ! \get_theme_support( 'title-tag' ) && ! $this->options->get( 'forcerewritetitle', false );
2✔
551
        }
552

553
        /**
554
         * Checks if the Title presenter needs to be removed.
555
         *
556
         * @param string[] $presenters The presenters.
557
         *
558
         * @return string[] The presenters.
559
         */
560
        private function maybe_remove_title_presenter( $presenters ) {
×
561
                // Do not remove the title if we're on a REST request.
562
                if ( $this->request->is_rest_request() ) {
×
563
                        return $presenters;
×
564
                }
565

566
                // Remove the title presenter if the theme is hardcoded to output a title tag so we don't have two title tags.
567
                if ( $this->should_title_presenter_be_removed() ) {
×
568
                        $presenters = \array_diff( $presenters, [ 'Title' ] );
×
569
                }
570

571
                return $presenters;
×
572
        }
573
}
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

© 2025 Coveralls, Inc