Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ test.js
# The rendered API docs files will live in the gh-pages branch
docs/
index.html

# Webstorm config folder
.idea
53 changes: 53 additions & 0 deletions lib/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
var CollectionRequest = require( './shared/collection-request' );
var inherit = require( 'util' ).inherits;
var _ = require( 'lodash' );

/**
* PostsRequest extends CollectionRequest to handle the /posts API endpoint
Expand Down Expand Up @@ -223,4 +224,56 @@ PostsRequest.prototype.statuses = function() {
return this;
};

/**
* Adds the Year filter into the request to retrieve posts for a given year
*
* @method year
* @chainable
* @param {Number} year integer representation of year requested
* @returns {CollectionRequest} The PostsRequest instance (for chaining)
*/
PostsRequest.prototype.year = function( year ) {
return this.filter( 'year', year );
};

/**
* Add the monthnum filter into the request to retrieve posts for a given month
*
* Filters posts by a given month in either Integer or String Format.
*
* @method month
* @chainable
* @param {Number|String} month Integer for month or month string ("january")
* @returns {CollectionRequest} The PostsRequest instance (for chaining)
*/
PostsRequest.prototype.month = function( month ) {
if ( _.isString( month ) ) {
// Append a arbitrary day and year to the month to parse the string into a Date
month = new Date( Date.parse( month + ' 1, 2012' ) );
// If month is NaN, then the passed string is not a valid month.
if ( isNaN( month ) ) {
return this;
}
// Js Dates are 0 indexed, WP Api requires a 1 indexed integer.
month = month.getMonth() + 1;
}
// If month is a Number, add the monthnum filter to the request
if ( _.isNumber( month ) ) {
return this.filter( 'monthnum', month );
}
return this;
};

/**
* Add the day filter into the request to retrieve posts for a given day
*
* @method day
* @chainable
* @param {Number} day Integer representation of the day requested
* @returns {CollectionRequest} The PostsRequest instance (for chaining)
*/
PostsRequest.prototype.day = function( day ) {
return this.filter( 'day', day );
};

module.exports = PostsRequest;
2 changes: 1 addition & 1 deletion lib/shared/collection-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ CollectionRequest.prototype._renderQuery = function() {
// Parse query parameters object into a query string, sorting the object
// properties by alphabetical order (consistent property ordering can make
// for easier caching of request URIs)
var queryString = qs.stringify( queryParams )
var queryString = qs.stringify( queryParams, { arrayFormat: 'brackets' } )
.split( '&' )
.sort()
.join( '&' );
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"li": "^1.0.0",
"lodash": "^2.4.1",
"node.extend": "^1.0.10",
"qs": "^2.3.3",
"qs": "^5.0.0",
"route-parser": "0.0.2",
"superagent": "^0.18.0"
},
Expand All @@ -55,6 +55,7 @@
"grunt-newer": "^0.7.0",
"grunt-simple-mocha": "^0.4.0",
"istanbul": "^0.3.22",
"jscs": "^2.7.0",
"jscs-stylish": "^0.3.0",
"jshint-stylish": "^0.2.0",
"load-grunt-tasks": "^0.6.0",
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe( 'wp.posts', function() {
'page'
]);

var uri = '/wp-json/wp/v2/posts?type%5B0%5D=cpt1&type%5B1%5D=cpt2&type%5B2%5D=page';
var uri = '/wp-json/wp/v2/posts?type%5B%5D=cpt1&type%5B%5D=cpt2&type%5B%5D=page';
expect( posts._renderURI() ).to.equal( uri );
});

Expand Down
7 changes: 7 additions & 0 deletions tests/lib/shared/collection-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,13 @@ describe( 'CollectionRequest', function() {
.equal( '?filter%5Bpost_status%5D=publish&filter%5Bs%5D=Some%20search%20string' );
});

it( 'propery parse array filters', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo :)

request._filters = { post__in: [ 0, 1 ] };
var query = request._renderQuery();
expect( query ).to
.equal( '?filter%5Bpost__in%5D%5B%5D=0&filter%5Bpost__in%5D%5B%5D=1' );
});

it( 'correctly merges taxonomy and regular filters & renders them in order', function() {
request._taxonomyFilters = {
cat: [ 7, 10 ]
Expand Down