diff --git a/tests/integration/tests/test-networkoperations.php b/tests/integration/tests/test-networkoperations.php index f61bd36..de8af1b 100644 --- a/tests/integration/tests/test-networkoperations.php +++ b/tests/integration/tests/test-networkoperations.php @@ -89,4 +89,37 @@ public function test_update_network_clears_cache() { $this->assertEquals( 'newdomain.com', $updated_network->domain, 'Network should have updated domain without manual cache flush' ); $this->assertEquals( '/newpath/', $updated_network->path, 'Network should have updated path without manual cache flush' ); } + + public function test_plugin_auto_activates_on_new_network() { + // Create a test user and grant super admin privileges. + $user_id = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + grant_super_admin( $user_id ); + + // Create a test network using add_network(). + $network_id = add_network( + array( + 'domain' => 'auto-activate-test.com', + 'path' => '/', + 'site_name' => 'Auto Activate Test', + 'network_name' => 'Test Network', + 'user_id' => $user_id, + 'network_admin_id' => $user_id, + ) + ); + + // Verify network was created successfully. + $this->assertNotWPError( $network_id, 'Network should be created successfully' ); + $this->assertIsInt( $network_id, 'Network ID should be an integer' ); + + // Get the active sitewide plugins for the new network. + $active_plugins = get_network_option( $network_id, 'active_sitewide_plugins', array() ); + + // Verify the plugin is auto-activated. + $this->assertIsArray( $active_plugins, 'Active sitewide plugins should be an array' ); + $this->assertArrayHasKey( 'wp-multi-network/wpmn-loader.php', $active_plugins, 'Plugin should be auto-activated on new network' ); + } } diff --git a/wp-multi-network/includes/functions.php b/wp-multi-network/includes/functions.php index a8afb59..d567aad 100644 --- a/wp-multi-network/includes/functions.php +++ b/wp-multi-network/includes/functions.php @@ -677,6 +677,13 @@ function add_network( $args = array() ) { // Clean the network cache. clean_network_cache( $new_network_id ); + // Self-activate on new network. + $existing_plugins = get_network_option( $new_network_id, 'active_sitewide_plugins', array() ); + if ( ! isset( $existing_plugins['wp-multi-network/wpmn-loader.php'] ) ) { + $existing_plugins['wp-multi-network/wpmn-loader.php'] = time(); + update_network_option( $new_network_id, 'active_sitewide_plugins', $existing_plugins ); + } + /** * Fires after a new network has been added. *