class-admin-menu.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Menu
  6. */
  7. /**
  8. * Registers the admin menu on the left of the admin area.
  9. */
  10. class WPSEO_Admin_Menu extends WPSEO_Base_Menu {
  11. /**
  12. * Registers all hooks to WordPress.
  13. *
  14. * @return void
  15. */
  16. public function register_hooks() {
  17. // Needs the lower than default priority so other plugins can hook underneath it without issue.
  18. add_action( 'admin_menu', array( $this, 'register_settings_page' ), 5 );
  19. }
  20. /**
  21. * Registers the menu item submenus.
  22. */
  23. public function register_settings_page() {
  24. $can_manage_options = $this->check_manage_capability();
  25. if ( $can_manage_options ) {
  26. /*
  27. * The current user has the capability to control anything.
  28. * This means that all submenus and dashboard can be shown.
  29. */
  30. global $admin_page_hooks;
  31. add_menu_page(
  32. 'Yoast SEO: ' . __( 'Dashboard', 'wordpress-seo' ),
  33. __( 'SEO', 'wordpress-seo' ) . ' ' . $this->get_notification_counter(),
  34. $this->get_manage_capability(),
  35. $this->get_page_identifier(),
  36. $this->get_admin_page_callback(),
  37. WPSEO_Utils::get_icon_svg(),
  38. '99.31337'
  39. );
  40. // Wipe notification bits from hooks.
  41. $admin_page_hooks[ $this->get_page_identifier() ] = 'seo';
  42. }
  43. // Get all submenu pages.
  44. $submenu_pages = $this->get_submenu_pages();
  45. // Add submenu items to the main menu if possible.
  46. if ( $can_manage_options ) {
  47. $this->register_submenu_pages( $submenu_pages );
  48. }
  49. /*
  50. * If the user does not have the general manage options capability,
  51. * we need to make sure the desired sub-item can be reached.
  52. */
  53. if ( ! $can_manage_options ) {
  54. $this->register_menu_pages( $submenu_pages );
  55. }
  56. }
  57. /**
  58. * Returns the list of registered submenu pages.
  59. *
  60. * @return array List of registered submenu pages.
  61. */
  62. public function get_submenu_pages() {
  63. global $wpseo_admin;
  64. $search_console_callback = null;
  65. $search_console_hook_callbacks = null;
  66. // Account for when the available submenu pages are requested from outside the admin.
  67. if ( isset( $wpseo_admin ) ) {
  68. $admin_features = $wpseo_admin->get_admin_features();
  69. $search_console_callback = array( $admin_features['google_search_console'], 'display' );
  70. $search_console_hook_callbacks = array( array( $admin_features['google_search_console'], 'set_help' ) );
  71. }
  72. // Submenu pages.
  73. $submenu_pages = array(
  74. $this->get_submenu_page( __( 'General', 'wordpress-seo' ), $this->get_page_identifier() ),
  75. $this->get_submenu_page( __( 'Search Appearance', 'wordpress-seo' ), 'wpseo_titles' ),
  76. $this->get_submenu_page(
  77. __( 'Search Console', 'wordpress-seo' ),
  78. 'wpseo_search_console',
  79. $search_console_callback,
  80. $search_console_hook_callbacks
  81. ),
  82. $this->get_submenu_page( __( 'Social', 'wordpress-seo' ), 'wpseo_social' ),
  83. $this->get_submenu_page( __( 'Tools', 'wordpress-seo' ), 'wpseo_tools' ),
  84. $this->get_submenu_page( $this->get_license_page_title(), 'wpseo_licenses' ),
  85. );
  86. /**
  87. * Filter: 'wpseo_submenu_pages' - Collects all submenus that need to be shown.
  88. *
  89. * @api array $submenu_pages List with all submenu pages.
  90. */
  91. return (array) apply_filters( 'wpseo_submenu_pages', $submenu_pages );
  92. }
  93. /**
  94. * Returns the notification count in HTML format.
  95. *
  96. * @return string The notification count in HTML format.
  97. */
  98. protected function get_notification_counter() {
  99. $notification_center = Yoast_Notification_Center::get();
  100. $notification_count = $notification_center->get_notification_count();
  101. // Add main page.
  102. /* translators: %s: number of notifications */
  103. $notifications = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) );
  104. $counter = sprintf( '<span class="update-plugins count-%1$d"><span class="plugin-count" aria-hidden="true">%1$d</span><span class="screen-reader-text">%2$s</span></span>', $notification_count, $notifications );
  105. return $counter;
  106. }
  107. /**
  108. * Returns the capability that is required to manage all options.
  109. *
  110. * @return string Capability to check against.
  111. */
  112. protected function get_manage_capability() {
  113. /**
  114. * Filter: 'wpseo_manage_options_capability' - Allow changing the capability users need to view the settings pages
  115. *
  116. * @deprecated 5.5
  117. * @api string unsigned The capability
  118. */
  119. return apply_filters_deprecated( 'wpseo_manage_options_capability', array( 'wpseo_manage_options' ), 'WPSEO 5.5.0', false, 'Use the introduced wpseo_manage_options capability instead.' );
  120. }
  121. }