Skip to content
Closed
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: fixed

Upgrade menu: do not display when the site is not connected or in offline mode
19 changes: 18 additions & 1 deletion projects/packages/admin-ui/src/class-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ public static function get_top_level_menu_item_url( $fallback = false ) {
* Checks whether the current site should show the upgrade menu item.
*
* The upgrade menu is only shown to administrators on free-plan sites
* that are not hosted on WordPress.com.
* that are not hosted on WordPress.com, are connected to WordPress.com,
* and are not in offline (development) mode.
*
* @return bool True if the upgrade menu should be shown.
*/
Expand All @@ -272,6 +273,22 @@ private static function should_show_upgrade_menu() {
}
}

// Don't show upsells when the site is in offline (development) mode.
if ( class_exists( '\Automattic\Jetpack\Status' ) ) {
$status = new \Automattic\Jetpack\Status();
if ( $status->is_offline_mode() ) {
return false;
}
}

// Don't show upsells when the site is not connected to WordPress.com.
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
$connection = new \Automattic\Jetpack\Connection\Manager();
if ( ! $connection->is_connected() ) {
return false;
}
}

// Only show to free-plan sites.
return self::is_free_plan();
}
Expand Down
91 changes: 91 additions & 0 deletions projects/packages/admin-ui/tests/php/Admin_Menu_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public function setUp(): void {
wp_dequeue_script( 'jetpack-admin-ui-upgrade-menu' );
wp_deregister_script( 'jetpack-admin-ui-upgrade-menu' );

// Default to a connected, non-offline site so existing tests cover only the plan checks.
self::set_connection_status( true );
remove_all_filters( 'jetpack_offline_mode' );
if ( class_exists( '\Automattic\Jetpack\Status\Cache' ) ) {
\Automattic\Jetpack\Status\Cache::clear();
}

$reflection = new \ReflectionClass( Admin_Menu::class );

if ( $reflection->hasProperty( 'menu_items' ) ) {
Expand Down Expand Up @@ -365,6 +372,36 @@ public function test_upgrade_menu_item_hidden_for_non_admin() {
$this->assertUpgradeMenuItemAbsent();
}

/**
* Upgrade item is absent when the site is in offline (development) mode.
*
* @return void
*/
public function test_upgrade_menu_item_hidden_when_offline_mode() {
wp_set_current_user( self::$admin_user_id );
add_filter( 'jetpack_offline_mode', '__return_true' );

Admin_Menu::init();
do_action( 'admin_menu' );

$this->assertUpgradeMenuItemAbsent();
}

/**
* Upgrade item is absent when the site is not connected to WordPress.com.
*
* @return void
*/
public function test_upgrade_menu_item_hidden_when_not_connected() {
wp_set_current_user( self::$admin_user_id );
self::set_connection_status( false );

Admin_Menu::init();
do_action( 'admin_menu' );

$this->assertUpgradeMenuItemAbsent();
}

/**
* Upgrade menu stylesheet is enqueued for a free-plan site.
*
Expand Down Expand Up @@ -441,6 +478,60 @@ public function test_upgrade_menu_item_styles_not_enqueued_when_site_has_product
$this->assertFalse( wp_style_is( 'jetpack-admin-ui-upgrade-menu', 'enqueued' ) );
}

/**
* No stylesheet enqueue when the site is in offline (development) mode.
*
* @return void
*/
public function test_upgrade_menu_item_styles_not_enqueued_when_offline_mode() {
wp_set_current_user( self::$admin_user_id );
add_filter( 'jetpack_offline_mode', '__return_true' );

Admin_Menu::add_upgrade_menu_item_styles();

$this->assertFalse( wp_style_is( 'jetpack-admin-ui-upgrade-menu', 'enqueued' ) );
}

/**
* No stylesheet enqueue when the site is not connected to WordPress.com.
*
* @return void
*/
public function test_upgrade_menu_item_styles_not_enqueued_when_not_connected() {
wp_set_current_user( self::$admin_user_id );
self::set_connection_status( false );

Admin_Menu::add_upgrade_menu_item_styles();

$this->assertFalse( wp_style_is( 'jetpack-admin-ui-upgrade-menu', 'enqueued' ) );
}

/**
* Overrides the Connection Manager's memoized `is_connected` status for the duration of a test.
*
* Avoids having to stand up real blog tokens just to flip the connection bit.
*
* @param bool $connected Whether to report the site as connected.
* @return void
*/
private static function set_connection_status( $connected ) {
if ( ! class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
return;
}

$reflection = new \ReflectionClass( \Automattic\Jetpack\Connection\Manager::class );
if ( ! $reflection->hasProperty( 'is_connected' ) ) {
return;
}

$property = $reflection->getProperty( 'is_connected' );
// @todo Remove this call once we no longer need to support PHP <8.1.
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}
$property->setValue( null, (bool) $connected );
}

/**
* Asserts the upgrade submenu item is present under the jetpack top-level menu.
*
Expand Down
Loading