-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix: Media Library Grid view displays attachments in the wrong order when order query var is not normalized #10680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
Add order normalization in the Attachments model initialize method to ensure the 'order' property is always 'ASC' or 'DESC' (defaulting to 'DESC' for invalid values). This provides consistent behavior across all attachment collections. Part of fixing Media Library Grid view ordering when order query var is not normalized. See #64467.
Since Query inherits from Attachments, and Attachments now normalizes the order property in its initialize method, remove the duplicate normalization code from Query.get() to avoid redundancy and ensure consistent behavior. Part of fixing Media Library Grid view ordering when order query var is not normalized. See #64467.
|
Hi @TrivediKavit! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
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. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| if ( 'string' === typeof options.props.order ) { | ||
| options.props.order = options.props.order.toUpperCase(); | ||
| if ( 'ASC' !== options.props.order && 'DESC' !== options.props.order ) { | ||
| options.props.order = 'DESC'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this use options.props.order = defaults.order.toUpperCase(); like the original code in query.js that you remove below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot normalize order directly on defaults (e.g., defaults.order.toUpperCase()) in the Attachments model, as defaults refers to Query.defaultProps, which is scoped to the Query class and unavailable in the base Attachments class.
References:
defaultPropsdefined inQuery: src/js/media/models/query.js#L166-L169defaultsapplied usingQuery.defaultProps: src/js/media/models/query.js#L244
Attempting defaults.order.toUpperCase() in Attachments.initialize() would fail, as defaults is undefined in that scope.
Behavior in Attachments Instances
Attachments is the base Backbone collection for media attachments. On instantiation (e.g., new wp.media.model.Attachments(models, options)), initialize:
- Sets up
this.propswithoptions.props. - If
options.props.orderis a string, normalizes it: uppercases and defaults to'DESC'if invalid. - Ensures consistent
orderfor sorting/filtering, handling case and invalid inputs.
Previously, without normalization in Attachments.initialize(), plain Attachments instances (non-Query subclasses) could use unnormalized order (e.g., 'desc' or 'pizza'), causing inconsistent media library grid sorting.
Impact on Query Instances
Query inherits from Attachments, so normalization is inherited.
- Before: Normalization in
Query.get()ensured validorderforQueryinstances. - After: Normalization in
Attachments.initialize()during instantiation (via inheritance).Query.get()applies defaults first, thennew Query()triggers normalization. - No change in
Querybehavior; the fix prevents bugs in otherAttachmentssubclasses or direct usage.
adamsilverstein
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, left one small point of feedback.
|
I tried adding some QUnit tests to validate the order normalization (with help from Claude) in #10682 |
Trac ticket: https://core.trac.wordpress.org/ticket/64467
Summary
This PR addresses the Media Library Grid view ordering issue when the
orderquery parameter is not normalized (e.g.,order=descinstead oforder=DESC). It consolidates order normalization logic in the baseAttachmentsmodel to ensure consistent behavior across all attachment collections.Changes Made
In
attachments.js:initializemethod: convertsoptions.props.orderto uppercase and defaults to'DESC'if invalid.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:Queryinherits fromAttachmentsand will now use the parent's normalization logic.Why This Change?
this.propsstarts normalized inAttachments.initialize(), preventing inconsistencies betweenQueryandAttachmentsinstances.orderproperty uniformly, fixing the grid view ordering bug.AttachmentsandQuerymodels.'ASC'/'DESC'values work as before, and invalid values now default to'DESC'.Safety and Scope
wp.media.model.Attachmentsand its subclasswp.media.model.Query. This doesn't alter public APIs or external behavior.'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.QueryextendsAttachments, all media collections benefit from consistent normalization without duplication.'ASC'/'DESC'values remain unchanged; only unnormalized inputs are fixed. The Trac ticket confirms this resolves the grid view ordering bug without side effects.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
Queryinstances still normalize order via inheritance./wp-admin/upload.php?mode=grid&orderby=date&order=descto confirm correct ordering.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.