diff --git a/projects/packages/agents-manager/changelog/add-agents-manager-ai-chat-button b/projects/packages/agents-manager/changelog/add-agents-manager-ai-chat-button new file mode 100644 index 000000000000..275814a28ecc --- /dev/null +++ b/projects/packages/agents-manager/changelog/add-agents-manager-ai-chat-button @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a standalone AI chat button to the admin bar. diff --git a/projects/packages/agents-manager/src/class-agents-manager.php b/projects/packages/agents-manager/src/class-agents-manager.php index 1ec923542725..505f574764d2 100644 --- a/projects/packages/agents-manager/src/class-agents-manager.php +++ b/projects/packages/agents-manager/src/class-agents-manager.php @@ -161,6 +161,23 @@ public function add_menu_panel( $wp_admin_bar ) { ); } + /** + * Add the standalone AI chat button to the admin bar. + * + * @param \WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. + */ + public function add_ai_chat_button( $wp_admin_bar ) { + $wp_admin_bar->add_menu( + array( + 'id' => 'agents-manager-ai-chat', + 'parent' => 'top-secondary', + 'title' => ' + + ', + ) + ); + } + /** * Enqueue Agents Manager scripts and add inline script data. */ @@ -235,6 +252,11 @@ function ( $wp_admin_bar ) use ( $use_disconnected ) { if ( ! $use_disconnected ) { add_action( 'admin_bar_menu', array( $this, 'add_menu_panel' ), 100 ); } + + // Standalone AI chat button, shown only in the unified experience. + if ( ! $use_disconnected && apply_filters( 'agents_manager_use_unified_experience', false ) ) { + add_action( 'admin_bar_menu', array( $this, 'add_ai_chat_button' ), 100 ); + } } /** diff --git a/projects/packages/agents-manager/tests/php/Agents_Manager_Test.php b/projects/packages/agents-manager/tests/php/Agents_Manager_Test.php index 1580bb65314a..3d056df55d69 100644 --- a/projects/packages/agents-manager/tests/php/Agents_Manager_Test.php +++ b/projects/packages/agents-manager/tests/php/Agents_Manager_Test.php @@ -496,6 +496,52 @@ public function test_enqueue_scripts_includes_use_unified_experience_when_enable remove_filter( 'agents_manager_use_unified_experience', '__return_true', 20 ); } + /** + * Tests that the standalone AI chat button is added to the admin bar when the + * unified experience is enabled. + */ + public function test_ai_chat_button_registered_in_unified_experience() { + // Set admin context so the admin bar nodes are registered. + require_once ABSPATH . 'wp-admin/includes/screen.php'; + set_current_screen( 'dashboard' ); + + // Register the script so enqueue_scripts can attach its inline data. + wp_register_script( 'agents-manager', 'https://example.com/agents-manager.js', array(), '1.0', true ); + + // Enable the unified experience (priority 20 runs after the class's own filter). + add_filter( 'agents_manager_use_unified_experience', '__return_true', 20 ); + + $this->agents_manager->enqueue_scripts(); + + $this->assertNotFalse( + has_action( 'admin_bar_menu', array( $this->agents_manager, 'add_ai_chat_button' ) ) + ); + + remove_filter( 'agents_manager_use_unified_experience', '__return_true', 20 ); + } + + /** + * Tests that the standalone AI chat button is not added to the admin bar when + * the unified experience is disabled. + */ + public function test_ai_chat_button_not_registered_without_unified_experience() { + // Same admin context as the enabled case, so only the unified flag differs. + require_once ABSPATH . 'wp-admin/includes/screen.php'; + set_current_screen( 'dashboard' ); + wp_register_script( 'agents-manager', 'https://example.com/agents-manager.js', array(), '1.0', true ); + + // Disable the unified experience (priority 20 runs after the class's own filter). + add_filter( 'agents_manager_use_unified_experience', '__return_false', 20 ); + + $this->agents_manager->enqueue_scripts(); + + $this->assertFalse( + has_action( 'admin_bar_menu', array( $this->agents_manager, 'add_ai_chat_button' ) ) + ); + + remove_filter( 'agents_manager_use_unified_experience', '__return_false', 20 ); + } + /** * Tests that should_display_menu_panel returns false by default. */