Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Stats v2: rename the WordPress.com Simple dashboard menu label from "Premium Analytics" to "Stats v2".
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public static function load_wpcom_simple_premium_analytics() {
array(
// A closure, not a string: we run on plugins_loaded, too early to translate.
// The package calls this back on admin_menu.
'menu_title' => fn () => __( 'Premium Analytics', 'jetpack-mu-wpcom' ),
'menu_title' => fn () => __( 'Stats v2', 'jetpack-mu-wpcom' ),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function ( $options ) {
// translated it on plugins_loaded, before the textdomain loads.
$menu_title = $options['menu_title'] ?? null;

return $menu_title instanceof \Closure && 'Premium Analytics' === $menu_title();
return $menu_title instanceof \Closure && 'Stats v2' === $menu_title();
}
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Rename the default admin menu label from "Analytics" to "Stats v2".
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private static function menu_title() {
// esc_html() as a broken label instead of falling back here.
self::$resolved_menu_title = is_string( $title ) && '' !== $title
? $title
: __( 'Analytics', 'jetpack-premium-analytics-pkg' );
: __( 'Stats v2', 'jetpack-premium-analytics-pkg' );

return self::$resolved_menu_title;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function test_is_dashboard_request_false_on_front_end() {
public function test_register_admin_menu_labels_the_page_from_the_package() {
$menu_item = $this->register_admin_menu_without_build();

$this->assertSame( 'Analytics', $menu_item[0] ?? null );
$this->assertSame( 'Stats v2', $menu_item[0] ?? null );
}

/**
Expand Down Expand Up @@ -269,7 +269,7 @@ public function test_register_admin_menu_falls_back_when_a_closure_returns_no_la

$menu_item = $this->register_admin_menu_without_build();

$this->assertSame( 'Analytics', $menu_item[0] ?? null );
$this->assertSame( 'Stats v2', $menu_item[0] ?? null );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Stats v2: add it as a separate menu instead of replacing the existing Stats menu, on any site.
15 changes: 0 additions & 15 deletions projects/plugins/jetpack/class-jetpack-stats-dashboard-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Automattic\Jetpack\Redirect;
use Automattic\Jetpack\Stats_Admin\WP_Dashboard_Odyssey_Widget as Dashboard_Stats_Widget;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host;

/**
* Class that adds the Jetpack stats widget to the WordPress admin dashboard.
Expand Down Expand Up @@ -42,20 +41,6 @@ public static function init() {
* Sets up the Jetpack Stats widget in the WordPress admin dashboard.
*/
public static function wp_dashboard_setup() {
/*
* The bundled Premium Analytics dashboard replaces the Stats widget when
* enabled (WOOA7S-1595).
*
* Never on WordPress.com Simple. This file is required standalone there —
* jetpack-mu-wpcom loads it on wp_dashboard_setup — into an environment where
* `Jetpack` is wpcom's own class, not the plugin's, so the method below does
* not exist and calling it fatals the whole dashboard. The plugin's bundled
* package is not what Simple sites run anyway.
*/
if ( ! ( new Host() )->is_wpcom_simple() && Jetpack::is_premium_analytics_enabled() ) {
return;
}

/**
* Filter whether the Jetpack Stats dashboard widget should be shown to the current user.
* By default, the dashboard widget is shown to users who can view_stats.
Expand Down
33 changes: 16 additions & 17 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,20 +842,18 @@ public static function configure_import_package() {
}

/**
* Whether the bundled Premium Analytics dashboard is enabled.
* Whether the bundled Stats v2 dashboard is enabled.
*
* Premium Analytics ships with the plugin behind this flag while it rolls
* out (WOOA7S-1595). When enabled it replaces the Stats wp-admin UI (menu,
* admin-bar entries, post-list column, and WP dashboard widget); the Stats
* module's tracking is unaffected — Premium Analytics depends on it.
* Stats v2 (formerly "Premium Analytics") ships with the plugin behind this
* flag while it rolls out (WOOA7S-1595). When enabled it adds its own admin
* menu alongside the existing Stats UI; it never replaces or hides the
* legacy Stats menu, admin-bar entries, post-list column, or WP dashboard
* widget. The Stats module's tracking is unaffected either way — Stats v2
* depends on it.
*
* The package has to be loadable for this to be true. The same answer both
* tears the Stats UI down and brings the dashboard up, so if the two could
* disagree a site missing the package would end up with neither.
*
* Resolved once per request: `configure()` asks first, on `plugins_loaded`,
* and the Stats module and dashboard widget ask much later. Without the
* cache a filter registered in between would be seen by only some of them.
* The package has to be loadable for this to be true, so a site with the
* flag on but a missing package answers false here and never adds the
* Stats v2 menu (a warning is logged instead).
*
* @since $$next-version$$
*
Expand Down Expand Up @@ -996,11 +994,12 @@ static function () {
}

/*
* Premium Analytics (WOOA7S-1595): bundled behind a flag while it rolls
* out. Unlike Stats above it must initialize on every request when
* enabled: its WooCommerce store-event tracker listens on the front end
* and its REST surfaces self-gate on rest_api_init. When enabled it
* replaces the Stats wp-admin UI (see modules/stats.php).
* Stats v2 (WOOA7S-1595): bundled behind a flag while it rolls out.
* Unlike Stats above it must initialize on every request when enabled:
* its WooCommerce store-event tracker listens on the front end and its
* REST surfaces self-gate on rest_api_init. It adds its own admin menu
* alongside the existing Stats UI (see modules/stats.php) rather than
* replacing it.
*/
if ( self::is_premium_analytics_enabled() ) {
// No menu_title here: the package labels its own menu on admin_menu.
Expand Down
16 changes: 4 additions & 12 deletions projects/plugins/jetpack/modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,16 @@
function stats_load() {
Jetpack::enable_module_configurable( __FILE__ );

// When the bundled Premium Analytics dashboard is enabled it replaces the
// Stats wp-admin UI (WOOA7S-1595): skip the menu, the admin-bar entries, and
// the post-list column — they all link to the removed Stats page. Tracking
// and the module's non-UI filters below are unaffected.
$stats_ui_replaced = Jetpack::is_premium_analytics_enabled();

// Only run the callback for those who can see the stats.
if ( ! $stats_ui_replaced && is_user_logged_in() && current_user_can( 'view_stats' ) ) {
if ( is_user_logged_in() && current_user_can( 'view_stats' ) ) {
add_action( 'admin_head', 'stats_admin_bar_head', 100 );
add_action( 'wp_head', 'stats_admin_bar_head', 100 );
}

if ( ! $stats_ui_replaced ) {
Admin_Post_List_Column::register();
Admin_Post_List_Column::register();

add_action( 'jetpack_admin_menu', 'stats_admin_menu' );
add_action( 'wp_before_admin_bar_render', 'stats_add_link_to_admin_bar_site_menu' );
}
add_action( 'jetpack_admin_menu', 'stats_admin_menu' );
add_action( 'wp_before_admin_bar_render', 'stats_add_link_to_admin_bar_site_menu' );

add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
* @package automattic/jetpack
*/

use Automattic\Jetpack\Constants;
use PHPUnit\Framework\Attributes\CoversClass;

require_once JETPACK__PLUGIN_DIR . 'class-jetpack-stats-dashboard-widget.php';
require_once JETPACK__PLUGIN_DIR . 'modules/stats.php';

/**
* Tests for Jetpack::is_premium_analytics_enabled() and the Stats UI it replaces.
* Tests for Jetpack::is_premium_analytics_enabled().
*
* @covers \Jetpack
*/
Expand All @@ -33,6 +33,7 @@ public function set_up() {
public function tear_down() {
delete_option( 'jetpack_premium_analytics_enabled' );
self::reset_flag_cache();
remove_action( 'jetpack_admin_menu', 'stats_admin_menu' );
parent::tear_down();
}

Expand Down Expand Up @@ -185,15 +186,17 @@ public function test_filter_registered_after_resolution_is_ignored() {
}

/**
* The Stats dashboard widget bows out before any of its own checks run.
* The Stats dashboard widget always carries on to its own visibility check:
* Stats v2 adds a menu rather than replacing the legacy widget, on any site.
*/
public function test_dashboard_widget_is_skipped_when_enabled() {
public function test_dashboard_widget_runs_when_enabled() {
update_option( 'jetpack_premium_analytics_enabled', 1 );

$reached = false;
$spy = function ( $show ) use ( &$reached ) {
$spy = function () use ( &$reached ) {
$reached = true;
return $show;
// Stop here: the rest of wp_dashboard_setup() is not what this test is about.
return false;
};
add_filter( 'jetpack_stats_dashboard_widget_show_to_user', $spy );

Expand All @@ -203,7 +206,7 @@ public function test_dashboard_widget_is_skipped_when_enabled() {
remove_filter( 'jetpack_stats_dashboard_widget_show_to_user', $spy );
}

$this->assertFalse( $reached, 'wp_dashboard_setup() should return before filtering widget visibility.' );
$this->assertTrue( $reached, 'wp_dashboard_setup() should reach its own visibility check regardless of the flag.' );
}

/**
Expand All @@ -228,33 +231,19 @@ public function test_dashboard_widget_runs_when_disabled() {
}

/**
* On WordPress.com Simple the widget never asks about the flag.
* The legacy Stats admin menu stays registered alongside Stats v2 instead of
* being suppressed, on any site.
*
* The jetpack-mu-wpcom package requires this file on its own there, into an environment
* where `Jetpack` is wpcom's class rather than the plugin's — asking would be
* a call to an undefined method, and it takes wp-admin down with it. Nothing
* in this suite can stand in for that class, so what is pinned here is the
* guard: on Simple the widget carries on regardless of the flag.
* Calling stats_load() also registers other hooks (pre_option_db_version,
* the stats_array filter, the upgrade-nudges action) that this test
* doesn't assert on and tear_down() doesn't remove — intentional, since
* this test only cares about the admin menu.
*/
public function test_dashboard_widget_ignores_the_flag_on_wpcom_simple() {
public function test_stats_admin_menu_kept_when_enabled() {
update_option( 'jetpack_premium_analytics_enabled', 1 );
Constants::set_constant( 'IS_WPCOM', true );

$reached = false;
$spy = function () use ( &$reached ) {
$reached = true;
// Stop here: the rest of wp_dashboard_setup() is not what this test is about.
return false;
};
add_filter( 'jetpack_stats_dashboard_widget_show_to_user', $spy );

try {
Jetpack_Stats_Dashboard_Widget::wp_dashboard_setup();
} finally {
remove_filter( 'jetpack_stats_dashboard_widget_show_to_user', $spy );
Constants::clear_single_constant( 'IS_WPCOM' );
}
stats_load();

$this->assertTrue( $reached, 'wp_dashboard_setup() should not consult the flag on Simple sites.' );
$this->assertNotFalse( has_action( 'jetpack_admin_menu', 'stats_admin_menu' ) );
}
}
Loading