Skip to content

fix: template selection blocked when product uses default (allow all) mode#754

Merged
superdav42 merged 1 commit intomainfrom
bugfix/site-template-default-mode-validation
Apr 7, 2026
Merged

fix: template selection blocked when product uses default (allow all) mode#754
superdav42 merged 1 commit intomainfrom
bugfix/site-template-default-mode-validation

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

@superdav42 superdav42 commented Apr 7, 2026

Summary

  • get_available_site_templates() returned [] (empty array) when a product's site template mode is MODE_DEFAULT ("Default - Allow All Site Templates")
  • The validation rule in class-site-template.php checks is_array($allowed_templates) && !in_array(...) — an empty array passes is_array(), so every template was blocked with "The selected template is not available for this product"
  • Fix: return false for MODE_DEFAULT to signal "no restriction", matching the existing is_array() gate used by the validator

Changes

inc/limitations/class-limit-site-templates.php

  • get_available_site_templates() returns false immediately when mode === MODE_DEFAULT
  • Removed the dead MODE_DEFAULT branch inside the loop (unreachable after the early return)

inc/limits/class-site-template-limits.php

  • is_template_available() guards against false from get_available_site_templates() before calling in_array(), preventing a PHP warning and correctly returning true for the default mode

Reproduction

  1. Create a product with Site Template Selection Mode = Default - Allow All Site Templates
  2. Add a Template Selection field to a checkout form (set to "All templates")
  3. Attempt to select any template during checkout
  4. Before fix: "The selected template is not available for this product."
  5. After fix: template selection proceeds normally

Summary by CodeRabbit

  • Bug Fixes
    • Improved template availability filtering logic to correctly handle default mode settings
    • Refined template eligibility checks to ensure only properly configured templates are presented as available options

… mode

get_available_site_templates() returned [] for MODE_DEFAULT, causing the
validation rule to block all templates with 'not available for this product'.

Return false for MODE_DEFAULT to signal no restriction, matching the
is_array() gate in the Site_Template validation rule. Guard is_template_available()
against the false return to avoid a PHP warning.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

📝 Walkthrough

Walkthrough

Two files modified to refine site template availability logic. The get_available_site_templates() method now treats MODE_DEFAULT as unrestricted by returning false immediately and narrows eligibility checks to only available or pre_selected templates. The is_template_available() method adds a guard clause to handle the false return value.

Changes

Cohort / File(s) Summary
Template Availability Logic
inc/limitations/class-limit-site-templates.php, inc/limits/class-site-template-limits.php
Modified get_available_site_templates() to return false immediately when MODE_DEFAULT is set, narrowed eligibility condition to exclude MODE_DEFAULT within the loop, and updated is_template_available() to add an explicit guard clause that returns true when the template method returns false.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through template halls,
Where MODE_DEFAULT no longer stalls,
false guards the gates with care divine,
Availability checks now align! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: it addresses template selection being blocked when a product uses default (allow all) mode, which is the core issue resolved in both modified files.
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/site-template-default-mode-validation

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

github-actions bot commented Apr 7, 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

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
inc/limitations/class-limit-site-templates.php (1)

204-214: ⚠️ Potential issue | 🔴 Critical

Breaking change: unguarded caller in class-template-switching-element.php will crash.

The return type changed from array to false|array. The call at inc/ui/class-template-switching-element.php:277 passes the result directly to array_map() without checking for false:

$available_templates = array_map('intval', $this->site->get_limitations()->site_templates->get_available_site_templates());

When get_available_site_templates() returns false (MODE_DEFAULT), array_map('intval', false) throws a TypeError in PHP 8.0+ or returns unexpected results in PHP 7.4, causing the subsequent in_array() call to fail.

Additionally, the PHPDoc at line 204 states @return array but should reflect @return array|false.

