Skip to content

fix(site-duplication): balance switch_to_blog stack so welcome emails send#1167

Merged
superdav42 merged 1 commit intomainfrom
fix/site-duplication-blog-context-leak
May 9, 2026
Merged

fix(site-duplication): balance switch_to_blog stack so welcome emails send#1167
superdav42 merged 1 commit intomainfrom
fix/site-duplication-blog-context-leak

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

Summary

  • New-site / welcome emails were silently failing for customers who signed up against a site template, while a manual Send test email from the network admin worked fine.
  • Root cause is in MUCD_Files::copy_files(): it pushes two switch_to_blog() frames onto the WordPress blog stack but only pops one. When duplication finishes, the request is still on the template/source blog instead of the network context. wp_mail() then runs against the template's admin_email, blogname, and any per-site SMTP plugin instead of the network's, so the welcome email never reaches the customer.
  • Fix: add the missing restore_current_blog() so the stack is fully unwound. The bug has been there since the initial MUCD import, so no other call sites needed adjusting.

Why test emails work but checkout emails don't

Path Goes through duplication? Blog context when wp_mail() runs
Network admin → Email → Send test No Network / admin (correct)
Checkout, no template No (wpmu_create_blog) Network / admin (correct)
Checkout, with template Yes (Site_Duplicator::duplicate_sitecopy_files) Template site (wrong)

That asymmetry exactly matches what customers reported: emails send when an admin tests them, fail during template-based signup, and succeed for non-template signups.

Tests

New file tests/WP_Ultimo/Helpers/Site_Duplicator_Context_Test.php adds two regression tests that fail on main and pass after the fix:

  1. test_copy_files_does_not_leak_blog_context — drives MUCD_Files::copy_files() directly with the actual filesystem recursion stubbed out via the existing mucd_copy_dirs filter, and asserts that get_current_blog_id() and ms_is_switched() are both unchanged after the call.
  2. test_duplicate_site_restores_blog_context — higher-level guard that runs the full Site_Duplicator::duplicate_site() pipeline and asserts the caller's blog context is preserved end-to-end.

Verified locally:

$ vendor/bin/phpunit --filter 'Site_Duplicator_Context_Test' --no-coverage
OK (2 tests, 7 assertions)

PHPCS and PHPStan are clean on both modified files.

Risk

  • Low. The fix only adds the missing restore_current_blog(); it does not change copy semantics or the order in which uploads info is read.
  • No public API change. No DB schema change. No new feature flag.
  • The two switch_to_blog() calls already happened on every prior duplication, so the second restore_current_blog() matches what was already pushed onto the stack.

… send

MUCD_Files::copy_files() pushed two switch_to_blog() frames (source then
target) onto the WordPress blog stack but only popped one. After the
function returned the caller was still on the source/template blog
context instead of the original network/admin context.

Customers reported that new-site / welcome emails were silently dropped
during template-based signups while a manual "send test" from the
network admin worked. The leak puts wp_mail() into the template site's
context, so it picks up that site's options (admin_email, blogname) and
any per-site SMTP plugin set — almost always different from the network
configuration the test path uses.

Add a second restore_current_blog() to fully unwind the stack, and add a
regression test class that drives MUCD_Files::copy_files() directly and
asserts the caller's blog id and the global _wp_switched_stack are both
unchanged after the call. The full duplicate_site() pipeline gets the
same context-preservation assertion as a higher-level guard.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 9, 2026

Warning

Rate limit exceeded

@superdav42 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 52 minutes and 37 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c1696ba5-c4e5-482e-83cc-16f4f1a8a19f

📥 Commits

Reviewing files that changed from the base of the PR and between 685cf60 and ca4a134.

📒 Files selected for processing (2)
  • inc/duplication/files.php
  • tests/WP_Ultimo/Helpers/Site_Duplicator_Context_Test.php
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/site-duplication-blog-context-leak

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.

@superdav42 superdav42 merged commit c428ca0 into main May 9, 2026
11 checks passed
@superdav42
Copy link
Copy Markdown
Collaborator Author

Summary

  • New-site / welcome emails were silently failing for customers who signed up against a site template, while a manual Send test email from the network admin worked fine.
  • Root cause is in MUCD_Files::copy_files(): it pushes two switch_to_blog() frames onto the WordPress blog stack but only pops one. When duplication finishes, the request is still on the template/source blog instead of the network context. wp_mail() then runs against the template's admin_email, blogname, and any per-site SMTP plugin instead of the network's, so the welcome email never reaches the customer.
  • Fix: add the missing restore_current_blog() so the stack is fully unwound. The bug has been there since the initial MUCD import, so no other call sites needed adjusting.

Why test emails work but checkout emails don't

