-
Notifications
You must be signed in to change notification settings - Fork 651
Add tax query #2145 #2273
Add tax query #2145 #2273
Changes from all commits
d5eb371
652df81
0cd2163
80cea68
c9bd0f1
4bd65f6
d82675c
a48089a
03a066e
5ff8675
e29beb4
8e39e49
082e111
f3b37e1
aa8f6a2
2b00395
226cf9d
5ce7a28
ab14e3b
9289110
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -627,6 +627,7 @@ protected function get_allowed_query_vars() { | |
| 'post_parent__not_in', | ||
| 'posts_per_page', | ||
| 'date_query', | ||
| 'tax_query', | ||
| ); | ||
| $valid_vars = array_merge( $valid_vars, $rest_valid ); | ||
|
|
||
|
|
@@ -1749,6 +1750,7 @@ public function get_collection_params() { | |
| ); | ||
| $params['filter'] = array( | ||
| 'description' => __( 'Use WP Query arguments to modify the response; private query vars require appropriate authorization.' ), | ||
| 'validate_callback' => array( $this, 'rest_validate_query_filter' ), | ||
| ); | ||
|
|
||
| $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); | ||
|
|
@@ -1784,4 +1786,86 @@ public function validate_user_can_query_private_statuses( $value, $request, $par | |
| return new WP_Error( 'rest_forbidden_status', __( 'Status is forbidden' ), array( 'status' => rest_authorization_required_code() ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Validate filter param. | ||
| * | ||
| * This function can be used to validate query params passed by the filter request param. | ||
| * | ||
| * @param mixed $value Value of the parameter. | ||
| * @param WP_REST_Request $request Request body. | ||
| * @param string $parameter Name of the parameter. Should be tax_query. | ||
| * @return WP_Error|boolean | ||
| */ | ||
| public function rest_validate_query_filter( $values, $request, $parameter ) { | ||
| // Returns true for validation by default if $values is empty or if a filter param is not being validated. | ||
| $is_valid = true; | ||
| if ( ! empty( $values ) ) { | ||
| foreach ( $values as $filter_param => $value ) { | ||
| switch ( $filter_param ) { | ||
| case 'tax_query' : | ||
| $is_valid = $this->rest_validate_tax_query( $value, $request, $filter_param ); | ||
| // If an error is found exit early and it will throw an error for request->has_valid_params(); | ||
| if ( is_wp_error( $is_valid ) ) { | ||
| return $is_valid; | ||
| } | ||
| continue; | ||
| default : | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| return $is_valid; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there are no
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😄 |
||
| } | ||
|
|
||
| /** | ||
| * Validate tax query. | ||
| * | ||
| * @param mixed $value Value of the parameter. | ||
| * @param WP_REST_Request $request Request body. | ||
| * @param string $parameter Name of the parameter. Should be tax_query. | ||
| * @param array $taxonomies Taxonomies that are to be shown in the REST API. | ||
| * @return WP_Error|boolean | ||
| */ | ||
| public function rest_validate_tax_query( $values, $request, $parameter, $taxonomies = array() ) { | ||
| if ( 'tax_query' !== $parameter ) { | ||
| return new WP_Error( 'rest_invalid_param', __( 'This validation method is only used to validate a tax query.' ), array( 'status' => 400 ) ); | ||
| } | ||
| $valid = true; | ||
| if ( ! empty( $values ) ) { | ||
| foreach ( $values as $key => $value ) { | ||
| if ( empty( $value ) ) { | ||
| //return new WP_Error( 'rest_invalid_param', __( 'A tax query parameter in the filter was empty.' ), array( 'status' => 400 ) ); | ||
| } | ||
| // Switch statement to handle different cases. | ||
| // Recursive call to function to handle nested taxonomy queries. | ||
| if ( is_array( $value ) && 'terms' !== $key ) { | ||
| $valid = $this->rest_validate_tax_query( $value, $request, $parameter, $taxonomies ); | ||
| // If an error is found keep returning it up the chain. | ||
| if ( is_wp_error( $valid ) ) { | ||
| return $valid; | ||
| } | ||
| } | ||
| if ( ! is_numeric( $key ) ) { | ||
| switch ( $key ) { | ||
| case 'taxonomy' : | ||
| // If $taxonomies is empty by default set it. On reoccurring calls the function overhead is avoided. | ||
| if ( empty( $taxonomies ) ) { | ||
| $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); | ||
| } | ||
| // Validate taxonomy. | ||
| if ( array_key_exists( $value, $taxonomies ) ) { | ||
| $valid = true; | ||
| } else { | ||
| /* translators: %s equals taxonomy name being checked. */ | ||
| return new WP_Error( 'rest_invalid_param', sprintf( __( 'The taxonomy %s specified in the filter does not exist.' ), $value ), array( 'status' => 400 ) ); | ||
| } | ||
| continue; | ||
| default : | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return $valid; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Nitpick, but for a single-case
switchI'd vote in favor of anif. (unless this is a code standard of which I was not aware)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.
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.