Skip to content

Commit 4c47c40

Browse files
committed
Stats admin: consolidate the dashboard registration decision in Main
Main::maybe_register_dashboard() is now the single place deciding when the Odyssey dashboard owns the Stats screen: always for unconnected sites (so Odyssey's pricing grid is reachable before connecting), and for connected sites when the stats module is active, Odyssey is enabled, and the request isn't a noheader chart image. The stats module just falls back to the old interface when it declines.
1 parent e1d2be3 commit 4c47c40

6 files changed

Lines changed: 108 additions & 281 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Significance: minor
22
Type: added
33

4-
Register the Stats dashboard before the site connects, so eligible new installations without a Stats plan can reach the pricing grid rendered by Odyssey.
4+
Register the Stats dashboard before the site connects, so new installations can reach the Stats pricing grid rendered by Odyssey.

projects/packages/stats-admin/src/class-main.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Automattic\Jetpack\Stats_Admin;
99

1010
use Automattic\Jetpack\Connection\Manager;
11+
use Automattic\Jetpack\Modules;
1112
use Automattic\Jetpack\Stats\Options as Stats_Options;
1213
use Automattic\Jetpack\Tracking;
1314

@@ -52,16 +53,9 @@ public static function init() {
5253
*/
5354
private function __construct() {
5455
add_action( 'rest_api_init', array( REST_Controller::class, 'register' ) );
55-
add_action( 'jetpack_site_registered', array( Pricing_Grid\Eligibility::class, 'record_connection_time' ) );
56-
57-
// The stats module normally registers the Stats menu, but modules only
58-
// load once the site is connected. Register the dashboard here for
59-
// unconnected sites only, so eligible new sites see the pricing grid
60-
// before connecting. Connected sites go exclusively through the stats
61-
// module (modules/stats.php), which also owns the noheader chart-image
62-
// paths used by the adminbar and the dashboard widget.
63-
if ( is_admin() && ! ( new Manager() )->is_connected() && Pricing_Grid\Eligibility::is_eligible_site() ) {
64-
Dashboard::init();
56+
57+
if ( is_admin() ) {
58+
self::maybe_register_dashboard();
6559
}
6660

6761
// Disable JITM assets on the Stats page.
@@ -82,6 +76,43 @@ function ( $show, $screen_id ) {
8276
add_filter( 'jetpack_stats_transient_cleanup_prefixes', array( $this, 'register_transient_cleanup_prefix' ) );
8377
}
8478

79+
/**
80+
* Register the Odyssey dashboard as the Stats menu when it should own the
81+
* Stats screen, and report whether it did.
82+
*
83+
* Called from two entry points covering disjoint cases: this package's
84+
* constructor for sites that haven't connected yet (the stats module,
85+
* which normally registers the menu, only loads once connected), and the
86+
* stats module's stats_admin_menu() for connected sites, which falls back
87+
* to the old Stats interface when this declines.
88+
*
89+
* @return bool Whether the dashboard was registered.
90+
*/
91+
public static function maybe_register_dashboard() {
92+
// Unconnected sites can't load the stats module; register the
93+
// dashboard so Odyssey — which hosts the Stats pricing grid and owns
94+
// its eligibility — is reachable before connecting.
95+
if ( ! ( new Manager() )->is_connected() ) {
96+
Dashboard::init();
97+
return true;
98+
}
99+
100+
// On connected sites the stats module owns the screen: respect the
101+
// Odyssey opt-out, and leave noheader chart-image requests (the
102+
// adminbar sparkline and dashboard widget) to the legacy interface.
103+
if (
104+
! ( new Modules() )->is_active( 'stats' )
105+
|| ! Stats_Options::get_option( 'enable_odyssey_stats' )
106+
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
107+
|| isset( $_GET['noheader'] )
108+
) {
109+
return false;
110+
}
111+
112+
Dashboard::init();
113+
return true;
114+
}
115+
85116
/**
86117
* Register the stats-admin transient prefix for cleanup.
87118
*

projects/packages/stats-admin/src/pricing-grid/class-eligibility.php

Lines changed: 0 additions & 124 deletions
This file was deleted.

projects/packages/stats-admin/tests/php/Eligibility_Test.php

Lines changed: 0 additions & 132 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Unit tests for the Main class.
4+
*
5+
* @package automattic/jetpack-stats-admin
6+
*/
7+
8+
namespace Automattic\Jetpack\Stats_Admin;
9+
10+
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11+
12+
/**
13+
* Unit tests for the Main class.
14+
*/
15+
class Main_Test extends TestCase {
16+
/**
17+
* Setting up the test.
18+
*/
19+
public function setUp(): void {
20+
parent::setUp();
21+
( new Connection_Manager() )->reset_connection_status();
22+
}
23+
24+
/**
25+
* Returning the environment into its initial state.
26+
*/
27+
public function tearDown(): void {
28+
parent::tearDown();
29+
( new Connection_Manager() )->reset_connection_status();
30+
}
31+
32+
/**
33+
* An unconnected site registers the dashboard, so Odyssey (and its
34+
* pricing grid) is reachable before connecting.
35+
*/
36+
public function test_maybe_register_dashboard_for_unconnected_site() {
37+
remove_filter( 'jetpack_options', array( $this, 'mock_jetpack_site_connection_options' ) );
38+
( new Connection_Manager() )->reset_connection_status();
39+
40+
$this->assertTrue( Main::maybe_register_dashboard() );
41+
}
42+
43+
/**
44+
* A connected site without the stats module active declines, leaving the
45+
* screen to the module's own registration.
46+
*/
47+
public function test_maybe_register_dashboard_declines_for_connected_site_without_module() {
48+
$this->assertFalse( Main::maybe_register_dashboard() );
49+
}
50+
51+
/**
52+
* A noheader chart-image request declines, so the legacy interface can
53+
* serve the raw image.
54+
*/
55+
public function test_maybe_register_dashboard_declines_for_noheader_request() {
56+
$_GET['noheader'] = '';
57+
58+
$this->assertFalse( Main::maybe_register_dashboard() );
59+
60+
unset( $_GET['noheader'] );
61+
}
62+
}

0 commit comments

Comments
 (0)