| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Menu |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the admin menu on the left of the admin area. |
| 10 |
*/ |
| 11 |
class WPSEO_Admin_Menu extends WPSEO_Base_Menu { |
| 12 |
|
| 13 |
/** |
| 14 |
* Registers all hooks to WordPress. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function register_hooks() { |
| 19 |
// Needs the lower than default priority so other plugins can hook underneath it without issue. |
| 20 |
add_action( 'admin_menu', [ $this, 'register_settings_page' ], 5 ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Registers the menu item submenus. |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function register_settings_page() { |
| 29 |
$manage_capability = $this->get_manage_capability(); |
| 30 |
$page_identifier = $this->get_page_identifier(); |
| 31 |
$admin_page_callback = $this->get_admin_page_callback(); |
| 32 |
|
| 33 |
// Get all submenu pages. |
| 34 |
$submenu_pages = $this->get_submenu_pages(); |
| 35 |
|
| 36 |
foreach ( $submenu_pages as $submenu_page ) { |
| 37 |
if ( WPSEO_Capability_Utils::current_user_can( $submenu_page[3] ) ) { |
| 38 |
$manage_capability = $submenu_page[3]; |
| 39 |
$page_identifier = $submenu_page[4]; |
| 40 |
$admin_page_callback = $submenu_page[5]; |
| 41 |
break; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
foreach ( $submenu_pages as $index => $submenu_page ) { |
| 46 |
$submenu_pages[ $index ][0] = $page_identifier; |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
* The current user has the capability to control anything. |
| 51 |
* This means that all submenus and dashboard can be shown. |
| 52 |
*/ |
| 53 |
global $admin_page_hooks; |
| 54 |
|
| 55 |
add_menu_page( |
| 56 |
'Yoast SEO: ' . __( 'Dashboard', 'wordpress-seo' ), |
| 57 |
'Yoast SEO ' . $this->get_notification_counter(), |
| 58 |
$manage_capability, |
| 59 |
$page_identifier, |
| 60 |
$admin_page_callback, |
| 61 |
$this->get_icon_svg(), |
| 62 |
99, |
| 63 |
); |
| 64 |
|
| 65 |
// Wipe notification bits from hooks. |
| 66 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride -- This is a deliberate action. |
| 67 |
$admin_page_hooks[ $page_identifier ] = 'seo'; |
| 68 |
|
| 69 |
// Add submenu items to the main menu if possible. |
| 70 |
$this->register_submenu_pages( $submenu_pages ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the list of registered submenu pages. |
| 75 |
* |
| 76 |
* @return array List of registered submenu pages. |
| 77 |
*/ |
| 78 |
public function get_submenu_pages() { |
| 79 |
global $wpseo_admin; |
| 80 |
|
| 81 |
$search_console_callback = null; |
| 82 |
|
| 83 |
// Account for when the available submenu pages are requested from outside the admin. |
| 84 |
if ( isset( $wpseo_admin ) ) { |
| 85 |
$google_search_console = new WPSEO_GSC(); |
| 86 |
$search_console_callback = [ $google_search_console, 'display' ]; |
| 87 |
} |
| 88 |
|
| 89 |
// Submenu pages. |
| 90 |
$submenu_pages = [ |
| 91 |
$this->get_submenu_page( |
| 92 |
__( 'Search Console', 'wordpress-seo' ), |
| 93 |
'wpseo_search_console', |
| 94 |
$search_console_callback, |
| 95 |
), |
| 96 |
$this->get_submenu_page( __( 'Tools', 'wordpress-seo' ), 'wpseo_tools' ), |
| 97 |
]; |
| 98 |
|
| 99 |
/** |
| 100 |
* Filter: 'wpseo_submenu_pages' - Collects all submenus that need to be shown. |
| 101 |
* |
| 102 |
* @param array $submenu_pages List with all submenu pages. |
| 103 |
*/ |
| 104 |
return (array) apply_filters( 'wpseo_submenu_pages', $submenu_pages ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the notification count in HTML format. |
| 109 |
* |
| 110 |
* @return string The notification count in HTML format. |
| 111 |
*/ |
| 112 |
protected function get_notification_counter() { |
| 113 |
$notification_center = Yoast_Notification_Center::get(); |
| 114 |
$notification_count = $notification_center->get_notification_count(); |
| 115 |
|
| 116 |
// Add main page. |
| 117 |
/* translators: Hidden accessibility text; %s: number of notifications. */ |
| 118 |
$notifications = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) ); |
| 119 |
|
| 120 |
return 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 ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns the capability that is required to manage all options. |
| 125 |
* |
| 126 |
* @return string Capability to check against. |
| 127 |
*/ |
| 128 |
protected function get_manage_capability() { |
| 129 |
return 'wpseo_manage_options'; |
| 130 |
} |
| 131 |
} |
| 132 |
|