front-end-integration.php
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
namespace Yoast\WP\SEO\Integrations;
use WPSEO_Replace_Vars;
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
use Yoast\WP\SEO\Context\Meta_Tags_Context;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Helpers\Request_Helper;
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
use Yoast\WP\SEO\Presenters\Abstract_Indexable_Presenter;
use Yoast\WP\SEO\Presenters\Debug\Marker_Close_Presenter;
use Yoast\WP\SEO\Presenters\Debug\Marker_Open_Presenter;
use Yoast\WP\SEO\Presenters\Title_Presenter;
use Yoast\WP\SEO\Surfaces\Helpers_Surface;
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class Front_End_Integration.
*/
class Front_End_Integration implements Integration_Interface {
/**
* The memoizer for the meta tags context.
*
* @var Meta_Tags_Context_Memoizer
*/
private $context_memoizer;
/**
* The container.
*
* @var ContainerInterface
*/
protected $container;
/**
* Represents the options helper.
*
* @var Options_Helper
*/
protected $options;
/**
* Represents the request helper.
*
* @var Request_Helper
*/
protected $request;
/**
* The helpers surface.
*
* @var Helpers_Surface
*/
protected $helpers;
/**
* The replace vars helper.
*
* @var WPSEO_Replace_Vars
*/
protected $replace_vars;
/**
* The presenters we loop through on each page load.
*
* @var string[]
*/
protected $base_presenters = [
'Title',
'Meta_Description',
'Robots',
];
/**
* The presenters we loop through on each page load.
*
* @var string[]
*/
protected $indexing_directive_presenters = [
'Canonical',
'Rel_Prev',
'Rel_Next',
];
/**
* The Open Graph specific presenters.
*
* @var string[]
*/
protected $open_graph_presenters = [
'Open_Graph\Locale',
'Open_Graph\Type',
'Open_Graph\Title',
'Open_Graph\Description',
'Open_Graph\Url',
'Open_Graph\Site_Name',
'Open_Graph\Article_Publisher',
'Open_Graph\Article_Author',
'Open_Graph\Article_Published_Time',
'Open_Graph\Article_Modified_Time',
'Open_Graph\Image',
'Meta_Author',
];
/**
* The Open Graph specific presenters that should be output on error pages.
*
* @var array
*/
protected $open_graph_error_presenters = [
'Open_Graph\Locale',
'Open_Graph\Title',
'Open_Graph\Site_Name',
];
/**
* The Twitter card specific presenters.
*
* @var string[]
*/
protected $twitter_card_presenters = [
'Twitter\Card',
'Twitter\Title',
'Twitter\Description',
'Twitter\Image',
'Twitter\Creator',
'Twitter\Site',
];
/**
* The Slack specific presenters.
*
* @var string[]
*/
protected $slack_presenters = [
'Slack\Enhanced_Data',
];
/**
* The Webmaster verification specific presenters.
*
* @var string[]
*/
protected $webmaster_verification_presenters = [
'Webmaster\Baidu',
'Webmaster\Bing',
'Webmaster\Google',
'Webmaster\Pinterest',
'Webmaster\Yandex',
];
/**
* Presenters that are only needed on singular pages.
*
* @var string[]
*/
protected $singular_presenters = [
'Meta_Author',
'Open_Graph\Article_Author',
'Open_Graph\Article_Publisher',
'Open_Graph\Article_Published_Time',
'Open_Graph\Article_Modified_Time',
'Twitter\Creator',
'Slack\Enhanced_Data',
];
/**
* The presenters we want to be last in our output.
*
* @var string[]
*/
protected $closing_presenters = [
'Schema',
];
/**
* Returns the conditionals based on which this loadable should be active.
*
* @return array The conditionals.
*/
public static function get_conditionals() {
return [ Front_End_Conditional::class ];
}
/**
* Front_End_Integration constructor.
*
* @codeCoverageIgnore It sets dependencies.
*
* @param Meta_Tags_Context_Memoizer $context_memoizer The meta tags context memoizer.
* @param ContainerInterface $service_container The DI container.
* @param Options_Helper $options The options helper.
* @param Request_Helper $request The request helper.
* @param Helpers_Surface $helpers The helpers surface.
* @param WPSEO_Replace_Vars $replace_vars The replace vars helper.
*/
public function __construct(
Meta_Tags_Context_Memoizer $context_memoizer,
ContainerInterface $service_container,
Options_Helper $options,
Request_Helper $request,
Helpers_Surface $helpers,
WPSEO_Replace_Vars $replace_vars
) {
$this->container = $service_container;
$this->context_memoizer = $context_memoizer;
$this->options = $options;
$this->request = $request;
$this->helpers = $helpers;
$this->replace_vars = $replace_vars;
}
/**
* Registers the appropriate hooks to show the SEO metadata on the frontend.
*
* Removes some actions to remove metadata that WordPress shows on the frontend,
* to avoid duplicate and/or mismatched metadata.
*/
public function register_hooks() {
\add_action( 'wp_head', [ $this, 'call_wpseo_head' ], 1 );
// Filter the title for compatibility with other plugins and themes.
\add_filter( 'wp_title', [ $this, 'filter_title' ], 15 );
// Filter the title for compatibility with block-based themes.
\add_filter( 'pre_get_document_title', [ $this, 'filter_title' ], 15 );
// Removes our robots presenter from the list when wp_robots is handling this.
\add_filter( 'wpseo_frontend_presenter_classes', [ $this, 'filter_robots_presenter' ] );
\add_action( 'wpseo_head', [ $this, 'present_head' ], -9999 );
\remove_action( 'wp_head', 'rel_canonical' );
\remove_action( 'wp_head', 'index_rel_link' );
\remove_action( 'wp_head', 'start_post_rel_link' );
\remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
\remove_action( 'wp_head', 'noindex', 1 );
\remove_action( 'wp_head', '_wp_render_title_tag', 1 );
\remove_action( 'wp_head', '_block_template_render_title_tag', 1 );
\remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
}
/**
* Filters the title, mainly used for compatibility reasons.
*
* @return string
*/
public function filter_title() {
$context = $this->context_memoizer->for_current_page();
$title_presenter = new Title_Presenter();
/** This filter is documented in src/integrations/front-end-integration.php */
$title_presenter->presentation = \apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context );
$title_presenter->replace_vars = $this->replace_vars;
$title_presenter->helpers = $this->helpers;
\remove_filter( 'pre_get_document_title', [ $this, 'filter_title' ], 15 );
$title = \esc_html( $title_presenter->get() );
\add_filter( 'pre_get_document_title', [ $this, 'filter_title' ], 15 );
return $title;
}
/**
* Filters our robots presenter, but only when wp_robots is attached to the wp_head action.
*
* @param array $presenters The presenters for current page.
*
* @return array The filtered presenters.
*/
public function filter_robots_presenter( $presenters ) {
if ( ! \function_exists( 'wp_robots' ) ) {
return $presenters;
}
if ( ! \has_action( 'wp_head', 'wp_robots' ) ) {
return $presenters;
}
if ( $this->request->is_rest_request() ) {
return $presenters;
}
return \array_diff( $presenters, [ 'Yoast\\WP\\SEO\\Presenters\\Robots_Presenter' ] );
}
/**
* Presents the head in the front-end. Resets wp_query if it's not the main query.
*
* @codeCoverageIgnore It just calls a WordPress function.
*/
public function call_wpseo_head() {
global $wp_query;
$old_wp_query = $wp_query;
// phpcs:ignore WordPress.WP.DiscouragedFunctions.wp_reset_query_wp_reset_query -- Reason: The recommended function, wp_reset_postdata, doesn't reset wp_query.
\wp_reset_query();
\do_action( 'wpseo_head' );
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Reason: we have to restore the query.
$GLOBALS['wp_query'] = $old_wp_query;
}
/**
* Echoes all applicable presenters for a page.
*/
public function present_head() {
$context = $this->context_memoizer->for_current_page();
$presenters = $this->get_presenters( $context->page_type, $context );
/**
* Filter 'wpseo_frontend_presentation' - Allow filtering the presentation used to output our meta values.
*
* @api Indexable_Presention The indexable presentation.
*/
$presentation = \apply_filters( 'wpseo_frontend_presentation', $context->presentation, $context );
echo \PHP_EOL;
foreach ( $presenters as $presenter ) {
$presenter->presentation = $presentation;
$presenter->helpers = $this->helpers;
$presenter->replace_vars = $this->replace_vars;
$output = $presenter->present();
if ( ! empty( $output ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput -- Presenters are responsible for correctly escaping their output.
echo "\t" . $output . \PHP_EOL;
}
}
echo \PHP_EOL . \PHP_EOL;
}
/**
* Returns all presenters for this page.
*
* @param string $page_type The page type.
* @param Meta_Tags_Context|null $context The meta tags context for the current page.
*
* @return Abstract_Indexable_Presenter[] The presenters.
*/
public function get_presenters( $page_type, $context = null ) {
if ( \is_null( $context ) ) {
$context = $this->context_memoizer->for_current_page();
}
$needed_presenters = $this->get_needed_presenters( $page_type );
$callback = static function( $presenter ) {
if ( ! \class_exists( $presenter ) ) {
return null;
}
return new $presenter();
};
$presenters = \array_filter( \array_map( $callback, $needed_presenters ) );
/**
* Filter 'wpseo_frontend_presenters' - Allow filtering the presenter instances in or out of the request.
*
* @param array $presenters The presenters.
* @param Meta_Tags_Context $context The meta tags context for the current page.
*
* @api Abstract_Indexable_Presenter[] List of presenter instances.
*/
$presenter_instances = \apply_filters( 'wpseo_frontend_presenters', $presenters, $context );
if ( ! \is_array( $presenter_instances ) ) {
$presenter_instances = $presenters;
}
$is_presenter_callback = static function ( $presenter_instance ) {
return $presenter_instance instanceof Abstract_Indexable_Presenter;
};
$presenter_instances = \array_filter( $presenter_instances, $is_presenter_callback );
return \array_merge(
[ new Marker_Open_Presenter() ],
$presenter_instances,
[ new Marker_Close_Presenter() ]
);
}
/**
* Generate the array of presenters we need for the current request.
*
* @param string $page_type The page type we're retrieving presenters for.
*
* @return string[] The presenters.
*/
private function get_needed_presenters( $page_type ) {
$presenters = $this->get_presenters_for_page_type( $page_type );
$presenters = $this->maybe_remove_title_presenter( $presenters );
$callback = static function ( $presenter ) {
return "Yoast\WP\SEO\Presenters\\{$presenter}_Presenter";
};
$presenters = \array_map( $callback, $presenters );
/**
* Filter 'wpseo_frontend_presenter_classes' - Allow filtering presenters in or out of the request.
*
* @param array $presenters List of presenters.
* @param string $page_type The current page type.
*/
$presenters = \apply_filters( 'wpseo_frontend_presenter_classes', $presenters, $page_type );
return $presenters;
}
/**
* Filters the presenters based on the page type.
*
* @param string $page_type The page type.
*
* @return string[] The presenters.
*/
private function get_presenters_for_page_type( $page_type ) {
if ( $page_type === 'Error_Page' ) {
$presenters = $this->base_presenters;
if ( $this->options->get( 'opengraph' ) === true ) {
$presenters = \array_merge( $presenters, $this->open_graph_error_presenters );
}
return \array_merge( $presenters, $this->closing_presenters );
}
$presenters = $this->get_all_presenters();
if ( \in_array( $page_type, [ 'Static_Home_Page', 'Home_Page' ], true ) ) {
$presenters = \array_merge( $presenters, $this->webmaster_verification_presenters );
}
// Filter out the presenters only needed for singular pages on non-singular pages.
if ( ! \in_array( $page_type, [ 'Post_Type', 'Static_Home_Page' ], true ) ) {
$presenters = \array_diff( $presenters, $this->singular_presenters );
}
return $presenters;
}
/**
* Returns a list of all available presenters based on settings.
*
* @return string[] The presenters.
*/
private function get_all_presenters() {
$presenters = \array_merge( $this->base_presenters, $this->indexing_directive_presenters );
if ( $this->options->get( 'opengraph' ) === true ) {
$presenters = \array_merge( $presenters, $this->open_graph_presenters );
}
if ( $this->options->get( 'twitter' ) === true && \apply_filters( 'wpseo_output_twitter_card', true ) !== false ) {
$presenters = \array_merge( $presenters, $this->twitter_card_presenters );
}
if ( $this->options->get( 'enable_enhanced_slack_sharing' ) === true && \apply_filters( 'wpseo_output_enhanced_slack_data', true ) !== false ) {
$presenters = \array_merge( $presenters, $this->slack_presenters );
}
return \array_merge( $presenters, $this->closing_presenters );
}
/**
* Whether the title presenter should be removed.
*
* @return bool True when the title presenter should be removed, false otherwise.
*/
public function should_title_presenter_be_removed() {
return ! \get_theme_support( 'title-tag' ) && ! $this->options->get( 'forcerewritetitle', false );
}
/**
* Checks if the Title presenter needs to be removed.
*
* @param string[] $presenters The presenters.
*
* @return string[] The presenters.
*/
private function maybe_remove_title_presenter( $presenters ) {
// Do not remove the title if we're on a REST request.
if ( $this->request->is_rest_request() ) {
return $presenters;
}
// Remove the title presenter if the theme is hardcoded to output a title tag so we don't have two title tags.
if ( $this->should_title_presenter_be_removed() ) {
$presenters = \array_diff( $presenters, [ 'Title' ] );
}
return $presenters;
}
}