Suggested PHPDoc fix
 	/**
 	 * Get available themes.
 	 *
 	 * `@since` 2.0.0
-	 * `@return` array
+	 * `@return` array|false Array of available template IDs, or false when MODE_DEFAULT (unrestricted).
 	 */
 	public function get_available_site_templates() {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@inc/limitations/class-limit-site-templates.php` around lines 204 - 214,
get_available_site_templates() in class-limit-site-templates.php now returns
false|array but its PHPDoc still says `@return` array and callers like
class-template-switching-element.php::line ~277 call array_map(...) directly
which will TypeError on false; update the PHPDoc for
get_available_site_templates() to `@return` array|false and modify callers (e.g.,
the call in class-template-switching-element.php where $available_templates =
array_map('intval',
$this->site->get_limitations()->site_templates->get_available_site_templates());)
to guard for false by treating false as an empty array before mapping (e.g.,
fetch the value into a variable, coalesce or conditional to [] when false, then
call array_map), ensuring subsequent in_array() checks operate on an array.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@inc/limitations/class-limit-site-templates.php`:
- Around line 204-214: get_available_site_templates() in
class-limit-site-templates.php now returns false|array but its PHPDoc still says
`@return` array and callers like class-template-switching-element.php::line ~277
call array_map(...) directly which will TypeError on false; update the PHPDoc
for get_available_site_templates() to `@return` array|false and modify callers
(e.g., the call in class-template-switching-element.php where
$available_templates = array_map('intval',
$this->site->get_limitations()->site_templates->get_available_site_templates());)
to guard for false by treating false as an empty array before mapping (e.g.,
fetch the value into a variable, coalesce or conditional to [] when false, then
call array_map), ensuring subsequent in_array() checks operate on an array.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 59281a7a-4c89-4512-b76e-ff32dfb40e1a

📥 Commits

Reviewing files that changed from the base of the PR and between 3006a0d and f092156.

📒 Files selected for processing (2)
  • inc/limitations/class-limit-site-templates.php
  • inc/limits/class-site-template-limits.php

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Performance Test Results

Performance test results for cd4fdb1 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 (-1 / -3% ) 37.74 MB 854.50 ms (+50.00 ms / +6% ) 156.00 ms (+4.00 ms / +3% ) 1076.50 ms (+46.00 ms / +4% ) 2008.00 ms 1928.85 ms (+50.25 ms / +3% ) 92.50 ms (+3.60 ms / +4% )
1 56 49.02 MB 942.50 ms (+44.50 ms / +5% ) 146.00 ms (+3.50 ms / +2% ) 1092.50 ms (+50.00 ms / +5% ) 2056.00 ms (+56.00 ms / +3% ) 1977.50 ms (+53.95 ms / +3% ) 81.45 ms (+3.25 ms / +4% )

@superdav42 superdav42 merged commit 1cad367 into main Apr 7, 2026
11 checks passed
@superdav42
Copy link
Copy Markdown
Collaborator Author

Summary

  • get_available_site_templates() returned [] (empty array) when a product's site template mode is MODE_DEFAULT ("Default - Allow All Site Templates")
  • The validation rule in class-site-template.php checks is_array($allowed_templates) && !in_array(...) — an empty array passes is_array(), so every template was blocked with "The selected template is not available for this product"
  • Fix: return false for MODE_DEFAULT to signal "no restriction", matching the existing is_array() gate used by the validator

Changes

inc/limitations/class-limit-site-templates.php

  • get_available_site_templates() returns false immediately when mode === MODE_DEFAULT
  • Removed the dead MODE_DEFAULT branch inside the loop (unreachable after the early return)
    inc/limits/class-site-template-limits.php
  • is_template_available() guards against false from get_available_site_templates() before calling in_array(), preventing a PHP warning and correctly returning true for the default mode

Reproduction

  1. Create a product with Site Template Selection Mode = Default - Allow All Site Templates
  2. Add a Template Selection field to a checkout form (set to "All templates")
  3. Attempt to select any template during checkout
  4. Before fix: "The selected template is not available for this product."
  5. After fix: template selection proceeds normally

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

aidevops.sh v3.6.144 spent 6m on this as a headless bash routine.

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