Skip to content

Commit

Permalink
Admin menu: Change order of Jetpack sub-menu items (#39095)
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-karen authored and gogdzl committed Oct 25, 2024
1 parent edb84a3 commit 0e5bc5a
Show file tree
Hide file tree
Showing 27 changed files with 186 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
2 changes: 1 addition & 1 deletion projects/packages/admin-ui/src/class-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function () {
remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 );

// Add an Anti-spam menu item for Jetpack.
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' ) );
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 );
},
4
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/packages/backup/src/class-jetpack-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public static function initialize() {
_x( 'VaultPress Backup', 'The Jetpack VaultPress Backup product name, without the Jetpack prefix', 'jetpack-backup-pkg' ),
'manage_options',
'jetpack-backup',
array( __CLASS__, 'plugin_settings_page' )
array( __CLASS__, 'plugin_settings_page' ),
7
);
add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/src/class-activitylog.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function add_submenu_jetpack() {
'manage_options',
esc_url( Redirect::get_url( 'cloud-activity-log-wp-menu', $args ) ),
null,
1
8
);
}
}
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/src/class-jetpack-manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function add_submenu_jetpack() {
'manage_options',
esc_url( Redirect::get_url( 'cloud-manage-dashboard-wp-menu', $args ) ),
null,
100
15
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/packages/search/src/dashboard/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public function add_wp_admin_submenu() {
_x( 'Search', 'product name shown in menu', 'jetpack-search-pkg' ),
'manage_options',
'jetpack-search',
array( $this, 'render' )
array( $this, 'render' ),
10
);
} else {
// always add the page, but hide it from the menu.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/packages/videopress/src/class-admin-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static function init() {
_x( 'VideoPress', 'The Jetpack VideoPress product name, without the Jetpack prefix', 'jetpack-videopress-pkg' ),
'manage_options',
self::ADMIN_PAGE_SLUG,
array( __CLASS__, 'plugin_settings_page' )
array( __CLASS__, 'plugin_settings_page' ),
3
);
add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/packages/wordads/src/dashboard/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function add_wp_admin_submenu() {
_x( 'WordAds', 'product name shown in menu', 'jetpack-wordads' ),
'manage_options',
'jetpack-wordads',
array( $this, 'render' )
array( $this, 'render' ),
12
);

add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/plugins/beta/src/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static function add_actions() {
'Beta Tester',
'update_plugins',
'jetpack-beta',
array( self::class, 'render' )
array( self::class, 'render' ),
16
);

if ( false !== self::$hookname ) {
Expand Down
3 changes: 2 additions & 1 deletion projects/plugins/boost/app/admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function handle_admin_menu() {
$menu_label,
'manage_options',
JETPACK_BOOST_SLUG,
array( $this, 'render_settings' )
array( $this, 'render_settings' ),
2
);
add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
63 changes: 59 additions & 4 deletions projects/plugins/jetpack/_inc/client/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,69 @@ export default connect(
} )( withRouter( Main ) )
);

// eslint-disable-next-line jsdoc/require-returns-check
/**
* Determines the page order of My Jetpack, Activity Log, Dashboard, and Settings in the left sidebar.
* @return {object} Object with keys for each page and values for the order of the page in the sidebar.
*/
function jetpackPageOrder() {
const jetpackParentMenu = document.querySelector( '#toplevel_page_jetpack' );
const pageOrder = {};

if ( jetpackParentMenu ) {
const jetpackSubMenu = jetpackParentMenu.querySelector( '.wp-submenu' );

if ( jetpackSubMenu ) {
const subMenuItems = jetpackSubMenu.querySelectorAll( 'li:not(.wp-submenu-head) a' );

const urlPatterns = [
{
key: 'dashboard',
pattern: '/wp-admin/admin.php?page=jetpack#/dashboard',
matchType: 'end',
},
{
key: 'activityLog',
pattern: 'https://jetpack.com/redirect/?source=cloud-activity-log-wp-menu',
matchType: 'start',
},
{
key: 'settings',
pattern: '/wp-admin/admin.php?page=jetpack#/settings',
matchType: 'end',
},
];

const findIndex = ( urlPattern, matchType ) => {
let foundIndex = -1;
subMenuItems.forEach( ( item, index ) => {
const href = item.href;
if (
( matchType === 'end' && href.endsWith( urlPattern ) ) ||
( matchType === 'start' && href.startsWith( urlPattern ) )
) {
foundIndex = index + 1;
}
} );
return foundIndex;
};

urlPatterns.forEach( ( { key, pattern, matchType } ) => {
const index = findIndex( pattern, matchType );
pageOrder[ key ] = index;
} );
return pageOrder;
}
}
}