Path Goes through duplication? Blog context when wp_mail() runs
Network admin → Email → Send test No Network / admin (correct)
Checkout, no template No (wpmu_create_blog) Network / admin (correct)
Checkout, with template Yes (Site_Duplicator::duplicate_sitecopy_files) Template site (wrong)
That asymmetry exactly matches what customers reported: emails send when an admin tests them, fail during template-based signup, and succeed for non-template signups.

Tests

New file tests/WP_Ultimo/Helpers/Site_Duplicator_Context_Test.php adds two regression tests that fail on main and pass after the fix:

  1. test_copy_files_does_not_leak_blog_context — drives MUCD_Files::copy_files() directly with the actual filesystem recursion stubbed out via the existing mucd_copy_dirs filter, and asserts that get_current_blog_id() and ms_is_switched() are both unchanged after the call.
  2. test_duplicate_site_restores_blog_context — higher-level guard that runs the full Site_Duplicator::duplicate_site() pipeline and asserts the caller's blog context is preserved end-to-end.
    Verified locally:
$ vendor/bin/phpunit --filter 'Site_Duplicator_Context_Test' --no-coverage
OK (2 tests, 7 assertions)

PHPCS and PHPStan are clean on both modified files.

Risk

  • Low. The fix only adds the missing restore_current_blog(); it does not change copy semantics or the order in which uploads info is read.
  • No public API change. No DB schema change. No new feature flag.
  • The two switch_to_blog() calls already happened on every prior duplication, so the second restore_current_blog() matches what was already pushed onto the stack.


Merged via PR #1167 to main.
Merged by deterministic merge pass (pulse-wrapper.sh).


aidevops.sh v3.15.12 spent 34s on this as a headless bash routine.

@superdav42
Copy link
Copy Markdown
Collaborator Author

Summary

  • New-site / welcome emails were silently failing for customers who signed up against a site template, while a manual Send test email from the network admin worked fine.
  • Root cause is in MUCD_Files::copy_files(): it pushes two switch_to_blog() frames onto the WordPress blog stack but only pops one. When duplication finishes, the request is still on the template/source blog instead of the network context. wp_mail() then runs against the template's admin_email, blogname, and any per-site SMTP plugin instead of the network's, so the welcome email never reaches the customer.
  • Fix: add the missing restore_current_blog() so the stack is fully unwound. The bug has been there since the initial MUCD import, so no other call sites needed adjusting.

Why test emails work but checkout emails don't

Path Goes through duplication? Blog context when wp_mail() runs
Network admin → Email → Send test No Network / admin (correct)
Checkout, no template No (wpmu_create_blog) Network / admin (correct)
Checkout, with template Yes (Site_Duplicator::duplicate_sitecopy_files) Template site (wrong)
That asymmetry exactly matches what customers reported: emails send when an admin tests them, fail during template-based signup, and succeed for non-template signups.

Tests

New file tests/WP_Ultimo/Helpers/Site_Duplicator_Context_Test.php adds two regression tests that fail on main and pass after the fix:

  1. test_copy_files_does_not_leak_blog_context — drives MUCD_Files::copy_files() directly with the actual filesystem recursion stubbed out via the existing mucd_copy_dirs filter, and asserts that get_current_blog_id() and ms_is_switched() are both unchanged after the call.
  2. test_duplicate_site_restores_blog_context — higher-level guard that runs the full Site_Duplicator::duplicate_site() pipeline and asserts the caller's blog context is preserved end-to-end.
    Verified locally:
$ vendor/bin/phpunit --filter 'Site_Duplicator_Context_Test' --no-coverage
OK (2 tests, 7 assertions)

PHPCS and PHPStan are clean on both modified files.

Risk

  • Low. The fix only adds the missing restore_current_blog(); it does not change copy semantics or the order in which uploads info is read.
  • No public API change. No DB schema change. No new feature flag.
  • The two switch_to_blog() calls already happened on every prior duplication, so the second restore_current_blog() matches what was already pushed onto the stack.


Merged via PR #1167 to main.
Merged by deterministic merge pass (pulse-wrapper.sh).


aidevops.sh v3.15.12 spent 30s on this as a headless bash routine.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

🔨 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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

Performance Test Results

Performance test results for 20113c8 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 41 37.78 MB 803.50 ms (-84.50 ms / -11% ) 149.00 ms 1002.50 ms (-98.00 ms / -10% ) 1916.00 ms (-162.00 ms / -8% ) 1826.40 ms (-170.80 ms / -9% ) 81.45 ms
1 56 49.12 MB 969.00 ms 143.00 ms 1109.50 ms 2132.00 ms 2054.50 ms 77.30 ms

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.

1 participant