Skip to content

fix: move network-activate handler to external JS file#871

Merged
superdav42 merged 2 commits intomainfrom
bugfix/836-network-activate-button
Apr 15, 2026
Merged

fix: move network-activate handler to external JS file#871
superdav42 merged 2 commits intomainfrom
bugfix/836-network-activate-button

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

@superdav42 superdav42 commented Apr 15, 2026

Summary

  • Fixes the Network Activate button on the Setup Wizard requirements page being non-functional — the inline <script> tag was rendered as visible text instead of executing
  • Extracts the jQuery click handler into assets/js/network-activate.js, enqueued via wp_enqueue_script with wp_localize_script for the translated error string
  • Removes the 55-line inline <script> block from views/wizards/setup/requirements_table.php

Root Cause

The view is loaded via wu_get_template_contents() (output buffering with ob_start/ob_get_clean). The captured string is returned as the desc value for a wizard section. When the wizard framework renders the section, the HTML is inserted as innerHTML — browsers do not execute <script> tags injected this way.

Files Changed

  • assets/js/network-activate.js (new) — standalone click handler using wu_network_activate.error_message from localized data
  • inc/admin-pages/class-setup-wizard-admin-page.php — enqueues the new script + localizes error string in register_scripts()
  • views/wizards/setup/requirements_table.php — removed inline <script> block

Testing

  • Existing Setup_Wizard_Admin_Page_Test suite passes (37 tests)
  • Manual: visit the Setup Wizard when the plugin is not network-activated, click "Network Activate" — the button should fire the AJAX request and reload on success

Resolves #836

Summary by CodeRabbit

  • Refactor
    • Improved code organization of network activation functionality for better maintainability.

The inline <script> in requirements_table.php was rendered as visible
text because wu_get_template_contents() captures output via ob_start
and injects it as innerHTML — browsers do not execute script tags
inserted this way.

Extract the click handler to assets/js/network-activate.js, enqueue it
via wp_enqueue_script in register_scripts(), and pass the translated
error string through wp_localize_script.

Resolves #836
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 15, 2026

📝 Walkthrough

Walkthrough

The changes extract an inline network activation handler from the setup wizard template into a separate, reusable JavaScript file (network-activate.js) and register it through the admin page class, improving code organization and maintainability.

Changes

Cohort / File(s) Summary
Network Activation JavaScript
assets/js/network-activate.js, inc/admin-pages/class-setup-wizard-admin-page.php
Created a new standalone JavaScript module to handle .wu-network-activate-btn clicks with AJAX network activation logic; registered and localized the script with error messaging in the setup wizard admin page class.
Setup Wizard Template
views/wizards/setup/requirements_table.php
Removed inline jQuery click handler for network activation button, migrating its logic to the separate JavaScript file.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

bug

Poem

