Skip to content

fix: enforce Allow Template Switching setting on the server - #2157

Merged
superdav42 merged 2 commits into
mainfrom
fix/template-switching-setting-enforcement
Nov 28, 2026
Merged

superdav42 merged 2 commits into
mainfrom
fix/template-switching-setting-enforcement

Conversation

@superdav42

@superdav42 superdav42 commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Enforce the "Allow Template Switching" setting server-side. It was only checked when the Site Actions link was built (class-site-actions-element.php), so a customer who opened the Template Switching page or called the wu_switch_template handler directly could overwrite their site with a template while switching was disabled network-wide.
  • Template_Switching_Element::switch_template() now returns a template_switching_disabled JSON error when the setting is off.
  • Template_Switching_Admin_Page::page_loaded() now ends with a 403 wp_die() when the setting is off.

Files

  • inc/ui/class-template-switching-element.php
  • inc/admin-pages/customer-panel/class-template-switching-admin-page.php

Verification

  • php -l on both files: no syntax errors.
  • vendor/bin/phpcs on both files: clean.
  • Not run: PHPUnit and a manual browser check. Suggested manual check: disable "Allow Template Switching", then confirm that opening ?page=wu-template-switching returns 403 and that POSTing wu-ajax wu_switch_template returns the error. Re-enable the setting and confirm switching still works.

Notes

  • The setting default stays true, matching the existing check in Site Actions.
  • The MODE_ASSIGN_TEMPLATE hiding in Site Actions is not enforced here. That is a separate behaviour and is out of scope for this fix.

Summary by CodeRabbit

  • Bug Fixes
    • When template switching is disabled, the admin page now returns an access-denied response with a clear notice and a link to return.
    • Template-switch requests are now rejected when switching is disabled, rather than processing the requested template. This applies even when the request is otherwise authorized.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The admin page and template-switching request now check allow_template_switching. When the setting is disabled, the page returns a 403 response, and the request returns a template_switching_disabled error.

Changes: Template Switching Controls

Layer / File(s) Summary
Enforce the template switching setting
inc/admin-pages/customer-panel/class-template-switching-admin-page.php, inc/ui/class-template-switching-element.php
When allow_template_switching is disabled, the admin page returns a 403 response with a back link. The template-switching request returns a template_switching_disabled JSON error.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 41ee1

Template switching is now enforced on the server for both the admin page and the AJAX handler when the network setting is disabled. No merge-blocking issues were found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title 'fix: enforce Allow Template Switching setting on the server' matches the main change. The raw summary shows both the admin page (page_loaded) and the AJAX handler (switch_template) now chec…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title 'fix: enforce Allow Template Switching setting on the server' matches the main change. The raw summary shows both the admin page (page_loaded) and the AJAX handler (switch_template) now check the allow_template_switching setting on the server and return a 403 or a template_switching_disabled JSON error when it is off. The PR objectives describe the same goal of enforcing the setting beyond the Site Actions link.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aedf6f16-90b2-46b0-b208-8befb4619a4c
Commits

Reviewing files that changed from the base of the PR and between 6f3772e and 41ee11f.

Files selected for processing: 2

  • inc/admin-pages/customer-panel/class-template-switching-admin-page.php
  • inc/ui/class-template-switching-element.php

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@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

@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 7174bd1 into main Nov 28, 2026
15 checks passed
@superdav42

Copy link
Copy Markdown
Collaborator Author

Summary

  • Enforce the "Allow Template Switching" setting server-side. It was only checked when the Site Actions link was built (class-site-actions-element.php), so a customer who opened the Template Switching page or called the wu_switch_template handler directly could overwrite their site with a template while switching was disabled network-wide.
  • Template_Switching_Element::switch_template() now returns a template_switching_disabled JSON error when the setting is off.
  • Template_Switching_Admin_Page::page_loaded() now ends with a 403 wp_die() when the setting is off.

Files

  • inc/ui/class-template-switching-element.php
  • inc/admin-pages/customer-panel/class-template-switching-admin-page.php

Verification

  • php -l on both files: no syntax errors.
  • vendor/bin/phpcs on both files: clean.
  • Not run: PHPUnit and a manual browser check. Suggested manual check: disable "Allow Template Switching", then confirm that opening ?page=wu-template-switching returns 403 and that POSTing wu-ajax wu_switch_template returns the error. Re-enable the setting and confirm switching still works.

Notes

  • The setting default stays true, matching the existing check in Site Actions.
  • The MODE_ASSIGN_TEMPLATE hiding in Site Actions is not enforced here. That is a separate behaviour and is out of scope for this fix.


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

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