Fix TypeError: Cannot read properties of undefined (reading 'localeCompare')#368
Fix TypeError: Cannot read properties of undefined (reading 'localeCompare')#368EarthmanWeb wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Guard against `window.acf.data.wp_version` being undefined before calling `localeCompare()` in both admin-commands.js and custom-post-type-commands.js. Falls back to '0' which safely registers all commands (pre-6.9 behavior).
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Good spot, thank you! As for the fix, I think it'd be better if we bailed early if Turns out that we already have checks that look like they're supposed to cover cases like these: secure-custom-fields/assets/src/js/commands/admin-commands.js Lines 34 to 36 in 8b53724 and They just don't seem to quite cover the case where |
|
Looking a bit more into this had me discover a related/underlying issue: When the command palette is invoked (Cmd+K) from a non-editor wp-admin screen, SCF options aren't added there. (This is due to the conditionals I mentioned.)
|


Summary
Fixes an
Uncaught TypeError: Cannot read properties of undefined (reading 'localeCompare')thrown on every admin page load fromscf-admin.min.js.Problem
Both
admin-commands.jsandcustom-post-type-commands.jsreadwindow.acf.data.wp_versionand immediately call.localeCompare()on it to determine if the WordPress version is 6.9+. However,wp_versionis only added toacf.dataconditionally inincludes/assets.php(line 622), inside a block that checks for Gutenberg/block editor context. On classic editor pages and other admin screens where that condition isn't met,wp_versionisundefined, causing the TypeError.Error:
This error fires on every admin page load via
requestIdleCallback.Fix
Added a fallback
|| '0'when readingwindow.acf.data.wp_versionin both files:When
wp_versionis undefined, the fallback'0'ensureslocaleCompare('6.9')returns< 0, so all commands are registered (pre-6.9 behavior). This is the safe default — registering extra commands is harmless, while crashing is not.Files Changed
assets/src/js/commands/admin-commands.js— Added|| '0'fallback (line 216)assets/src/js/commands/custom-post-type-commands.js— Added|| '0'fallback (line 41)tests/js/commands/admin-commands.test.js— Added test case forundefinedwp_versiontests/js/commands/custom-post-type-commands.test.js— Added test case forundefinedwp_versionTest Plan
npm run test:unitpasses with new undefined version test caseswp_versionis available