🐰 A button clicked, a script refactored clean,
From template inline to modules pristine,
Network activation flows with grace,
Code organized in its proper place! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the primary change: extracting an inline click handler from a view template into an external JavaScript file for proper execution.
Linked Issues check ✅ Passed The code changes successfully implement the core requirement from issue #836: replacing the read-more link with a network-activate button that attempts to activate the plugin via AJAX, with fallback UI when activation fails.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing the network-activate button functionality: extracting handler logic, enqueuing scripts, and localizing strings. No extraneous modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/836-network-activate-button

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@assets/js/network-activate.js`:
- Around line 46-50: The code deriving errorMsg currently only checks
response.data.message; update the logic in network-activate.js (the block that
sets var errorMsg = wu_network_activate.error_message) to handle WP_Error
serialized payloads by detecting when response.data is an array
(Array.isArray(response.data)) and, if so, extracting the message from the first
element (e.g., response.data[0].message) before falling back to
response.data.message and then to the original
wu_network_activate.error_message; ensure this also guards for missing
elements/fields to avoid runtime errors.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 21f6edf3-1442-4966-887c-5d43f3f8e20a

📥 Commits

Reviewing files that changed from the base of the PR and between f362296 and a707abf.

📒 Files selected for processing (3)
  • assets/js/network-activate.js
  • inc/admin-pages/class-setup-wizard-admin-page.php
  • views/wizards/setup/requirements_table.php
💤 Files with no reviewable changes (1)
  • views/wizards/setup/requirements_table.php

Comment on lines +46 to +50
var errorMsg = wu_network_activate.error_message;

if (response.data && response.data.message) {
errorMsg = response.data.message;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

In WordPress core, what JSON structure is returned by wp_send_json_error( new WP_Error('code', 'message') )?

💡 Result:

The JSON structure returned by wp_send_json_error( new WP_Error('code', 'message') ) in WordPress core is: json { "success": false, "data": [ { "code": "code", "message": "message" } ] } This is generated by processing the WP_Error object's errors property, which for a single error is an associative array with the code as key and array of messages as value. The function iterates over each code and message, creating an array of objects under data, with success always set to false. The response is then passed to wp_send_json for output and termination.

Citations:


🏁 Script executed:

# Find and read the network-activate.js file
fd -t f network-activate.js

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 110


🏁 Script executed:

# Read the relevant section of the file to confirm the code snippet
if [ -f "assets/js/network-activate.js" ]; then
  wc -l assets/js/network-activate.js
  sed -n '40,60p' assets/js/network-activate.js
fi

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 536


🏁 Script executed:

# Check the AJAX handler file mentioned in the review
fd -t f class-setup-wizard-admin-page.php

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 130


🏁 Script executed:

# If found, check lines 160-185 for WP_Error usage
if [ -f "inc/admin-pages/class-setup-wizard-admin-page.php" ]; then
  sed -n '160,185p' inc/admin-pages/class-setup-wizard-admin-page.php
fi

Repository: Ultimate-Multisite/ultimate-multisite

Length of output: 659


Handle WP_Error array payloads when deriving the error text.

At lines 48–49, only response.data.message is checked. The AJAX handler returns WP_Error instances (lines 167 and 179 in inc/admin-pages/class-setup-wizard-admin-page.php), and WordPress serializes these as { "success": false, "data": [ { "code": "...", "message": "..." } ] }. The current code will not extract the backend message from this array format.

🔧 Suggested patch
-				var errorMsg = wu_network_activate.error_message;
-
-				if (response.data && response.data.message) {
-					errorMsg = response.data.message;
-				}
+				var errorMsg = wu_network_activate.error_message;
+
+				if (response && response.data) {
+					if (response.data.message) {
+						errorMsg = response.data.message;
+					} else if (Array.isArray(response.data) && response.data[0] && response.data[0].message) {
+						errorMsg = response.data[0].message;
+					}
+				}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@assets/js/network-activate.js` around lines 46 - 50, The code deriving
errorMsg currently only checks response.data.message; update the logic in
network-activate.js (the block that sets var errorMsg =
wu_network_activate.error_message) to handle WP_Error serialized payloads by
detecting when response.data is an array (Array.isArray(response.data)) and, if
so, extracting the message from the first element (e.g.,
response.data[0].message) before falling back to response.data.message and then
to the original wu_network_activate.error_message; ensure this also guards for
missing elements/fields to avoid runtime errors.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 15, 2026

Performance Test Results

Performance test results for 1e036f7 are in 🛎️!

Note: the numbers in parentheses show the difference to the previous (baseline) test run. Differences below 2% or 0.5 in absolute values are not shown.

URL: /

Run DB Queries Memory Before Template Template WP Total LCP TTFB LCP - TTFB
0 40 37.75 MB 833.50 ms (-64.50 ms / -8% ) 166.50 ms 1008.00 ms (-70.00 ms / -7% ) 1908.00 ms (-138.00 ms / -7% ) 1802.10 ms (-149.00 ms / -8% ) 93.55 ms (+4.95 ms / +5% )
1 56 49.03 MB 916.00 ms (-36.00 ms / -4% ) 148.00 ms 1067.00 ms (-33.50 ms / -3% ) 2062.00 ms 1983.05 ms 80.80 ms

@github-actions
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@superdav42 superdav42 merged commit 9f04bc2 into main Apr 15, 2026
11 checks passed
@superdav42 superdav42 deleted the bugfix/836-network-activate-button branch April 15, 2026 19:08
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.

If ultimate multisite is not network active in setup wizard it should try to activate it

1 participant