Skip to content

Fix template validation type mismatch causing "template not available" error#353

Open
superdav42 wants to merge 1 commit intomainfrom
fix/template-validation-type-mismatch-351
Open

Fix template validation type mismatch causing "template not available" error#353
superdav42 wants to merge 1 commit intomainfrom
fix/template-validation-type-mismatch-351

Conversation

@superdav42
Copy link
Collaborator

@superdav42 superdav42 commented Mar 5, 2026

Summary

  • Bug: Fixes The selected template is not available for this product. #351 - Customers getting "The selected template is not available for this product" error even when templates are configured correctly
  • Root cause: Template IDs stored as string keys in the limitations array were not being converted to integers, causing type mismatch with the validation rule which uses absint() on the submitted template ID
  • Fix: Convert site_id to integer in both get_available_site_templates() and get_pre_selected_site_template() methods

Changes

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

    • Line 224: Added absint($site_id) in get_available_site_templates()
    • Line 252: Added absint($site_id) in get_pre_selected_site_template()
  2. tests/WP_Ultimo/Objects/Limitations_Test.php:

    • Added test_available_site_templates_returns_integers() regression test
    • Test validates that template IDs are returned as integers
    • Test validates strict type checking with in_array(..., true)

Technical Details

The issue occurs because:

  1. Product limitations store template IDs as array keys (e.g., ['123' => ['behavior' => 'available']])
  2. Array keys in PHP are strings when parsed from JSON/arrays
  3. The validation rule at inc/helpers/validation-rules/class-site-template.php:50 converts the submitted template ID to integer using absint()
  4. The comparison at line 104 uses in_array($template_id, $allowed_templates) which can fail with string/int mismatches

Even though in_array() without strict mode should handle string/int comparison, there are edge cases where this fails, particularly after array merging operations.

Testing

  • New regression test added: test_available_site_templates_returns_integers()
  • Test validates integer type consistency
  • Test validates strict comparison works correctly
  • Manual testing: Create a product with template restrictions and verify checkout works

Related

@commercial-hippie Could you please test this fix on your site? This should resolve the "The selected template is not available for this product" error you're experiencing.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed site template handling to ensure IDs are consistently converted to integers, improving validation accuracy and data integrity for both available and pre-selected templates.
  • Tests

    • Added regression tests to verify site template identifiers are properly handled as integers.

…_templates

Fixes #351 - Template validation failing with 'The selected template is not
available for this product' error.

Root cause: Template IDs stored as string keys in the limitations array were
not being converted to integers, causing type mismatch with the validation
rule which uses absint() on the submitted template ID.

Changes:
- Updated get_available_site_templates() to convert site_id to integer
- Updated get_pre_selected_site_template() to convert site_id to integer
- Added regression test to ensure template IDs are returned as integers
- Test validates strict type checking with in_array(..., true)

This ensures consistent integer comparison in the Site_Template validation
rule (inc/helpers/validation-rules/class-site-template.php:104).
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2026

📝 Walkthrough

Walkthrough

This pull request fixes a type consistency issue in site template handling by converting site IDs to integers using absint() in two methods within the limitations class, ensuring type alignment with validation logic. A regression test is added to verify the conversion works correctly with string-keyed template IDs.

Changes

Cohort / File(s) Summary
Type Conversion in Site Template Logic
inc/limitations/class-limit-site-templates.php
Applied absint() to site ID values in get_available_site_templates() and get_pre_selected_site_template() methods to ensure integer types are returned, aligning with validation expectations.
Regression Test for Integer Type Validation
tests/WP_Ultimo/Objects/Limitations_Test.php
Added test_available_site_templates_returns_integers() to verify that template IDs are converted to integers when processing string-keyed site template limitations, including strict array membership checks.

Poem

🐰 A template's ID once wore a string,
But integers are what validation seeks to bring,
With absint() applied with careful hand,
The types align as conversion was planned,
Now tests confirm the fix rings true and clear! ✨

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 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 clearly identifies the main change: fixing a type mismatch in template validation that causes a specific error.
Linked Issues check ✅ Passed The PR addresses the root cause of issue #351 by converting template IDs to integers in get_available_site_templates and get_pre_selected_site_template, ensuring type consistency for validation comparisons.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the template ID type mismatch: converting to integers in two methods and adding a regression test to verify the fix.
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 (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/template-validation-type-mismatch-351

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

github-actions bot commented Mar 5, 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
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.

🧹 Nitpick comments (1)
tests/WP_Ultimo/Objects/Limitations_Test.php (1)

921-950: Add an assertion for get_pre_selected_site_template() to cover the second production change.

This test already covers get_available_site_templates() well; adding one assertion for the pre-selected path closes the remaining regression gap.

✅ Suggested test addition
 public function test_available_site_templates_returns_integers(): void {

 	$limitations = new Limitations([
 		'site_templates' => [
 			'enabled' => true,
 			'mode'    => 'choose_available_templates',
 			'limit'   => [
 				'123' => ['behavior' => 'available'],
 				'456' => ['behavior' => 'pre_selected'],
 				'789' => ['behavior' => 'not_available'],
 			],
 		],
 	]);

 	$available = $limitations->site_templates->get_available_site_templates();
+	$pre_selected = $limitations->site_templates->get_pre_selected_site_template();

 	// Should return integers, not strings
 	$this->assertContains(123, $available, 'Template 123 should be in available array as integer');
 	$this->assertContains(456, $available, 'Template 456 should be in available array as integer');
 	$this->assertNotContains(789, $available, 'Template 789 should not be available');
+	$this->assertSame(456, $pre_selected, 'Pre-selected template ID should be returned as integer');

 	// Verify strict type checking
 	foreach ($available as $template_id) {
 		$this->assertIsInt($template_id, 'All template IDs should be integers');
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/WP_Ultimo/Objects/Limitations_Test.php` around lines 921 - 950, Add an
assertion in test_available_site_templates_returns_integers to cover the
pre-selected path: call
$limitations->site_templates->get_pre_selected_site_template() and assert it
returns the expected template ID (456) as an integer (use assertSame or
assertIsInt + assertEquals) so the pre-selected code path is exercised and the
returned ID is strictly typed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/WP_Ultimo/Objects/Limitations_Test.php`:
- Around line 921-950: Add an assertion in
test_available_site_templates_returns_integers to cover the pre-selected path:
call $limitations->site_templates->get_pre_selected_site_template() and assert
it returns the expected template ID (456) as an integer (use assertSame or
assertIsInt + assertEquals) so the pre-selected code path is exercised and the
returned ID is strictly typed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6b37777b-980f-4ea1-86b9-6e2cdbc7038c

📥 Commits

Reviewing files that changed from the base of the PR and between fa58d9d and 717112f.

📒 Files selected for processing (2)
  • inc/limitations/class-limit-site-templates.php
  • tests/WP_Ultimo/Objects/Limitations_Test.php

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.

The selected template is not available for this product.

1 participant