Skip to content

Commit 6e35adb

Browse files
committed
Add user connection check
1 parent 5d908bc commit 6e35adb

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

projects/packages/admin-ui/src/class-admin-menu.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ private static function should_show_upgrade_menu() {
279279
}
280280
}
281281

282-
// Only show after the site is connected.
283-
if ( ! self::is_site_connected() ) {
282+
// Only show after the site and current user are connected.
283+
if ( ! self::is_site_and_user_connected() ) {
284284
return false;
285285
}
286286

@@ -289,25 +289,30 @@ private static function should_show_upgrade_menu() {
289289
}
290290

291291
/**
292-
* Checks whether the site is connected to WordPress.com.
292+
* Checks whether the site and current user are connected to WordPress.com.
293293
*
294-
* @return bool True if the site has a connected blog ID.
294+
* @return bool True if site and current user are connected.
295295
*/
296-
private static function is_site_connected() {
296+
private static function is_site_and_user_connected() {
297297
$connection_manager = self::$connection_manager;
298298
if ( ! $connection_manager && class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
299299
$connection_manager = new \Automattic\Jetpack\Connection\Manager();
300300
}
301301

302-
if ( $connection_manager && is_callable( array( $connection_manager, 'is_connected' ) ) ) {
303-
return (bool) $connection_manager->is_connected();
302+
if (
303+
$connection_manager
304+
&& is_callable( array( $connection_manager, 'is_connected' ) )
305+
&& is_callable( array( $connection_manager, 'is_user_connected' ) )
306+
) {
307+
return (bool) $connection_manager->is_connected()
308+
&& (bool) $connection_manager->is_user_connected( get_current_user_id() );
304309
}
305310

306311
return false;
307312
}
308313

309314
/**
310-
* Sets the connection manager dependency.
315+
* Sets the connection manager dependency; used by tests.
311316
*
312317
* @param object|null $connection_manager Connection manager object.
313318
* @return void

projects/packages/admin-ui/tests/php/Admin_Menu_Test.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function setUp(): void {
9696
->disableOriginalConstructor()
9797
->getMock();
9898
$connection->method( 'is_connected' )->willReturn( true );
99+
$connection->method( 'is_user_connected' )->willReturn( true );
99100
Admin_Menu::set_connection_manager( $connection );
100101
wp_dequeue_style( 'jetpack-admin-ui-upgrade-menu' );
101102
wp_deregister_style( 'jetpack-admin-ui-upgrade-menu' );
@@ -219,6 +220,10 @@ public function test_upgrade_menu_item_shown_for_free_plan_admin() {
219220
$connection->expects( $this->once() )
220221
->method( 'is_connected' )
221222
->willReturn( true );
223+
$connection->expects( $this->once() )
224+
->method( 'is_user_connected' )
225+
->with( self::$admin_user_id )
226+
->willReturn( true );
222227
Admin_Menu::set_connection_manager( $connection );
223228

224229
Admin_Menu::init();

0 commit comments

Comments
 (0)