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

Adds tax query support to GET wp/v2/posts with custom validation function for #2145. Let me know what we can do to improve this.

Simple call for posts in the uncategorized category taxonomy.
/wp/v2/posts?tax_query[0][taxonomy]=category&tax_query[0][field]=slug&tax_query[0][terms]=uncategorized

More Complex:
/wp/v2/posts?tax_query[relation]=OR&tax_query[0][taxonomy]=category&tax_query[0][field]=slug&tax_query[0][terms][0]=quotes&tax_query[1][relation]=AND&tax_query[1][0][taxonomy]=post_format&tax_query[1][0][field]=slug&tax_query[1][0][terms][0]=post-format-quote&tax_query[1][1][taxonomy]=category&tax_query[1][1][field]=slug&tax_query[1][1][terms][0]=wisdom

Adds tax query support to GET /wp/v2/posts/.  Added a tax query
validation function as well.  Hopefully I can pass travis first time for
once!
Fix post controller test with comma
@BE-Webdesign
Copy link
Member Author

Test failing because tax_query is potentially a slow query so is it not a good idea to support this? Need assistance! Is my validation what is throwing that error?

@joehoyle
Copy link
Member

This goes somewhat hand-in-hand with date query, I'm not sure whether we want to match the same naming as WP internally for the fields, or if we should support the full tax query.

A first iteration may be good to allow tax_query as part of the filter param?

@joehoyle joehoyle added this to the 2.0 Beta 13 milestone Feb 13, 2016
Reverting yoda conditions to the way they were.
…ax-query-WP-API#2145

# Conflicts:
#	lib/endpoints/class-wp-rest-posts-controller.php
Resolving Conflicts
Exposing tax_query in the filter as opposed to a query param.
@BE-Webdesign
Copy link
Member Author

So at this point it is probably ridiculous to merge this one line of code in. So @joehoyle if you want to add this into #2287 it would probably make sense to do it there instead of 8 commits for one line of code haha! If I am missing something that needs to be done with tax_query as a filter param let me know. Currently it works well with something like:

GET wp/v2/posts/?filter[tax_query][0][taxonomy]=category&filter[tax_query][0][field]=slug&filter[tax_query][0][terms]=uncategorized

I think this combined with Joe's #2287 should solve #2145 and #2277 and add some nice parameters to handle taxonomy querying.

@joehoyle
Copy link
Member

I think we'll still want this PR, as it adds it to filter. I didn't use this technique in #2287, as we need to providing whitelisting for the taxonomies in filter[tax_query] which I believe we don't currently have. @BE-Webdesign I thought you had done something like that already when you make tax_query a top level param?

@BE-Webdesign
Copy link
Member Author

@joehoyle I had a validation function that validated the tax_query itself which did include white listing minus the show_in_rest => true. I will try to figure out a way to add that in. Do we want that in depth of validation of the filter param if tax_query is set or do we just want to white list taxonomies that are show_in_rest => true?

Adding validation back in for tax_query as a filter param.  Also added a
function for validating filter params.  Not sure if this is necessary,
it runs relatively fast still feedback appreciated for what direction
this should take.
@BE-Webdesign
Copy link
Member Author

Definitely will need some feedback on the direction for this latest commit. I tested it with #2287 and there are no conflicts. So if you wanted to find posts that were both in tags 1 and categories 1 then with #2287 you could do something like this.

GET wp/v2/posts/?tags=1&categories=1&filter[tax_query][relation]=AND

I think this in combination with @joehoyle 's work will make handling more robust tax queries a lot simpler. Feedback is always appreciated!

@danielbachhuber
Copy link
Member

Definitely will need some feedback on the direction for this latest commit.

Holy moly. I need to think on this one for a little bit.

@BE-Webdesign
Copy link
Member Author

@danielbachhuber I spoke briefly with @rachelbaker one night about validation of the filter param. We both kind of came to the conclusion that it would be overkill. So we can decide whether we want to selectively validate certain arguments passed into the filter or whether we want none at all. I think validating all of them would just be too much code and as new WP_Query args are added it would make maintaining the REST API more difficult as well.

