PageRenderTime 37ms CodeModel.GetById 14ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/wp-seo-main.php

https://bitbucket.org/carloskikea/helpet
PHP | 533 lines | 287 code | 98 blank | 148 comment | 58 complexity | 96b5008b1ce82794aa5e19d346edd99b MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Main
  6 */
  7
  8if ( ! 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 */
 18define( 'WPSEO_VERSION', '7.3' );
 19
 20if ( ! defined( 'WPSEO_PATH' ) ) {
 21	define( 'WPSEO_PATH', plugin_dir_path( WPSEO_FILE ) );
 22}
 23
 24if ( ! defined( 'WPSEO_BASENAME' ) ) {
 25	define( 'WPSEO_BASENAME', plugin_basename( WPSEO_FILE ) );
 26}
 27
 28/* ***************************** CLASS AUTOLOADING *************************** */
 29
 30/**
 31 * Auto load our class files
 32 *
 33 * @param string $class Class name.
 34 *
 35 * @return void
 36 */
 37function wpseo_auto_load( $class ) {
 38	static $classes = null;
 39
 40	if ( $classes === null ) {
 41		$classes = array(
 42			'wp_list_table'   => ABSPATH . 'wp-admin/includes/class-wp-list-table.php',
 43			'walker_category' => ABSPATH . 'wp-includes/category-template.php',
 44			'pclzip'          => ABSPATH . 'wp-admin/includes/class-pclzip.php',
 45		);
 46	}
 47
 48	$cn = strtolower( $class );
 49
 50	if ( ! class_exists( $class ) && isset( $classes[ $cn ] ) ) {
 51		require_once $classes[ $cn ];
 52	}
 53}
 54
 55if ( file_exists( WPSEO_PATH . 'vendor/autoload_52.php' ) ) {
 56	require WPSEO_PATH . 'vendor/autoload_52.php';
 57}
 58elseif ( ! class_exists( 'WPSEO_Options' ) ) { // Still checking since might be site-level autoload R.
 59	add_action( 'admin_init', 'yoast_wpseo_missing_autoload', 1 );
 60
 61	return;
 62}
 63
 64if ( function_exists( 'spl_autoload_register' ) ) {
 65	spl_autoload_register( 'wpseo_auto_load' );
 66}
 67
 68/* ********************* DEFINES DEPENDING ON AUTOLOADED CODE ********************* */
 69
 70/**
 71 * Defaults to production, for safety
 72 */
 73if ( ! defined( 'YOAST_ENVIRONMENT' ) ) {
 74	define( 'YOAST_ENVIRONMENT', 'production' );
 75}
 76
 77/**
 78 * Only use minified assets when we are in a production environment
 79 */
 80if ( ! defined( 'WPSEO_CSSJS_SUFFIX' ) ) {
 81	define( 'WPSEO_CSSJS_SUFFIX', ( 'development' !== YOAST_ENVIRONMENT ) ? '.min' : '' );
 82}
 83
 84/* ***************************** PLUGIN (DE-)ACTIVATION *************************** */
 85
 86/**
 87 * Run single site / network-wide activation of the plugin.
 88 *
 89 * @param bool $networkwide Whether the plugin is being activated network-wide.
 90 */
 91function wpseo_activate( $networkwide = false ) {
 92	if ( ! is_multisite() || ! $networkwide ) {
 93		_wpseo_activate();
 94	}
 95	else {
 96		/* Multi-site network activation - activate the plugin for all blogs */
 97		wpseo_network_activate_deactivate( true );
 98	}
 99}
100
101/**
102 * Run single site / network-wide de-activation of the plugin.
103 *
104 * @param bool $networkwide Whether the plugin is being de-activated network-wide.
105 */
106function wpseo_deactivate( $networkwide = false ) {
107	if ( ! is_multisite() || ! $networkwide ) {
108		_wpseo_deactivate();
109	}
110	else {
111		/* Multi-site network activation - de-activate the plugin for all blogs */
112		wpseo_network_activate_deactivate( false );
113	}
114}
115
116/**
117 * Run network-wide (de-)activation of the plugin
118 *
119 * @param bool $activate True for plugin activation, false for de-activation.
120 */
121function wpseo_network_activate_deactivate( $activate = true ) {
122	global $wpdb;
123
124	$network_blogs = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d", $wpdb->siteid ) );
125
126	if ( is_array( $network_blogs ) && $network_blogs !== array() ) {
127		foreach ( $network_blogs as $blog_id ) {
128			switch_to_blog( $blog_id );
129
130			if ( $activate === true ) {
131				_wpseo_activate();
132			}
133			else {
134				_wpseo_deactivate();
135			}
136
137			restore_current_blog();
138		}
139	}
140}
141
142/**
143 * Runs on activation of the plugin.
144 */
145function _wpseo_activate() {
146	require_once WPSEO_PATH . 'inc/wpseo-functions.php';
147	require_once WPSEO_PATH . 'inc/class-wpseo-installation.php';
148
149	wpseo_load_textdomain(); // Make sure we have our translations available for the defaults.
150
151	new WPSEO_Installation();
152
153	WPSEO_Options::get_instance();
154	if ( ! is_multisite() ) {
155		WPSEO_Options::initialize();
156	}
157	else {
158		WPSEO_Options::maybe_set_multisite_defaults( true );
159	}
160	WPSEO_Options::ensure_options_exist();
161
162	if ( is_multisite() && ms_is_switched() ) {
163		delete_option( 'rewrite_rules' );
164	}
165	else {
166		$wpseo_rewrite = new WPSEO_Rewrite();
167		$wpseo_rewrite->schedule_flush();
168	}
169
170	do_action( 'wpseo_register_roles' );
171	WPSEO_Role_Manager_Factory::get()->add();
172
173	do_action( 'wpseo_register_capabilities' );
174	WPSEO_Capability_Manager_Factory::get()->add();
175
176	// Clear cache so the changes are obvious.
177	WPSEO_Utils::clear_cache();
178
179	// Create the text link storage table.
180	$link_installer = new WPSEO_Link_Installer();
181	$link_installer->install();
182
183	// Trigger reindex notification.
184	$notifier = new WPSEO_Link_Notifier();
185	$notifier->manage_notification();
186
187	// Schedule cronjob when it doesn't exists on activation.
188	$wpseo_onpage = new WPSEO_OnPage();
189	$wpseo_onpage->activate_hooks();
190
191	do_action( 'wpseo_activate' );
192}
193/**
194 * On deactivation, flush the rewrite rules so XML sitemaps stop working.
195 */
196function _wpseo_deactivate() {
197	require_once WPSEO_PATH . 'inc/wpseo-functions.php';
198
199	if ( is_multisite() && ms_is_switched() ) {
200		delete_option( 'rewrite_rules' );
201	}
202	else {
203		add_action( 'shutdown', 'flush_rewrite_rules' );
204	}
205
206	// Register capabilities, to make sure they are cleaned up.
207	do_action( 'wpseo_register_roles' );
208	do_action( 'wpseo_register_capabilities' );
209
210	// Clean up capabilities.
211	WPSEO_Role_Manager_Factory::get()->remove();
212	WPSEO_Capability_Manager_Factory::get()->remove();
213
214	// Clear cache so the changes are obvious.
215	WPSEO_Utils::clear_cache();
216
217	do_action( 'wpseo_deactivate' );
218}
219
220/**
221 * Run wpseo activation routine on creation / activation of a multisite blog if WPSEO is activated
222 * network-wide.
223 *
224 * Will only be called by multisite actions.
225 *
226 * {@internal Unfortunately will fail if the plugin is in the must-use directory.
227 *            {@link https://core.trac.wordpress.org/ticket/24205} }}
228 *
229 * @param int $blog_id Blog ID.
230 */
231function wpseo_on_activate_blog( $blog_id ) {
232	if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
233		require_once ABSPATH . 'wp-admin/includes/plugin.php';
234	}
235
236	if ( is_plugin_active_for_network( plugin_basename( WPSEO_FILE ) ) ) {
237		switch_to_blog( $blog_id );
238		wpseo_activate( false );
239		restore_current_blog();
240	}
241}
242
243
244/* ***************************** PLUGIN LOADING *************************** */
245
246/**
247 * Load translations
248 */
249function wpseo_load_textdomain() {
250	$wpseo_path = str_replace( '\\', '/', WPSEO_PATH );
251	$mu_path    = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
252
253	if ( false !== stripos( $wpseo_path, $mu_path ) ) {
254		load_muplugin_textdomain( 'wordpress-seo', dirname( WPSEO_BASENAME ) . '/languages/' );
255	}
256	else {
257		load_plugin_textdomain( 'wordpress-seo', false, dirname( WPSEO_BASENAME ) . '/languages/' );
258	}
259}
260
261add_action( 'plugins_loaded', 'wpseo_load_textdomain' );
262
263
264/**
265 * On plugins_loaded: load the minimum amount of essential files for this plugin
266 */
267function wpseo_init() {
268	require_once WPSEO_PATH . 'inc/wpseo-functions.php';
269	require_once WPSEO_PATH . 'inc/wpseo-functions-deprecated.php';
270
271	// Make sure our option and meta value validation routines and default values are always registered and available.
272	WPSEO_Options::get_instance();
273	WPSEO_Meta::init();
274
275	if ( version_compare( WPSEO_Options::get( 'version', 1 ), WPSEO_VERSION, '<' ) ) {
276		if ( function_exists( 'opcache_reset' ) ) {
277			opcache_reset();
278		}
279
280		new WPSEO_Upgrade();
281		// Get a cleaned up version of the $options.
282	}
283
284	if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
285		$GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite();
286	}
287
288	if ( WPSEO_Options::get( 'enable_xml_sitemap' ) === true ) {
289		$GLOBALS['wpseo_sitemaps'] = new WPSEO_Sitemaps();
290	}
291
292	if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
293		require_once WPSEO_PATH . 'inc/wpseo-non-ajax-functions.php';
294	}
295
296	// Init it here because the filter must be present on the frontend as well or it won't work in the customizer.
297	new WPSEO_Customizer();
298
299	/*
300	 * Initializes the link watcher for both the frontend and backend.
301	 * Required to process scheduled items properly.
302	 */
303	$link_watcher = new WPSEO_Link_Watcher_Loader();
304	$link_watcher->load();
305
306	// Loading Ryte integration.
307	$wpseo_onpage = new WPSEO_OnPage();
308	$wpseo_onpage->register_hooks();
309}
310
311/**
312 * Loads the rest api endpoints.
313 */
314function wpseo_init_rest_api() {
315	// We can't do anything when requirements are not met.
316	if ( ! WPSEO_Utils::is_api_available() ) {
317		return;
318	}
319
320	// Boot up REST API.
321	$configuration_service = new WPSEO_Configuration_Service();
322	$configuration_service->initialize();
323
324	$link_reindex_endpoint = new WPSEO_Link_Reindex_Post_Endpoint( new WPSEO_Link_Reindex_Post_Service() );
325	$link_reindex_endpoint->register();
326
327	$statistics_service  = new WPSEO_Statistics_Service( new WPSEO_Statistics() );
328	$statistics_endpoint = new WPSEO_Endpoint_Statistics( $statistics_service );
329	$statistics_endpoint->register();
330
331	$ryte_endpoint_service = new WPSEO_Ryte_Service( new WPSEO_OnPage_Option() );
332	$ryte_endpoint         = new WPSEO_Endpoint_Ryte( $ryte_endpoint_service );
333	$ryte_endpoint->register();
334}
335
336/**
337 * Used to load the required files on the plugins_loaded hook, instead of immediately.
338 */
339function wpseo_frontend_init() {
340	add_action( 'init', 'initialize_wpseo_front' );
341
342	if ( WPSEO_Options::get( 'breadcrumbs-enable' ) === true ) {
343		/**
344		 * If breadcrumbs are active (which they supposedly are if the users has enabled this settings,
345		 * there's no reason to have bbPress breadcrumbs as well.
346		 *
347		 * {@internal The class itself is only loaded when the template tag is encountered
348		 *            via the template tag function in the wpseo-functions.php file.}}
349		 */
350		add_filter( 'bbp_get_breadcrumb', '__return_false' );
351	}
352
353	add_action( 'template_redirect', 'wpseo_frontend_head_init', 999 );
354}
355
356/**
357 * Instantiate the different social classes on the frontend
358 */
359function wpseo_frontend_head_init() {
360	if ( WPSEO_Options::get( 'twitter' ) === true ) {
361		add_action( 'wpseo_head', array( 'WPSEO_Twitter', 'get_instance' ), 40 );
362	}
363
364	if ( WPSEO_Options::get( 'opengraph' ) === true ) {
365		$GLOBALS['wpseo_og'] = new WPSEO_OpenGraph();
366	}
367
368}
369
370/**
371 * Used to load the required files on the plugins_loaded hook, instead of immediately.
372 */
373function wpseo_admin_init() {
374	new WPSEO_Admin_Init();
375}
376
377
378/* ***************************** BOOTSTRAP / HOOK INTO WP *************************** */
379$spl_autoload_exists = function_exists( 'spl_autoload_register' );
380$filter_exists       = function_exists( 'filter_input' );
381
382if ( ! $spl_autoload_exists ) {
383	add_action( 'admin_init', 'yoast_wpseo_missing_spl', 1 );
384}
385
386if ( ! $filter_exists ) {
387	add_action( 'admin_init', 'yoast_wpseo_missing_filter', 1 );
388}
389
390if ( ! wp_installing() && ( $spl_autoload_exists && $filter_exists ) ) {
391	add_action( 'plugins_loaded', 'wpseo_init', 14 );
392	add_action( 'rest_api_init', 'wpseo_init_rest_api' );
393
394	if ( is_admin() ) {
395
396		new Yoast_Alerts();
397
398		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
399			require_once WPSEO_PATH . 'admin/ajax.php';
400
401			// Plugin conflict ajax hooks.
402			new Yoast_Plugin_Conflict_Ajax();
403
404			if ( filter_input( INPUT_POST, 'action' ) === 'inline-save' ) {
405				add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
406			}
407		}
408		else {
409			add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
410		}
411	}
412	else {
413		add_action( 'plugins_loaded', 'wpseo_frontend_init', 15 );
414	}
415
416	add_action( 'plugins_loaded', 'load_yoast_notifications' );
417}
418
419// Activation and deactivation hook.
420register_activation_hook( WPSEO_FILE, 'wpseo_activate' );
421register_deactivation_hook( WPSEO_FILE, 'wpseo_deactivate' );
422add_action( 'wpmu_new_blog', 'wpseo_on_activate_blog' );
423add_action( 'activate_blog', 'wpseo_on_activate_blog' );
424
425// Registers SEO capabilities.
426$wpseo_register_capabilities = new WPSEO_Register_Capabilities();
427$wpseo_register_capabilities->register_hooks();
428
429// Registers SEO roles.
430$wpseo_register_capabilities = new WPSEO_Register_Roles();
431$wpseo_register_capabilities->register_hooks();
432
433/**
434 * Wraps for notifications center class.
435 */
436function load_yoast_notifications() {
437	// Init Yoast_Notification_Center class.
438	Yoast_Notification_Center::get();
439}
440
441
442/**
443 * Throw an error if the PHP SPL extension is disabled (prevent white screens) and self-deactivate plugin
444 *
445 * @since 1.5.4
446 *
447 * @return void
448 */
449function yoast_wpseo_missing_spl() {
450	if ( is_admin() ) {
451		add_action( 'admin_notices', 'yoast_wpseo_missing_spl_notice' );
452
453		yoast_wpseo_self_deactivate();
454	}
455}
456
457/**
458 * Returns the notice in case of missing spl extension
459 */
460function yoast_wpseo_missing_spl_notice() {
461	$message = esc_html__( 'The Standard PHP Library (SPL) extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
462	yoast_wpseo_activation_failed_notice( $message );
463}
464
465/**
466 * Throw an error if the Composer autoload is missing and self-deactivate plugin
467 *
468 * @return void
469 */
470function yoast_wpseo_missing_autoload() {
471	if ( is_admin() ) {
472		add_action( 'admin_notices', 'yoast_wpseo_missing_autoload_notice' );
473
474		yoast_wpseo_self_deactivate();
475	}
476}
477
478/**
479 * Returns the notice in case of missing Composer autoload
480 */
481function yoast_wpseo_missing_autoload_notice() {
482	/* 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 */
483	$message = esc_html__( 'The %1$s plugin installation is incomplete. Please refer to %2$sinstallation instructions%3$s.', 'wordpress-seo' );
484	$message = sprintf( $message, 'Yoast SEO', '<a href="https://github.com/Yoast/wordpress-seo#installation">', '</a>' );
485	yoast_wpseo_activation_failed_notice( $message );
486}
487
488/**
489 * Throw an error if the filter extension is disabled (prevent white screens) and self-deactivate plugin
490 *
491 * @since 2.0
492 *
493 * @return void
494 */
495function yoast_wpseo_missing_filter() {
496	if ( is_admin() ) {
497		add_action( 'admin_notices', 'yoast_wpseo_missing_filter_notice' );
498
499		yoast_wpseo_self_deactivate();
500	}
501}
502
503/**
504 * Returns the notice in case of missing filter extension
505 */
506function yoast_wpseo_missing_filter_notice() {
507	$message = esc_html__( 'The filter extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
508	yoast_wpseo_activation_failed_notice( $message );
509}
510
511/**
512 * Echo's the Activation failed notice with any given message.
513 *
514 * @param string $message Message string.
515 */
516function yoast_wpseo_activation_failed_notice( $message ) {
517	echo '<div class="error"><p>' . esc_html__( 'Activation failed:', 'wordpress-seo' ) . ' ' . $message . '</p></div>';
518}
519
520/**
521 * The method will deactivate the plugin, but only once, done by the static $is_deactivated
522 */
523function yoast_wpseo_self_deactivate() {
524	static $is_deactivated;
525
526	if ( $is_deactivated === null ) {
527		$is_deactivated = true;
528		deactivate_plugins( plugin_basename( WPSEO_FILE ) );
529		if ( isset( $_GET['activate'] ) ) {
530			unset( $_GET['activate'] );
531		}
532	}
533}