WPSEO_Admin_Init

Performs the load on admin side.

Defined (1)

The class is defined in the following location(s).

/admin/class-admin-init.php  
  1. class WPSEO_Admin_Init { 
  2.  
  3. /** 
  4. * Holds the Yoast SEO Options 
  5. * @var array 
  6. */ 
  7. private $options; 
  8.  
  9. /** 
  10. * Holds the global `$pagenow` variable's value. 
  11. * @var string 
  12. */ 
  13. private $pagenow; 
  14.  
  15. /** 
  16. * Holds the asset manager. 
  17. * @var WPSEO_Admin_Asset_Manager 
  18. */ 
  19. private $asset_manager; 
  20.  
  21. /** 
  22. * Class constructor 
  23. */ 
  24. public function __construct() { 
  25. $this->options = WPSEO_Options::get_option( 'wpseo_xml' ); 
  26.  
  27. $GLOBALS['wpseo_admin'] = new WPSEO_Admin; 
  28.  
  29. $this->pagenow = $GLOBALS['pagenow']; 
  30.  
  31. $this->asset_manager = new WPSEO_Admin_Asset_Manager(); 
  32.  
  33. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_dismissible' ) ); 
  34. add_action( 'admin_init', array( $this, 'tagline_notice' ), 15 ); 
  35. add_action( 'admin_init', array( $this, 'blog_public_notice' ), 15 ); 
  36. add_action( 'admin_init', array( $this, 'permalink_notice' ), 15 ); 
  37. add_action( 'admin_init', array( $this, 'page_comments_notice' ), 15 ); 
  38. add_action( 'admin_init', array( $this, 'ga_compatibility_notice' ), 15 ); 
  39. add_action( 'admin_init', array( $this, 'yoast_plugin_compatibility_notification' ), 15 ); 
  40. add_action( 'admin_init', array( $this, 'recalculate_notice' ), 15 ); 
  41. add_action( 'admin_init', array( $this->asset_manager, 'register_assets' ) ); 
  42. add_action( 'admin_init', array( $this, 'show_hook_deprecation_warnings' ) ); 
  43. add_action( 'admin_init', array( 'WPSEO_Plugin_Conflict', 'hook_check_for_plugin_conflicts' ) ); 
  44.  
  45. $this->load_meta_boxes(); 
  46. $this->load_taxonomy_class(); 
  47. $this->load_admin_page_class(); 
  48. $this->load_admin_user_class(); 
  49. $this->load_xml_sitemaps_admin(); 
  50.  
  51. /** 
  52. * Enqueue our styling for dismissible yoast notifications. 
  53. */ 
  54. public function enqueue_dismissible() { 
  55. $this->asset_manager->enqueue_style( 'dismissible' ); 
  56.  
  57. /** 
  58. * Helper to verify if the current user has already seen the about page for the current version 
  59. * @return bool 
  60. */ 
  61. private function seen_about() { 
  62. $seen_about_version = substr( get_user_meta( get_current_user_id(), 'wpseo_seen_about_version', true ), 0, 3 ); 
  63. $last_minor_version = substr( WPSEO_VERSION, 0, 3 ); 
  64.  
  65. return version_compare( $seen_about_version, $last_minor_version, '>=' ); 
  66.  
  67. /** 
  68. * Notify about the default tagline if the user hasn't changed it 
  69. */ 
  70. public function tagline_notice() { 
  71.  
  72. $current_url = ( is_ssl() ? 'https://' : 'http://' ); 
  73. $current_url .= sanitize_text_field( $_SERVER['SERVER_NAME'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] ); 
  74. $customize_url = add_query_arg( array( 
  75. 'url' => urlencode( $current_url ),  
  76. ), wp_customize_url() ); 
  77.  
  78. $info_message = sprintf( 
  79. /** translators: 1: link open tag; 2: link close tag. */ 
  80. __( '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' ),  
  81. '<a href="' . esc_attr( $customize_url ) . '">',  
  82. '</a>' 
  83. ); 
  84.  
  85. $notification_options = array( 
  86. 'type' => Yoast_Notification::ERROR,  
  87. 'id' => 'wpseo-dismiss-tagline-notice',  
  88. 'capabilities' => 'manage_options',  
  89. ); 
  90.  
  91. $tagline_notification = new Yoast_Notification( $info_message, $notification_options ); 
  92.  
  93. $notification_center = Yoast_Notification_Center::get(); 
  94. if ( $this->has_default_tagline() ) { 
  95. $notification_center->add_notification( $tagline_notification ); 
  96. else { 
  97. $notification_center->remove_notification( $tagline_notification ); 
  98.  
  99. /** 
  100. * Add an alert if the blog is not publicly visible 
  101. */ 
  102. public function blog_public_notice() { 
  103.  
  104. $info_message = '<strong>' . __( 'Huge SEO Issue: You\'re blocking access to robots.', 'wordpress-seo' ) . '</strong> '; 
  105. $info_message .= sprintf( 
  106. /** translators: %1$s resolves to the opening tag of the link to the reading settings, %1$s resolves to the closing tag for the link */ 
  107. __( 'You must %1$sgo to your Reading Settings%2$s and uncheck the box for Search Engine Visibility.', 'wordpress-seo' ),  
  108. '<a href="' . esc_url( admin_url( 'options-reading.php' ) ) . '">',  
  109. '</a>' 
  110. ); 
  111.  
  112. $notification_options = array( 
  113. 'type' => Yoast_Notification::ERROR,  
  114. 'id' => 'wpseo-dismiss-blog-public-notice',  
  115. 'priority' => 1.0,  
  116. 'capabilities' => 'manage_options',  
  117. ); 
  118.  
  119. $notification = new Yoast_Notification( $info_message, $notification_options ); 
  120.  
  121. $notification_center = Yoast_Notification_Center::get(); 
  122. if ( ! $this->is_blog_public() ) { 
  123. $notification_center->add_notification( $notification ); 
  124. else { 
  125. $notification_center->remove_notification( $notification ); 
  126.  
  127. /** 
  128. * Display notice to disable comment pagination 
  129. */ 
  130. public function page_comments_notice() { 
  131.  
  132. $info_message = __( 'Paging comments is enabled, this is not needed in 999 out of 1000 cases, we recommend to disable it.', 'wordpress-seo' ); 
  133. $info_message .= '<br/>'; 
  134.  
  135. $info_message .= sprintf( 
  136. /** translators: %1$s resolves to the opening tag of the link to the comment setting page, %2$s resolves to the closing tag of the link */ 
  137. __( 'Simply uncheck the box before "Break comments into pages..." on the %1$sComment settings page%2$s.', 'wordpress-seo' ),  
  138. '<a href="' . esc_url( admin_url( 'options-discussion.php' ) ) . '">',  
  139. '</a>' 
  140. ); 
  141.  
  142. $notification_options = array( 
  143. 'type' => Yoast_Notification::WARNING,  
  144. 'id' => 'wpseo-dismiss-page_comments-notice',  
  145. 'capabilities' => 'manage_options',  
  146. ); 
  147.  
  148. $tagline_notification = new Yoast_Notification( $info_message, $notification_options ); 
  149.  
  150. $notification_center = Yoast_Notification_Center::get(); 
  151. if ( $this->has_page_comments() ) { 
  152. $notification_center->add_notification( $tagline_notification ); 
  153. else { 
  154. $notification_center->remove_notification( $tagline_notification ); 
  155.  
  156. /** 
  157. * Returns whether or not the site has the default tagline 
  158. * @return bool 
  159. */ 
  160. public function has_default_tagline() { 
  161. $blog_description = get_bloginfo( 'description' ); 
  162. $default_blog_description = 'Just another WordPress site'; 
  163. $translated_blog_description = __( 'Just another WordPress site' ); 
  164. return $translated_blog_description === $blog_description || $default_blog_description === $blog_description; 
  165.  
  166. /** 
  167. * Show alert when the permalink doesn't contain %postname% 
  168. */ 
  169. public function permalink_notice() { 
  170.  
  171. $info_message = __( 'You do not have your postname in the URL of your posts and pages, it is highly recommended that you do. Consider setting your permalink structure to <strong>/%postname%/</strong>.', 'wordpress-seo' ); 
  172. $info_message .= '<br/>'; 
  173. $info_message .= sprintf( 
  174. /** translators: %1$s resolves to the starting tag of the link to the permalink settings page, %2$s resolves to the closing tag of the link */ 
  175. __( 'You can fix this on the %1$sPermalink settings page%2$s.', 'wordpress-seo' ),  
  176. '<a href="' . admin_url( 'options-permalink.php' ) . '">',  
  177. '</a>' 
  178. ); 
  179.  
  180. $notification_options = array( 
  181. 'type' => Yoast_Notification::WARNING,  
  182. 'id' => 'wpseo-dismiss-permalink-notice',  
  183. 'capabilities' => 'manage_options',  
  184. 'priority' => 0.8,  
  185. ); 
  186.  
  187. $notification = new Yoast_Notification( $info_message, $notification_options ); 
  188.  
  189. $notification_center = Yoast_Notification_Center::get(); 
  190. if ( ! $this->has_postname_in_permalink() ) { 
  191. $notification_center->add_notification( $notification ); 
  192. else { 
  193. $notification_center->remove_notification( $notification ); 
  194.  
  195. /** 
  196. * Are page comments enabled 
  197. * @return bool 
  198. */ 
  199. public function has_page_comments() { 
  200. return '1' === get_option( 'page_comments' ); 
  201.  
  202. /** 
  203. * Shows a notice to the user if they have Google Analytics for WordPress 5.4.3 installed because it causes an error 
  204. * on the google search console page. 
  205. */ 
  206. public function ga_compatibility_notice() { 
  207.  
  208. $notification = $this->get_compatibility_notification(); 
  209. $notification_center = Yoast_Notification_Center::get(); 
  210.  
  211. if ( defined( 'GAWP_VERSION' ) && '5.4.3' === GAWP_VERSION ) { 
  212. $notification_center->add_notification( $notification ); 
  213. else { 
  214. $notification_center->remove_notification( $notification ); 
  215.  
  216. /** 
  217. * Build compatibility problem notification 
  218. * @return Yoast_Notification 
  219. */ 
  220. private function get_compatibility_notification() { 
  221. $info_message = sprintf( 
  222. /** translators: %1$s expands to Yoast SEO, %2$s expands to 5.4.3, %3$s expands to Google Analytics by Yoast */ 
  223. __( '%1$s detected you are using version %2$s of %3$s, please update to the latest version to prevent compatibility issues.', 'wordpress-seo' ),  
  224. 'Yoast SEO',  
  225. '5.4.3',  
  226. 'Google Analytics by Yoast' 
  227. ); 
  228.  
  229. return new Yoast_Notification( 
  230. $info_message,  
  231. array( 
  232. 'id' => 'gawp-compatibility-notice',  
  233. 'type' => Yoast_Notification::ERROR,  
  234. ); 
  235.  
  236. /** 
  237. * Add an alert if outdated versions of Yoast SEO plugins are running. 
  238. */ 
  239. public function yoast_plugin_compatibility_notification() { 
  240. $compatibility_checker = new WPSEO_Plugin_Compatibility( WPSEO_VERSION ); 
  241. $plugins = $compatibility_checker->get_installed_plugins_compatibility(); 
  242.  
  243. $notification_center = Yoast_Notification_Center::get(); 
  244.  
  245. foreach ( $plugins as $name => $plugin ) { 
  246. $type = ( $plugin['active'] ) ? Yoast_Notification::ERROR : Yoast_Notification::WARNING; 
  247. $notification = $this->get_yoast_seo_compatibility_notification( $name, $plugin, $type ); 
  248.  
  249. if ( $plugin['compatible'] === false ) { 
  250. $notification_center->add_notification( $notification ); 
  251. else { 
  252. $notification_center->remove_notification( $notification ); 
  253.  
  254. /** 
  255. * Build Yoast SEO compatibility problem notification 
  256. * @param string $name The plugin name to use for the unique ID. 
  257. * @param array $plugin The plugin to retrieve the data from. 
  258. * @param string $level The severity level to use for the notification. 
  259. * @return Yoast_Notification 
  260. */ 
  261. private function get_yoast_seo_compatibility_notification( $name, $plugin, $level = Yoast_Notification::WARNING ) { 
  262. $info_message = sprintf( 
  263. /** translators: %1$s expands to Yoast SEO, %2$s expands to the plugin version, %3$s expands to the plugin name */ 
  264. __( '%1$s detected you are using version %2$s of %3$s, please update to the latest version to prevent compatibility issues.', 'wordpress-seo' ),  
  265. 'Yoast SEO',  
  266. $plugin['version'],  
  267. $plugin['title'] 
  268. ); 
  269.  
  270. return new Yoast_Notification( 
  271. $info_message,  
  272. array( 
  273. 'id' => 'wpseo-outdated-yoast-seo-plugin-' . $name,  
  274. 'type' => $level,  
  275. ); 
  276.  
  277. /** 
  278. * Shows the notice for recalculating the post. the Notice will only be shown if the user hasn't dismissed it before. 
  279. */ 
  280. public function recalculate_notice() { 
  281. // Just a return, because we want to temporary disable this notice (#3998 and #4532). 
  282. return; 
  283.  
  284. if ( filter_input( INPUT_GET, 'recalculate' ) === '1' ) { 
  285. update_option( 'wpseo_dismiss_recalculate', '1' ); 
  286. return; 
  287.  
  288. $can_access = is_multisite() ? WPSEO_Utils::grant_access() : current_user_can( 'manage_options' ); 
  289. if ( $can_access && ! $this->is_site_notice_dismissed( 'wpseo_dismiss_recalculate' ) ) { 
  290. Yoast_Notification_Center::get()->add_notification( 
  291. new Yoast_Notification( 
  292. sprintf( 
  293. /** translators: 1: is a link to 'admin_url / admin.php?page=wpseo_tools&recalculate=1' 2: closing link tag */ 
  294. __( 'We\'ve updated our SEO score algorithm. %1$sRecalculate the SEO scores%2$s for all posts and pages.', 'wordpress-seo' ),  
  295. '<a href="' . admin_url( 'admin.php?page=wpseo_tools&recalculate=1' ) . '">',  
  296. '</a>' 
  297. ),  
  298. array( 
  299. 'type' => 'updated yoast-dismissible',  
  300. 'id' => 'wpseo-dismiss-recalculate',  
  301. 'nonce' => wp_create_nonce( 'wpseo-dismiss-recalculate' ),  
  302. ); 
  303.  
  304. /** 
  305. * Check if the user has dismissed the given notice (by $notice_name) 
  306. * @param string $notice_name The name of the notice that might be dismissed. 
  307. * @return bool 
  308. */ 
  309. private function is_site_notice_dismissed( $notice_name ) { 
  310. return '1' === get_option( $notice_name, true ); 
  311.  
  312. /** 
  313. * Helper to verify if the user is currently visiting one of our admin pages. 
  314. * @return bool 
  315. */ 
  316. private function on_wpseo_admin_page() { 
  317. return 'admin.php' === $this->pagenow && strpos( filter_input( INPUT_GET, 'page' ), 'wpseo' ) === 0; 
  318.  
  319. /** 
  320. * Determine whether we should load the meta box class and if so, load it. 
  321. */ 
  322. private function load_meta_boxes() { 
  323.  
  324. $is_editor = WPSEO_Metabox::is_post_overview( $this->pagenow ) || WPSEO_Metabox::is_post_edit( $this->pagenow ); 
  325. $is_inline_save = filter_input( INPUT_POST, 'action' ) === 'inline-save'; 
  326.  
  327. /** 
  328. * Filter: 'wpseo_always_register_metaboxes_on_admin' - Allow developers to change whether 
  329. * the WPSEO metaboxes are only registered on the typical pages (lean loading) or always 
  330. * registered when in admin. 
  331. * @api bool Whether to always register the metaboxes or not. Defaults to false. 
  332. */ 
  333. if ( $is_editor || $is_inline_save || apply_filters( 'wpseo_always_register_metaboxes_on_admin', false ) 
  334. ) { 
  335. $GLOBALS['wpseo_metabox'] = new WPSEO_Metabox; 
  336. $GLOBALS['wpseo_meta_columns'] = new WPSEO_Meta_Columns(); 
  337.  
  338. /** 
  339. * Determine if we should load our taxonomy edit class and if so, load it. 
  340. */ 
  341. private function load_taxonomy_class() { 
  342. if ( 
  343. WPSEO_Taxonomy::is_term_edit( $this->pagenow ) 
  344. || WPSEO_Taxonomy::is_term_overview( $this->pagenow ) 
  345. ) { 
  346. new WPSEO_Taxonomy; 
  347.  
  348. /** 
  349. * Determine if we should load our admin pages class and if so, load it. 
  350. * Loads admin page class for all admin pages starting with `wpseo_`. 
  351. */ 
  352. private function load_admin_user_class() { 
  353. if ( in_array( $this->pagenow, array( 'user-edit.php', 'profile.php' ) ) && current_user_can( 'edit_users' ) ) { 
  354. new WPSEO_Admin_User_Profile; 
  355.  
  356. /** 
  357. * Determine if we should load our admin pages class and if so, load it. 
  358. * Loads admin page class for all admin pages starting with `wpseo_`. 
  359. */ 
  360. private function load_admin_page_class() { 
  361.  
  362. if ( $this->on_wpseo_admin_page() ) { 
  363. // For backwards compatabilty, this still needs a global, for now... 
  364. $GLOBALS['wpseo_admin_pages'] = new WPSEO_Admin_Pages; 
  365.  
  366. // Only register the yoast i18n when the page is a Yoast SEO page. 
  367. if ( WPSEO_Utils::is_yoast_seo_free_page( filter_input( INPUT_GET, 'page' ) ) ) { 
  368. $this->register_i18n_promo_class(); 
  369.  
  370. /** 
  371. * Register the promotion class for our GlotPress instance 
  372. * @link https://github.com/Yoast/i18n-module 
  373. */ 
  374. private function register_i18n_promo_class() { 
  375. // BC, because an older version of the i18n-module didn't have this class. 
  376. new Yoast_I18n_WordPressOrg_v2( 
  377. array( 
  378. 'textdomain' => 'wordpress-seo',  
  379. 'plugin_name' => 'Yoast SEO',  
  380. 'hook' => 'wpseo_admin_promo_footer',  
  381. ); 
  382.  
  383. /** 
  384. * See if we should start our XML Sitemaps Admin class 
  385. */ 
  386. private function load_xml_sitemaps_admin() { 
  387. if ( $this->options['enablexmlsitemap'] === true ) { 
  388. new WPSEO_Sitemaps_Admin; 
  389.  
  390. /** 
  391. * Check if the site is set to be publicly visible 
  392. * @return bool 
  393. */ 
  394. private function is_blog_public() { 
  395. return '1' == get_option( 'blog_public' ); 
  396.  
  397. /** 
  398. * Shows deprecation warnings to the user if a plugin has registered a filter we have deprecated. 
  399. */ 
  400. public function show_hook_deprecation_warnings() { 
  401. global $wp_filter; 
  402.  
  403. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  404. return false; 
  405.  
  406. // WordPress hooks that have been deprecated in Yoast SEO 3.0. 
  407. $deprecated_30 = array( 
  408. 'wpseo_pre_analysis_post_content',  
  409. 'wpseo_metadesc_length',  
  410. 'wpseo_metadesc_length_reason',  
  411. 'wpseo_body_length_score',  
  412. 'wpseo_linkdex_results',  
  413. 'wpseo_snippet',  
  414. ); 
  415.  
  416. $deprecated_notices = array_intersect( 
  417. $deprecated_30,  
  418. array_keys( $wp_filter ) 
  419. ); 
  420.  
  421. foreach ( $deprecated_notices as $deprecated_filter ) { 
  422. _deprecated_function( 
  423. /** translators: %s expands to the actual filter/action that has been used. */ 
  424. sprintf( __( '%s filter/action', 'wordpress-seo' ), $deprecated_filter ),  
  425. 'WPSEO 3.0',  
  426. 'javascript' 
  427. ); 
  428.  
  429. /** 
  430. * Check if there is a dismiss notice action. 
  431. * @param string $notice_name The name of the notice to dismiss. 
  432. * @return bool 
  433. */ 
  434. private function dismiss_notice( $notice_name ) { 
  435. return filter_input( INPUT_GET, $notice_name ) === '1' && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), $notice_name ); 
  436.  
  437. /** 
  438. * Check if the permalink uses %postname% 
  439. * @return bool 
  440. */ 
  441. private function has_postname_in_permalink() { 
  442. return ( false !== strpos( get_option( 'permalink_structure' ), '%postname%' ) ); 
  443.  
  444. /********************** DEPRECATED METHODS **********************/ 
  445.  
  446. /** 
  447. * Returns whether or not the user has seen the tagline notice 
  448. * @deprecated 3.3 
  449. * @codeCoverageIgnore 
  450. * @return bool 
  451. */ 
  452. public function seen_tagline_notice() { 
  453. _deprecated_function( __METHOD__, 'WPSEO 3.3.0' ); 
  454. return false; 
  455.  
  456. /** 
  457. * Redirect first time or just upgraded users to the about screen. 
  458. * @deprecated 3.5 
  459. * @codeCoverageIgnore 
  460. */ 
  461. public function after_update_notice() { 
  462. _deprecated_function( __METHOD__, 'WPSEO 3.5' );