Change 'checked' to 'selected' for select list options#843
Conversation
📝 WalkthroughWalkthroughA template file in the checkout fields module was updated to use the correct WordPress helper function for marking selected options in a select field, changing from Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 `@views/checkout/fields/field-select.php`:
- Line 45: The placeholder <option> currently lacks a value attribute and uses
selected(! $field->value) which mis-selects when $field->value is a falsy but
valid value (e.g., "0"); update the option to include an explicit value (e.g.,
value="") and replace the boolean-negation selected call with the two-argument
comparison selected($field->value, '') so selection only occurs when the actual
field value equals the empty placeholder; target the <option> line that echoes
esc_html($field->placeholder) and the selected(...) invocation to make this
change.
🪄 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: 14daeb39-43a0-47d4-8b06-d5718b56b554
📒 Files selected for processing (1)
views/checkout/fields/field-select.php
| <?php if ($field->placeholder) : ?> | ||
|
|
||
| <option <?php checked(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option> | ||
| <option <?php selected(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option> |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify whether this pattern appears elsewhere and should be corrected consistently.
rg -nP --type=php -C2 'selected\(\s*!\s*\$[A-Za-z_][A-Za-z0-9_>\-\[\]\'"]*\s*\)'Repository: Ultimate-Multisite/ultimate-multisite
Length of output: 149
🏁 Script executed:
cat -n views/checkout/fields/field-select.php | head -70Repository: Ultimate-Multisite/ultimate-multisite
Length of output: 2037
🏁 Script executed:
# Search for selected() calls with boolean operators, using simpler pattern
rg -n --type=php 'selected\(' views/checkout/fields/ -A1 -B1Repository: Ultimate-Multisite/ultimate-multisite
Length of output: 544
Add explicit value attribute to placeholder option and fix falsy comparison
Line 45 uses selected(! $field->value) which incorrectly selects the placeholder when $field->value is a falsy but valid value like the string '0'. Additionally, the placeholder option lacks a value attribute. Use proper two-argument comparison instead:
Suggested fix
- <option <?php selected(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>
+ <option value="" <?php selected('', (string) $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option>📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| <option <?php selected(! $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option> | |
| <option value="" <?php selected('', (string) $field->value); ?> class="wu-opacity-75"><?php echo esc_html($field->placeholder); ?></option> |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@views/checkout/fields/field-select.php` at line 45, The placeholder <option>
currently lacks a value attribute and uses selected(! $field->value) which
mis-selects when $field->value is a falsy but valid value (e.g., "0"); update
the option to include an explicit value (e.g., value="") and replace the
boolean-negation selected call with the two-argument comparison
selected($field->value, '') so selection only occurs when the actual field value
equals the empty placeholder; target the <option> line that echoes
esc_html($field->placeholder) and the selected(...) invocation to make this
change.
|
@commercial-hippie Thank you so much for this catch — this is a fantastic contribution! This bug has been lurking in the codebase since the very first commit (November 2024) and survived through seven subsequent commits over 16 months without anyone noticing. Using Really appreciate you taking the time to find this, fix it, and submit the PR. Merged! We'll follow up with a small additional improvement based on CodeRabbit's review (adding an explicit Thanks again for contributing to Ultimate Multisite! |
The placeholder <option> used selected(! $field->value) which incorrectly
selected the placeholder when $field->value was a falsy but valid value
like '0'. Replace with two-argument selected('', (string) $field->value)
and add an explicit value="" attribute.
Follow-up to PR #843 — based on CodeRabbit review suggestion.
…ute (#859) The placeholder <option> used selected(! $field->value) which incorrectly selected the placeholder when $field->value was a falsy but valid value like '0'. Replace with two-argument selected('', (string) $field->value) and add an explicit value="" attribute. Follow-up to PR #843 — based on CodeRabbit review suggestion.
Pre-selected values don't show correctly in the checkout forms, currently printing out "checked" instead of "selected".
Summary by CodeRabbit