Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 9, 2025

Plan to auto-activate plugin on new networks via WP-CLI

  • Understand the issue: Plugin auto-activates on new networks when created via UI but not via WP-CLI
  • Locate the auto-activation code in the admin class (lines 1039-1045 in class-wp-ms-networks-admin.php)
  • Move the auto-activation logic from the admin handler to the core add_network() function in functions.php
  • Add test to verify plugin activation on new networks
  • Fix test failure: Create user with super admin privileges
  • Run code review and security checks
  • Finalize the implementation

Summary

Successfully implemented auto-activation of the WP Multi Network plugin when creating networks via WP-CLI, achieving parity with the UI behavior.

Changes Made

  1. Modified add_network() function in wp-multi-network/includes/functions.php:

    • Added plugin auto-activation code that runs after network creation
    • Placed strategically after network setup and before the action hook
    • Uses the same mechanism as the UI handler for consistency
  2. Added comprehensive test in tests/integration/tests/test-networkoperations.php:

    • Test verifies plugin is automatically activated on new networks
    • Fixed: Now creates a test user and grants super admin privileges before creating network
    • Passes user_id and network_admin_id to add_network() function
    • Checks that the plugin key exists in the active_sitewide_plugins option

Test Fix

The test was failing because add_network() requires a valid user with super admin privileges. The fix:

  • Creates a test user with administrator role
  • Grants super admin privileges using grant_super_admin()
  • Passes both user_id and network_admin_id to the add_network() call

This matches the pattern used in other tests like test-rest-networks-controller.php.

Original prompt

This section details on the original issue you should resolve

<issue_title>This plugin is not auto-activated on new networks when created via WP-CLI</issue_title>
<issue_description>Here's another UI/CLI parity conundrum...

When creating a new network via the UI, this plugin auto-activates itself on the new network. When doing so via WP-CLI, it does not. I realise it's possible to do this with another WP-CLI command after creating the new network, but it means parsing the new network ID out of the CLI return, then issuing the command.

I wonder if it would be sensible also to auto-activate by default in WP-CLI? I support this idea (for parity with the UI and therefore with people's expectations) but you may think otherwise.</issue_description>

<agent_instructions>This is a good start: christianwach@b501a92</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix plugin auto-activation for new networks in WP-CLI Auto-activate plugin on networks created via WP-CLI Dec 9, 2025
Copilot AI requested a review from JJJ December 9, 2025 01:29
@JJJ
Copy link
Collaborator

JJJ commented Dec 9, 2025

@copilot Same PHPUnit failure as in previous PRs:

1) WPMN_Tests_NetworkOperations::test_plugin_auto_activates_on_new_network
Network should be created successfully User does not exist.
Failed asserting that WP_Error Object (...) is not an instance of class "WP_Error".

/tmp/wordpress-tests-lib/includes/abstract-testcase.php:869
/home/runner/work/wp-multi-network/wp-multi-network/tests/integration/tests/test-networkoperations.php:105

FAILURES!
Tests: 33, Assertions: 142, Failures: 1.

Make sure that the user exists and was granted super admin privileges. Thanks!

Copy link
Contributor Author

Copilot AI commented Dec 9, 2025

@copilot Same PHPUnit failure as in previous PRs:

...

Fixed in 7d7b51a. The test now creates a user with administrator role and grants super admin privileges before creating the network, then passes the user_id to add_network().

@JJJ JJJ marked this pull request as ready for review December 9, 2025 01:52
Copilot AI review requested due to automatic review settings December 9, 2025 01:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements auto-activation of the WP Multi Network plugin when creating networks via WP-CLI, achieving feature parity with the UI behavior. The implementation moves the auto-activation logic from the UI handler into the core add_network() function, which is called by both the UI and WP-CLI.

Key Changes

  • Auto-activation logic added to add_network() function to ensure plugin activates on new networks regardless of creation method
  • Comprehensive test added to verify plugin auto-activates when networks are created programmatically
  • Test properly handles user creation and super admin privileges required by network operations

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
wp-multi-network/includes/functions.php Added auto-activation code that sets active_sitewide_plugins after network creation, ensuring plugin is active on newly created networks
tests/integration/tests/test-networkoperations.php Added test that creates a network with proper user privileges and verifies the plugin is auto-activated in the network options

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JJJ JJJ merged commit 3ca65be into master Dec 9, 2025
20 checks passed
@JJJ JJJ deleted the copilot/auto-activate-plugin-wp-cli branch December 9, 2025 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

This plugin is not auto-activated on new networks when created via WP-CLI

2 participants