class-admin-menu.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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', [ $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. // Account for when the available submenu pages are requested from outside the admin.
  66. if ( isset( $wpseo_admin ) ) {
  67. $google_search_console = new WPSEO_GSC();
  68. $search_console_callback = [ $google_search_console, 'display' ];
  69. }
  70. // Submenu pages.
  71. $submenu_pages = [
  72. $this->get_submenu_page( __( 'General', 'wordpress-seo' ), $this->get_page_identifier() ),
  73. $this->get_submenu_page( __( 'Search Appearance', 'wordpress-seo' ), 'wpseo_titles' ),
  74. $this->get_submenu_page(
  75. __( 'Search Console', 'wordpress-seo' ),
  76. 'wpseo_search_console',
  77. $search_console_callback
  78. ),
  79. $this->get_submenu_page( __( 'Social', 'wordpress-seo' ), 'wpseo_social' ),
  80. $this->get_submenu_page( __( 'Tools', 'wordpress-seo' ), 'wpseo_tools' ),
  81. $this->get_submenu_page( $this->get_license_page_title(), 'wpseo_licenses' ),
  82. ];
  83. /**
  84. * Filter: 'wpseo_submenu_pages' - Collects all submenus that need to be shown.
  85. *
  86. * @api array $submenu_pages List with all submenu pages.
  87. */
  88. return (array) apply_filters( 'wpseo_submenu_pages', $submenu_pages );
  89. }
  90. /**
  91. * Returns the notification count in HTML format.
  92. *
  93. * @return string The notification count in HTML format.
  94. */
  95. protected function get_notification_counter() {
  96. $notification_center = Yoast_Notification_Center::get();
  97. $notification_count = $notification_center->get_notification_count();
  98. // Add main page.
  99. /* translators: %s: number of notifications */
  100. $notifications = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) );
  101. $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 );
  102. return $counter;
  103. }
  104. /**
  105. * Returns the capability that is required to manage all options.
  106. *
  107. * @return string Capability to check against.
  108. */
  109. protected function get_manage_capability() {
  110. return 'wpseo_manage_options';
  111. }
  112. }