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

https://bitbucket.org/carloskikea/helpet · PHP · 272 lines · 126 code · 35 blank · 111 comment · 16 complexity · ef6ce60f98cfe65ef1281d5d3135ff05 MD5 · raw file

  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 implements WPSEO_WordPress_Integration {
  11. /** @var WPSEO_Menu Menu */
  12. protected $menu;
  13. /**
  14. * Constructs the Admin Menu.
  15. *
  16. * @param WPSEO_Menu $menu Menu to use.
  17. */
  18. public function __construct( WPSEO_Menu $menu ) {
  19. $this->menu = $menu;
  20. }
  21. /**
  22. * Registers all hooks to WordPress.
  23. *
  24. * @return void
  25. */
  26. public function register_hooks() {
  27. // Needs the lower than default priority so other plugins can hook underneath it without issue.
  28. add_action( 'admin_menu', array( $this, 'register_settings_page' ), 5 );
  29. }
  30. /**
  31. * Registers the menu item submenus.
  32. */
  33. public function register_settings_page() {
  34. $can_manage_options = WPSEO_Capability_Utils::current_user_can( $this->get_manage_capability() );
  35. if ( $can_manage_options ) {
  36. /*
  37. * The current user has the capability to control anything.
  38. * This means that all submenus and dashboard can be shown.
  39. */
  40. global $admin_page_hooks;
  41. add_menu_page(
  42. 'Yoast SEO: ' . __( 'Dashboard', 'wordpress-seo' ),
  43. __( 'SEO', 'wordpress-seo' ) . ' ' . $this->get_notification_counter(),
  44. $this->get_manage_capability(),
  45. $this->menu->get_page_identifier(),
  46. $this->get_admin_page_callback(),
  47. WPSEO_Utils::get_icon_svg(),
  48. '99.31337'
  49. );
  50. $admin_page_hooks[ $this->menu->get_page_identifier() ] = 'seo'; // Wipe notification bits from hooks. R.
  51. }
  52. // Get all submenu pages.
  53. $submenu_pages = $this->get_submenu_pages();
  54. // Add submenu items to the main menu if possible.
  55. if ( $can_manage_options ) {
  56. $this->register_submenu_pages( $submenu_pages );
  57. }
  58. /*
  59. * If the user does not have the general manage options capability,
  60. * we need to make sure the desired sub-item can be reached.
  61. */
  62. if ( ! $can_manage_options ) {
  63. $this->register_menu_pages( $submenu_pages );
  64. }
  65. }
  66. /**
  67. * Registers submenu pages as menu pages.
  68. *
  69. * @param array $submenu_pages List of submenu pages.
  70. */
  71. protected function register_menu_pages( $submenu_pages ) {
  72. if ( ! is_array( $submenu_pages ) || $submenu_pages === array() ) {
  73. return;
  74. }
  75. // Loop through submenu pages and add them.
  76. foreach ( $submenu_pages as $submenu_page ) {
  77. if ( $submenu_page[3] === $this->get_manage_capability() ) {
  78. continue;
  79. }
  80. // Register submenu as menu page.
  81. add_menu_page(
  82. 'Yoast SEO: ' . $submenu_page[2],
  83. $submenu_page[2],
  84. $submenu_page[3],
  85. $submenu_page[4],
  86. $submenu_page[5],
  87. WPSEO_Utils::get_icon_svg(),
  88. '99.31337'
  89. );
  90. }
  91. }
  92. /**
  93. * Returns the list of registered submenu pages.
  94. *
  95. * @return array List of registered submenu pages.
  96. */
  97. protected function get_submenu_pages() {
  98. global $wpseo_admin;
  99. /** WPSEO_Admin $wpseo_admin */
  100. $admin_features = $wpseo_admin->get_admin_features();
  101. // Submenu pages.
  102. $submenu_pages = array(
  103. $this->get_submenu_page( __( 'General', 'wordpress-seo' ), $this->menu->get_page_identifier() ),
  104. $this->get_submenu_page( __( 'Search Appearance', 'wordpress-seo' ), 'wpseo_titles' ),
  105. $this->get_submenu_page(
  106. __( 'Search Console', 'wordpress-seo' ),
  107. 'wpseo_search_console',
  108. array( $admin_features['google_search_console'], 'display' ),
  109. array( array( $admin_features['google_search_console'], 'set_help' ) )
  110. ),
  111. $this->get_submenu_page( __( 'Social', 'wordpress-seo' ), 'wpseo_social' ),
  112. $this->get_submenu_page( __( 'Tools', 'wordpress-seo' ), 'wpseo_tools' ),
  113. $this->get_submenu_page( $this->get_license_page_title(), 'wpseo_licenses' ),
  114. );
  115. /**
  116. * Filter: 'wpseo_submenu_pages' - Collects all submenus that need to be shown.
  117. *
  118. * @api array $submenu_pages List with all submenu pages.
  119. */
  120. return (array) apply_filters( 'wpseo_submenu_pages', $submenu_pages );
  121. }
  122. /**
  123. * Creates a submenu formatted array.
  124. *
  125. * @param string $page_title Page title to use.
  126. * @param string $page_slug Page slug to use.
  127. * @param callable $callback Optional. Callback which handles the page request.
  128. * @param callable[] $hook Optional. Hook to trigger when the page is registered.
  129. *
  130. * @return array Formatted submenu.
  131. */
  132. protected function get_submenu_page( $page_title, $page_slug, $callback = null, $hook = null ) {
  133. if ( $callback === null ) {
  134. $callback = $this->get_admin_page_callback();
  135. }
  136. return array(
  137. $this->menu->get_page_identifier(),
  138. '',
  139. $page_title,
  140. $this->get_manage_capability(),
  141. $page_slug,
  142. $callback,
  143. $hook,
  144. );
  145. }
  146. /**
  147. * Registers the submenu pages.
  148. *
  149. * This is only done when the user has the `wpseo_manage_options` capability,
  150. * thus all capabilities can be set to this capability.
  151. *
  152. * @param array $submenu_pages List of submenu pages to register.
  153. *
  154. * @return void
  155. */
  156. protected function register_submenu_pages( $submenu_pages ) {
  157. if ( ! is_array( $submenu_pages ) || $submenu_pages === array() ) {
  158. return;
  159. }
  160. // Loop through submenu pages and add them.
  161. foreach ( $submenu_pages as $submenu_page ) {
  162. $page_title = $submenu_page[2];
  163. // We cannot use $submenu_page[1] because add-ons define that, so hard-code this value.
  164. if ( $submenu_page[4] === 'wpseo_licenses' ) {
  165. $page_title = $this->get_license_page_title();
  166. }
  167. $page_title .= ' - Yoast SEO';
  168. /*
  169. * Add submenu page.
  170. *
  171. * If we don't register this on `wpseo_manage_options`, admin users with only this capability
  172. * will not be able to see the submenus which are configured with something else,
  173. * thus all submenu pages are registered with the `wpseo_manage_options` capability here.
  174. */
  175. $admin_page = add_submenu_page( $submenu_page[0], $page_title, $submenu_page[2], $this->get_manage_capability(), $submenu_page[4], $submenu_page[5] );
  176. // Check if we need to hook.
  177. if ( isset( $submenu_page[6] ) && ( is_array( $submenu_page[6] ) && $submenu_page[6] !== array() ) ) {
  178. foreach ( $submenu_page[6] as $submenu_page_action ) {
  179. add_action( 'load-' . $admin_page, $submenu_page_action );
  180. }
  181. }
  182. }
  183. // Use WordPress global $submenu to directly access it's properties.
  184. global $submenu;
  185. if ( isset( $submenu[ $this->menu->get_page_identifier() ] ) && WPSEO_Capability_Utils::current_user_can( $this->get_manage_capability() ) ) {
  186. $submenu[ $this->menu->get_page_identifier() ][0][0] = __( 'General', 'wordpress-seo' );
  187. }
  188. }
  189. /**
  190. * Returns the notification count in HTML format.
  191. *
  192. * @return string The notification count in HTML format.
  193. */
  194. protected function get_notification_counter() {
  195. $notification_center = Yoast_Notification_Center::get();
  196. $notification_count = $notification_center->get_notification_count();
  197. // Add main page.
  198. /* translators: %s: number of notifications */
  199. $notifications = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) );
  200. $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 );
  201. return $counter;
  202. }
  203. /**
  204. * Returns the capability that is required to manage all options.
  205. *
  206. * @return string Capability to check against.
  207. */
  208. protected function get_manage_capability() {
  209. /**
  210. * Filter: 'wpseo_manage_options_capability' - Allow changing the capability users need to view the settings pages
  211. *
  212. * @deprecated 5.5
  213. * @api string unsigned The capability
  214. */
  215. 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.' );
  216. }
  217. /**
  218. * Returns the page handler callback.
  219. *
  220. * @return array Callback page handler.
  221. */
  222. protected function get_admin_page_callback() {
  223. return array( $this->menu, 'load_page' );
  224. }
  225. /**
  226. * Returns the page title to use for the licenses page.
  227. *
  228. * @return string The title for the license page.
  229. */
  230. protected function get_license_page_title() {
  231. static $title = null;
  232. if ( $title === null ) {
  233. $title = __( 'Premium', 'wordpress-seo' );
  234. }
  235. return $title;
  236. }
  237. }