Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Conversation

@BE-Webdesign
Copy link
Member

Currently I exposed everything for date parameters. Whether we want
just date query or just top level or make date query params top level
and then parse a date query, is up for discussion. I am up for improving
this, this is a start and something that works.

If you want to use the top level just pass them as arguments.
GET wp/v2/posts/?year=2016&monthnum=1

For posts from January, 2016.

If you want to use date query do something like:
GET /wp/v2/posts/?date_query[0][after][year]=2001&date_query[0][before][2020]

This will return posts after the year 2001 and before 2020.

Currently I exposed everything for date parameters.  Whether we want
just date query or just top level or make date query params top level
and then parse a date query, is up for discussion. I am up for improving
this, this is a start and something that works.
@BE-Webdesign
Copy link
Member Author

This is for #2144, new to GitHub should have referenced that above.

Testing to see if this fixes failed test on array assertion.
Attempt to fix testing failures by adding new params into the test files
themselves.  If I am not supposed to do this let me know.
Coolest part was I found out that this year is a leap year from this
code and testing! Do
/wp/v2/posts/?date_query[0][year]=2015&date_query[0][month]=2&date_query[0][day]=29.
If it doesn't fail it's a leap year! Hooray.

Added validation.  This is still not error proof as I need to handle
weird cases with how the top level params work, like when monthnum is
specified without year.  Maybe chopping top level parameter support
would be nice, but would leave this incomplete so I will continue to add
error handling for those strange cases.
@joehoyle joehoyle changed the title Potential-enhancement-#2144 Add date query to posts endpoints Feb 13, 2016
@joehoyle
Copy link
Member

This is looking like a good first step @BE-Webdesign, I think we'll likely want to look at the param names, for example I think we're unlikely to match the wp_query names like w as those are very cryptic. I seem to remember discussing with @rmccue and @danielbachhuber briefly as to whether it would be best to just support date_query rather than all the other shorthand ones - however we should probably talk about this.

Code wise, this is very neat and clear - I'd typically steer away from fixing whitespace / yoda conditions at the same time though, unless it's that piece of code you are changing.

This is a good PoC and we should look to iterate on it after discussing at the next meeting in slack 👍

@joehoyle joehoyle added this to the 2.0 Beta 13 milestone Feb 13, 2016
@BE-Webdesign
Copy link
Member Author

@joehoyle As my schedule is right now I should be able to attend the meeting and am up for helping out with any changes the team needs me to make. Thanks, for the tips I will not change the yoda conditions, makes sense. I did that in my other tax_query support so I will remove that at some point today in both of them.

@rmccue
Copy link
Member

rmccue commented Feb 15, 2016

Per #2144 (comment) I think we're best off only supporting before and after at the top level, and supporting filter[date_query] separately. The other query vars are a bit complex and inconsistent, and 80% of expected behaviour can be supported with just before and after.

@rmccue
Copy link
Member

rmccue commented Feb 15, 2016

(Agreed with @joehoyle on the code quality though, this is a super neat PR 👍 )

Supporting only before and after top level now and filter[date_query] as
suggested in WP-API#2144.  Food for thought: if both filter[date_query] and
before or after are specified filter[date_query] will override.  Do we
want that or do we want before after to override filter[date_query]? Let
me know if I interpreted your feedback correctly or if I went in the
wrong direction.
@BE-Webdesign
Copy link
Member Author

@rachelbaker , @joehoyle, @rmccue, @danielbachhuber , is this latest revision on the right track?

$params['context']['default'] = 'view';

$params['after'] = array(
'description' => __( 'The after parameter in a date query, send as an ISO8601 or strtotime() compliant date string. See wp-includes/date.php' ),
Copy link
Member

Choose a reason for hiding this comment

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

Accepting strtotime()-compatible values seems a bit sketchy to me, given we don't accept them anywhere else in the API and they're very much a PHP-ism.

If we change format=>date-time, then we get validation for ISO8601 for free and don't have to introduce a validation callback.

Copy link
Member Author

Choose a reason for hiding this comment

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

@danielbachhuber - Excellent suggestion, will get on that.

@danielbachhuber
Copy link
Member

is this latest revision on the right track?

I think so, aside from my one comment.

Changing after and before parameter to date-time format.
@BE-Webdesign
Copy link
Member Author

@danielbachhuber Let me know if I need to tweak this anymore. Thanks for the feedback! I am learning a lot faster so big thanks to all of the WP-API team for helping me out!

@danielbachhuber
Copy link
Member

Let me know if I need to tweak this anymore.

Can you add some tests covering both valid and invalid date parameters supplied?

@BE-Webdesign
Copy link
Member Author

Can you add some tests covering both valid and invalid date parameters supplied?

I have to learn how to do that first, but I believe I can get that done.

First attempt at adding tests for valid and invalid before after
queries.
Second attempt at adding test.
Fixed the tests not sure if they are good enough though.  Finally
installed PHPUnit and stuff so my PRs should be cleaner in the future!
Need feedback if the tests are good enough.
@BE-Webdesign
Copy link
Member Author

@danielbachhuber I think this is ready to go, maybe? Before and after would need to be added to posts, pages, and media on #924.


// Set before into date query. Date query must be specified as an array of an array.
if ( isset( $request['before'] ) ) {
$args['date_query'][0]['before'] = $request['before'];
Copy link
Member

Choose a reason for hiding this comment

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

To prevent a PHP notice, can we create the date_query array before trying to assign the value?

$params['context']['default'] = 'view';

$params['after'] = array(
'description' => __( 'The after parameter in a date query, send as an ISO8601 compliant date string.' ),
Copy link
Member

Choose a reason for hiding this comment

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

Clients don't necessarily know what a "date query" is, given this is a WordPress-ism. Also, "after parameter" and "send" are implied in the context. What about something like:

"Limit response to resources published after a given ISO8601 compliant date."

@danielbachhuber
Copy link
Member

I think this is ready to go, maybe?

Tests look pretty good. I left a few small follow-up comments to address.

Before and after would need to be added to posts, pages, and media on #924.

Yep, I've got a script to update that.

@BE-Webdesign
Copy link
Member Author

Okay, thanks for the comments. I will make those changes now!

Changed before, after description to be less WordPress oriented.  Added
empty array for $args['date_query'], to prevent PHP Notice
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants