PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / wp-seo-main.php

wp-seo-main.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at wp-seo-main.php

651 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Main
6 */
7
8 if ( ! function_exists( 'add_filter' ) ) {
9 header( 'Status: 403 Forbidden' );
10 header( 'HTTP/1.1 403 Forbidden' );
11 exit();
12 }
13
14 /**
15 * {@internal Nobody should be able to overrule the real version number as this can cause
16 * serious issues with the options, so no if ( ! defined() ).}}
17 */
18 define( 'WPSEO_VERSION', '18.2' );
19
20
21 if ( ! defined( 'WPSEO_PATH' ) ) {
22 define( 'WPSEO_PATH', plugin_dir_path( WPSEO_FILE ) );
23 }
24
25 if ( ! defined( 'WPSEO_BASENAME' ) ) {
26 define( 'WPSEO_BASENAME', plugin_basename( WPSEO_FILE ) );
27 }
28
29 /*
30 * {@internal The prefix constants are used to build prefixed versions of dependencies.
31 * These should not be changed on run-time, thus missing the ! defined() check.}}
32 */
33 define( 'YOAST_VENDOR_NS_PREFIX', 'YoastSEO_Vendor' );
34 define( 'YOAST_VENDOR_DEFINE_PREFIX', 'YOASTSEO_VENDOR__' );
35 define( 'YOAST_VENDOR_PREFIX_DIRECTORY', 'vendor_prefixed' );
36
37 define( 'YOAST_SEO_PHP_REQUIRED', '5.6' );
38 define( 'YOAST_SEO_WP_TESTED', '5.9' );
39 define( 'YOAST_SEO_WP_REQUIRED', '5.6' );
40
41 if ( ! defined( 'WPSEO_NAMESPACES' ) ) {
42 define( 'WPSEO_NAMESPACES', true );
43 }
44
45
46 /* ***************************** CLASS AUTOLOADING *************************** */
47
48 /**
49 * Autoload our class files.
50 *
51 * @param string $class Class name.
52 *
53 * @return void
54 */
55 function wpseo_auto_load( $class ) {
56 static $classes = null;
57
58 if ( $classes === null ) {
59 $classes = [
60 'wp_list_table' => ABSPATH . 'wp-admin/includes/class-wp-list-table.php',
61 'walker_category' => ABSPATH . 'wp-includes/category-template.php',
62 ];
63 }
64
65 $cn = strtolower( $class );
66
67 if ( ! class_exists( $class ) && isset( $classes[ $cn ] ) ) {
68 require_once $classes[ $cn ];
69 }
70 }
71
72 $yoast_autoload_file = WPSEO_PATH . 'vendor/autoload.php';
73
74 if ( is_readable( $yoast_autoload_file ) ) {
75 $yoast_autoloader = require $yoast_autoload_file;
76 }
77 elseif ( ! class_exists( 'WPSEO_Options' ) ) { // Still checking since might be site-level autoload R.
78 add_action( 'admin_init', 'yoast_wpseo_missing_autoload', 1 );
79
80 return;
81 }
82
83 if ( function_exists( 'spl_autoload_register' ) ) {
84 spl_autoload_register( 'wpseo_auto_load' );
85 }
86 require_once WPSEO_PATH . 'src/functions.php';
87
88 /* ********************* DEFINES DEPENDING ON AUTOLOADED CODE ********************* */
89
90 /**
91 * Defaults to production, for safety.
92 */
93 if ( ! defined( 'YOAST_ENVIRONMENT' ) ) {
94 define( 'YOAST_ENVIRONMENT', 'production' );
95 }
96
97 if ( YOAST_ENVIRONMENT === 'development' && isset( $yoast_autoloader ) ) {
98 add_action(
99 'plugins_loaded',
100 /**
101 * Reregisters the autoloader so that Yoast SEO is at the front.
102 * This prevents conflicts with the development versions of our addons.
103 * An anonymous function is used so we can use the autoloader variable.
104 * As this is only loaded in development removing this action is not a concern.
105 *
106 * @return void
107 */
108 static function() use ( $yoast_autoloader ) {
109 $yoast_autoloader->unregister();
110 $yoast_autoloader->register( true );
111 },
112 1
113 );
114 }
115
116 /**
117 * Only use minified assets when we are in a production environment.
118 */
119 if ( ! defined( 'WPSEO_CSSJS_SUFFIX' ) ) {
120 define( 'WPSEO_CSSJS_SUFFIX', ( YOAST_ENVIRONMENT !== 'development' ) ? '.min' : '' );
121 }
122
123 /* ***************************** PLUGIN (DE-)ACTIVATION *************************** */
124
125 /**
126 * Run single site / network-wide activation of the plugin.
127 *
128 * @param bool $networkwide Whether the plugin is being activated network-wide.
129 */
130 function wpseo_activate( $networkwide = false ) {
131 if ( ! is_multisite() || ! $networkwide ) {
132 _wpseo_activate();
133 }
134 else {
135 /* Multi-site network activation - activate the plugin for all blogs. */
136 wpseo_network_activate_deactivate( true );
137 }
138
139 // This is done so that the 'uninstall_{$file}' is triggered.
140 register_uninstall_hook( WPSEO_FILE, '__return_false' );
141 }
142
143 /**
144 * Run single site / network-wide de-activation of the plugin.
145 *
146 * @param bool $networkwide Whether the plugin is being de-activated network-wide.
147 */
148 function wpseo_deactivate( $networkwide = false ) {
149 if ( ! is_multisite() || ! $networkwide ) {
150 _wpseo_deactivate();
151 }
152 else {
153 /* Multi-site network activation - de-activate the plugin for all blogs. */
154 wpseo_network_activate_deactivate( false );
155 }
156 }
157
158 /**
159 * Run network-wide (de-)activation of the plugin.
160 *
161 * @param bool $activate True for plugin activation, false for de-activation.
162 */
163 function wpseo_network_activate_deactivate( $activate = true ) {
164 global $wpdb;
165
166 $network_blogs = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d", $wpdb->siteid ) );
167
168 if ( is_array( $network_blogs ) && $network_blogs !== [] ) {
169 foreach ( $network_blogs as $blog_id ) {
170 switch_to_blog( $blog_id );
171
172 if ( $activate === true ) {
173 _wpseo_activate();
174 }
175 else {
176 _wpseo_deactivate();
177 }
178
179 restore_current_blog();
180 }
181 }
182 }
183
184 /**
185 * Runs on activation of the plugin.
186 */
187 function _wpseo_activate() {
188 require_once WPSEO_PATH . 'inc/wpseo-functions.php';
189 require_once WPSEO_PATH . 'inc/class-wpseo-installation.php';
190
191 wpseo_load_textdomain(); // Make sure we have our translations available for the defaults.
192
193 new WPSEO_Installation();
194
195 WPSEO_Options::get_instance();
196 if ( ! is_multisite() ) {
197 WPSEO_Options::initialize();
198 }
199 else {
200 WPSEO_Options::maybe_set_multisite_defaults( true );
201 }
202 WPSEO_Options::ensure_options_exist();
203
204 if ( is_multisite() && ms_is_switched() ) {
205 delete_option( 'rewrite_rules' );
206 }
207 else {
208 if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
209 // Constructor has side effects so this registers all hooks.
210 $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite();
211 }
212 add_action( 'shutdown', 'flush_rewrite_rules' );
213 }
214
215 // Reset tracking to be disabled by default.
216 if ( ! YoastSEO()->helpers->product->is_premium() ) {
217 WPSEO_Options::set( 'tracking', false );
218 }
219
220 WPSEO_Options::set( 'indexing_reason', 'first_install' );
221 WPSEO_Options::set( 'first_time_install', true );
222 if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
223 WPSEO_Options::set( 'should_redirect_after_install_free', true );
224 }
225 else {
226 WPSEO_Options::set( 'activation_redirect_timestamp_free', \time() );
227 }
228
229 do_action( 'wpseo_register_roles' );
230 WPSEO_Role_Manager_Factory::get()->add();
231
232 do_action( 'wpseo_register_capabilities' );
233 WPSEO_Capability_Manager_Factory::get()->add();
234
235 // Clear cache so the changes are obvious.
236 WPSEO_Utils::clear_cache();
237
238 // Schedule cronjob when it doesn't exists on activation.
239 $wpseo_ryte = new WPSEO_Ryte();
240 $wpseo_ryte->activate_hooks();
241
242 do_action( 'wpseo_activate' );
243 }
244
245 /**
246 * On deactivation, flush the rewrite rules so XML sitemaps stop working.
247 */
248 function _wpseo_deactivate() {
249 require_once WPSEO_PATH . 'inc/wpseo-functions.php';
250
251 if ( is_multisite() && ms_is_switched() ) {
252 delete_option( 'rewrite_rules' );
253 }
254 else {
255 add_action( 'shutdown', 'flush_rewrite_rules' );
256 }
257
258 // Register capabilities, to make sure they are cleaned up.
259 do_action( 'wpseo_register_roles' );
260 do_action( 'wpseo_register_capabilities' );
261
262 // Clean up capabilities.
263 WPSEO_Role_Manager_Factory::get()->remove();
264 WPSEO_Capability_Manager_Factory::get()->remove();
265
266 // Clear cache so the changes are obvious.
267 WPSEO_Utils::clear_cache();
268
269 do_action( 'wpseo_deactivate' );
270 }
271
272 /**
273 * Run wpseo activation routine on creation / activation of a multisite blog if WPSEO is activated
274 * network-wide.
275 *
276 * Will only be called by multisite actions.
277 *
278 * {@internal Unfortunately will fail if the plugin is in the must-use directory.
279 * {@link https://core.trac.wordpress.org/ticket/24205} }}
280 *
281 * @param int|WP_Site $blog_id Blog ID.
282 */
283 function wpseo_on_activate_blog( $blog_id ) {
284 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
285 require_once ABSPATH . 'wp-admin/includes/plugin.php';
286 }
287
288 if ( $blog_id instanceof WP_Site ) {
289 $blog_id = (int) $blog_id->blog_id;
290 }
291
292 if ( is_plugin_active_for_network( WPSEO_BASENAME ) ) {
293 switch_to_blog( $blog_id );
294 wpseo_activate( false );
295 restore_current_blog();
296 }
297 }
298
299 /* ***************************** PLUGIN LOADING *************************** */
300
301 /**
302 * Load translations.
303 */
304 function wpseo_load_textdomain() {
305 $wpseo_path = str_replace( '\\', '/', WPSEO_PATH );
306 $mu_path = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
307
308 if ( stripos( $wpseo_path, $mu_path ) !== false ) {
309 load_muplugin_textdomain( 'wordpress-seo', dirname( WPSEO_BASENAME ) . '/languages/' );
310 }
311 else {
312 load_plugin_textdomain( 'wordpress-seo', false, dirname( WPSEO_BASENAME ) . '/languages/' );
313 }
314 }
315
316 add_action( 'plugins_loaded', 'wpseo_load_textdomain' );
317
318
319 /**
320 * On plugins_loaded: load the minimum amount of essential files for this plugin.
321 */
322 function wpseo_init() {
323 require_once WPSEO_PATH . 'inc/wpseo-functions.php';
324 require_once WPSEO_PATH . 'inc/wpseo-functions-deprecated.php';
325
326 // Make sure our option and meta value validation routines and default values are always registered and available.
327 WPSEO_Options::get_instance();
328 WPSEO_Meta::init();
329
330 if ( version_compare( WPSEO_Options::get( 'version', 1 ), WPSEO_VERSION, '<' ) ) {
331 if ( function_exists( 'opcache_reset' ) ) {
332 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Prevent notices when opcache.restrict_api is set.
333 @opcache_reset();
334 }
335
336 new WPSEO_Upgrade();
337 // Get a cleaned up version of the $options.
338 }
339
340 if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
341 $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite();
342 }
343
344 if ( WPSEO_Options::get( 'enable_xml_sitemap' ) === true ) {
345 $GLOBALS['wpseo_sitemaps'] = new WPSEO_Sitemaps();
346 }
347
348 if ( ! wp_doing_ajax() ) {
349 require_once WPSEO_PATH . 'inc/wpseo-non-ajax-functions.php';
350 }
351
352 // Init it here because the filter must be present on the frontend as well or it won't work in the customizer.
353 new WPSEO_Customizer();
354
355 $integrations = [];
356 $integrations[] = new WPSEO_Slug_Change_Watcher();
357
358 foreach ( $integrations as $integration ) {
359 $integration->register_hooks();
360 }
361
362 // Loading Ryte integration.
363 $wpseo_ryte = new WPSEO_Ryte();
364 $wpseo_ryte->register_hooks();
365 }
366
367 /**
368 * Loads the rest api endpoints.
369 */
370 function wpseo_init_rest_api() {
371 // We can't do anything when requirements are not met.
372 if ( ! WPSEO_Utils::is_api_available() ) {
373 return;
374 }
375
376 // Boot up REST API.
377 $statistics_service = new WPSEO_Statistics_Service( new WPSEO_Statistics() );
378
379 $endpoints = [];
380 $endpoints[] = new WPSEO_Endpoint_File_Size( new WPSEO_File_Size_Service() );
381 $endpoints[] = new WPSEO_Endpoint_Statistics( $statistics_service );
382
383 foreach ( $endpoints as $endpoint ) {
384 $endpoint->register();
385 }
386 }
387
388 /**
389 * Used to load the required files on the plugins_loaded hook, instead of immediately.
390 */
391 function wpseo_admin_init() {
392 new WPSEO_Admin_Init();
393 }
394
395 /**
396 * Initialize the WP-CLI integration.
397 *
398 * The WP-CLI integration needs PHP 5.3 support, which should be automatically
399 * enforced by the check for the WP_CLI constant. As WP-CLI itself only runs
400 * on PHP 5.3+, the constant should only be set when requirements are met.
401 */
402 function wpseo_cli_init() {
403 if ( YoastSEO()->helpers->product->is_premium() ) {
404 WP_CLI::add_command(
405 'yoast redirect list',
406 'WPSEO_CLI_Redirect_List_Command',
407 [ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
408 );
409
410 WP_CLI::add_command(
411 'yoast redirect create',
412 'WPSEO_CLI_Redirect_Create_Command',
413 [ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
414 );
415
416 WP_CLI::add_command(
417 'yoast redirect update',
418 'WPSEO_CLI_Redirect_Update_Command',
419 [ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
420 );
421
422 WP_CLI::add_command(
423 'yoast redirect delete',
424 'WPSEO_CLI_Redirect_Delete_Command',
425 [ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
426 );
427
428 WP_CLI::add_command(
429 'yoast redirect has',
430 'WPSEO_CLI_Redirect_Has_Command',
431 [ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
432 );
433
434 WP_CLI::add_command(
435 'yoast redirect follow',
436 'WPSEO_CLI_Redirect_Follow_Command',
437 [ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
438 );
439 }
440 }
441
442 /* ***************************** BOOTSTRAP / HOOK INTO WP *************************** */
443 $spl_autoload_exists = function_exists( 'spl_autoload_register' );
444 $filter_exists = function_exists( 'filter_input' );
445
446 if ( ! $spl_autoload_exists ) {
447 add_action( 'admin_init', 'yoast_wpseo_missing_spl', 1 );
448 }
449
450 if ( ! $filter_exists ) {
451 add_action( 'admin_init', 'yoast_wpseo_missing_filter', 1 );
452 }
453
454 if ( ! wp_installing() && ( $spl_autoload_exists && $filter_exists ) ) {
455 add_action( 'plugins_loaded', 'wpseo_init', 14 );
456 add_action( 'rest_api_init', 'wpseo_init_rest_api' );
457
458 if ( is_admin() ) {
459
460 new Yoast_Notifications();
461
462 $yoast_addon_manager = new WPSEO_Addon_Manager();
463 $yoast_addon_manager->register_hooks();
464
465 if ( wp_doing_ajax() ) {
466 require_once WPSEO_PATH . 'admin/ajax.php';
467
468 // Plugin conflict ajax hooks.
469 new Yoast_Plugin_Conflict_Ajax();
470
471 if ( filter_input( INPUT_POST, 'action' ) === 'inline-save' ) {
472 add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
473 }
474 }
475 else {
476 add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
477 }
478 }
479
480 add_action( 'plugins_loaded', 'load_yoast_notifications' );
481
482 if ( defined( 'WP_CLI' ) && WP_CLI ) {
483 add_action( 'plugins_loaded', 'wpseo_cli_init', 20 );
484 }
485
486 add_action( 'init', [ 'WPSEO_Replace_Vars', 'setup_statics_once' ] );
487
488 // Initializes the Yoast indexables for the first time.
489 YoastSEO();
490
491 /**
492 * Action called when the Yoast SEO plugin file has loaded.
493 */
494 do_action( 'wpseo_loaded' );
495 }
496
497 // Activation and deactivation hook.
498 register_activation_hook( WPSEO_FILE, 'wpseo_activate' );
499 register_deactivation_hook( WPSEO_FILE, 'wpseo_deactivate' );
500
501 // Wpmu_new_blog has been deprecated in 5.1 and replaced by wp_insert_site.
502 global $wp_version;
503 if ( version_compare( $wp_version, '5.1', '<' ) ) {
504 add_action( 'wpmu_new_blog', 'wpseo_on_activate_blog' );
505 }
506 else {
507 add_action( 'wp_initialize_site', 'wpseo_on_activate_blog', 99 );
508 }
509
510 add_action( 'activate_blog', 'wpseo_on_activate_blog' );
511
512 // Registers SEO capabilities.
513 $wpseo_register_capabilities = new WPSEO_Register_Capabilities();
514 $wpseo_register_capabilities->register_hooks();
515
516 // Registers SEO roles.
517 $wpseo_register_capabilities = new WPSEO_Register_Roles();
518 $wpseo_register_capabilities->register_hooks();
519
520 /**
521 * Wraps for notifications center class.
522 */
523 function load_yoast_notifications() {
524 // Init Yoast_Notification_Center class.
525 Yoast_Notification_Center::get();
526 }
527
528
529 /**
530 * Throw an error if the PHP SPL extension is disabled (prevent white screens) and self-deactivate plugin.
531 *
532 * @since 1.5.4
533 *
534 * @return void
535 */
536 function yoast_wpseo_missing_spl() {
537 if ( is_admin() ) {
538 add_action( 'admin_notices', 'yoast_wpseo_missing_spl_notice' );
539
540 yoast_wpseo_self_deactivate();
541 }
542 }
543
544 /**
545 * Returns the notice in case of missing spl extension.
546 */
547 function yoast_wpseo_missing_spl_notice() {
548 $message = esc_html__( 'The Standard PHP Library (SPL) extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
549 yoast_wpseo_activation_failed_notice( $message );
550 }
551
552 /**
553 * Throw an error if the Composer autoload is missing and self-deactivate plugin.
554 *
555 * @return void
556 */
557 function yoast_wpseo_missing_autoload() {
558 if ( is_admin() ) {
559 add_action( 'admin_notices', 'yoast_wpseo_missing_autoload_notice' );
560
561 yoast_wpseo_self_deactivate();
562 }
563 }
564
565 /**
566 * Returns the notice in case of missing Composer autoload.
567 */
568 function yoast_wpseo_missing_autoload_notice() {
569 /* translators: %1$s expands to Yoast SEO, %2$s / %3$s: links to the installation manual in the Readme for the Yoast SEO code repository on GitHub */
570 $message = esc_html__( 'The %1$s plugin installation is incomplete. Please refer to %2$sinstallation instructions%3$s.', 'wordpress-seo' );
571 $message = sprintf( $message, 'Yoast SEO', '<a href="https://github.com/Yoast/wordpress-seo#installation">', '</a>' );
572 yoast_wpseo_activation_failed_notice( $message );
573 }
574
575 /**
576 * Throw an error if the filter extension is disabled (prevent white screens) and self-deactivate plugin.
577 *
578 * @since 2.0
579 *
580 * @return void
581 */
582 function yoast_wpseo_missing_filter() {
583 if ( is_admin() ) {
584 add_action( 'admin_notices', 'yoast_wpseo_missing_filter_notice' );
585
586 yoast_wpseo_self_deactivate();
587 }
588 }
589
590 /**
591 * Returns the notice in case of missing filter extension.
592 */
593 function yoast_wpseo_missing_filter_notice() {
594 $message = esc_html__( 'The filter extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
595 yoast_wpseo_activation_failed_notice( $message );
596 }
597
598 /**
599 * Echo's the Activation failed notice with any given message.
600 *
601 * @param string $message Message string.
602 */
603 function yoast_wpseo_activation_failed_notice( $message ) {
604 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This function is only called in 3 places that are safe.
605 echo '<div class="error"><p>' . esc_html__( 'Activation failed:', 'wordpress-seo' ) . ' ' . strip_tags( $message, '<a>' ) . '</p></div>';
606 }
607
608 /**
609 * The method will deactivate the plugin, but only once, done by the static $is_deactivated.
610 */
611 function yoast_wpseo_self_deactivate() {
612 static $is_deactivated;
613
614 if ( $is_deactivated === null ) {
615 $is_deactivated = true;
616 deactivate_plugins( WPSEO_BASENAME );
617 if ( isset( $_GET['activate'] ) ) {
618 unset( $_GET['activate'] );
619 }
620 }
621 }
622
623 /* ********************* DEPRECATED METHODS ********************* */
624
625 /**
626 * Instantiate the different social classes on the frontend.
627 *
628 * @deprecated 14.0
629 * @codeCoverageIgnore
630 */
631 function wpseo_frontend_head_init() {
632 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
633 }
634
635 /**
636 * Used to load the required files on the plugins_loaded hook, instead of immediately.
637 *
638 * @deprecated 14.0
639 * @codeCoverageIgnore
640 */
641 function wpseo_frontend_init() {
642 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
643 }
644
645 /**
646 * Aliasses added in order to keep compatibility with Yoast SEO: Local.
647 */
648 class_alias( '\Yoast\WP\SEO\Initializers\Initializer_Interface', '\Yoast\WP\SEO\WordPress\Initializer' );
649 class_alias( '\Yoast\WP\SEO\Loadable_Interface', '\Yoast\WP\SEO\WordPress\Loadable' );
650 class_alias( '\Yoast\WP\SEO\Integrations\Integration_Interface', '\Yoast\WP\SEO\WordPress\Integration' );
651