Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.10% |
75 / 117 |
|
22.22% |
2 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Admin_Menu | |
64.66% |
75 / 116 |
|
22.22% |
2 / 9 |
81.08 | |
0.00% |
0 / 1 |
| reregister_menu_items | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| get_preferred_view | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
| should_disable_links_manager | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| add_upgrades_menu | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
6.04 | |||
| add_appearance_menu | |
95.45% |
21 / 22 |
|
0.00% |
0 / 1 |
4 | |||
| add_plugins_menu | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| add_users_menu | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| render_upsell_nudge | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
42 | |||
| get_upsell_nudge | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Menu file. |
| 4 | * |
| 5 | * @package automattic/jetpack-masterbar |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Masterbar; |
| 9 | |
| 10 | require_once __DIR__ . '/class-base-admin-menu.php'; |
| 11 | |
| 12 | /** |
| 13 | * Class Admin_Menu. |
| 14 | */ |
| 15 | class Admin_Menu extends Base_Admin_Menu { |
| 16 | |
| 17 | /** |
| 18 | * Create the desired menu output. |
| 19 | */ |
| 20 | public function reregister_menu_items() { |
| 21 | $this->add_upgrades_menu(); |
| 22 | $this->add_appearance_menu(); |
| 23 | $this->add_plugins_menu(); |
| 24 | $this->add_users_menu(); |
| 25 | |
| 26 | // Remove Links Manager menu since its usage is discouraged. https://github.com/Automattic/wp-calypso/issues/51188. |
| 27 | // @see https://core.trac.wordpress.org/ticket/21307#comment:73. |
| 28 | if ( $this->should_disable_links_manager() ) { |
| 29 | remove_menu_page( 'link-manager.php' ); |
| 30 | } |
| 31 | |
| 32 | ksort( $GLOBALS['menu'] ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the preferred view for the given screen. |
| 37 | * |
| 38 | * @param string $screen Screen identifier. |
| 39 | * @param bool $fallback_global_preference (Optional) Whether the global preference for all screens should be used |
| 40 | * as fallback if there is no specific preference for the given screen. |
| 41 | * Default: true. |
| 42 | * @return string |
| 43 | */ |
| 44 | public function get_preferred_view( $screen, $fallback_global_preference = true ) { |
| 45 | $force_default_view = in_array( $screen, array( 'users.php', 'options-general.php' ), true ); |
| 46 | $use_wp_admin = $this->use_wp_admin_interface(); |
| 47 | |
| 48 | // When no preferred view has been set for "Users > All Users" or "Settings > General", keep the previous |
| 49 | // behavior that forced the default view regardless of the global preference. |
| 50 | // This behavior is overriden by the wpcom_admin_interface option when it is set to wp-admin. |
| 51 | if ( ! $use_wp_admin && $fallback_global_preference && $force_default_view ) { |
| 52 | $preferred_view = parent::get_preferred_view( $screen, false ); |
| 53 | if ( self::UNKNOWN_VIEW === $preferred_view ) { |
| 54 | return self::DEFAULT_VIEW; |
| 55 | } |
| 56 | return $preferred_view; |
| 57 | } |
| 58 | |
| 59 | return parent::get_preferred_view( $screen, $fallback_global_preference ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Check if Links Manager is being used. |
| 64 | */ |
| 65 | public function should_disable_links_manager() { |
| 66 | // The max ID number of the auto-generated links. |
| 67 | // See /wp-content/mu-plugins/wpcom-wp-install-defaults.php in WP.com. |
| 68 | $max_default_id = 10; |
| 69 | |
| 70 | // We are only checking the latest entry link_id so are limiting the query to 1. |
| 71 | $link_manager_links = get_bookmarks( |
| 72 | array( |
| 73 | 'orderby' => 'link_id', |
| 74 | 'order' => 'DESC', |
| 75 | 'limit' => 1, |
| 76 | 'hide_invisible' => 0, |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | // Ordered links by ID descending, check if the first ID is more than $max_default_id. |
| 81 | if ( is_countable( $link_manager_links ) && count( $link_manager_links ) > 0 && $link_manager_links[0]->link_id > $max_default_id ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Adds Upgrades menu. |
| 90 | * |
| 91 | * @param string $plan The current WPCOM plan of the blog. |
| 92 | */ |
| 93 | public function add_upgrades_menu( $plan = null ) { |
| 94 | global $menu; |
| 95 | |
| 96 | $menu_exists = false; |
| 97 | foreach ( $menu as $item ) { |
| 98 | if ( 'paid-upgrades.php' === $item[2] ) { |
| 99 | $menu_exists = true; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if ( ! $menu_exists ) { |
| 105 | if ( $plan ) { |
| 106 | // Add display:none as a default for cases when CSS is not loaded. |
| 107 | $site_upgrades = '%1$s<span class="inline-text" style="display:none">%2$s</span>'; |
| 108 | $site_upgrades = sprintf( |
| 109 | $site_upgrades, |
| 110 | __( 'Upgrades', 'jetpack-masterbar' ), |
| 111 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText |
| 112 | __( $plan, 'jetpack-masterbar' ) |
| 113 | ); |
| 114 | } else { |
| 115 | $site_upgrades = __( 'Upgrades', 'jetpack-masterbar' ); |
| 116 | } |
| 117 | // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. |
| 118 | add_menu_page( __( 'Upgrades', 'jetpack-masterbar' ), $site_upgrades, 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 2.99 ); |
| 119 | } |
| 120 | // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. |
| 121 | add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack-masterbar' ), __( 'Plans', 'jetpack-masterbar' ), 'manage_options', 'https://wordpress.com/plans/' . $this->domain, null, 1 ); |
| 122 | // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. |
| 123 | add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack-masterbar' ), __( 'Purchases', 'jetpack-masterbar' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 2 ); |
| 124 | |
| 125 | if ( ! $menu_exists ) { |
| 126 | // Remove the submenu auto-created by Core. |
| 127 | $this->hide_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Adds Appearance menu. |
| 133 | * |
| 134 | * @return string The Customizer URL. |
| 135 | */ |
| 136 | public function add_appearance_menu() { |
| 137 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 138 | $default_customize_slug = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' ); |
| 139 | $default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug ); |
| 140 | // TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links(). |
| 141 | $default_customize_header_slug_2 = admin_url( 'themes.php?page=custom-header' ); |
| 142 | $default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug ); |
| 143 | // TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links(). |
| 144 | $default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) ); |
| 145 | |
| 146 | if ( $this->is_api_request ) { |
| 147 | // In case this is an api request we will have to add the 'return' querystring via JS. |
| 148 | $customize_url = 'customize.php'; |
| 149 | } else { |
| 150 | $customize_url = $default_customize_slug; |
| 151 | } |
| 152 | |
| 153 | $submenus_to_update = array( |
| 154 | $default_customize_slug => $customize_url, |
| 155 | $default_customize_header_slug_1 => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ), |
| 156 | $default_customize_header_slug_2 => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ), |
| 157 | $default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ), |
| 158 | $default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ), |
| 159 | ); |
| 160 | |
| 161 | if ( self::DEFAULT_VIEW === $this->get_preferred_view( 'themes.php' ) ) { |
| 162 | $submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain; |
| 163 | } |
| 164 | |
| 165 | $this->update_submenus( 'themes.php', $submenus_to_update ); |
| 166 | |
| 167 | $this->hide_submenu_page( 'themes.php', 'custom-header' ); |
| 168 | $this->hide_submenu_page( 'themes.php', 'custom-background' ); |
| 169 | |
| 170 | return $customize_url; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Adds Plugins menu. |
| 175 | */ |
| 176 | public function add_plugins_menu() { |
| 177 | if ( self::CLASSIC_VIEW === $this->get_preferred_view( 'plugins.php' ) ) { |
| 178 | return; |
| 179 | } |
| 180 | $this->hide_submenu_page( 'plugins.php', 'plugin-install.php' ); |
| 181 | $this->hide_submenu_page( 'plugins.php', 'plugin-editor.php' ); |
| 182 | |
| 183 | $this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Adds Users menu. |
| 188 | */ |
| 189 | public function add_users_menu() { |
| 190 | $submenus_to_update = array( |
| 191 | 'profile.php' => 'https://wordpress.com/me', |
| 192 | ); |
| 193 | |
| 194 | if ( self::DEFAULT_VIEW === $this->get_preferred_view( 'users.php' ) ) { |
| 195 | $submenus_to_update['users.php'] = 'https://wordpress.com/people/team/' . $this->domain; |
| 196 | $submenus_to_update['user-new.php'] = 'https://wordpress.com/people/new/' . $this->domain; |
| 197 | } |
| 198 | |
| 199 | $slug = current_user_can( 'list_users' ) ? 'users.php' : 'profile.php'; |
| 200 | $this->update_submenus( $slug, $submenus_to_update ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Renders the upsell nudge directly in the admin menu. |
| 205 | * |
| 206 | * This renders server-side via the `adminmenu` hook to avoid the layout |
| 207 | * shift caused by the previous AJAX-based approach. |
| 208 | */ |
| 209 | public function render_upsell_nudge() { |
| 210 | // Skip if jetpack-mu-wpcom is already rendering the upsell banner. |
| 211 | if ( has_action( 'adminmenu', 'wpcom_add_sidebar_notice_menu_page' ) ) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | /** This action is already documented in \Automattic\Jetpack\JITMS\JITM */ |
| 216 | if ( ! apply_filters( 'jetpack_just_in_time_msgs', true ) ) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | $nudge = $this->get_upsell_nudge(); |
| 221 | if ( ! $nudge ) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | $link = $nudge['link']; |
| 226 | if ( str_starts_with( $link, '/' ) ) { |
| 227 | $link = 'https://wordpress.com' . $link; |
| 228 | } |
| 229 | ?> |
| 230 | <li class="wp-not-current-submenu menu-top menu-icon-generic toplevel_page_site-notices" id="toplevel_page_site-notices"> |
| 231 | <a href="<?php echo esc_url( $link ); ?>" class="wp-not-current-submenu menu-top menu-icon-generic toplevel_page_site-notices"> |
| 232 | <div class="wp-menu-arrow"> |
| 233 | <div></div> |
| 234 | </div> |
| 235 | <div class="wp-menu-image dashicons-before dashicons-admin-generic" aria-hidden="true"><br></div> |
| 236 | <div class="wp-menu-name"> |
| 237 | <div class="upsell_banner"> |
| 238 | <div class="banner__info"> |
| 239 | <div class="banner__title"> |
| 240 | <?php echo wp_kses( $nudge['content'], array() ); ?> |
| 241 | </div> |
| 242 | </div> |
| 243 | <div class="banner__action"> |
| 244 | <button type="button" class="button"> |
| 245 | <?php echo wp_kses( $nudge['cta'], array() ); ?> |
| 246 | </button> |
| 247 | </div> |
| 248 | <?php if ( $nudge['dismissible'] ) : ?> |
| 249 | <svg xmlns="http://www.w3.org/2000/svg" data-feature_class="<?php echo esc_attr( $nudge['feature_class'] ); ?>" data-feature_id="<?php echo esc_attr( $nudge['id'] ); ?>" viewBox="0 0 24 24" class="gridicon gridicons-cross dismissible-card__close-icon" height="24" width="24"><g><path d="M18.36 19.78L12 13.41l-6.36 6.37-1.42-1.42L10.59 12 4.22 5.64l1.42-1.42L12 10.59l6.36-6.36 1.41 1.41L13.41 12l6.36 6.36z"></path></g></svg> |
| 250 | <?php endif; ?> |
| 251 | </div> |
| 252 | </div> |
| 253 | </a> |
| 254 | </li> |
| 255 | <script> |
| 256 | ( function ( el ) { |
| 257 | if ( el && el.parentNode ) { |
| 258 | el.parentNode.prepend( el ); |
| 259 | } |
| 260 | } )( document.getElementById( 'toplevel_page_site-notices' ) ); |
| 261 | </script> |
| 262 | <?php |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Returns the first available upsell nudge. |
| 267 | * Needs to be implemented separately for each child menu class. |
| 268 | * Empty by default. |
| 269 | * |
| 270 | * @return array |
| 271 | */ |
| 272 | public function get_upsell_nudge() { |
| 273 | return array(); |
| 274 | } |
| 275 | } |