PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/iamgraeme/royalmile
PHP | 480 lines | 257 code | 87 blank | 136 comment | 57 complexity | 5b5f569e9a948cfbc3b50e3cf47c2cc8 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Main
  4. */
  5. if ( ! function_exists( 'add_filter' ) ) {
  6. header( 'Status: 403 Forbidden' );
  7. header( 'HTTP/1.1 403 Forbidden' );
  8. exit();
  9. }
  10. /**
  11. * @internal Nobody should be able to overrule the real version number as this can cause serious issues
  12. * with the options, so no if ( ! defined() )
  13. */
  14. define( 'WPSEO_VERSION', '3.3.4' );
  15. if ( ! defined( 'WPSEO_PATH' ) ) {
  16. define( 'WPSEO_PATH', plugin_dir_path( WPSEO_FILE ) );
  17. }
  18. if ( ! defined( 'WPSEO_BASENAME' ) ) {
  19. define( 'WPSEO_BASENAME', plugin_basename( WPSEO_FILE ) );
  20. }
  21. /* ***************************** CLASS AUTOLOADING *************************** */
  22. /**
  23. * Auto load our class files
  24. *
  25. * @param string $class Class name.
  26. *
  27. * @return void
  28. */
  29. function wpseo_auto_load( $class ) {
  30. static $classes = null;
  31. if ( $classes === null ) {
  32. $classes = array(
  33. 'wp_list_table' => ABSPATH . 'wp-admin/includes/class-wp-list-table.php',
  34. 'walker_category' => ABSPATH . 'wp-includes/category-template.php',
  35. 'pclzip' => ABSPATH . 'wp-admin/includes/class-pclzip.php',
  36. );
  37. }
  38. $cn = strtolower( $class );
  39. if ( ! class_exists( $class ) && isset( $classes[ $cn ] ) ) {
  40. require_once( $classes[ $cn ] );
  41. }
  42. }
  43. if ( file_exists( WPSEO_PATH . '/vendor/autoload_52.php' ) ) {
  44. require WPSEO_PATH . '/vendor/autoload_52.php';
  45. }
  46. elseif ( ! class_exists( 'WPSEO_Options' ) ) { // Still checking since might be site-level autoload R.
  47. add_action( 'admin_init', 'yoast_wpseo_missing_autoload', 1 );
  48. return;
  49. }
  50. if ( function_exists( 'spl_autoload_register' ) ) {
  51. spl_autoload_register( 'wpseo_auto_load' );
  52. }
  53. /* ********************* DEFINES DEPENDING ON AUTOLOADED CODE ********************* */
  54. /**
  55. * Defaults to production, for safety
  56. */
  57. if ( ! defined( 'YOAST_ENVIRONMENT' ) ) {
  58. define( 'YOAST_ENVIRONMENT', 'production' );
  59. }
  60. /**
  61. * Only use minified assets when we are in a production environment
  62. */
  63. if ( ! defined( 'WPSEO_CSSJS_SUFFIX' ) ) {
  64. define( 'WPSEO_CSSJS_SUFFIX', ( 'development' !== YOAST_ENVIRONMENT ) ? '.min' : '' );
  65. }
  66. /* ***************************** PLUGIN (DE-)ACTIVATION *************************** */
  67. /**
  68. * Run single site / network-wide activation of the plugin.
  69. *
  70. * @param bool $networkwide Whether the plugin is being activated network-wide.
  71. */
  72. function wpseo_activate( $networkwide = false ) {
  73. if ( ! is_multisite() || ! $networkwide ) {
  74. _wpseo_activate();
  75. }
  76. else {
  77. /* Multi-site network activation - activate the plugin for all blogs */
  78. wpseo_network_activate_deactivate( true );
  79. }
  80. }
  81. /**
  82. * Run single site / network-wide de-activation of the plugin.
  83. *
  84. * @param bool $networkwide Whether the plugin is being de-activated network-wide.
  85. */
  86. function wpseo_deactivate( $networkwide = false ) {
  87. if ( ! is_multisite() || ! $networkwide ) {
  88. _wpseo_deactivate();
  89. }
  90. else {
  91. /* Multi-site network activation - de-activate the plugin for all blogs */
  92. wpseo_network_activate_deactivate( false );
  93. }
  94. }
  95. /**
  96. * Run network-wide (de-)activation of the plugin
  97. *
  98. * @param bool $activate True for plugin activation, false for de-activation.
  99. */
  100. function wpseo_network_activate_deactivate( $activate = true ) {
  101. global $wpdb;
  102. $network_blogs = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d", $wpdb->siteid ) );
  103. if ( is_array( $network_blogs ) && $network_blogs !== array() ) {
  104. foreach ( $network_blogs as $blog_id ) {
  105. switch_to_blog( $blog_id );
  106. if ( $activate === true ) {
  107. _wpseo_activate();
  108. }
  109. else {
  110. _wpseo_deactivate();
  111. }
  112. restore_current_blog();
  113. }
  114. }
  115. }
  116. /**
  117. * Runs on activation of the plugin.
  118. */
  119. function _wpseo_activate() {
  120. require_once( WPSEO_PATH . 'inc/wpseo-functions.php' );
  121. wpseo_load_textdomain(); // Make sure we have our translations available for the defaults.
  122. WPSEO_Options::get_instance();
  123. if ( ! is_multisite() ) {
  124. WPSEO_Options::initialize();
  125. }
  126. else {
  127. WPSEO_Options::maybe_set_multisite_defaults( true );
  128. }
  129. WPSEO_Options::ensure_options_exist();
  130. if ( is_multisite() && ms_is_switched() ) {
  131. delete_option( 'rewrite_rules' );
  132. }
  133. else {
  134. $wpseo_rewrite = new WPSEO_Rewrite();
  135. $wpseo_rewrite->schedule_flush();
  136. }
  137. wpseo_add_capabilities();
  138. // Clear cache so the changes are obvious.
  139. WPSEO_Utils::clear_cache();
  140. do_action( 'wpseo_activate' );
  141. }
  142. /**
  143. * On deactivation, flush the rewrite rules so XML sitemaps stop working.
  144. */
  145. function _wpseo_deactivate() {
  146. require_once( WPSEO_PATH . 'inc/wpseo-functions.php' );
  147. if ( is_multisite() && ms_is_switched() ) {
  148. delete_option( 'rewrite_rules' );
  149. }
  150. else {
  151. add_action( 'shutdown', 'flush_rewrite_rules' );
  152. }
  153. wpseo_remove_capabilities();
  154. // Clear cache so the changes are obvious.
  155. WPSEO_Utils::clear_cache();
  156. do_action( 'wpseo_deactivate' );
  157. }
  158. /**
  159. * Run wpseo activation routine on creation / activation of a multisite blog if WPSEO is activated
  160. * network-wide.
  161. *
  162. * Will only be called by multisite actions.
  163. *
  164. * @internal Unfortunately will fail if the plugin is in the must-use directory
  165. * @see https://core.trac.wordpress.org/ticket/24205
  166. *
  167. * @param int $blog_id Blog ID.
  168. */
  169. function wpseo_on_activate_blog( $blog_id ) {
  170. if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
  171. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  172. }
  173. if ( is_plugin_active_for_network( plugin_basename( WPSEO_FILE ) ) ) {
  174. switch_to_blog( $blog_id );
  175. wpseo_activate( false );
  176. restore_current_blog();
  177. }
  178. }
  179. /* ***************************** PLUGIN LOADING *************************** */
  180. /**
  181. * Load translations
  182. */
  183. function wpseo_load_textdomain() {
  184. $wpseo_path = str_replace( '\\', '/', WPSEO_PATH );
  185. $mu_path = str_replace( '\\', '/', WPMU_PLUGIN_DIR );
  186. if ( false !== stripos( $wpseo_path, $mu_path ) ) {
  187. load_muplugin_textdomain( 'wordpress-seo', dirname( WPSEO_BASENAME ) . '/languages/' );
  188. }
  189. else {
  190. load_plugin_textdomain( 'wordpress-seo', false, dirname( WPSEO_BASENAME ) . '/languages/' );
  191. }
  192. }
  193. add_action( 'plugins_loaded', 'wpseo_load_textdomain' );
  194. /**
  195. * On plugins_loaded: load the minimum amount of essential files for this plugin
  196. */
  197. function wpseo_init() {
  198. require_once( WPSEO_PATH . 'inc/wpseo-functions.php' );
  199. require_once( WPSEO_PATH . 'inc/wpseo-functions-deprecated.php' );
  200. // Make sure our option and meta value validation routines and default values are always registered and available.
  201. WPSEO_Options::get_instance();
  202. WPSEO_Meta::init();
  203. $options = WPSEO_Options::get_options( array( 'wpseo', 'wpseo_permalinks', 'wpseo_xml' ) );
  204. if ( version_compare( $options['version'], WPSEO_VERSION, '<' ) ) {
  205. new WPSEO_Upgrade();
  206. // Get a cleaned up version of the $options.
  207. $options = WPSEO_Options::get_options( array( 'wpseo', 'wpseo_permalinks', 'wpseo_xml' ) );
  208. }
  209. if ( $options['stripcategorybase'] === true ) {
  210. $GLOBALS['wpseo_rewrite'] = new WPSEO_Rewrite;
  211. }
  212. if ( $options['enablexmlsitemap'] === true ) {
  213. $GLOBALS['wpseo_sitemaps'] = new WPSEO_Sitemaps;
  214. }
  215. if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
  216. require_once( WPSEO_PATH . 'inc/wpseo-non-ajax-functions.php' );
  217. }
  218. // Init it here because the filter must be present on the frontend as well or it won't work in the customizer.
  219. new WPSEO_Customizer();
  220. }
  221. /**
  222. * Used to load the required files on the plugins_loaded hook, instead of immediately.
  223. */
  224. function wpseo_frontend_init() {
  225. add_action( 'init', 'initialize_wpseo_front' );
  226. $options = WPSEO_Options::get_option( 'wpseo_internallinks' );
  227. if ( $options['breadcrumbs-enable'] === true ) {
  228. /**
  229. * If breadcrumbs are active (which they supposedly are if the users has enabled this settings,
  230. * there's no reason to have bbPress breadcrumbs as well.
  231. *
  232. * @internal The class itself is only loaded when the template tag is encountered via
  233. * the template tag function in the wpseo-functions.php file
  234. */
  235. add_filter( 'bbp_get_breadcrumb', '__return_false' );
  236. }
  237. add_action( 'template_redirect', 'wpseo_frontend_head_init', 999 );
  238. }
  239. /**
  240. * Instantiate the different social classes on the frontend
  241. */
  242. function wpseo_frontend_head_init() {
  243. $options = WPSEO_Options::get_option( 'wpseo_social' );
  244. if ( $options['twitter'] === true ) {
  245. add_action( 'wpseo_head', array( 'WPSEO_Twitter', 'get_instance' ), 40 );
  246. }
  247. if ( $options['opengraph'] === true ) {
  248. $GLOBALS['wpseo_og'] = new WPSEO_OpenGraph;
  249. }
  250. }
  251. /**
  252. * Used to load the required files on the plugins_loaded hook, instead of immediately.
  253. */
  254. function wpseo_admin_init() {
  255. new WPSEO_Admin_Init();
  256. }
  257. /* ***************************** BOOTSTRAP / HOOK INTO WP *************************** */
  258. $spl_autoload_exists = function_exists( 'spl_autoload_register' );
  259. $filter_exists = function_exists( 'filter_input' );
  260. if ( ! $spl_autoload_exists ) {
  261. add_action( 'admin_init', 'yoast_wpseo_missing_spl', 1 );
  262. }
  263. if ( ! $filter_exists ) {
  264. add_action( 'admin_init', 'yoast_wpseo_missing_filter', 1 );
  265. }
  266. if ( ! function_exists( 'wp_installing' ) ) {
  267. /**
  268. * We need to define wp_installing in WordPress versions older than 4.4
  269. *
  270. * @return bool
  271. */
  272. function wp_installing() {
  273. return defined( 'WP_INSTALLING' );
  274. }
  275. }
  276. if ( ! wp_installing() && ( $spl_autoload_exists && $filter_exists ) ) {
  277. add_action( 'plugins_loaded', 'wpseo_init', 14 );
  278. if ( is_admin() ) {
  279. new Yoast_Alerts();
  280. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  281. require_once( WPSEO_PATH . 'admin/ajax.php' );
  282. // Crawl Issue Manager AJAX hooks.
  283. new WPSEO_GSC_Ajax();
  284. // Plugin conflict ajax hooks.
  285. new Yoast_Plugin_Conflict_Ajax();
  286. if ( filter_input( INPUT_POST, 'action' ) === 'inline-save' ) {
  287. add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
  288. }
  289. }
  290. else {
  291. add_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
  292. }
  293. }
  294. else {
  295. add_action( 'plugins_loaded', 'wpseo_frontend_init', 15 );
  296. }
  297. add_action( 'plugins_loaded', 'load_yoast_notifications' );
  298. }
  299. // Activation and deactivation hook.
  300. register_activation_hook( WPSEO_FILE, 'wpseo_activate' );
  301. register_deactivation_hook( WPSEO_FILE, 'wpseo_deactivate' );
  302. add_action( 'wpmu_new_blog', 'wpseo_on_activate_blog' );
  303. add_action( 'activate_blog', 'wpseo_on_activate_blog' );
  304. // Loading OnPage integration.
  305. new WPSEO_OnPage();
  306. /**
  307. * Wraps for notifications center class.
  308. */
  309. function load_yoast_notifications() {
  310. // Init Yoast_Notification_Center class.
  311. Yoast_Notification_Center::get();
  312. }
  313. /**
  314. * Throw an error if the PHP SPL extension is disabled (prevent white screens) and self-deactivate plugin
  315. *
  316. * @since 1.5.4
  317. *
  318. * @return void
  319. */
  320. function yoast_wpseo_missing_spl() {
  321. if ( is_admin() ) {
  322. add_action( 'admin_notices', 'yoast_wpseo_missing_spl_notice' );
  323. yoast_wpseo_self_deactivate();
  324. }
  325. }
  326. /**
  327. * Returns the notice in case of missing spl extension
  328. */
  329. function yoast_wpseo_missing_spl_notice() {
  330. $message = esc_html__( 'The Standard PHP Library (SPL) extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
  331. yoast_wpseo_activation_failed_notice( $message );
  332. }
  333. /**
  334. * Throw an error if the Composer autoload is missing and self-deactivate plugin
  335. *
  336. * @return void
  337. */
  338. function yoast_wpseo_missing_autoload() {
  339. if ( is_admin() ) {
  340. add_action( 'admin_notices', 'yoast_wpseo_missing_autoload_notice' );
  341. yoast_wpseo_self_deactivate();
  342. }
  343. }
  344. /**
  345. * Returns the notice in case of missing Composer autoload
  346. */
  347. function yoast_wpseo_missing_autoload_notice() {
  348. /* 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 */
  349. $message = esc_html__( 'The %1$s plugin installation is incomplete. Please refer to %2$sinstallation instructions%3$s.', 'wordpress-seo' );
  350. $message = sprintf( $message, 'Yoast SEO', '<a href="https://github.com/Yoast/wordpress-seo#installation">', '</a>' );
  351. yoast_wpseo_activation_failed_notice( $message );
  352. }
  353. /**
  354. * Throw an error if the filter extension is disabled (prevent white screens) and self-deactivate plugin
  355. *
  356. * @since 2.0
  357. *
  358. * @return void
  359. */
  360. function yoast_wpseo_missing_filter() {
  361. if ( is_admin() ) {
  362. add_action( 'admin_notices', 'yoast_wpseo_missing_filter_notice' );
  363. yoast_wpseo_self_deactivate();
  364. }
  365. }
  366. /**
  367. * Returns the notice in case of missing filter extension
  368. */
  369. function yoast_wpseo_missing_filter_notice() {
  370. $message = esc_html__( 'The filter extension seem to be unavailable. Please ask your web host to enable it.', 'wordpress-seo' );
  371. yoast_wpseo_activation_failed_notice( $message );
  372. }
  373. /**
  374. * Echo's the Activation failed notice with any given message.
  375. *
  376. * @param string $message Message string.
  377. */
  378. function yoast_wpseo_activation_failed_notice( $message ) {
  379. echo '<div class="error"><p>' . __( 'Activation failed:', 'wordpress-seo' ) . ' ' . $message . '</p></div>';
  380. }
  381. /**
  382. * The method will deactivate the plugin, but only once, done by the static $is_deactivated
  383. */
  384. function yoast_wpseo_self_deactivate() {
  385. static $is_deactivated;
  386. if ( $is_deactivated === null ) {
  387. $is_deactivated = true;
  388. deactivate_plugins( plugin_basename( WPSEO_FILE ) );
  389. if ( isset( $_GET['activate'] ) ) {
  390. unset( $_GET['activate'] );
  391. }
  392. }
  393. }