class-admin-menu.php
209 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Menu Registration |
| 4 | * |
| 5 | * @package automattic/jetpack-admin-ui |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Admin_UI; |
| 9 | |
| 10 | /** |
| 11 | * This class offers a wrapper to add_submenu_page and makes sure stand-alone plugin's menu items are always added under the Jetpack top level menu. |
| 12 | * If the Jetpack top level was not previously registered by other plugin, it will be registered here. |
| 13 | */ |
| 14 | class Admin_Menu { |
| 15 | |
| 16 | const PACKAGE_VERSION = '0.5.1'; |
| 17 | |
| 18 | /** |
| 19 | * Whether this class has been initialized |
| 20 | * |
| 21 | * @var boolean |
| 22 | */ |
| 23 | private static $initialized = false; |
| 24 | |
| 25 | /** |
| 26 | * List of menu items enqueued to be added |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | private static $menu_items = array(); |
| 31 | |
| 32 | /** |
| 33 | * Initialize the class and set up the main hook |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public static function init() { |
| 38 | if ( ! self::$initialized ) { |
| 39 | self::$initialized = true; |
| 40 | self::handle_akismet_menu(); |
| 41 | add_action( 'admin_menu', array( __CLASS__, 'admin_menu_hook_callback' ), 1000 ); // Jetpack uses 998. |
| 42 | add_action( 'network_admin_menu', array( __CLASS__, 'admin_menu_hook_callback' ), 1000 ); // Jetpack uses 998. |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Handles the Akismet menu item when used alongside other stand-alone plugins |
| 48 | * |
| 49 | * When Jetpack plugin is present, Akismet menu item is moved under the Jetpack top level menu, but if Akismet is active alongside other stand-alone plugins, |
| 50 | * we use this method to move the menu item. |
| 51 | */ |
| 52 | private static function handle_akismet_menu() { |
| 53 | if ( class_exists( 'Akismet_Admin' ) ) { |
| 54 | add_action( |
| 55 | 'admin_menu', |
| 56 | function () { |
| 57 | // Prevent Akismet from adding a menu item. |
| 58 | remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); |
| 59 | |
| 60 | // Add an Anti-spam menu item for Jetpack. |
| 61 | self::add_menu( __( 'Akismet Anti-spam', 'jetpack-admin-ui' ), __( 'Akismet Anti-spam', 'jetpack-admin-ui' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ), 6 ); |
| 62 | }, |
| 63 | 4 |
| 64 | ); |
| 65 | |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Callback to the admin_menu and network_admin_menu hooks that will register the enqueued menu items |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public static function admin_menu_hook_callback() { |
| 75 | $can_see_toplevel_menu = true; |
| 76 | $jetpack_plugin_present = class_exists( 'Jetpack_React_Page' ); |
| 77 | $icon = method_exists( '\Automattic\Jetpack\Assets\Logo', 'get_base64_logo' ) |
| 78 | ? ( new \Automattic\Jetpack\Assets\Logo() )->get_base64_logo() |
| 79 | : 'dashicons-admin-plugins'; |
| 80 | |
| 81 | if ( ! $jetpack_plugin_present ) { |
| 82 | add_menu_page( |
| 83 | 'Jetpack', |
| 84 | 'Jetpack', |
| 85 | 'edit_posts', |
| 86 | 'jetpack', |
| 87 | '__return_null', |
| 88 | $icon, |
| 89 | 3 |
| 90 | ); |
| 91 | |
| 92 | // If Jetpack plugin is not present, user will only be able to see this menu if they have enough capability to at least one of the sub menus being added. |
| 93 | $can_see_toplevel_menu = false; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * The add_sub_menu function has a bug and will not keep the right order of menu items. |
| 98 | * |
| 99 | * @see https://core.trac.wordpress.org/ticket/52035 |
| 100 | * Let's order the items before registering them. |
| 101 | * Since this all happens after the Jetpack plugin menu items were added, all items will be added after Jetpack plugin items - unless position is very low number (smaller than the number of menu items present in Jetpack plugin). |
| 102 | */ |
| 103 | usort( |
| 104 | self::$menu_items, |
| 105 | function ( $a, $b ) { |
| 106 | $position_a = empty( $a['position'] ) ? 0 : $a['position']; |
| 107 | $position_b = empty( $b['position'] ) ? 0 : $b['position']; |
| 108 | $result = $position_a <=> $position_b; |
| 109 | |
| 110 | if ( 0 === $result ) { |
| 111 | $result = strcmp( $a['menu_title'], $b['menu_title'] ); |
| 112 | } |
| 113 | |
| 114 | return $result; |
| 115 | } |
| 116 | ); |
| 117 | |
| 118 | foreach ( self::$menu_items as $menu_item ) { |
| 119 | if ( ! current_user_can( $menu_item['capability'] ) ) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | $can_see_toplevel_menu = true; |
| 124 | |
| 125 | add_submenu_page( |
| 126 | 'jetpack', |
| 127 | $menu_item['page_title'], |
| 128 | $menu_item['menu_title'], |
| 129 | $menu_item['capability'], |
| 130 | $menu_item['menu_slug'], |
| 131 | $menu_item['function'], |
| 132 | $menu_item['position'] |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | if ( ! $jetpack_plugin_present ) { |
| 137 | remove_submenu_page( 'jetpack', 'jetpack' ); |
| 138 | } |
| 139 | |
| 140 | if ( ! $can_see_toplevel_menu ) { |
| 141 | remove_menu_page( 'jetpack' ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Adds a new submenu to the Jetpack Top level menu |
| 147 | * |
| 148 | * The parameters this method accepts are the same as @see add_submenu_page. This class will |
| 149 | * aggreagate all menu items registered by stand-alone plugins and make sure they all go under the same |
| 150 | * Jetpack top level menu. It will also handle the top level menu registration in case the Jetpack plugin is not present. |
| 151 | * |
| 152 | * @param string $page_title The text to be displayed in the title tags of the page when the menu |
| 153 | * is selected. |
| 154 | * @param string $menu_title The text to be used for the menu. |
| 155 | * @param string $capability The capability required for this menu to be displayed to the user. |
| 156 | * @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu |
| 157 | * and only include lowercase alphanumeric, dashes, and underscores characters |
| 158 | * to be compatible with sanitize_key(). |
| 159 | * @param callable $function The function to be called to output the content for this page. |
| 160 | * @param int $position The position in the menu order this item should appear. Leave empty typically. |
| 161 | * |
| 162 | * @return string The resulting page's hook_suffix |
| 163 | */ |
| 164 | public static function add_menu( $page_title, $menu_title, $capability, $menu_slug, $function, $position = null ) { |
| 165 | self::init(); |
| 166 | self::$menu_items[] = compact( 'page_title', 'menu_title', 'capability', 'menu_slug', 'function', 'position' ); |
| 167 | |
| 168 | /** |
| 169 | * Let's return the page hook so consumers can use. |
| 170 | * We know all pages will be under Jetpack top level menu page, so we can hardcode the first part of the string. |
| 171 | * Using get_plugin_page_hookname here won't work because the top level page is not registered yet. |
| 172 | */ |
| 173 | return 'jetpack_page_' . $menu_slug; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets the slug for the first item under the Jetpack top level menu |
| 178 | * |
| 179 | * @return string|null |
| 180 | */ |
| 181 | public static function get_top_level_menu_item_slug() { |
| 182 | global $submenu; |
| 183 | if ( ! empty( $submenu['jetpack'] ) ) { |
| 184 | $item = reset( $submenu['jetpack'] ); |
| 185 | if ( isset( $item[2] ) ) { |
| 186 | return $item[2]; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Gets the URL for the first item under the Jetpack top level menu |
| 193 | * |
| 194 | * @param string $fallback If Jetpack menu is not there or no children is found, return this fallback instead. Default to admin_url(). |
| 195 | * @return string |
| 196 | */ |
| 197 | public static function get_top_level_menu_item_url( $fallback = false ) { |
| 198 | $slug = self::get_top_level_menu_item_slug(); |
| 199 | |
| 200 | if ( $slug ) { |
| 201 | $url = menu_page_url( $slug, false ); |
| 202 | return $url; |
| 203 | } |
| 204 | |
| 205 | $url = $fallback ? $fallback : admin_url(); |
| 206 | return $url; |
| 207 | } |
| 208 | } |
| 209 |