Skip to content

Commit

Permalink
Admin UI | Add remove_menu to Admin_Menu class (#41422)
Browse files Browse the repository at this point in the history
* Add remove_menu method to Admin_Menu class

* Use Admin_Menu::remove_menu to handle old Social menu item

* Add changelog
  • Loading branch information
manzoorwanijk authored Jan 30, 2025
1 parent b614623 commit 2cf35b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Added remove_menu method to Admin_Menu class
20 changes: 20 additions & 0 deletions projects/packages/admin-ui/src/class-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ public static function add_menu( $page_title, $menu_title, $capability, $menu_sl
return 'jetpack_page_' . $menu_slug;
}

/**
* Removes an already added submenu
*
* @param string $menu_slug The slug of the submenu to remove.
*
* @return array|false The removed submenu on success, false if not found.
*/
public static function remove_menu( $menu_slug ) {

foreach ( self::$menu_items as $index => $menu_item ) {
if ( $menu_item['menu_slug'] === $menu_slug ) {
unset( self::$menu_items[ $index ] );

return $menu_item;
}
}

return false;
}

/**
* Gets the slug for the first item under the Jetpack top level menu
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Use Admin_Menu::remove_menu to handle old Social menu item
36 changes: 3 additions & 33 deletions projects/packages/publicize/src/class-social-admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,6 @@ public static function init() {
*/
private function __construct() {
add_action( 'admin_menu', array( $this, 'add_menu' ) );

/**
* Admin_Menu::add_menu uses 1000, so we use 2000
* to ensure we remove the old Social menu item after it has been added.
*/
add_action( 'admin_menu', array( $this, 'remove_old_social_menu_item' ), 2000 );
}

/**
* Remove the page added by old versions of Social.
*/
public function remove_old_social_menu_item() {

$social_version = Publicize_Script_Data::get_plugin_info()['social']['version'];

// If it's the old social version, remove the submenu page.
// TODO Update the version and operator before next Social release.
if ( $social_version && version_compare( $social_version, '6.1.0', '<' ) ) {
/**
* `add_submenu_page` allows multiple submenus with the same slug,
* but `remove_submenu_page` only removes the first one it finds with the given slug.
*
* We add the menu using `admin_menu` hook unlike the old Social plugin,
* which used the `init` hook, which runs before `admin_menu`.
* So, the old Social plugin's menu is before the new one in $submenu global.
*
* So, `remove_submenu_page` should remove the menu added by the old Social plugin.
*
* @see https://developer.wordpress.org/reference/functions/add_submenu_page
* @see https://developer.wordpress.org/reference/functions/remove_submenu_page
*/
remove_submenu_page( 'jetpack', 'jetpack-social' );
}
}

/**
Expand All @@ -86,6 +53,9 @@ public function add_menu() {
return;
}

// Remove the old Social menu item, if it exists.
Admin_Menu::remove_menu( 'jetpack-social' );

$page_suffix = Admin_Menu::add_menu(
__( 'Jetpack Social', 'jetpack-publicize-pkg' ),
_x( 'Social', 'The Jetpack Social product name, without the Jetpack prefix', 'jetpack-publicize-pkg' ),
Expand Down

0 comments on commit 2cf35b5

Please sign in to comment.