PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

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