From 13f6113a95844e19d136f7a9812b3bf7c668ed95 Mon Sep 17 00:00:00 2001 From: bogdanungureanu Date: Tue, 29 Jul 2025 19:56:58 +0300 Subject: [PATCH 1/4] Untangling: Replace Calypso stats menu w WP-Admin version --- .../changelog/update-stats-link-to-wp-admin | 5 +++++ .../wpcom-admin-menu/wpcom-admin-menu.php | 21 ++++++++++++++++++ .../changelog/update-stats-link-to-wp-admin | 5 +++++ .../src/admin-menu/class-admin-menu.php | 9 -------- .../admin-menu/class-atomic-admin-menu.php | 22 ------------------- .../src/admin-menu/class-wpcom-admin-menu.php | 18 --------------- .../masterbar/tests/php/Admin_Menu_Test.php | 17 -------------- 7 files changed, 31 insertions(+), 66 deletions(-) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/update-stats-link-to-wp-admin create mode 100644 projects/packages/masterbar/changelog/update-stats-link-to-wp-admin diff --git a/projects/packages/jetpack-mu-wpcom/changelog/update-stats-link-to-wp-admin b/projects/packages/jetpack-mu-wpcom/changelog/update-stats-link-to-wp-admin new file mode 100644 index 0000000000000..eb9d48461eb12 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/update-stats-link-to-wp-admin @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Change the link of the Stats menu to point to the WP-Admin version + + 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 f37b9a1b00ffb..723bbecdd5d5d 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 @@ -9,6 +9,7 @@ use Automattic\Jetpack\Connection\Manager as Connection_Manager; use Automattic\Jetpack\Redirect; +use Automattic\Jetpack\Status\Host; use Automattic\Jetpack\Subscribers_Dashboard\Dashboard as Subscribers_Dashboard; require_once __DIR__ . '/../../common/wpcom-callout.php'; @@ -97,6 +98,26 @@ function wpcom_add_my_home_menu() { } add_action( 'admin_menu', 'wpcom_add_my_home_menu' ); +/** + * Add the top menu item for the "Stats". + * + * @return void + */ +function wpcom_add_stats_menu() { + $host = new Host(); + + /** + * If the site is Atomic and the module is disabled, the page doesn't exist. + */ + if ( $host->is_atomic_platform() && ! Jetpack::is_module_active( 'stats' ) ) { + return; + } + + add_menu_page( __( 'Stats', 'jetpack-mu-wpcom' ), __( 'Stats', 'jetpack-mu-wpcom' ), 'view_stats', 'admin.php?page=stats', null, 'dashicons-chart-bar', 3 ); +} + +add_action( 'admin_menu', 'wpcom_add_stats_menu' ); + /** * Adds a Hosting menu. */ diff --git a/projects/packages/masterbar/changelog/update-stats-link-to-wp-admin b/projects/packages/masterbar/changelog/update-stats-link-to-wp-admin new file mode 100644 index 0000000000000..eb9d48461eb12 --- /dev/null +++ b/projects/packages/masterbar/changelog/update-stats-link-to-wp-admin @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Change the link of the Stats menu to point to the WP-Admin version + + 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 272982cb26eb6..8b48bb62c118a 100644 --- a/projects/packages/masterbar/src/admin-menu/class-admin-menu.php +++ b/projects/packages/masterbar/src/admin-menu/class-admin-menu.php @@ -32,7 +32,6 @@ public function __construct() { public function reregister_menu_items() { // Remove separators. remove_menu_page( 'separator1' ); - $this->add_stats_menu(); $this->add_upgrades_menu(); $this->add_posts_menu(); $this->add_media_menu(); @@ -117,14 +116,6 @@ public function add_my_mailboxes_menu() { add_menu_page( __( 'My Mailboxes', 'jetpack-masterbar' ), __( 'My Mailboxes', 'jetpack-masterbar' ), 'manage_options', 'https://wordpress.com/mailboxes/' . $this->domain, null, 'dashicons-email', 4.64424 ); } - /** - * Adds Stats menu. - */ - public function add_stats_menu() { - // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. - add_menu_page( __( 'Stats', 'jetpack-masterbar' ), __( 'Stats', 'jetpack-masterbar' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); - } - /** * Adds Upgrades menu. * 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 4560bfcb4988f..1a6c1aaea8076 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 @@ -265,28 +265,6 @@ public function add_jetpack_menu() { } } - /** - * Adds Stats menu. - */ - public function add_stats_menu() { - $menu_title = __( 'Stats', 'jetpack-masterbar' ); - if ( - ! $this->is_api_request && - ( new Modules() )->is_active( 'stats' ) && - function_exists( 'stats_get_image_chart_src' ) - ) { - $img_src = esc_attr( - stats_get_image_chart_src( 'admin-bar-hours-scale-2x', array( 'masterbar' => '' ) ) - ); - $alt = esc_attr__( 'Hourly views', 'jetpack-masterbar' ); - - $menu_title .= "$alt"; - } - - // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. - add_menu_page( __( 'Stats', 'jetpack-masterbar' ), $menu_title, 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); - } - /** * Adds Upgrades menu. * diff --git a/projects/packages/masterbar/src/admin-menu/class-wpcom-admin-menu.php b/projects/packages/masterbar/src/admin-menu/class-wpcom-admin-menu.php index cc8f9c6e86702..b90c0bee738ca 100644 --- a/projects/packages/masterbar/src/admin-menu/class-wpcom-admin-menu.php +++ b/projects/packages/masterbar/src/admin-menu/class-wpcom-admin-menu.php @@ -164,24 +164,6 @@ public function get_upsell_nudge() { } } - /** - * Adds Stats menu. - */ - public function add_stats_menu() { - $menu_title = __( 'Stats', 'jetpack-masterbar' ); - - if ( ! $this->is_api_request ) { - $menu_title .= sprintf( - '%2$s', - esc_url( site_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ), - esc_attr__( 'Hourly views', 'jetpack-masterbar' ) - ); - } - - // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. - add_menu_page( __( 'Stats', 'jetpack-masterbar' ), $menu_title, 'read', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); - } - /** * Gets the current plan and stores it in $this->current_plan so the database is only called once per request. * diff --git a/projects/packages/masterbar/tests/php/Admin_Menu_Test.php b/projects/packages/masterbar/tests/php/Admin_Menu_Test.php index d1a34c3e28619..80c0a64b8d3da 100644 --- a/projects/packages/masterbar/tests/php/Admin_Menu_Test.php +++ b/projects/packages/masterbar/tests/php/Admin_Menu_Test.php @@ -120,23 +120,6 @@ public function test_get_preferred_view() { $this->assertSame( 'default', static::$admin_menu->get_preferred_view( 'options-general.php' ) ); } - /** - * Tests add_stats_menu - */ - public function test_add_stats_menu() { - global $menu; - - static::$admin_menu->add_stats_menu(); - // Ignore position keys, since the key used for the Stats menu contains a pseudorandom number - // that we shouldn't hardcode. The only thing that matters is that the menu should be in the - // 3rd position regardless of the key. - // @see https://core.trac.wordpress.org/ticket/40927 - ksort( $menu ); - $menu_items = array_values( $menu ); - - $this->assertSame( 'https://wordpress.com/stats/day/' . static::$domain, $menu_items[2][2] ); - } - /** * Tests add_upgrades_menu */ From 538b067b9231e35dbbd6c434743cf1d510b7c7d9 Mon Sep 17 00:00:00 2001 From: bogdanungureanu Date: Wed, 30 Jul 2025 11:41:42 +0300 Subject: [PATCH 2/4] Update test. Since the stats menu is not registered with nav-unification, the count should be lower. --- projects/packages/masterbar/tests/php/Admin_Menu_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/masterbar/tests/php/Admin_Menu_Test.php b/projects/packages/masterbar/tests/php/Admin_Menu_Test.php index 80c0a64b8d3da..3b5cf3ed5fa10 100644 --- a/projects/packages/masterbar/tests/php/Admin_Menu_Test.php +++ b/projects/packages/masterbar/tests/php/Admin_Menu_Test.php @@ -105,7 +105,7 @@ public function test_admin_menu_output() { static::$admin_menu->reregister_menu_items(); - $this->assertCount( 18, $menu, 'Admin menu should not have unexpected top menu items.' ); + $this->assertCount( 17, $menu, 'Admin menu should not have unexpected top menu items.' ); $this->assertEquals( static::$submenu_data[''], $submenu[''], 'Submenu items without parent should stay the same.' ); } From 9186d68037fa2d8a96e716a7bc6c1c3e82fbfef6 Mon Sep 17 00:00:00 2001 From: bogdanungureanu Date: Wed, 30 Jul 2025 15:19:06 +0300 Subject: [PATCH 3/4] Fix linting --- .../src/features/wpcom-admin-menu/wpcom-admin-menu.php | 2 ++ 1 file changed, 2 insertions(+) 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 723bbecdd5d5d..59f3342cd7e39 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 @@ -109,10 +109,12 @@ function wpcom_add_stats_menu() { /** * If the site is Atomic and the module is disabled, the page doesn't exist. */ + // @phan-suppress-next-line PhanUndeclaredClassMethod -- We need to suppress it for WordPress.com tests. if ( $host->is_atomic_platform() && ! Jetpack::is_module_active( 'stats' ) ) { return; } + // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. add_menu_page( __( 'Stats', 'jetpack-mu-wpcom' ), __( 'Stats', 'jetpack-mu-wpcom' ), 'view_stats', 'admin.php?page=stats', null, 'dashicons-chart-bar', 3 ); } From be440017c81e0aa87dca2602855cc214c04270f2 Mon Sep 17 00:00:00 2001 From: mmtr <1233880+mmtr@users.noreply.github.com> Date: Mon, 4 Aug 2025 15:31:57 +0200 Subject: [PATCH 4/4] Add menu back to Jetpack sites --- .../src/admin-menu/class-jetpack-admin-menu.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/projects/packages/masterbar/src/admin-menu/class-jetpack-admin-menu.php b/projects/packages/masterbar/src/admin-menu/class-jetpack-admin-menu.php index 840552da24118..c27647504cabe 100644 --- a/projects/packages/masterbar/src/admin-menu/class-jetpack-admin-menu.php +++ b/projects/packages/masterbar/src/admin-menu/class-jetpack-admin-menu.php @@ -41,6 +41,7 @@ public function reregister_menu_items() { parent::reregister_menu_items(); + $this->add_stats_menu(); $this->add_feedback_menu(); $this->add_cpt_menus(); $this->add_wp_admin_menu(); @@ -81,6 +82,14 @@ public function get_cpt_menu_link( $ptype_obj ) { } } + /** + * Adds Stats menu. + */ + public function add_stats_menu() { + // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539. + add_menu_page( __( 'Stats', 'jetpack-masterbar' ), __( 'Stats', 'jetpack-masterbar' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); + } + /** * Adds Posts menu. */