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

Agents Manager: Make is_enabled() a public static method so consumers can check enablement without duplicating filter logic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is worth adding some tests in Agents_Manager_Test? Seems like new public methods we could easily cover with unit tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it!

Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,15 @@ function ( $wp_admin_bar ) use ( $use_disconnected ) {
private function get_variant() {
// CIAB/Next Admin: only load when disconnected (connected CIAB is handled by Help Center).
if ( $this->is_ciab_environment() ) {
if ( $this->is_enabled() && $this->is_jetpack_disconnected() ) {
if ( self::is_enabled() && $this->is_jetpack_disconnected() ) {
return 'ciab-disconnected';
}
return null;
}

// Frontend: load disconnected variant for eligible logged-in editors.
if ( ! is_admin() ) {
if ( $this->is_loading_on_frontend() && $this->is_enabled() ) {
if ( $this->is_loading_on_frontend() && self::is_enabled() ) {
return 'wp-admin-disconnected';
}
return null;
Expand All @@ -292,7 +292,7 @@ private function get_variant() {
return null;
}

if ( ! $this->is_enabled() ) {
if ( ! self::is_enabled() ) {
return null;
}

Expand All @@ -310,14 +310,14 @@ private function get_variant() {
*
* @return bool
*/
private function is_enabled() {
public static function is_enabled() {
// Full unified experience: Agents Manager with support guides, Help Center takeover, etc.
if ( apply_filters( 'agents_manager_use_unified_experience', false ) ) {
return true;
}

// Block editor only: Agents Manager replaces Big Sky's native UI. Hooked by Big Sky.
if ( $this->is_block_editor() && apply_filters( 'agents_manager_enabled_in_block_editor', false ) ) {
if ( self::is_block_editor() && apply_filters( 'agents_manager_enabled_in_block_editor', false ) ) {
return true;
}

Expand Down Expand Up @@ -708,7 +708,7 @@ private function is_loading_on_frontend() {
*
* @return bool True if the current screen is the block editor.
*/
private function is_block_editor() {
private static function is_block_editor() {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,143 @@ public function test_should_enqueue_script_returns_false_in_ciab_when_connected(
$this->assertFalse( $result );
}

/**
* Tests that is_enabled returns false by default when no filters are active.
*/
public function test_is_enabled_returns_false_by_default() {
// Ensure no block editor context.
$this->assertFalse( is_admin() );

$result = Agents_Manager::is_enabled();

$this->assertFalse( $result );
}

/**
* Tests that is_enabled returns true when the unified experience filter returns true.
*/
public function test_is_enabled_returns_true_when_unified_experience_enabled() {
add_filter( 'agents_manager_use_unified_experience', '__return_true', 20 );

$result = Agents_Manager::is_enabled();

remove_filter( 'agents_manager_use_unified_experience', '__return_true', 20 );

$this->assertTrue( $result );
}

/**
* Tests that is_enabled returns true when in block editor and agents_manager_enabled_in_block_editor filter is true.
*/
public function test_is_enabled_returns_true_in_block_editor_when_block_editor_filter_enabled() {
require_once ABSPATH . 'wp-admin/includes/screen.php';

// Set up block editor context.
set_current_screen( 'post' );
$screen = get_current_screen();

$reflection = new \ReflectionClass( $screen );
$property = $reflection->getProperty( 'is_block_editor' );
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}
$property->setValue( $screen, true );

add_filter( 'agents_manager_enabled_in_block_editor', '__return_true' );

$result = Agents_Manager::is_enabled();

remove_filter( 'agents_manager_enabled_in_block_editor', '__return_true' );

$this->assertTrue( $result );
}

/**
* Tests that is_enabled returns false when in block editor but block editor filter is not enabled.
*/
public function test_is_enabled_returns_false_in_block_editor_when_block_editor_filter_disabled() {
require_once ABSPATH . 'wp-admin/includes/screen.php';

// Set up block editor context.
set_current_screen( 'post' );
$screen = get_current_screen();

$reflection = new \ReflectionClass( $screen );
$property = $reflection->getProperty( 'is_block_editor' );
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}
$property->setValue( $screen, true );

// Do not add agents_manager_enabled_in_block_editor filter — default is false.
$result = Agents_Manager::is_enabled();

$this->assertFalse( $result );
}

/**
* Tests that is_enabled returns false when not in block editor even if block editor filter is true.
*/
public function test_is_enabled_returns_false_when_not_in_block_editor_even_if_block_editor_filter_enabled() {
// Set to a non-block-editor admin screen.
$this->set_admin_context();

add_filter( 'agents_manager_enabled_in_block_editor', '__return_true' );

$result = Agents_Manager::is_enabled();

remove_filter( 'agents_manager_enabled_in_block_editor', '__return_true' );

$this->assertFalse( $result );
}

/**
* Tests that is_enabled returns false for widgets screen even if block editor filter is true.
*
* The widgets screen has the block editor flag but is excluded from is_block_editor().
*/
public function test_is_enabled_returns_false_for_widgets_screen_with_block_editor_filter() {
require_once ABSPATH . 'wp-admin/includes/screen.php';

// Set up widgets screen with block editor flag.
set_current_screen( 'widgets' );
$screen = get_current_screen();

$reflection = new \ReflectionClass( $screen );
$property = $reflection->getProperty( 'is_block_editor' );
if ( PHP_VERSION_ID < 80100 ) {
$property->setAccessible( true );
}
$property->setValue( $screen, true );

add_filter( 'agents_manager_enabled_in_block_editor', '__return_true' );

$result = Agents_Manager::is_enabled();

remove_filter( 'agents_manager_enabled_in_block_editor', '__return_true' );

$this->assertFalse( $result );
}

/**
* Tests that is_enabled prioritises the unified experience filter over the block editor filter.
*
* When the unified experience filter is true, is_enabled should return true
* regardless of block editor state.
*/
public function test_is_enabled_unified_experience_takes_priority_over_block_editor() {
// Not in block editor context.
$this->assertFalse( is_admin() );

add_filter( 'agents_manager_use_unified_experience', '__return_true', 20 );

$result = Agents_Manager::is_enabled();

remove_filter( 'agents_manager_use_unified_experience', '__return_true', 20 );

$this->assertTrue( $result );
}

/**
* Tests that should_enqueue_script returns false on WooCommerce Admin home page.
*
Expand Down