From ff26ecb6c6ac8876b9562205034b437a9e8efe87 Mon Sep 17 00:00:00 2001 From: Bogdan Ungureanu Date: Thu, 19 Dec 2024 15:25:53 +0200 Subject: [PATCH] RDV: Remove the wpcom_admin_interface hook for the admin_menu hook (#40669) * RDV: Remove the wpcom_admin_interface hook for the admin_menu hook * Remove the alias functions with calls directly to get_option --- .../changelog/fix-wp-admin-rdv | 4 ++++ .../wpcom-admin-bar/wpcom-admin-bar.php | 2 +- .../wpcom-admin-interface.php | 15 +++++++++++++++ .../wpcom-admin-menu/wpcom-admin-menu.php | 19 +++---------------- .../wpcom-sidebar-notice.php | 2 +- .../features/wpcom-themes/wpcom-themes.php | 2 +- .../masterbar/changelog/fix-wp-admin-rdv | 4 ++++ .../src/admin-menu/class-admin-menu.php | 2 +- .../admin-menu/class-atomic-admin-menu.php | 12 ++++++------ .../src/admin-menu/class-base-admin-menu.php | 15 --------------- 10 files changed, 36 insertions(+), 41 deletions(-) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/fix-wp-admin-rdv create mode 100644 projects/packages/masterbar/changelog/fix-wp-admin-rdv diff --git a/projects/packages/jetpack-mu-wpcom/changelog/fix-wp-admin-rdv b/projects/packages/jetpack-mu-wpcom/changelog/fix-wp-admin-rdv new file mode 100644 index 0000000000000..cc90987517731 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/fix-wp-admin-rdv @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Exclude the wpcom_admin_interface from the admin_menu action diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-bar/wpcom-admin-bar.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-bar/wpcom-admin-bar.php index a4e006a51ce38..575a7e3218f49 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-bar/wpcom-admin-bar.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-bar/wpcom-admin-bar.php @@ -263,7 +263,7 @@ function wpcom_replace_edit_profile_menu_to_me( $wp_admin_bar ) { * @return string Name of the admin bar class. */ function wpcom_custom_wpcom_admin_bar_class( $wp_admin_bar_class ) { - if ( ! wpcom_is_using_default_admin_menu() ) { + if ( get_option( 'wpcom_admin_interface' ) === 'wp-admin' ) { return $wp_admin_bar_class; } diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-interface/wpcom-admin-interface.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-interface/wpcom-admin-interface.php index e6a7caecc799e..91e2394b86a7b 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-interface/wpcom-admin-interface.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-interface/wpcom-admin-interface.php @@ -194,6 +194,21 @@ function wpcom_admin_get_user_option_jetpack( $value ) { add_filter( 'get_user_option_jetpack_admin_menu_preferred_views', 'wpcom_admin_get_user_option_jetpack' ); add_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option', 10 ); +add_action( + 'admin_menu', + function () { + remove_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option' ); + }, + PHP_INT_MIN +); + +add_action( + 'admin_menu', + function () { + add_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option', 10 ); + }, + PHP_INT_MAX +); /** * Hides the "View" switcher on WP Admin screens enforced by the "Remove duplicate views" experiment. */ diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-menu/wpcom-admin-menu.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-menu/wpcom-admin-menu.php index aafe2e0edb12c..a44371e89ec14 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-menu/wpcom-admin-menu.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-admin-menu/wpcom-admin-menu.php @@ -33,24 +33,11 @@ function current_user_has_wpcom_account() { return $has_account; } -/** - * Check if the user has the default (Calypso) Admin menu. - * - * @return bool - */ -function wpcom_is_using_default_admin_menu() { - remove_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option' ); - $option = get_option( 'wpcom_admin_interface' ) !== 'wp-admin'; - add_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option', 10 ); - - return $option; -} - /** * Adds a Hosting menu. */ function wpcom_add_hosting_menu() { - if ( wpcom_is_using_default_admin_menu() ) { + if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) { return; } @@ -162,7 +149,7 @@ function wpcom_add_hosting_menu() { function wpcom_add_jetpack_submenu() { $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM; $is_atomic_site = ! $is_simple_site; - $uses_wp_admin_interface = ! wpcom_is_using_default_admin_menu(); + $uses_wp_admin_interface = get_option( 'wpcom_admin_interface' ) === 'wp-admin'; if ( ! $uses_wp_admin_interface ) { return; @@ -392,7 +379,7 @@ function wpcom_add_plugins_menu() { global $menu; $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM; $is_atomic_site = ! $is_simple_site; - $uses_wp_admin_interface = ! wpcom_is_using_default_admin_menu(); + $uses_wp_admin_interface = get_option( 'wpcom_admin_interface' ) === 'wp-admin'; if ( $is_simple_site ) { $has_plugins_menu = false; diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php index d8ce8b474ddd2..90b1dd2898ff6 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php @@ -10,7 +10,7 @@ use Automattic\Jetpack\Connection\Manager as Connection_Manager; use Automattic\Jetpack\Jetpack_Mu_Wpcom; -if ( wpcom_is_using_default_admin_menu() ) { +if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) { return; } diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-themes/wpcom-themes.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-themes/wpcom-themes.php index f5932690ed893..6d6ede007345a 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-themes/wpcom-themes.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-themes/wpcom-themes.php @@ -56,7 +56,7 @@ function wpcom_themes_show_banner() { * Registers an "Appearance > Theme Showcase" menu. */ function wpcom_themes_add_theme_showcase_menu() { - if ( wpcom_is_using_default_admin_menu() ) { + if ( get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) { return; } diff --git a/projects/packages/masterbar/changelog/fix-wp-admin-rdv b/projects/packages/masterbar/changelog/fix-wp-admin-rdv new file mode 100644 index 0000000000000..cc90987517731 --- /dev/null +++ b/projects/packages/masterbar/changelog/fix-wp-admin-rdv @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Exclude the wpcom_admin_interface from the admin_menu action diff --git a/projects/packages/masterbar/src/admin-menu/class-admin-menu.php b/projects/packages/masterbar/src/admin-menu/class-admin-menu.php index eb0c7f3cdd7eb..3b0085f29391d 100644 --- a/projects/packages/masterbar/src/admin-menu/class-admin-menu.php +++ b/projects/packages/masterbar/src/admin-menu/class-admin-menu.php @@ -356,7 +356,7 @@ public function add_tools_menu() { // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack-masterbar' ), __( 'Marketing', 'jetpack-masterbar' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 ); - if ( $this->is_using_default_admin_menu() ) { + if ( ! $this->use_wp_admin_interface() ) { // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. add_submenu_page( 'tools.php', esc_attr__( 'Monetize', 'jetpack-masterbar' ), __( 'Monetize', 'jetpack-masterbar' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 ); } diff --git a/projects/packages/masterbar/src/admin-menu/class-atomic-admin-menu.php b/projects/packages/masterbar/src/admin-menu/class-atomic-admin-menu.php index c58244fd06a2c..017891701b8c5 100644 --- a/projects/packages/masterbar/src/admin-menu/class-atomic-admin-menu.php +++ b/projects/packages/masterbar/src/admin-menu/class-atomic-admin-menu.php @@ -47,7 +47,7 @@ function () { ); // Add notices to the settings pages when there is a Calypso page available. - if ( get_option( 'wpcom_admin_interface' ) === 'wp-admin' ) { + if ( $this->use_wp_admin_interface() ) { add_action( 'current_screen', array( $this, 'add_settings_page_notice' ) ); } } @@ -79,7 +79,7 @@ public function reregister_menu_items() { $this->remove_gutenberg_menu(); // We don't need the `My Mailboxes` when the interface is set to wp-admin or the site is a staging site, - if ( $this->is_using_default_admin_menu() && ! get_option( 'wpcom_is_staging_site' ) ) { + if ( ! $this->use_wp_admin_interface() && ! get_option( 'wpcom_is_staging_site' ) ) { $this->add_my_mailboxes_menu(); } @@ -132,7 +132,7 @@ public function add_users_menu() { $this->update_submenus( $slug, $submenus_to_update ); } - if ( $this->is_using_default_admin_menu() ) { + if ( ! $this->use_wp_admin_interface() ) { // The 'Subscribers' menu exists in the Jetpack menu for Classic wp-admin interface, so only add it for non-wp-admin interfaces. // // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. add_submenu_page( 'users.php', esc_attr__( 'Subscribers', 'jetpack-masterbar' ), __( 'Subscribers', 'jetpack-masterbar' ), 'list_users', 'https://wordpress.com/subscribers/' . $this->domain, null ); @@ -322,7 +322,7 @@ public function get_upsell_nudge() { */ public function add_jetpack_menu() { // This is supposed to be the same as class-admin-menu but with a different position specified for the Jetpack menu. - if ( ! $this->is_using_default_admin_menu() ) { + if ( $this->use_wp_admin_interface() ) { parent::create_jetpack_menu( 2, false ); } else { parent::add_jetpack_menu(); @@ -445,7 +445,7 @@ public function add_options_menu() { // Hide Settings > Performance when the interface is set to wp-admin. // This is due to these settings are mostly also available in Jetpack > Settings, in the Performance tab. - if ( ! $this->is_using_default_admin_menu() ) { + if ( $this->use_wp_admin_interface() ) { $this->hide_submenu_page( 'options-general.php', 'https://wordpress.com/settings/performance/' . $this->domain ); } } @@ -457,7 +457,7 @@ public function add_tools_menu() { parent::add_tools_menu(); // Link the Tools menu to Available Tools when the interface is set to wp-admin. - if ( ! $this->is_using_default_admin_menu() ) { + if ( $this->use_wp_admin_interface() ) { // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. add_submenu_page( 'tools.php', esc_attr__( 'Available Tools', 'jetpack-masterbar' ), __( 'Available Tools', 'jetpack-masterbar' ), 'edit_posts', 'tools.php', null, 0 ); } diff --git a/projects/packages/masterbar/src/admin-menu/class-base-admin-menu.php b/projects/packages/masterbar/src/admin-menu/class-base-admin-menu.php index 65400fff1d1bc..dfc5ccb3954c1 100644 --- a/projects/packages/masterbar/src/admin-menu/class-base-admin-menu.php +++ b/projects/packages/masterbar/src/admin-menu/class-base-admin-menu.php @@ -756,21 +756,6 @@ public function use_wp_admin_interface() { return 'wp-admin' === get_option( 'wpcom_admin_interface' ); } - /** - * Check if the user has the default (Calypso) Admin menu. - * - * @return bool - */ - public function is_using_default_admin_menu() { - remove_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option' ); - $option = get_option( 'wpcom_admin_interface' ) !== 'wp-admin'; - if ( function_exists( 'wpcom_admin_interface_pre_get_option' ) ) { - add_filter( 'pre_option_wpcom_admin_interface', 'wpcom_admin_interface_pre_get_option', 10 ); - } - - return $option; - } - /** * Create the desired menu output. */