wp-seo-main.php 17 KB

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