/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. 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', '7.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. // Loading Ryte integration.
  249. $wpseo_onpage = new WPSEO_OnPage();
  250. $wpseo_onpage->register_hooks();
  251. }
  252. /**
  253. * Loads the rest api endpoints.
  254. */
  255. function wpseo_init_rest_api() {
  256. // We can't do anything when requirements are not met.
  257. if ( ! WPSEO_Utils::is_api_available() ) {
  258. return;
  259. }
  260. // Boot up REST API.
  261. $configuration_service = new WPSEO_Configuration_Service();
  262. $configuration_service->initialize();
  263. $link_reindex_endpoint = new WPSEO_Link_Reindex_Post_Endpoint( new WPSEO_Link_Reindex_Post_Service() );
  264. $link_reindex_endpoint->register();
  265. $statistics_service = new WPSEO_Statistics_Service( new WPSEO_Statistics() );
  266. $statistics_endpoint = new WPSEO_Endpoint_Statistics( $statistics_service );
  267. $statistics_endpoint->register();
  268. $ryte_endpoint_service = new WPSEO_Ryte_Service( new WPSEO_OnPage_Option() );
  269. $ryte_endpoint = new WPSEO_Endpoint_Ryte( $ryte_endpoint_service );
  270. $ryte_endpoint->register();
  271. }
  272. /**
  273. * Used to load the required files on the plugins_loaded hook, instead of immediately.
  274. */
  275. function wpseo_frontend_init() {
  276. add_action( 'init', 'initialize_wpseo_front' );
  277. if ( WPSEO_Options::get( 'breadcrumbs-enable' ) === true ) {
  278. /**
  279. * If breadcrumbs are active (which they supposedly are if the users has enabled this settings,
  280. * there's no reason to have bbPress breadcrumbs as well.
  281. *
  282. * {@internal The class itself is only loaded when the template tag is encountered
  283. * via the template tag function in the wpseo-functions.php file.}}
  284. */
  285. add_filter( 'bbp_get_breadcrumb', '__return_false' );
  286. }
  287. add_action( 'template_redirect', 'wpseo_frontend_head_init', 999 );
  288. }
  289. /**
  290. * Instantiate the different social classes on the frontend
  291. */
  292. function wpseo_frontend_head_init() {
  293. if ( WPSEO_Options::get( 'twitter' ) === true ) {
  294. add_action( 'wpseo_head', array( 'WPSEO_Twitter', 'get_instance' ), 40 );
  295. }
  296. if ( WPSEO_Options::get( 'opengraph' ) === true ) {
  297. $GLOBALS['wpseo_og'] = new WPSEO_OpenGraph();
  298. }
  299. }
  300. /**
  301. * Used to load the required files on the plugins_loaded hook, instead of immediately.
  302. */
  303. function wpseo_admin_init() {
  304. new WPSEO_Admin_Init();
  305. }
  306. /* ***************************** BOOTSTRAP / HOOK INTO WP *************************** */
  307. $spl_autoload_exists = function_exists( 'spl_autoload_register' );
  308. $filter_exists = function_exists( 'filter_input' );
  309. if ( ! $spl_autoload_exists ) {
  310. add_action( 'admin_init', 'yoast_wpseo_missing_spl', 1 );
  311. }
  312. if ( ! $filter_exists ) {
  313. add_action( 'admin_init', 'yoast_wpseo_missing_filter', 1 );
  314. }
  315. if ( ! wp_installing() && ( $spl_autoload_exists && $filter_exists ) ) {
  316. add_action( 'plugins_loaded', 'wpseo_init', 14 );
  317. add_action( 'rest_api_init', 'wpseo_init_rest_api' );
  318. if ( is_admin() ) {
  319. new Yoast_Alerts();
  320. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  321. require_once WPSEO_PATH . 'admin/ajax.php';
  322. // Plugin conflict ajax hooks.
  323. new Yoast_Plugin_Conflict_Ajax();
  324. if ( filter_input( INPUT_POST, 'action' ) === 'inline-save' ) {
  325. add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
  326. }
  327. }
  328. else {
  329. add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
  330. }
  331. }
  332. else {
  333. add_action( 'plugins_loaded', 'wpseo_frontend_init', 15 );
  334. }
  335. add_action( 'plugins_loaded', 'load_yoast_notifications' );
  336. }
  337. // Activation and deactivation hook.
  338. register_activation_hook( WPSEO_FILE, 'wpseo_activate' );
  339. register_deactivation_hook( WPSEO_FILE, 'wpseo_deactivate' );
  340. add_action( 'wpmu_new_blog', 'wpseo_on_activate_blog' );
  341. add_action( 'activate_blog', 'wpseo_on_activate_blog' );
  342. // Registers SEO capabilities.
  343. $wpseo_register_capabilities = new WPSEO_Register_Capabilities();
  344. $wpseo_register_capabilities->register_hooks();
  345. // Registers SEO roles.
  346. $wpseo_register_capabilities = new WPSEO_Register_Roles();
  347. $wpseo_register_capabilities->register_hooks();
  348. /**
  349. * Wraps for notifications center class.
  350. */
  351. function load_yoast_notifications() {
  352. // Init Yoast_Notification_Center class.
  353. Yoast_Notification_Center::get();
  354. }
  355. /**
  356. * Throw an error if the PHP SPL extension is disabled (prevent white screens) and self-deactivate plugin
  357. *
  358. * @since 1.5.4
  359. *
  360. * @return void
  361. */
  362. function yoast_wpseo_missing_spl() {
  363. if ( is_admin() ) {
  364. add_action( 'admin_notices', 'yoast_wpseo_missing_spl_notice' );
  365. yoast_wpseo_self_deactivate();
  366. }
  367. }
  368. /**
  369. * Returns the notice in case of missing spl extension
  370. */
  371. function yoast_wpseo_missing_spl_notice() {
  372. $message = esc_html__( 'The Standard PHP Library (SPL) extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
  373. yoast_wpseo_activation_failed_notice( $message );
  374. }
  375. /**
  376. * Throw an error if the Composer autoload is missing and self-deactivate plugin
  377. *
  378. * @return void
  379. */
  380. function yoast_wpseo_missing_autoload() {
  381. if ( is_admin() ) {
  382. add_action( 'admin_notices', 'yoast_wpseo_missing_autoload_notice' );
  383. yoast_wpseo_self_deactivate();
  384. }
  385. }
  386. /**
  387. * Returns the notice in case of missing Composer autoload
  388. */
  389. function yoast_wpseo_missing_autoload_notice() {
  390. /* 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 */
  391. $message = esc_html__( 'The %1$s plugin installation is incomplete. Please refer to %2$sinstallation instructions%3$s.', 'wordpress-seo' );
  392. $message = sprintf( $message, 'Yoast SEO', '<a href="https://github.com/Yoast/wordpress-seo#installation">', '</a>' );
  393. yoast_wpseo_activation_failed_notice( $message );
  394. }
  395. /**
  396. * Throw an error if the filter extension is disabled (prevent white screens) and self-deactivate plugin
  397. *
  398. * @since 2.0
  399. *
  400. * @return void
  401. */
  402. function yoast_wpseo_missing_filter() {
  403. if ( is_admin() ) {
  404. add_action( 'admin_notices', 'yoast_wpseo_missing_filter_notice' );
  405. yoast_wpseo_self_deactivate();
  406. }
  407. }
  408. /**
  409. * Returns the notice in case of missing filter extension
  410. */
  411. function yoast_wpseo_missing_filter_notice() {
  412. $message = esc_html__( 'The filter extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
  413. yoast_wpseo_activation_failed_notice( $message );
  414. }
  415. /**
  416. * Echo's the Activation failed notice with any given message.
  417. *
  418. * @param string $message Message string.
  419. */
  420. function yoast_wpseo_activation_failed_notice( $message ) {
  421. echo '<div class="error"><p>' . esc_html__( 'Activation failed:', 'wordpress-seo' ) . ' ' . $message . '</p></div>';
  422. }
  423. /**
  424. * The method will deactivate the plugin, but only once, done by the static $is_deactivated
  425. */
  426. function yoast_wpseo_self_deactivate() {
  427. static $is_deactivated;
  428. if ( $is_deactivated === null ) {
  429. $is_deactivated = true;
  430. deactivate_plugins( plugin_basename( WPSEO_FILE ) );
  431. if ( isset( $_GET['activate'] ) ) {
  432. unset( $_GET['activate'] );
  433. }
  434. }
  435. }