Repository navigation
Fix multicheck not saving when only the 0 option is checked - #1572
Merged
jtsternberg merged 2 commits intoOct 11, 2026
Merged
Conversation
CMB2_Sanitize::_is_empty_array() used array_filter() to decide if a submitted array was empty. array_filter() also drops 0 and '0', so a multicheck with only the option keyed 0 checked was treated as empty and its value was deleted on save. Use CMB2_Utils::filter_empty() instead. It keeps 0 and '0' and still drops null, '', false and empty arrays, so the original goal of the check (not saving arrays with no real values) stays the same. Fixes CMB2#1535
2 tasks done
2 tasks done
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1572 +/- ##
==========================================
Coverage 72.16% 72.16%
Complexity 1911 1911
==========================================
Files 53 53
Lines 5034 5034
==========================================
Hits 3633 3633
Misses 1401 1401
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jtsternberg
added a commit
that referenced
this pull request
Oct 11, 2026
…staged Staging such a fix keeps a data-loss bug as the default. Decided while reviewing #1572 (multicheck dropping a lone '0'); precedent dba5776 and 7e521ed. Format changes such as reinterpreting stored values stay staged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KpfVcxMH9VE8CRmGRvjJPS Co-Authored-By: jtsternberg-bot <me+jtbot@jtsternberg.com>
jtsternberg
added a commit
that referenced
this pull request
Oct 11, 2026
The C3 data-loss exception ships this unstaged on the condition that the changelog spells out what integrations now see. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KpfVcxMH9VE8CRmGRvjJPS Co-Authored-By: jtsternberg-bot <me+jtbot@jtsternberg.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
CMB2_Sanitize::_is_empty_array()usedarray_filter()to check if a submitted array was empty.array_filter()also drops0and'0', so when the only checkedmulticheckoption has the key0, the valuearray( '0' )is treated as empty.default_sanitization()then returns''andCMB2_Field::save_field()deletes the value.This PR switches the check to the existing
CMB2_Utils::filter_empty()helper, which keeps0and'0'and still dropsnull,'',falseand empty arrays.Before:
After:
The check was added in 111bdad ("Fix empty arrays saving as a value") to stop arrays like
array( '' )from being saved. That still works the same after this change.Motivation and Context
Fixes #1535.
Repro: a
multicheckfield with'options' => array( 0 => 'Zero', 1 => 'One' ). Check only "Zero" and save. The page reloads with nothing checked and the meta is deleted. If any other option is also checked, it saves fine.Risk Level
Low.
_is_empty_array()has one caller,CMB2_Sanitize::default_sanitization(), which every field type without its own sanitizer goes through. The only behavior change: a non-repeatable array value that holds only0or'0'is now saved instead of deleted. For that value,save_field()now callsupdate_data()andcmb2_save_fieldfires with$action = 'updated', where before it calledremove_data()with$action = 'removed'. Nothing else changes:'',nullorfalseare still treated as empty, so unchecking every option still removes the value.'multiple' => truebranch, group subfields, options pages and the REST API all use the same path, so they all get the fix.Repeatable fields still drop
'0'entries through a separatearray_filter()inCMB2_Field::sanitization_cb(). That is older behavior and is not touched here.Testing procedure
Test_CMB2_Types::test_multicheck_sanitization_keeps_zero_value. It checks thatarray( '0' )andarray( 0 )are kept, and thatarray()andarray( '', null, false )still return''.Test_CMB2_Field::test_cmb2_multicheck_field_saves_zero_value. It goes throughsave_field(): saving only'0'storesarray( '0' )in post meta, saving'0'with another option stores both, and saving an empty array removes the meta.composer phpcsis clean.multicheck_inlinefield, amulticheckinside a group and amulticheckon an options page. Checking only "Zero" now stays checked after saving, and unchecking everything clears the value.Types of changes
Checklist:
Screenshots
No UI changes.