From 1a89363345b2506e0e16228b23c6c6eb4104f895 Mon Sep 17 00:00:00 2001 From: arthur Date: Thu, 7 Mar 2024 19:46:28 +0800 Subject: [PATCH 1/8] Ecommerce Admin Menu: Get rid of Jetpack Atomic_Admin_Menu dependency --- ...lass-wc-calypso-bridge-base-admin-menu.php | 396 ++++++++++++++++++ ...wc-calypso-bridge-ecommerce-admin-menu.php | 91 ++-- includes/class-wc-calypso-bridge-jetpack.php | 34 +- 3 files changed, 461 insertions(+), 60 deletions(-) create mode 100644 includes/class-wc-calypso-bridge-base-admin-menu.php diff --git a/includes/class-wc-calypso-bridge-base-admin-menu.php b/includes/class-wc-calypso-bridge-base-admin-menu.php new file mode 100644 index 00000000..9beeaca9 --- /dev/null +++ b/includes/class-wc-calypso-bridge-base-admin-menu.php @@ -0,0 +1,396 @@ +is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST || isset( $_SERVER['REQUEST_URI'] ) && str_starts_with( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' ); + $this->domain = ( new Status() )->get_site_suffix(); + + add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 ); + } + + /** + * Returns class instance. + * + * @return Admin_Menu + */ + public static function get_instance() { + $class = static::class; + + if ( empty( static::$instances[ $class ] ) ) { + static::$instances[ $class ] = new $class(); + } + + return static::$instances[ $class ]; + } + + /** + * Updates the menu data of the given menu slug. + * + * @param string $slug Slug of the menu to update. + * @param string $url New menu URL. + * @param string $title New menu title. + * @param string $cap New menu capability. + * @param string $icon New menu icon. + * @param int $position New menu position. + * @return bool Whether the menu has been updated. + */ + public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { + global $menu, $submenu; + + $menu_item = null; + $menu_position = null; + + foreach ( $menu as $i => $item ) { + if ( $slug === $item[2] ) { + $menu_item = $item; + $menu_position = $i; + break; + } + } + + if ( ! $menu_item ) { + return false; + } + + if ( $title ) { + $menu_item[0] = $title; + $menu_item[3] = esc_attr( $title ); + } + + if ( $cap ) { + $menu_item[1] = $cap; + } + + // Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus). + if ( $url ) { + $this->hide_submenu_page( $slug, $slug ); + + if ( ! isset( $submenu[ $slug ] ) || ! $this->has_visible_items( $submenu[ $slug ] ) ) { + $menu_item[2] = $url; + } + } + + if ( $icon ) { + $menu_item[4] = 'menu-top'; + $menu_item[6] = $icon; + } + + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + unset( $menu[ $menu_position ] ); + if ( $position ) { + $menu_position = $position; + } + $this->set_menu_item( $menu_item, $menu_position ); + + // Only add submenu when there are other submenu items. + if ( $url && isset( $submenu[ $slug ] ) && $this->has_visible_items( $submenu[ $slug ] ) ) { + add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 ); + } + + return true; + } + + /** + * Updates the submenus of the given menu slug. + * + * It hides the menu by adding the `hide-if-js` css class and duplicates the submenu with the new slug. + * + * @param string $slug Menu slug. + * @param array $submenus_to_update Array of new submenu slugs. + */ + public function update_submenus( $slug, $submenus_to_update ) { + global $submenu; + + if ( ! isset( $submenu[ $slug ] ) ) { + return; + } + + // This is needed for cases when the submenus to update have the same new slug. + $submenus_to_update = array_filter( + $submenus_to_update, + static function ( $item, $old_slug ) { + return $item !== $old_slug; + }, + ARRAY_FILTER_USE_BOTH + ); + + /** + * Iterate over all submenu items and add the hide the submenus with CSS classes. + * This is done separately of the second foreach because the position of the submenu might change. + */ + foreach ( $submenu[ $slug ] as $index => $item ) { + if ( ! array_key_exists( $item[2], $submenus_to_update ) ) { + continue; + } + + $this->hide_submenu_element( $index, $slug, $item ); + } + + $submenu_items = array_values( $submenu[ $slug ] ); + + /** + * Iterate again over the submenu array. We need a copy of the array because add_submenu_page will add new elements + * to submenu array that might cause an infinite loop. + */ + foreach ( $submenu_items as $i => $submenu_item ) { + if ( ! array_key_exists( $submenu_item[2], $submenus_to_update ) ) { + continue; + } + + add_submenu_page( + $slug, + isset( $submenu_item[3] ) ? $submenu_item[3] : '', + isset( $submenu_item[0] ) ? $submenu_item[0] : '', + isset( $submenu_item[1] ) ? $submenu_item[1] : 'read', + $submenus_to_update[ $submenu_item[2] ], + '', + 0 === $i ? 0 : $i + 1 + ); + } + } + + /** + * Adds a menu separator. + * + * @param int $position The position in the menu order this item should appear. + * @param string $cap Optional. The capability required for this menu to be displayed to the user. + * Default: 'read'. + */ + public function add_admin_menu_separator( $position = null, $cap = 'read' ) { + $menu_item = array( + '', // Menu title (ignored). + $cap, // Required capability. + wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique). + '', // Page title (ignored). + 'wp-menu-separator', // CSS class. Identifies this item as a separator. + ); + + $this->set_menu_item( $menu_item, $position ); + } + + /** + * Hide the submenu page based on slug and return the item that was hidden. + * + * Instead of actually removing the submenu item, a safer approach is to hide it and filter it in the API response. + * In this manner we'll avoid breaking third-party plugins depending on items that no longer exist. + * + * A false|array value is returned to be consistent with remove_submenu_page() function + * + * @param string $menu_slug The parent menu slug. + * @param string $submenu_slug The submenu slug that should be hidden. + * @return false|array + */ + public function hide_submenu_page( $menu_slug, $submenu_slug ) { + global $submenu; + + if ( ! isset( $submenu[ $menu_slug ] ) ) { + return false; + } + + foreach ( $submenu[ $menu_slug ] as $i => $item ) { + if ( $submenu_slug !== $item[2] ) { + continue; + } + + $this->hide_submenu_element( $i, $menu_slug, $item ); + + return $item; + } + + return false; + } + + /** + * Apply the hide-if-js CSS class to a submenu item. + * + * @param int $index The position of a submenu item in the submenu array. + * @param string $parent_slug The parent slug. + * @param array $item The submenu item. + */ + public function hide_submenu_element( $index, $parent_slug, $item ) { + global $submenu; + + $css_classes = empty( $item[4] ) ? self::HIDE_CSS_CLASS : $item[4] . ' ' . self::HIDE_CSS_CLASS; + + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $submenu [ $parent_slug ][ $index ][4] = $css_classes; + } + + /** + * Check if the menu has submenu items visible + * + * @param array $submenu_items The submenu items. + * @return bool + */ + public function has_visible_items( $submenu_items ) { + $visible_items = array_filter( + $submenu_items, + array( $this, 'is_item_visible' ) + ); + + return array() !== $visible_items; + } + + /** + * Return the number of existing submenu items under the supplied parent slug. + * + * @param string $parent_slug The slug of the parent menu. + * @return int The number of submenu items under $parent_slug. + */ + public function get_submenu_item_count( $parent_slug ) { + global $submenu; + + if ( empty( $parent_slug ) || empty( $submenu[ $parent_slug ] ) || ! is_array( $submenu[ $parent_slug ] ) ) { + return 0; + } + + return count( $submenu[ $parent_slug ] ); + } + + /** + * Adds the given menu item in the specified position. + * + * @param array $item The menu item to add. + * @param int $position The position in the menu order this item should appear. + */ + public function set_menu_item( $item, $position = null ) { + global $menu; + + // Handle position (avoids overwriting menu items already populated in the given position). + // Inspired by https://core.trac.wordpress.org/browser/trunk/src/wp-admin/menu.php?rev=49837#L160. + if ( null === $position ) { + $menu[] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } elseif ( isset( $menu[ "$position" ] ) ) { + $position = $position + substr( base_convert( md5( $item[2] . $item[0] ), 16, 10 ), -5 ) * 0.00001; + $menu[ "$position" ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } else { + $menu[ $position ] = $item; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + } + } + + /** + * Hide menus that are unauthorized and don't have visible submenus and cases when the menu has the same slug + * as the first submenu item. + * + * This must be done at the end of menu and submenu manipulation in order to avoid performing this check each time + * the submenus are altered. + */ + public function hide_parent_of_hidden_submenus() { + global $menu, $submenu; + + $this->sort_hidden_submenus(); + + foreach ( $menu as $menu_index => $menu_item ) { + $has_submenus = isset( $submenu[ $menu_item[2] ] ); + + // Skip if the menu doesn't have submenus. + if ( ! $has_submenus ) { + continue; + } + + // If the first submenu item is hidden then we should also hide the parent. + // Since the submenus are ordered by self::HIDE_CSS_CLASS (hidden submenus should be at the end of the array), + // we can say that if the first submenu is hidden then we should also hide the menu. + $first_submenu_item = array_values( $submenu[ $menu_item[2] ] )[0]; + $is_first_submenu_visible = $this->is_item_visible( $first_submenu_item ); + + // if the user does not have access to the menu and the first submenu is hidden, then hide the menu. + if ( ! current_user_can( $menu_item[1] ) && ! $is_first_submenu_visible ) { + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $menu[ $menu_index ][4] = self::HIDE_CSS_CLASS; + } + + // if the menu has the same slug as the first submenu then hide the submenu. + if ( $menu_item[2] === $first_submenu_item[2] && ! $is_first_submenu_visible ) { + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $menu[ $menu_index ][4] = self::HIDE_CSS_CLASS; + } + } + } + + /** + * Sort the hidden submenus by moving them at the end of the array in order to avoid WP using them as default URLs. + * + * This operation has to be done at the end of submenu manipulation in order to guarantee that the hidden submenus + * are at the end of the array. + */ + public function sort_hidden_submenus() { + global $submenu; + + foreach ( $submenu as $menu_slug => $submenu_items ) { + foreach ( $submenu_items as $submenu_index => $submenu_item ) { + if ( $this->is_item_visible( $submenu_item ) ) { + continue; + } + + unset( $submenu[ $menu_slug ][ $submenu_index ] ); + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $submenu[ $menu_slug ][] = $submenu_item; + } + } + } + + /** + * Check if the given item is visible or not in the admin menu. + * + * @param array $item A menu or submenu array. + */ + public function is_item_visible( $item ) { + return ! isset( $item[4] ) || ! str_contains( $item[4], self::HIDE_CSS_CLASS ); + } + + /** + * Whether the current user has indicated they want to use the wp-admin interface for the given screen. + * + * @return bool + */ + public function use_wp_admin_interface() { + return 'wp-admin' === get_option( 'wpcom_admin_interface' ); + } + + /** + * Create the desired menu output. + */ + abstract public function reregister_menu_items(); +} diff --git a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php index 93f38134..9f2a9dca 100644 --- a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php +++ b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php @@ -1,16 +1,19 @@ handle_free_trial_menu(); - } - if ( ! $this->is_api_request ) { add_filter( 'submenu_file', array( $this, 'modify_woocommerce_menu_highlighting' ), 99999 ); } @@ -73,27 +69,54 @@ public function __construct() { } /** - * Modify admin menu for the Ecommerce Free Trial plan. + * Returns class instance. + * + * @return Ecommerce_Atomic_Admin_Menu */ - protected function handle_free_trial_menu() { + public static function get_instance() { + $class = static::class; - add_action( 'admin_menu', function() { + if ( empty( static::$instances[ $class ] ) ) { + static::$instances[ $class ] = new $class(); + } - // Hide Extensions > Manage and the new Extensions page. - $this->hide_submenu_page( 'woocommerce', 'admin.php?page=wc-addons§ion=helper' ); - $this->hide_submenu_page( 'woocommerce', 'wc-admin&path=/extensions' ); + return static::$instances[ $class ]; + } - // Move Feedback under Jetpack > Feedback. - $this->hide_submenu_page( 'feedback', 'edit.php?post_type=feedback' ); - remove_menu_page( 'feedback' ); - add_submenu_page( 'jetpack', __( 'Feedback', 'wc-calypso-bridge' ), __( 'Feedback', 'wc-calypso-bridge' ), 'manage_woocommerce', 'edit.php?post_type=feedback', '', 10 ); + /** + * Create the desired menu output. + */ + public function reregister_menu_items() { + $this->add_plugins_menu(); + $this->add_options_menu(); + $this->add_jetpack_menu(); + $this->add_my_home_menu(); + $this->maybe_remove_customizer_menu(); + $this->add_woocommerce_menu(); - // Hide Tools > Marketing and Tools > Earn submenus. - $site_suffix = WC_Calypso_Bridge_Instance()->get_site_slug(); - $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/marketing/tools/%s', $site_suffix ) ); - $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/earn/%s', $site_suffix ) ); + // Handle menu for ecommerce free trial. + if ( wc_calypso_bridge_is_ecommerce_trial_plan() ) { + $this->handle_free_trial_menu(); + } + } - }, 99999 ); + /** + * Modify admin menu for the Ecommerce Free Trial plan. + */ + protected function handle_free_trial_menu() { + // Hide Extensions > Manage and the new Extensions page. + $this->hide_submenu_page( 'woocommerce', 'admin.php?page=wc-addons§ion=helper' ); + $this->hide_submenu_page( 'woocommerce', 'wc-admin&path=/extensions' ); + + // Move Feedback under Jetpack > Feedback. + $this->hide_submenu_page( 'feedback', 'edit.php?post_type=feedback' ); + remove_menu_page( 'feedback' ); + add_submenu_page( 'jetpack', __( 'Feedback', 'wc-calypso-bridge' ), __( 'Feedback', 'wc-calypso-bridge' ), 'manage_woocommerce', 'edit.php?post_type=feedback', '', 10 ); + + // Hide Tools > Marketing and Tools > Earn submenus. + $site_suffix = WC_Calypso_Bridge_Instance()->get_site_slug(); + $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/marketing/tools/%s', $site_suffix ) ); + $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/earn/%s', $site_suffix ) ); } /** @@ -152,11 +175,11 @@ protected function handle_orders_menu() { * @return void */ public function add_plugins_menu() { - if ( wc_calypso_bridge_is_ecommerce_trial_plan() ) { + if ( ! wc_calypso_bridge_is_ecommerce_trial_plan() ) { return; } - return parent::add_plugins_menu(); + remove_menu_page( 'https://wordpress.com/plugins/' . $this->domain ); } /** @@ -566,8 +589,6 @@ private function reorder_analytics_menu() { * Introduce 'Settings > Anti-Spam' and remove 'Settings > Jetpack' from Settings. */ public function add_options_menu() { - parent::add_options_menu(); - // Introduce 'Settings > Anti-Spam'. add_submenu_page( 'options-general.php', __( 'Anti-Spam', 'wc-calypso-bridge' ), __( 'Anti-Spam', 'wc-calypso-bridge' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ), 12 ); // Remove 'Settings > Jetpack' from Settings. @@ -581,8 +602,6 @@ public function add_jetpack_menu() { global $submenu; - parent::add_jetpack_menu(); - // Remove Jetpack Search menu item. Already exposed in the Jetpack Dashboard. $this->hide_submenu_page( 'jetpack', 'jetpack-search' ); @@ -623,10 +642,4 @@ public function add_jetpack_menu() { } ); } } - - /** - * Remove Stats menu. - */ - public function add_stats_menu() { - } } diff --git a/includes/class-wc-calypso-bridge-jetpack.php b/includes/class-wc-calypso-bridge-jetpack.php index d862339d..05420fe8 100644 --- a/includes/class-wc-calypso-bridge-jetpack.php +++ b/includes/class-wc-calypso-bridge-jetpack.php @@ -50,35 +50,27 @@ public function __construct() { * Initialize hooks. */ public function init() { - /** - * Inject the Ecommerce admin menu controller into Jetpack. + * `ecommerce_new_woo_atomic_navigation_enabled` filter. * - * @since 1.9.8 + * This filter is used to revert the ecommerce menu back to the atomic one. It's also useful for debugging purposes. * - * @param string $menu_controller_class The name of the menu controller class. - * @return string + * @since 1.9.12 + * + * @param bool $enabled + * @return bool */ - add_filter( 'jetpack_admin_menu_class', function ( $menu_controller_class ) { + $is_wooexpress_navigation_enabled = (bool) apply_filters( 'ecommerce_new_woo_atomic_navigation_enabled', 'yes' === get_option( 'wooexpress_navigation_enabled', 'yes' ) ); + if ( $is_wooexpress_navigation_enabled && class_exists( '\Jetpack' ) && \Jetpack::is_module_active( 'sso' ) ) { + require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php'; /** - * `ecommerce_new_woo_atomic_navigation_enabled` filter. - * - * This filter is used to revert the ecommerce menu back to the atomic one. It's also useful for debugging purposes. - * - * @since 1.9.12 + * Apply the Ecommerce admin menu. * - * @param bool $enabled - * @return bool + * @since 1.9.8 */ - - if ( (bool) apply_filters( 'ecommerce_new_woo_atomic_navigation_enabled', 'yes' === get_option( 'wooexpress_navigation_enabled', 'yes' ) ) && class_exists( '\Automattic\Jetpack\Dashboard_Customizations\Atomic_Admin_Menu' ) && class_exists('\Jetpack') && \Jetpack::is_module_active( 'sso' ) ) { - require_once WC_CALYPSO_BRIDGE_PLUGIN_PATH . '/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php'; - return Ecommerce_Atomic_Admin_Menu::class; - } - - return $menu_controller_class; - } ); + WC_Calypso_Bridge_Ecommerce_Admin_Menu::get_instance(); + } /** * Limits Jetpack Modules to those relevant to Ecommerce Plan users. From 7b0fbd3374c311c9aac285b7623689b610957ef9 Mon Sep 17 00:00:00 2001 From: arthur Date: Thu, 7 Mar 2024 20:59:29 +0800 Subject: [PATCH 2/8] Don't add Jetpack Status and Stats menu when nav redesign is enabled --- .../class-wc-calypso-bridge-ecommerce-admin-menu.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php index 9f2a9dca..9b0467bb 100644 --- a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php +++ b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php @@ -608,10 +608,13 @@ public function add_jetpack_menu() { // Move Akismet under Settings $this->hide_submenu_page( 'jetpack', 'akismet-key-config' ); - // Move Jetpack status screen from 'Settings > Jetpack' to 'Tools > Jetpack Status'. - add_submenu_page( 'tools.php', esc_attr__( 'Jetpack Status', 'wc-calypso-bridge' ), __( 'Jetpack Status', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/settings/jetpack/' . $this->domain, null, 100 ); + if ( function_exists( 'wpcom_is_nav_redesign_enabled' ) && wpcom_is_nav_redesign_enabled() ) { + // Move Jetpack status screen from 'Settings > Jetpack' to 'Tools > Jetpack Status'. + add_submenu_page( 'tools.php', esc_attr__( 'Jetpack Status', 'wc-calypso-bridge' ), __( 'Jetpack Status', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/settings/jetpack/' . $this->domain, null, 100 ); - add_submenu_page( 'jetpack', esc_attr__( 'Jetpack Stats', 'wc-calypso-bridge' ), __( 'Stats', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/stats/day/' . $this->domain, null, 100 ); + // Add Jetpack Stats to 'Jetpack > Stats'. + add_submenu_page( 'jetpack', esc_attr__( 'Jetpack Stats', 'wc-calypso-bridge' ), __( 'Stats', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/stats/day/' . $this->domain, null, 100 ); + } // Order Jetpack submenu to have Dashboard first followed by Stats. if ( ! empty( $submenu['jetpack'] ) && is_array( $submenu['jetpack'] ) ) { From 42dafad858fe0a39a2144daf26e8c86edc847b17 Mon Sep 17 00:00:00 2001 From: arthur Date: Thu, 7 Mar 2024 21:07:14 +0800 Subject: [PATCH 3/8] Hide Plugins menu for free trial plan --- ...lass-wc-calypso-bridge-base-admin-menu.php | 2 +- ...wc-calypso-bridge-ecommerce-admin-menu.php | 31 ++++++------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/includes/class-wc-calypso-bridge-base-admin-menu.php b/includes/class-wc-calypso-bridge-base-admin-menu.php index 9beeaca9..37aac7a7 100644 --- a/includes/class-wc-calypso-bridge-base-admin-menu.php +++ b/includes/class-wc-calypso-bridge-base-admin-menu.php @@ -44,7 +44,7 @@ abstract class WC_Calypso_Bridge_Base_Admin_Menu { */ protected function __construct() { $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST || isset( $_SERVER['REQUEST_URI'] ) && str_starts_with( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' ); - $this->domain = ( new Status() )->get_site_suffix(); + $this->domain = WC_Calypso_Bridge_Instance()->get_site_slug(); add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 ); } diff --git a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php index 9b0467bb..6e456876 100644 --- a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php +++ b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php @@ -87,7 +87,6 @@ public static function get_instance() { * Create the desired menu output. */ public function reregister_menu_items() { - $this->add_plugins_menu(); $this->add_options_menu(); $this->add_jetpack_menu(); $this->add_my_home_menu(); @@ -114,9 +113,15 @@ protected function handle_free_trial_menu() { add_submenu_page( 'jetpack', __( 'Feedback', 'wc-calypso-bridge' ), __( 'Feedback', 'wc-calypso-bridge' ), 'manage_woocommerce', 'edit.php?post_type=feedback', '', 10 ); // Hide Tools > Marketing and Tools > Earn submenus. - $site_suffix = WC_Calypso_Bridge_Instance()->get_site_slug(); - $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/marketing/tools/%s', $site_suffix ) ); - $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/earn/%s', $site_suffix ) ); + $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/marketing/tools/%s', $this->domain ) ); + $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/earn/%s', $this->domain ) ); + + // Hide Plugins + if ( function_exists( 'wpcom_is_nav_redesign_enabled' ) && wpcom_is_nav_redesign_enabled() ) { + remove_menu_page( 'plugins.php' ); + } else { + remove_menu_page( 'https://wordpress.com/plugins/' . $this->domain ); + } } /** @@ -166,22 +171,6 @@ protected function handle_orders_menu() { } } - /** - * Override the base implementation of add_plugins_menu() to avoid - * adding the Plugins menu for eCommerce trials. - * - * @since 2.0.8 - * - * @return void - */ - public function add_plugins_menu() { - if ( ! wc_calypso_bridge_is_ecommerce_trial_plan() ) { - return; - } - - remove_menu_page( 'https://wordpress.com/plugins/' . $this->domain ); - } - /** * Groups WooCommerce items. */ @@ -608,7 +597,7 @@ public function add_jetpack_menu() { // Move Akismet under Settings $this->hide_submenu_page( 'jetpack', 'akismet-key-config' ); - if ( function_exists( 'wpcom_is_nav_redesign_enabled' ) && wpcom_is_nav_redesign_enabled() ) { + if ( ! function_exists( 'wpcom_is_nav_redesign_enabled' ) || ! wpcom_is_nav_redesign_enabled() ) { // Move Jetpack status screen from 'Settings > Jetpack' to 'Tools > Jetpack Status'. add_submenu_page( 'tools.php', esc_attr__( 'Jetpack Status', 'wc-calypso-bridge' ), __( 'Jetpack Status', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/settings/jetpack/' . $this->domain, null, 100 ); From da1a6c87e145a28c5beb38253a12009d209489d3 Mon Sep 17 00:00:00 2001 From: arthur Date: Fri, 8 Mar 2024 14:41:52 +0800 Subject: [PATCH 4/8] Address feedback --- ...class-wc-calypso-bridge-base-admin-menu.php | 3 +-- ...-wc-calypso-bridge-ecommerce-admin-menu.php | 18 ++---------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/includes/class-wc-calypso-bridge-base-admin-menu.php b/includes/class-wc-calypso-bridge-base-admin-menu.php index 37aac7a7..810fe232 100644 --- a/includes/class-wc-calypso-bridge-base-admin-menu.php +++ b/includes/class-wc-calypso-bridge-base-admin-menu.php @@ -1,7 +1,5 @@ Date: Fri, 8 Mar 2024 15:40:37 +0800 Subject: [PATCH 5/8] Remove Stats menu on the top level --- includes/class-wc-calypso-bridge-ecommerce-admin-menu.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php index 52833be0..7e0eeb5a 100644 --- a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php +++ b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php @@ -587,7 +587,8 @@ public function add_jetpack_menu() { // Move Jetpack status screen from 'Settings > Jetpack' to 'Tools > Jetpack Status'. add_submenu_page( 'tools.php', esc_attr__( 'Jetpack Status', 'wc-calypso-bridge' ), __( 'Jetpack Status', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/settings/jetpack/' . $this->domain, null, 100 ); - // Add Jetpack Stats to 'Jetpack > Stats'. + // Move Jetpack Stats to 'Jetpack > Stats'. + remove_menu_page( 'https://wordpress.com/stats/day/' . $this->domain ); add_submenu_page( 'jetpack', esc_attr__( 'Jetpack Stats', 'wc-calypso-bridge' ), __( 'Stats', 'wc-calypso-bridge' ), 'manage_options', 'https://wordpress.com/stats/day/' . $this->domain, null, 100 ); } From 410d3e9cebb88bb8203988172f904fc587a5f15d Mon Sep 17 00:00:00 2001 From: arthur Date: Fri, 8 Mar 2024 15:46:19 +0800 Subject: [PATCH 6/8] Add changelog --- readme.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.txt b/readme.txt index 3abdb272..5ace29d7 100644 --- a/readme.txt +++ b/readme.txt @@ -22,6 +22,9 @@ This section describes how to install the plugin and get it working. == Changelog == += Unreleased = +* Fix the Woo Express navigation is missing when the wpcom_is_nav_redesign_enabled is enabled #xxx + = 2.3.10 = * Force square_cash_app_pay and square_credit_card order on the payment settings page -- follow up issue #1447 From 2be0730517cb06c2a19015ea11a74679c4737b65 Mon Sep 17 00:00:00 2001 From: arthur Date: Thu, 14 Mar 2024 11:22:46 +0900 Subject: [PATCH 7/8] Fix the menu without the nav redesign --- .../class-wc-calypso-bridge-ecommerce-admin-menu.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php index 7e0eeb5a..d782a196 100644 --- a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php +++ b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php @@ -103,11 +103,7 @@ protected function handle_free_trial_menu() { $this->hide_submenu_page( 'tools.php', sprintf( 'https://wordpress.com/earn/%s', $this->domain ) ); // Hide Plugins - if ( function_exists( 'wpcom_is_nav_redesign_enabled' ) && wpcom_is_nav_redesign_enabled() ) { - remove_menu_page( 'plugins.php' ); - } else { - remove_menu_page( 'https://wordpress.com/plugins/' . $this->domain ); - } + remove_menu_page( 'plugins.php' ); } /** @@ -124,6 +120,7 @@ protected function handle_orders_menu() { // Create the toplevel menu from scratch. $this->hide_submenu_page( 'woocommerce', 'wc-orders' ); + $this->hide_submenu_page( 'woocommerce', 'edit.php?post_type=shop_order' ); add_menu_page( __( 'Orders', 'woocommerce' ), __( 'Orders', 'woocommerce' ), 'edit_shop_orders', 'admin.php?page=wc-orders', null, 'dashicons-cart', 40 ); add_submenu_page( 'admin.php?page=wc-orders', __( 'Orders', 'woocommerce' ), __( 'Orders', 'woocommerce' ), 'edit_shop_orders', 'admin.php?page=wc-orders', null, 1 ); add_submenu_page( 'admin.php?page=wc-orders', __( 'Add New Order', 'woocommerce' ), __( 'Add New', 'woocommerce' ), 'edit_shop_orders', 'admin.php?page=wc-orders&action=new', null, 2 ); @@ -239,6 +236,9 @@ public function add_my_home_menu() { } $this->update_menu( 'index.php', 'admin.php?page=wc-admin', $label, 'edit_posts', 'dashicons-admin-home' ); + if ( ! function_exists( 'wpcom_is_nav_redesign_enabled' ) || ! wpcom_is_nav_redesign_enabled() ) { + remove_submenu_page( 'index.php', 'https://wordpress.com/home/' . $this->domain ); + } } /** From c789eb1f173434dfb82adecd4ed20d7486828e81 Mon Sep 17 00:00:00 2001 From: Louis Laugesen Date: Mon, 18 Mar 2024 16:46:34 +1100 Subject: [PATCH 8/8] My Home fixes --- ...wc-calypso-bridge-ecommerce-admin-menu.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php index d782a196..65f370da 100644 --- a/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php +++ b/includes/class-wc-calypso-bridge-ecommerce-admin-menu.php @@ -239,6 +239,28 @@ public function add_my_home_menu() { if ( ! function_exists( 'wpcom_is_nav_redesign_enabled' ) || ! wpcom_is_nav_redesign_enabled() ) { remove_submenu_page( 'index.php', 'https://wordpress.com/home/' . $this->domain ); } + + // Replace "Hosting" (/home) link with "Hosting" (/plans). + $this->update_menu( + 'wpcom-hosting-menu', + esc_url( "https://wordpress.com/plans/{$this->domain}" ), + esc_attr__( 'Hosting', 'wc-calypso-bridge' ), + 'manage_options', + 'dashicons-cloud', + 3 + ); + + // Remove "Hosting" submenu item created by the above. + remove_submenu_page( + 'wpcom-hosting-menu', + esc_url( "https://wordpress.com/plans/{$this->domain}" ) + ); + + // Remove "My Home" submenu item. + remove_submenu_page( + 'wpcom-hosting-menu', + esc_url( "https://wordpress.com/home/{$this->domain}" ) + ); } /**