wordpress-develop
wordpress-develop copied to clipboard
Fix: Media Library Grid view displays attachments in the wrong order when order query var is not normalized
Trac ticket: https://core.trac.wordpress.org/ticket/64467
Summary
This PR addresses the Media Library Grid view ordering issue when the order query parameter is not normalized (e.g., order=desc instead of order=DESC). It consolidates order normalization logic in the base Attachments model to ensure consistent behavior across all attachment collections.
Changes Made
In attachments.js:
- Added order normalization in the
initializemethod: convertsoptions.props.orderto uppercase and defaults to'DESC'if invalid. - Changed
this.props.set( _.defaults( options.props || {} ) );tooptions.props = _.defaults( options.props || {} );followed by normalization and thenthis.props.set( options.props );for better control.
In query.js:
- Removed duplicate order normalization code, as
Queryinherits fromAttachmentsand will now use the parent's normalization logic.
Why This Change?
-
Fixes Root Cause: Ensures
this.propsstarts normalized inAttachments.initialize(), preventing inconsistencies betweenQueryandAttachmentsinstances. -
Consistency: All attachment collections now handle the
orderproperty uniformly, fixing the grid view ordering bug. -
DRY Principle: Eliminates code duplication between
AttachmentsandQuerymodels. - Robustness: Prevents invalid order values from causing UI sorting issues.
-
Backwards Compatibility: No breaking changes; existing valid
'ASC'/'DESC'values work as before, and invalid values now default to'DESC'.
Safety and Scope
-
Scope: The changes only affect how order props are normalized internally in
wp.media.model.Attachmentsand its subclasswp.media.model.Query. This doesn't alter public APIs or external behavior. -
Normalization Is Defensive: We're ensuring order is always
'ASC'or'DESC'(defaulting to'DESC'), which aligns with existing expectations in the comparator and filters. Invalid inputs (like'desc'or'pizza') now behave predictably instead of causing silent failures. -
Inheritance: Since
QueryextendsAttachments, all media collections benefit from consistent normalization without duplication. -
No Breaking Changes: Valid
'ASC'/'DESC'values remain unchanged; only unnormalized inputs are fixed. The Trac ticket confirms this resolves the grid view ordering bug without side effects. -
Potential Edge Cases: If a plugin or theme manually instantiates these models with custom
ordervalues, they might see normalized output, but that's actually an improvement (consistent behavior). The media JS isn't loaded on the frontend by default, so no impact on public-facing sites unless explicitly enqueued.
Testing
- Verified that attachment sorting works correctly in the media library grid view.
- Ensured
Queryinstances still normalize order via inheritance. - Tested with URLs like
/wp-admin/upload.php?mode=grid&orderby=date&order=descto confirm correct ordering. - No adverse effects on other WordPress Core functionality.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.