Ensure the Attachments model properties are correctly set for Query collections.

After shifting sorting and searching logic from the `Query` collection in [21898], it became apparent that `Query` collections should also have an accurate `props` model, as the model controls the aforementioned searching and sorting.

see #21921, #21809, and #21390.


git-svn-id: http://core.svn.wordpress.org/trunk@21900 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Daryl Koopersmith 2012-09-18 22:19:05 +00:00
parent 9481b4ae89
commit 751ecf9fd5

View File

@ -440,15 +440,18 @@ if ( typeof wp === 'undefined' )
} }
} }
}, { }, {
defaultArgs: { defaultProps: {
posts_per_page: 40,
orderby: 'date', orderby: 'date',
order: 'DESC' order: 'DESC'
}, },
defaultArgs: {
posts_per_page: 40
},
orderby: { orderby: {
allowed: [ 'name', 'author', 'date', 'title', 'modified', 'parent', 'ID' ], allowed: [ 'name', 'author', 'date', 'title', 'modified', 'uploadedTo', 'id' ],
keymap: { valuemap: {
'id': 'ID', 'id': 'ID',
'uploadedTo': 'parent' 'uploadedTo': 'parent'
} }
@ -465,39 +468,49 @@ if ( typeof wp === 'undefined' )
return function( props, options ) { return function( props, options ) {
var args = {}, var args = {},
orderby = Query.orderby, orderby = Query.orderby,
defaults = Query.defaultArgs, defaults = Query.defaultProps,
query; query;
// Remove the `query` property. This isn't linked to a query,
// this *is* the query.
delete props.query;
// Fill default args.
_.defaults( props, defaults );
// Normalize the order.
props.order = props.order.toUpperCase();
if ( 'DESC' !== props.order && 'ASC' !== props.order )
props.order = defaults.order.toUpperCase();
// Ensure we have a valid orderby value.
if ( ! _.contains( orderby.allowed, props.orderby ) )
props.orderby = defaults.orderby;
// Generate the query `args` object.
// Correct any differing property names. // Correct any differing property names.
_.each( props, function( value, prop ) { _.each( props, function( value, prop ) {
args[ Query.propmap[ prop ] || prop ] = value; args[ Query.propmap[ prop ] || prop ] = value;
}); });
// Fill default args. // Fill any other default query args.
_.defaults( args, defaults ); _.defaults( args, Query.defaultArgs );
// Normalize the order. // `props.orderby` does not always map directly to `args.orderby`.
args.order = args.order.toUpperCase();
if ( 'DESC' !== args.order && 'ASC' !== args.order )
args.order = defaults.order.toUpperCase();
// Set allowed orderby values.
// These map directly to attachment keys in most scenarios.
// Substitute exceptions specified in orderby.keymap. // Substitute exceptions specified in orderby.keymap.
args.orderby = orderby.keymap[ args.orderby ] || args.orderby; args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;
// Ensure we have a valid orderby value. // Search the query cache for matches.
if ( ! _.contains( orderby.allowed, args.orderby ) )
args.orderby = defaults.orderby;
// Search the query cache.
query = _.find( queries, function( query ) { query = _.find( queries, function( query ) {
return _.isEqual( query.args, args ); return _.isEqual( query.args, args );
}); });
// Otherwise, create a new query and add it to the cache. // Otherwise, create a new query and add it to the cache.
if ( ! query ) { if ( ! query ) {
query = new Query( [], _.extend( options || {}, { args: args } ) ); query = new Query( [], _.extend( options || {}, {
props: props,
args: args
} ) );
queries.push( query ); queries.push( query );
} }