PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/class-admin-init.php

https://gitlab.com/ngochuynh1991/cuacuon
PHP | 449 lines | 249 code | 68 blank | 132 comment | 39 complexity | 84af813d79d42d755dcf7ee358f29032 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. /**
  6. * Performs the load on admin side.
  7. */
  8. class WPSEO_Admin_Init {
  9. /**
  10. * Holds the Yoast SEO Options
  11. *
  12. * @var array
  13. */
  14. private $options;
  15. /**
  16. * Holds the global `$pagenow` variable's value.
  17. *
  18. * @var string
  19. */
  20. private $pagenow;
  21. /**
  22. * Holds the asset manager.
  23. *
  24. * @var WPSEO_Admin_Asset_Manager
  25. */
  26. private $asset_manager;
  27. /**
  28. * Class constructor
  29. */
  30. public function __construct() {
  31. $this->options = WPSEO_Options::get_option( 'wpseo_xml' );
  32. $GLOBALS['wpseo_admin'] = new WPSEO_Admin;
  33. $this->pagenow = $GLOBALS['pagenow'];
  34. $this->asset_manager = new WPSEO_Admin_Asset_Manager();
  35. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_dismissible' ) );
  36. add_action( 'admin_init', array( $this, 'after_update_notice' ), 15 );
  37. add_action( 'admin_init', array( $this, 'tagline_notice' ), 15 );
  38. add_action( 'admin_init', array( $this, 'ga_compatibility_notice' ), 15 );
  39. add_action( 'admin_init', array( $this, 'recalculate_notice' ), 15 );
  40. add_action( 'admin_init', array( $this, 'ignore_tour' ) );
  41. add_action( 'admin_init', array( $this, 'load_tour' ) );
  42. add_action( 'admin_init', array( $this->asset_manager, 'register_assets' ) );
  43. add_action( 'admin_init', array( $this, 'show_hook_deprecation_warnings' ) );
  44. $this->load_meta_boxes();
  45. $this->load_taxonomy_class();
  46. $this->load_admin_page_class();
  47. $this->load_admin_user_class();
  48. $this->load_xml_sitemaps_admin();
  49. $this->sync_about_version_from_cookie();
  50. }
  51. /**
  52. * Get about version seen from cookie if set.
  53. */
  54. protected function sync_about_version_from_cookie() {
  55. $user_id = get_current_user_id();
  56. $meta_seen_about_version = get_user_meta( $user_id, 'wpseo_seen_about_version', true );
  57. $cookie_key = 'wpseo_seen_about_version_' . $user_id;
  58. $cookie_seen_about_version = isset( $_COOKIE[ $cookie_key ] ) ? $_COOKIE[ $cookie_key ] : '';
  59. if ( ! empty( $cookie_seen_about_version ) ) {
  60. if ( version_compare( $cookie_seen_about_version, $meta_seen_about_version, '>' ) ) {
  61. update_user_meta( $user_id, 'wpseo_seen_about_version', $cookie_seen_about_version );
  62. $meta_seen_about_version = $cookie_seen_about_version;
  63. }
  64. }
  65. if ( $cookie_seen_about_version !== $meta_seen_about_version ) {
  66. setcookie( $cookie_key, $meta_seen_about_version, ( $_SERVER['REQUEST_TIME'] + YEAR_IN_SECONDS ) );
  67. }
  68. }
  69. /**
  70. * For WP versions older than 4.2, this includes styles and a script to make notices dismissible.
  71. */
  72. public function enqueue_dismissible() {
  73. if ( version_compare( $GLOBALS['wp_version'], '4.2', '<' ) ) {
  74. $this->asset_manager->enqueue_script( 'dismissable' );
  75. $this->asset_manager->enqueue_style( 'dismissable' );
  76. }
  77. }
  78. /**
  79. * Redirect first time or just upgraded users to the about screen.
  80. */
  81. public function after_update_notice() {
  82. $can_access = is_multisite() ? WPSEO_Utils::grant_access() : current_user_can( 'manage_options' );
  83. if ( $can_access && $this->has_ignored_tour() && ! $this->seen_about() ) {
  84. if ( filter_input( INPUT_GET, 'intro' ) === '1' || $this->dismiss_notice( 'wpseo-dismiss-about' ) ) {
  85. update_user_meta( get_current_user_id(), 'wpseo_seen_about_version', WPSEO_VERSION );
  86. return;
  87. }
  88. /* translators: %1$s expands to Yoast SEO, $2%s to the version number, %3$s and %4$s to anchor tags with link to intro page */
  89. $info_message = sprintf(
  90. __( '%1$s has been updated to version %2$s. %3$sClick here%4$s to find out what\'s new!', 'wordpress-seo' ),
  91. 'Yoast SEO',
  92. WPSEO_VERSION,
  93. '<a href="' . admin_url( 'admin.php?page=wpseo_dashboard&intro=1' ) . '">',
  94. '</a>'
  95. );
  96. $notification_options = array(
  97. 'type' => 'updated',
  98. 'id' => 'wpseo-dismiss-about',
  99. 'nonce' => wp_create_nonce( 'wpseo-dismiss-about' ),
  100. );
  101. Yoast_Notification_Center::get()->add_notification( new Yoast_Notification( $info_message, $notification_options ) );
  102. }
  103. }
  104. /**
  105. * Helper to verify if the current user has already seen the about page for the current version
  106. *
  107. * @return bool
  108. */
  109. private function seen_about() {
  110. $seen_about_version = substr( get_user_meta( get_current_user_id(), 'wpseo_seen_about_version', true ), 0, 3 );
  111. $last_minor_version = substr( WPSEO_VERSION, 0, 3 );
  112. return version_compare( $seen_about_version, $last_minor_version, '>=' );
  113. }
  114. /**
  115. * Notify about the default tagline if the user hasn't changed it
  116. */
  117. public function tagline_notice() {
  118. // Just a return, because we want to temporary disable this notice (#3998).
  119. return;
  120. if ( current_user_can( 'manage_options' ) && $this->has_default_tagline() && ! $this->seen_tagline_notice() ) {
  121. // Only add the notice on GET requests, not in the customizer, and not in "action" type submits to prevent faulty return url.
  122. if ( 'GET' !== filter_input( INPUT_SERVER, 'REQUEST_METHOD' ) || is_customize_preview() || null !== filter_input( INPUT_GET, 'action' ) ) {
  123. return;
  124. }
  125. $current_url = ( is_ssl() ? 'https://' : 'http://' );
  126. $current_url .= sanitize_text_field( $_SERVER['SERVER_NAME'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] );
  127. $customize_url = add_query_arg( array(
  128. 'url' => urlencode( $current_url ),
  129. ), wp_customize_url() );
  130. $info_message = sprintf(
  131. __( 'You still have the default WordPress tagline, even an empty one is probably better. %1$sYou can fix this in the customizer%2$s.', 'wordpress-seo' ),
  132. '<a href="' . esc_attr( $customize_url ) . '">',
  133. '</a>'
  134. );
  135. $notification_options = array(
  136. 'type' => 'error',
  137. 'id' => 'wpseo-dismiss-tagline-notice',
  138. 'nonce' => wp_create_nonce( 'wpseo-dismiss-tagline-notice' ),
  139. );
  140. Yoast_Notification_Center::get()->add_notification( new Yoast_Notification( $info_message, $notification_options ) );
  141. }
  142. }
  143. /**
  144. * Returns whether or not the site has the default tagline
  145. *
  146. * @return bool
  147. */
  148. public function has_default_tagline() {
  149. return __( 'Just another WordPress site' ) === get_bloginfo( 'description' );
  150. }
  151. /**
  152. * Returns whether or not the user has seen the tagline notice
  153. *
  154. * @return bool
  155. */
  156. public function seen_tagline_notice() {
  157. // Check if the current request contain action to dismiss the notice.
  158. if ( $this->dismiss_notice( 'wpseo-dismiss-tagline-notice' ) ) {
  159. update_user_meta( get_current_user_id(), 'wpseo_seen_tagline_notice', 'seen' );
  160. }
  161. return 'seen' === get_user_meta( get_current_user_id(), 'wpseo_seen_tagline_notice', true );
  162. }
  163. /**
  164. * Shows a notice to the user if they have Google Analytics for WordPress 5.4.3 installed because it causes an error
  165. * on the google search console page.
  166. */
  167. public function ga_compatibility_notice() {
  168. if ( defined( 'GAWP_VERSION' ) && '5.4.3' === GAWP_VERSION ) {
  169. $info_message = sprintf(
  170. /* translators: %1$s expands to Yoast SEO, %2$s expands to 5.4.3, %3$s expands to Google Analytics by Yoast */
  171. __( '%1$s detected you are using version %2$s of %3$s, please update to the latest version to prevent compatibility issues.', 'wordpress-seo' ),
  172. 'Yoast SEO',
  173. '5.4.3',
  174. 'Google Analytics by Yoast'
  175. );
  176. $notification_options = array(
  177. 'type' => 'error',
  178. );
  179. Yoast_Notification_Center::get()->add_notification( new Yoast_Notification( $info_message, $notification_options ) );
  180. }
  181. }
  182. /**
  183. * Shows the notice for recalculating the post. the Notice will only be shown if the user hasn't dismissed it before.
  184. */
  185. public function recalculate_notice() {
  186. // Just a return, because we want to temporary disable this notice (#3998).
  187. return;
  188. if ( filter_input( INPUT_GET, 'recalculate' ) === '1' ) {
  189. update_option( 'wpseo_dismiss_recalculate', '1' );
  190. return;
  191. }
  192. $can_access = is_multisite() ? WPSEO_Utils::grant_access() : current_user_can( 'manage_options' );
  193. if ( $can_access && ! $this->is_site_notice_dismissed( 'wpseo_dismiss_recalculate' ) ) {
  194. Yoast_Notification_Center::get()->add_notification(
  195. new Yoast_Notification(
  196. /* translators: 1: is a link to 'admin_url / admin.php?page=wpseo_tools&recalculate=1' 2: closing link tag */
  197. sprintf(
  198. __( 'We\'ve updated our SEO score algorithm. %1$sClick here to recalculate the SEO scores%2$s for all posts and pages.', 'wordpress-seo' ),
  199. '<a href="' . admin_url( 'admin.php?page=wpseo_tools&recalculate=1' ) . '">',
  200. '</a>'
  201. ),
  202. array(
  203. 'type' => 'updated yoast-dismissible',
  204. 'id' => 'wpseo-dismiss-recalculate',
  205. 'nonce' => wp_create_nonce( 'wpseo-dismiss-recalculate' ),
  206. )
  207. )
  208. );
  209. }
  210. }
  211. /**
  212. * Check if the user has dismissed the given notice (by $notice_name)
  213. *
  214. * @param string $notice_name The name of the notice that might be dismissed.
  215. *
  216. * @return bool
  217. */
  218. private function is_site_notice_dismissed( $notice_name ) {
  219. return '1' === get_option( $notice_name, true );
  220. }
  221. /**
  222. * Helper to verify if the user is currently visiting one of our admin pages.
  223. *
  224. * @return bool
  225. */
  226. private function on_wpseo_admin_page() {
  227. return 'admin.php' === $this->pagenow && strpos( filter_input( INPUT_GET, 'page' ), 'wpseo' ) === 0;
  228. }
  229. /**
  230. * Determine whether we should load the meta box class and if so, load it.
  231. */
  232. private function load_meta_boxes() {
  233. $is_editor = in_array( $this->pagenow, array( 'edit.php', 'post.php', 'post-new.php' ) );
  234. $is_inline_save = filter_input( INPUT_POST, 'action' ) === 'inline-save';
  235. /**
  236. * Filter: 'wpseo_always_register_metaboxes_on_admin' - Allow developers to change whether
  237. * the WPSEO metaboxes are only registered on the typical pages (lean loading) or always
  238. * registered when in admin.
  239. *
  240. * @api bool Whether to always register the metaboxes or not. Defaults to false.
  241. */
  242. if ( $is_editor || $is_inline_save || in_array( $this->pagenow, array(
  243. 'edit.php',
  244. 'post.php',
  245. 'post-new.php',
  246. ) ) || apply_filters( 'wpseo_always_register_metaboxes_on_admin', false )
  247. ) {
  248. $GLOBALS['wpseo_metabox'] = new WPSEO_Metabox;
  249. $GLOBALS['wpseo_meta_columns'] = new WPSEO_Meta_Columns();
  250. }
  251. }
  252. /**
  253. * Determine if we should load our taxonomy edit class and if so, load it.
  254. */
  255. private function load_taxonomy_class() {
  256. if (
  257. WPSEO_Taxonomy::is_term_edit( $this->pagenow )
  258. || WPSEO_Taxonomy::is_term_overview( $this->pagenow )
  259. ) {
  260. new WPSEO_Taxonomy;
  261. }
  262. }
  263. /**
  264. * Determine if we should load our admin pages class and if so, load it.
  265. *
  266. * Loads admin page class for all admin pages starting with `wpseo_`.
  267. */
  268. private function load_admin_user_class() {
  269. if ( in_array( $this->pagenow, array( 'user-edit.php', 'profile.php' ) ) && current_user_can( 'edit_users' ) ) {
  270. new WPSEO_Admin_User_Profile;
  271. }
  272. }
  273. /**
  274. * Determine if we should load our admin pages class and if so, load it.
  275. *
  276. * Loads admin page class for all admin pages starting with `wpseo_`.
  277. */
  278. private function load_admin_page_class() {
  279. if ( $this->on_wpseo_admin_page() ) {
  280. // For backwards compatabilty, this still needs a global, for now...
  281. $GLOBALS['wpseo_admin_pages'] = new WPSEO_Admin_Pages;
  282. $this->register_i18n_promo_class();
  283. }
  284. }
  285. /**
  286. * Register the promotion class for our GlotPress instance
  287. *
  288. * @link https://github.com/Yoast/i18n-module
  289. */
  290. private function register_i18n_promo_class() {
  291. new yoast_i18n(
  292. array(
  293. 'textdomain' => 'wordpress-seo',
  294. 'project_slug' => 'wordpress-seo',
  295. 'plugin_name' => 'Yoast SEO',
  296. 'hook' => 'wpseo_admin_footer',
  297. 'glotpress_url' => 'http://translate.yoast.com/gp/',
  298. 'glotpress_name' => 'Yoast Translate',
  299. 'glotpress_logo' => 'https://translate.yoast.com/gp-templates/images/Yoast_Translate.svg',
  300. 'register_url' => 'https://translate.yoast.com/gp/projects#utm_source=plugin&utm_medium=promo-box&utm_campaign=wpseo-i18n-promo',
  301. )
  302. );
  303. }
  304. /**
  305. * See if we should start our tour.
  306. */
  307. public function load_tour() {
  308. $restart_tour = filter_input( INPUT_GET, 'wpseo_restart_tour' );
  309. if ( $restart_tour ) {
  310. delete_user_meta( get_current_user_id(), 'wpseo_ignore_tour' );
  311. }
  312. if ( ! $this->has_ignored_tour() ) {
  313. add_action( 'admin_enqueue_scripts', array( 'WPSEO_Pointers', 'get_instance' ) );
  314. }
  315. }
  316. /**
  317. * See if we should start our XML Sitemaps Admin class
  318. */
  319. private function load_xml_sitemaps_admin() {
  320. if ( $this->options['enablexmlsitemap'] === true ) {
  321. new WPSEO_Sitemaps_Admin;
  322. }
  323. }
  324. /**
  325. * Returns the value of the ignore tour.
  326. *
  327. * @return bool
  328. */
  329. private function has_ignored_tour() {
  330. $user_meta = get_user_meta( get_current_user_id(), 'wpseo_ignore_tour' );
  331. return ! empty( $user_meta );
  332. }
  333. /**
  334. * Listener for the ignore tour GET value. If this one is set, just set the user meta to true.
  335. */
  336. public function ignore_tour() {
  337. if ( filter_input( INPUT_GET, 'wpseo_ignore_tour' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'wpseo-ignore-tour' ) ) {
  338. update_user_meta( get_current_user_id(), 'wpseo_ignore_tour', true );
  339. }
  340. }
  341. /**
  342. * Shows deprecation warnings to the user if a plugin has registered a filter we have deprecated.
  343. */
  344. public function show_hook_deprecation_warnings() {
  345. global $wp_filter;
  346. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  347. return false;
  348. }
  349. // WordPress hooks that have been deprecated in Yoast SEO 3.0.
  350. $deprecated_30 = array(
  351. 'wpseo_pre_analysis_post_content',
  352. 'wpseo_metadesc_length',
  353. 'wpseo_metadesc_length_reason',
  354. 'wpseo_body_length_score',
  355. 'wpseo_linkdex_results',
  356. 'wpseo_snippet',
  357. );
  358. $deprecated_notices = array_intersect(
  359. $deprecated_30,
  360. array_keys( $wp_filter )
  361. );
  362. foreach ( $deprecated_notices as $deprecated_filter ) {
  363. _deprecated_function(
  364. /* %s expands to the actual filter/action that has been used. */
  365. sprintf( __( '%s filter/action', 'wordpress-seo' ), $deprecated_filter ),
  366. 'WPSEO 3.0',
  367. 'javascript'
  368. );
  369. }
  370. }
  371. /**
  372. * Check if there is a dismiss notice action.
  373. *
  374. * @param string $notice_name The name of the notice to dismiss.
  375. *
  376. * @return bool
  377. */
  378. private function dismiss_notice( $notice_name ) {
  379. return filter_input( INPUT_GET, $notice_name ) === '1' && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), $notice_name );
  380. }
  381. }