Skip to content

Commit 3ca65be

Browse files
CopilotJJJCopilot
authored
Auto-activate plugin on networks created via WP-CLI (#234)
* Add plugin auto-activation in add_network() function for WP-CLI parity * Add test to verify plugin auto-activation on new networks * Fix test: Create user with super admin privileges for network creation * Update wp-multi-network/includes/functions.php --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: JJJ <[email protected]> Co-authored-by: John James Jacoby <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 9572192 commit 3ca65be

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

tests/integration/tests/test-networkoperations.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,37 @@ public function test_update_network_clears_cache() {
8989
$this->assertEquals( 'newdomain.com', $updated_network->domain, 'Network should have updated domain without manual cache flush' );
9090
$this->assertEquals( '/newpath/', $updated_network->path, 'Network should have updated path without manual cache flush' );
9191
}
92+
93+
public function test_plugin_auto_activates_on_new_network() {
94+
// Create a test user and grant super admin privileges.
95+
$user_id = $this->factory->user->create(
96+
array(
97+
'role' => 'administrator',
98+
)
99+
);
100+
grant_super_admin( $user_id );
101+
102+
// Create a test network using add_network().
103+
$network_id = add_network(
104+
array(
105+
'domain' => 'auto-activate-test.com',
106+
'path' => '/',
107+
'site_name' => 'Auto Activate Test',
108+
'network_name' => 'Test Network',
109+
'user_id' => $user_id,
110+
'network_admin_id' => $user_id,
111+
)
112+
);
113+
114+
// Verify network was created successfully.
115+
$this->assertNotWPError( $network_id, 'Network should be created successfully' );
116+
$this->assertIsInt( $network_id, 'Network ID should be an integer' );
117+
118+
// Get the active sitewide plugins for the new network.
119+
$active_plugins = get_network_option( $network_id, 'active_sitewide_plugins', array() );
120+
121+
// Verify the plugin is auto-activated.
122+
$this->assertIsArray( $active_plugins, 'Active sitewide plugins should be an array' );
123+
$this->assertArrayHasKey( 'wp-multi-network/wpmn-loader.php', $active_plugins, 'Plugin should be auto-activated on new network' );
124+
}
92125
}

wp-multi-network/includes/functions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,13 @@ function add_network( $args = array() ) {
677677
// Clean the network cache.
678678
clean_network_cache( $new_network_id );
679679

680+
// Self-activate on new network.
681+
$existing_plugins = get_network_option( $new_network_id, 'active_sitewide_plugins', array() );
682+
if ( ! isset( $existing_plugins['wp-multi-network/wpmn-loader.php'] ) ) {
683+
$existing_plugins['wp-multi-network/wpmn-loader.php'] = time();
684+
update_network_option( $new_network_id, 'active_sitewide_plugins', $existing_plugins );
685+
}
686+
680687
/**
681688
* Fires after a new network has been added.
682689
*

0 commit comments

Comments
 (0)