PluginProbe ʕ •ᴥ•ʔ
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 21.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v21.0
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 19.1 19.10 19.11 19.12 19.13 19.14 19.2 19.3 19.4 19.5 19.5.1 19.6 19.6.1 19.7 19.7.1 19.7.2 19.8 19.9 20.0 20.1 20.10 20.11 20.12 20.13 20.2 20.2.1 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21.0 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.8.1 21.9 21.9.1 22.0 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 23.0 23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 24.0 24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.8.1 24.9 25.0 25.1 25.2 25.3 25.3.1 25.4 25.5 25.6 25.7 25.8 25.9 26.0 26.1 26.1.1 26.2 26.3 26.4 26.5 26.6 26.7 26.8 26.9 27.0 27.1 27.1.1 27.2 27.3 27.4
wordpress-seo / wp-seo-main.php
wordpress-seo Last commit date
admin 2 years ago css 2 years ago images 2 years ago inc 2 years ago js 2 years ago lib 3 years ago packages 3 years ago src 2 years ago vendor 2 years ago vendor_prefixed 3 years ago index.php 10 years ago license.txt 8 years ago readme.txt 2 years ago wp-seo-main.php 2 years ago wp-seo.php 2 years ago wpml-config.xml 3 years ago
wp-seo-main.php
565 lines
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', '21.0' );
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', '7.2.5' );
38 define( 'YOAST_SEO_WP_TESTED', '6.3' );
39 define( 'YOAST_SEO_WP_REQUIRED', '6.1' );
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_name Class name.
52 *
53 * @return void
54 */
55 function wpseo_auto_load( $class_name ) {
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_name );
66
67 if ( ! class_exists( $class_name ) && 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 update_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 WPSEO_Options::set( 'indexing_reason', 'first_install' );
216 WPSEO_Options::set( 'first_time_install', true );
217 if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
218 WPSEO_Options::set( 'should_redirect_after_install_free', true );
219 }
220 else {
221 WPSEO_Options::set( 'activation_redirect_timestamp_free', time() );
222 }
223
224 // Reset tracking to be disabled by default.
225 if ( ! YoastSEO()->helpers->product->is_premium() && WPSEO_Options::get( 'toggled_tracking' ) !== true ) {
226 WPSEO_Options::set( 'tracking', false );
227 }
228 do_action( 'wpseo_register_roles' );
229 WPSEO_Role_Manager_Factory::get()->add();
230
231 do_action( 'wpseo_register_capabilities' );
232 WPSEO_Capability_Manager_Factory::get()->add();
233
234 // Clear cache so the changes are obvious.
235 WPSEO_Utils::clear_cache();
236
237 do_action( 'wpseo_activate' );
238 }
239
240 /**
241 * On deactivation, flush the rewrite rules so XML sitemaps stop working.
242 */
243 function _wpseo_deactivate() {
244 require_once WPSEO_PATH . 'inc/wpseo-functions.php';
245
246 if ( is_multisite() && ms_is_switched() ) {
247 update_option( 'rewrite_rules', '' );
248 }
249 else {
250 add_action( 'shutdown', 'flush_rewrite_rules' );
251 }
252
253 // Register capabilities, to make sure they are cleaned up.
254 do_action( 'wpseo_register_roles' );
255 do_action( 'wpseo_register_capabilities' );
256
257 // Clean up capabilities.
258 WPSEO_Role_Manager_Factory::get()->remove();
259 WPSEO_Capability_Manager_Factory::get()->remove();
260
261 // Clear cache so the changes are obvious.
262 WPSEO_Utils::clear_cache();
263
264 do_action( 'wpseo_deactivate' );
265 }
266
267 /**
268 * Run wpseo activation routine on creation / activation of a multisite blog if WPSEO is activated
269 * network-wide.
270 *
271 * Will only be called by multisite actions.
272 *
273 * {@internal Unfortunately will fail if the plugin is in the must-use directory.
274 * {@link https://core.trac.wordpress.org/ticket/24205} }}
275 *
276 * @param int|WP_Site $blog_id Blog ID.
277 */
278 function wpseo_on_activate_blog( $blog_id ) {
279 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
280 require_once ABSPATH . 'wp-admin/includes/plugin.php';
281 }
282
283 if ( $blog_id instanceof WP_Site ) {
284 $blog_id = (int) $blog_id->blog_id;
285 }
286
287 if ( is_plugin_active_for_network( WPSEO_BASENAME ) ) {
288 switch_to_blog( $blog_id );
289 wpseo_activate( false );
290 restore_current_blog();
291 }
292 }
293
294 /* ***************************** PLUGIN LOADING *************************** */
295
296 /**
297 * Load translations.
298 */
299 function wpseo_load_textdomain() {
300 $wpseo_path = str_replace( '\\', '/', WPSEO_PATH );
301 $mu_path = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
302
303 if ( stripos( $wpseo_path, $mu_path ) !== false ) {
304 load_muplugin_textdomain( 'wordpress-seo', dirname( WPSEO_BASENAME ) . '/languages/' );
305 }
306 else {
307 load_plugin_textdomain( 'wordpress-seo', false, dirname( WPSEO_BASENAME ) . '/languages/' );
308 }
309 }
310
311 add_action( 'plugins_loaded', 'wpseo_load_textdomain' );
312
313
314 /**
315 * On plugins_loaded: load the minimum amount of essential files for this plugin.
316 */
317 function wpseo_init() {
318 require_once WPSEO_PATH . 'inc/wpseo-functions.php';
319 require_once WPSEO_PATH . 'inc/wpseo-functions-deprecated.php';
320
321 // Make sure our option and meta value validation routines and default values are always registered and available.
322 WPSEO_Options::get_instance();
323 WPSEO_Meta::init();
324
325 if ( version_compare( WPSEO_Options::get( 'version', 1 ), WPSEO_VERSION, '<' ) ) {
326 if ( function_exists( 'opcache_reset' ) ) {
327 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Prevent notices when opcache.restrict_api is set.
328 @opcache_reset();
329 }
330
331 new WPSEO_Upgrade();
332 // Get a cleaned up version of the $options.
333 }
334
335 if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
336 $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite();
337 }
338
339 if ( WPSEO_Options::get( 'enable_xml_sitemap' ) === true ) {
340 $GLOBALS['wpseo_sitemaps'] = new WPSEO_Sitemaps();
341 }
342
343 if ( ! wp_doing_ajax() ) {
344 require_once WPSEO_PATH . 'inc/wpseo-non-ajax-functions.php';
345 }
346
347 // Init it here because the filter must be present on the frontend as well or it won't work in the customizer.
348 new WPSEO_Customizer();
349
350 $integrations = [];
351 $integrations[] = new WPSEO_Slug_Change_Watcher();
352
353 foreach ( $integrations as $integration ) {
354 $integration->register_hooks();
355 }
356 }
357
358 /**
359 * Loads the rest api endpoints.
360 */
361 function wpseo_init_rest_api() {
362 // We can't do anything when requirements are not met.
363 if ( ! WPSEO_Utils::is_api_available() ) {
364 return;
365 }
366
367 // Boot up REST API.
368 $statistics_service = new WPSEO_Statistics_Service( new WPSEO_Statistics() );
369
370 $endpoints = [];
371 $endpoints[] = new WPSEO_Endpoint_File_Size( new WPSEO_File_Size_Service() );
372 $endpoints[] = new WPSEO_Endpoint_Statistics( $statistics_service );
373
374 foreach ( $endpoints as $endpoint ) {
375 $endpoint->register();
376 }
377 }
378
379 /**
380 * Used to load the required files on the plugins_loaded hook, instead of immediately.
381 */
382 function wpseo_admin_init() {
383 new WPSEO_Admin_Init();
384 }
385
386 /* ***************************** BOOTSTRAP / HOOK INTO WP *************************** */
387 $spl_autoload_exists = function_exists( 'spl_autoload_register' );
388 $filter_exists = function_exists( 'filter_input' );
389
390 if ( ! $spl_autoload_exists ) {
391 add_action( 'admin_init', 'yoast_wpseo_missing_spl', 1 );
392 }
393
394 if ( ! $filter_exists ) {
395 add_action( 'admin_init', 'yoast_wpseo_missing_filter', 1 );
396 }
397
398 if ( ! wp_installing() && ( $spl_autoload_exists && $filter_exists ) ) {
399 add_action( 'plugins_loaded', 'wpseo_init', 14 );
400 add_action( 'rest_api_init', 'wpseo_init_rest_api' );
401
402 if ( is_admin() ) {
403
404 new Yoast_Notifications();
405
406 $yoast_addon_manager = new WPSEO_Addon_Manager();
407 $yoast_addon_manager->register_hooks();
408
409 if ( wp_doing_ajax() ) {
410 require_once WPSEO_PATH . 'admin/ajax.php';
411
412 // Plugin conflict ajax hooks.
413 new Yoast_Plugin_Conflict_Ajax();
414
415 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: We are not processing form information but only loading the admin init class.
416 if ( isset( $_POST['action'] ) && is_string( $_POST['action'] ) ) {
417 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are not processing form information but only loading the admin init class, We are strictly comparing only.
418 if ( wp_unslash( $_POST['action'] ) === 'inline-save' ) {
419 add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
420 }
421 }
422 }
423 else {
424 add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
425 }
426 }
427
428 add_action( 'plugins_loaded', 'load_yoast_notifications' );
429
430 add_action( 'init', [ 'WPSEO_Replace_Vars', 'setup_statics_once' ] );
431
432 // Initializes the Yoast indexables for the first time.
433 YoastSEO();
434
435 /**
436 * Action called when the Yoast SEO plugin file has loaded.
437 */
438 do_action( 'wpseo_loaded' );
439 }
440
441 // Activation and deactivation hook.
442 register_activation_hook( WPSEO_FILE, 'wpseo_activate' );
443 register_deactivation_hook( WPSEO_FILE, 'wpseo_deactivate' );
444
445 add_action( 'wp_initialize_site', 'wpseo_on_activate_blog', 99 );
446 add_action( 'activate_blog', 'wpseo_on_activate_blog' );
447
448 // Registers SEO capabilities.
449 $wpseo_register_capabilities = new WPSEO_Register_Capabilities();
450 $wpseo_register_capabilities->register_hooks();
451
452 // Registers SEO roles.
453 $wpseo_register_capabilities = new WPSEO_Register_Roles();
454 $wpseo_register_capabilities->register_hooks();
455
456 /**
457 * Wraps for notifications center class.
458 */
459 function load_yoast_notifications() {
460 // Init Yoast_Notification_Center class.
461 Yoast_Notification_Center::get();
462 }
463
464
465 /**
466 * Throw an error if the PHP SPL extension is disabled (prevent white screens) and self-deactivate plugin.
467 *
468 * @since 1.5.4
469 *
470 * @return void
471 */
472 function yoast_wpseo_missing_spl() {
473 if ( is_admin() ) {
474 add_action( 'admin_notices', 'yoast_wpseo_missing_spl_notice' );
475
476 yoast_wpseo_self_deactivate();
477 }
478 }
479
480 /**
481 * Returns the notice in case of missing spl extension.
482 */
483 function yoast_wpseo_missing_spl_notice() {
484 $message = esc_html__( 'The Standard PHP Library (SPL) extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
485 yoast_wpseo_activation_failed_notice( $message );
486 }
487
488 /**
489 * Throw an error if the Composer autoload is missing and self-deactivate plugin.
490 *
491 * @return void
492 */
493 function yoast_wpseo_missing_autoload() {
494 if ( is_admin() ) {
495 add_action( 'admin_notices', 'yoast_wpseo_missing_autoload_notice' );
496
497 yoast_wpseo_self_deactivate();
498 }
499 }
500
501 /**
502 * Returns the notice in case of missing Composer autoload.
503 */
504 function yoast_wpseo_missing_autoload_notice() {
505 /* 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 */
506 $message = esc_html__( 'The %1$s plugin installation is incomplete. Please refer to %2$sinstallation instructions%3$s.', 'wordpress-seo' );
507 $message = sprintf( $message, 'Yoast SEO', '<a href="https://github.com/Yoast/wordpress-seo#installation">', '</a>' );
508 yoast_wpseo_activation_failed_notice( $message );
509 }
510
511 /**
512 * Throw an error if the filter extension is disabled (prevent white screens) and self-deactivate plugin.
513 *
514 * @since 2.0
515 *
516 * @return void
517 */
518 function yoast_wpseo_missing_filter() {
519 if ( is_admin() ) {
520 add_action( 'admin_notices', 'yoast_wpseo_missing_filter_notice' );
521
522 yoast_wpseo_self_deactivate();
523 }
524 }
525
526 /**
527 * Returns the notice in case of missing filter extension.
528 */
529 function yoast_wpseo_missing_filter_notice() {
530 $message = esc_html__( 'The filter extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
531 yoast_wpseo_activation_failed_notice( $message );
532 }
533
534 /**
535 * Echo's the Activation failed notice with any given message.
536 *
537 * @param string $message Message string.
538 */
539 function yoast_wpseo_activation_failed_notice( $message ) {
540 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This function is only called in 3 places that are safe.
541 echo '<div class="error"><p>' . esc_html__( 'Activation failed:', 'wordpress-seo' ) . ' ' . strip_tags( $message, '<a>' ) . '</p></div>';
542 }
543
544 /**
545 * The method will deactivate the plugin, but only once, done by the static $is_deactivated.
546 */
547 function yoast_wpseo_self_deactivate() {
548 static $is_deactivated;
549
550 if ( $is_deactivated === null ) {
551 $is_deactivated = true;
552 deactivate_plugins( WPSEO_BASENAME );
553 if ( isset( $_GET['activate'] ) ) {
554 unset( $_GET['activate'] );
555 }
556 }
557 }
558
559 /**
560 * Aliasses added in order to keep compatibility with Yoast SEO: Local.
561 */
562 class_alias( '\Yoast\WP\SEO\Initializers\Initializer_Interface', '\Yoast\WP\SEO\WordPress\Initializer' );
563 class_alias( '\Yoast\WP\SEO\Loadable_Interface', '\Yoast\WP\SEO\WordPress\Loadable' );
564 class_alias( '\Yoast\WP\SEO\Integrations\Integration_Interface', '\Yoast\WP\SEO\WordPress\Integration' );
565