Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: support filtering taxonomies by hierarchical attribute #11652

Merged
merged 6 commits into from
Jun 9, 2022
Merged
Changes from 1 commit
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
68 changes: 68 additions & 0 deletions includes/REST_API/Stories_Taxonomies_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Google\Web_Stories\Infrastructure\Delayed;
use Google\Web_Stories\Infrastructure\Registerable;
use Google\Web_Stories\Infrastructure\Service;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Taxonomies_Controller;
Expand Down Expand Up @@ -83,6 +84,73 @@ public function prepare_item_for_response( $taxonomy, $request ): WP_REST_Respon
return apply_filters( 'rest_prepare_taxonomy', $response, $taxonomy, $request );
}

/**
* Retrieves all public taxonomies.
*
* Adds support for filtering by the hierarchical attribute.
*
* @since 1.22.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
// Retrieve the list of registered collection query parameters.
$registered = $this->get_collection_params();

if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
$taxonomies = get_object_taxonomies( $request['type'], 'objects' );
} else {
$taxonomies = get_taxonomies( '', 'objects' );
}

if ( isset( $registered['hierarchical'], $request['hierarchical'] ) ) {
$taxonomies = wp_filter_object_list( $taxonomies, [ 'hierarchical' => (bool) $request['hierarchical'] ] );
}

$data = [];

foreach ( $taxonomies as $tax_type => $value ) {
if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
continue;
}

$tax = $this->prepare_item_for_response( $value, $request );
$tax = $this->prepare_response_for_collection( $tax );
$data[ $tax_type ] = $tax;
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
}

if ( empty( $data ) ) {
// Response should still be returned as a JSON object when it is empty.
$data = (object) $data;
}

return rest_ensure_response( $data );
}

/**
* Retrieves the query params for collections.
*
* Adds support for filtering by the hierarchical attribute.
*
* @since 1.22.0
*
* @return array Collection parameters.
*/
public function get_collection_params(): array {
$query_params = parent::get_collection_params();

$query_params['per_page']['default'] = 100;

$query_params['hierarchical'] = [
'description' => __( 'Whether to show only hierarchical taxonomies.', 'web-stories' ),
'type' => 'boolean',
'default' => false,
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
];

return $query_params;
}

/**
* Register the service.
*
Expand Down