I'm pretty sure WP_Query() validates the arguments passed to it so the validation only really serves as a way to tell the user they made a bad request, which might be important, might not. As we have more top level params, that cover a lot of use cases, I imagine the filter param will be used less and probably by power users who will understand WP_Query so validation may not be necessary at all. @joehoyle , @rmccue , @rachelbaker , @danielbachhuber thoughts would be appreciated!

@joehoyle
Copy link
Member

@BE-Webdesign the problem with not sanitizing / filtering the tax_query is we would be potentially allowing taxonomies to be queried that have specifically not been opted into the REST API via show_in_rest. This is why we can't rely on WP_Query to do this, as that will allow any taxonomy that has a query var to be used.

I think the sanitize function for tax_query shouldn't be too difficult when it gets down to it. Especially if we on'y check the taxonomy names, rather than a full sanitization of all values.

@BE-Webdesign
Copy link
Member Author

@joehoyle Okay so cut this down to just checking for valid taxonomy names which will be queried against for show_in_rest => true? Will other filter params be validate/sanitized at some point as well?

continue;
}
}
return $is_valid;
Copy link
Contributor

Choose a reason for hiding this comment

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

If there are no $values then would't this code throw an error since $is_valid is not declared anywhere? I would just return true here if you want that to be the default value, or declare $is_valid = true before the foreach.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ya I should put an empty check there and have some declaration of $is_valid, most likely this is being tossed anyways for something simpler. This is more of an experiment rather than what is being merged. But thank you for pointing that out because that is a glaring mistake haha 😄

Simplifying validation.  Condensed the validation function to just worry
about the relation parameter whether it is AND or OR, and making sure
that taxonomies that are queried against only have show_in_rest => true
@BE-Webdesign
Copy link
Member Author

Going to simplify this some more.

Adding tests for valid filter and invalid filter for tax queries.
Simplifies validation more.
@BE-Webdesign
Copy link
Member Author

Failing for use of tax_query potentially being a slow query.

@BE-Webdesign
Copy link
Member Author

@kjbenk Thanks for the tip!!! Going to put it in.

Adding in WPCS whitelisting for tax_query. A great suggestion by kjbenk.
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
$request->set_param( 'filter', array(
// WPCS: tax_query ok.
'tax_query' => array(
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to add the comments inline. So this would be the correct syntax 'tax_query' => array( // WPCS: tax_query ok. or at least I think :)

Causes PHP CodeSniffer to ignore the tax query lines.
@codecov-io
Copy link

codecov-io commented Aug 8, 2016

Current coverage is 94.57% (diff: 97.29%)

Merging #2273 into develop will increase coverage by 0.02%

@@            develop      #2273   diff @@
==========================================
  Files            11         11          
  Lines          3612       3649    +37   
  Methods         172        174     +2   
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           3415       3451    +36   
- Misses          197        198     +1   
  Partials          0          0          

Powered by Codecov. Last update c661101...9289110

}
continue;
default :
continue;
Copy link
Contributor

@kadamwhite kadamwhite Sep 12, 2016

Choose a reason for hiding this comment

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

Nitpick, but for a single-case switch I'd vote in favor of an if. (unless this is a code standard of which I was not aware)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it was originally an entire validation of tax_query, but now it handles only one case. If this ever wanted to be expanded though it should go back to switch. I can make a modification to it sometime soon.

BE-Webdesign added a commit to BE-Webdesign/WP-API that referenced this pull request Oct 1, 2016
Fixes WP-API#2145, this was put into a new PR to make it cleaner.  Original PR
is in WP-API#2273.  This enables `tax_query` in the `filter` param and
validates that the taxonomies that are queried are `show_in_rest =>
true`
@BE-Webdesign
Copy link
Member Author

Closed in favor of #2748.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants