diff --git a/projects/packages/admin-ui/changelog/add-remove_menu-to-admin_menu-class b/projects/packages/admin-ui/changelog/add-remove_menu-to-admin_menu-class new file mode 100644 index 0000000000000..ebac40afa3d90 --- /dev/null +++ b/projects/packages/admin-ui/changelog/add-remove_menu-to-admin_menu-class @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Added remove_menu method to Admin_Menu class diff --git a/projects/packages/admin-ui/src/class-admin-menu.php b/projects/packages/admin-ui/src/class-admin-menu.php index 40e504788359d..288ace58f491d 100644 --- a/projects/packages/admin-ui/src/class-admin-menu.php +++ b/projects/packages/admin-ui/src/class-admin-menu.php @@ -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 * diff --git a/projects/packages/publicize/changelog/add-remove_menu-to-admin_menu-class b/projects/packages/publicize/changelog/add-remove_menu-to-admin_menu-class new file mode 100644 index 0000000000000..e5473dc498bd4 --- /dev/null +++ b/projects/packages/publicize/changelog/add-remove_menu-to-admin_menu-class @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Use Admin_Menu::remove_menu to handle old Social menu item diff --git a/projects/packages/publicize/src/class-social-admin-page.php b/projects/packages/publicize/src/class-social-admin-page.php index 4c4ee52418e1a..6aaf0e3d39b9b 100644 --- a/projects/packages/publicize/src/class-social-admin-page.php +++ b/projects/packages/publicize/src/class-social-admin-page.php @@ -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' ); - } } /** @@ -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' ),