/**
* Manages changing the visuals of the sub-nav items on the left sidebar when the React app changes routes
*
* @param pageOrder
*/
window.wpNavMenuClassChange = function (
pageOrder = { myJetpack: 1, activityLog: 2, dashboard: 3, settings: 4 }
) {
window.wpNavMenuClassChange = function () {
const pageOrder = jetpackPageOrder();

let hash = window.location.hash;
let page = new URLSearchParams( window.location.search );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

use Automattic\Jetpack\Admin_UI\Admin_Menu;
use Automattic\Jetpack\Assets\Logo;
use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
Expand Down Expand Up @@ -77,6 +78,21 @@ public function add_page_actions( $hook ) {
}
}

/**
* Remove the main Jetpack submenu if a site is in offline mode or connected.
* At that point, admins can access the Jetpack Dashboard instead.
*
* @since $$next-version$$
*/
public function remove_jetpack_menu() {
if (
( new Status() )->is_offline_mode()
|| Jetpack::is_connection_ready()
) {
remove_submenu_page( 'jetpack', 'jetpack' );
}
}

/**
* Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active.
*
Expand All @@ -86,8 +102,14 @@ public function add_page_actions( $hook ) {
*/
public function jetpack_add_dashboard_sub_nav_item() {
if ( ( new Status() )->is_offline_mode() || Jetpack::is_connection_ready() ) {
add_submenu_page( 'jetpack', __( 'Dashboard', 'jetpack' ), __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'jetpack#/dashboard', '__return_null', 1 );
remove_submenu_page( 'jetpack', 'jetpack' );
Admin_Menu::add_menu(
__( 'Dashboard', 'jetpack' ),
__( 'Dashboard', 'jetpack' ),
'jetpack_admin_page',
Jetpack::admin_url( array( 'page' => 'jetpack#/dashboard' ) ),
null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- See https://core.trac.wordpress.org/ticket/52539.
14
);
}
}

Expand Down Expand Up @@ -165,7 +187,14 @@ private function can_access_settings() {
*/
public function jetpack_add_settings_sub_nav_item() {
if ( $this->can_access_settings() ) {
add_submenu_page( 'jetpack', __( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'jetpack#/settings', '__return_null' );
Admin_Menu::add_menu(
__( 'Settings', 'jetpack' ),
__( 'Settings', 'jetpack' ),
'jetpack_admin_page',
Jetpack::admin_url( array( 'page' => 'jetpack#/settings' ) ),
null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- See https://core.trac.wordpress.org/ticket/52539.
13
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Admin menu: Change order of Jetpack sub-menu items
1 change: 1 addition & 0 deletions projects/plugins/jetpack/class.jetpack-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private function __construct() {

add_action( 'admin_init', array( $jetpack_react, 'react_redirects' ), 0 );
add_action( 'admin_menu', array( $jetpack_react, 'add_actions' ), 998 );
add_action( 'admin_menu', array( $jetpack_react, 'remove_jetpack_menu' ), 2000 );
add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
Expand Down
36 changes: 26 additions & 10 deletions projects/plugins/jetpack/modules/scan/class-admin-sidebar-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Scan;

use Automattic\Jetpack\Admin_UI\Admin_Menu;
use Automattic\Jetpack\My_Jetpack\Products\Backup;
use Automattic\Jetpack\Redirect;
use Automattic\Jetpack\Status\Host;
Expand Down Expand Up @@ -68,22 +69,37 @@ public function maybe_add_admin_link() {
}

if ( $this->should_show_scan() ) {
$menu_label = __( 'Scan', 'jetpack' );
$url = Redirect::get_url( 'calypso-scanner' );
add_submenu_page( 'jetpack', $menu_label, esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>', 'manage_options', esc_url( $url ), null, $this->get_link_offset() );
Admin_Menu::add_menu(
__( 'Scan', 'jetpack' ),
__( 'Scan', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
'manage_options',
esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ),
null,
$this->get_link_offset()
);
}

// Add scan item which shows history page only. This is mutally exclusive from the scan item above and is only shown for Atomic sitse.
if ( $this->should_show_scan_history_only() ) {
$menu_label = __( 'Scan', 'jetpack' );
$url = Redirect::get_url( 'cloud-scan-history-wp-menu' );
add_submenu_page( 'jetpack', $menu_label, esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>', 'manage_options', esc_url( $url ), null, $this->get_link_offset() );
Admin_Menu::add_menu(
__( 'Scan', 'jetpack' ),
__( 'Scan', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
'manage_options',
esc_url( Redirect::get_url( 'cloud-scan-history-wp-menu' ) ),
null,
$this->get_link_offset()
);
}

if ( $this->should_show_backup() ) {
$menu_label = __( 'VaultPress', 'jetpack' );
$url = Redirect::get_url( 'calypso-backups' );
add_submenu_page( 'jetpack', $menu_label, esc_html( $menu_label ) . ' <span class="dashicons dashicons-external"></span>', 'manage_options', esc_url( $url ), null, $this->get_link_offset() );
Admin_Menu::add_menu(
__( 'VaultPress', 'jetpack' ),
__( 'VaultPress', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
'manage_options',
esc_url( Redirect::get_url( 'calypso-backups' ) ),
null,
$this->get_link_offset()
);
}
}

Expand All @@ -96,7 +112,7 @@ public function maybe_add_admin_link() {
*/
private function get_link_offset() {
global $submenu;
$offset = 0;
$offset = 9;

if ( ! array_key_exists( 'jetpack', $submenu ) ) {
return $offset;
Expand Down
9 changes: 5 additions & 4 deletions projects/plugins/jetpack/modules/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.

use Automattic\Jetpack\Admin_UI\Admin_Menu;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
use Automattic\Jetpack\Redirect;
Expand Down Expand Up @@ -1000,13 +1001,13 @@ public function add_subscribers_menu() {
array( 'site' => $blog_id ? $blog_id : $status->get_site_suffix() )
);

add_submenu_page(
'jetpack',
esc_attr__( 'Subscribers', 'jetpack' ),
Admin_Menu::add_menu(
__( 'Subscribers', 'jetpack' ),
__( 'Subscribers', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
'manage_options',
esc_url( $link ),
null
null,
11
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/plugins/protect/src/class-jetpack-protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public function admin_page_init() {
$menu_label,
'manage_options',
'jetpack-protect',
array( $this, 'plugin_settings_page' )
array( $this, 'plugin_settings_page' ),
5
);

add_action( 'load-' . $page_suffix, array( $this, 'enqueue_admin_scripts' ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin menu: change order of Jetpack sub-menu items
3 changes: 2 additions & 1 deletion projects/plugins/social/src/class-jetpack-social.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function __construct( $connection_manager = null ) {
_x( 'Social', 'The Jetpack Social product name, without the Jetpack prefix', 'jetpack-social' ),
'manage_options',
'jetpack-social',
array( $this, 'plugin_settings_page' )
array( $this, 'plugin_settings_page' ),
4
);

add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
Expand Down

0 comments on commit 0e5bc5a

Please sign in to comment.