Skip to content

Commit

Permalink
adds links to terms search results
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Jun 18, 2020
1 parent f558221 commit 029c768
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion lib/class-wp-rest-term-search-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,48 @@ public function prepare_item( $id, array $fields ) {
* @return array Links for the given item.
*/
public function prepare_item_links( $id ) {
return array();
$term = get_term( $id );

$links = array();

$item_route = $this->detect_rest_item_route( $term );
if ( ! empty( $item_route ) ) {
$links['self'] = array(
'href' => rest_url( $item_route ),
'embeddable' => true,
);
}

$links['about'] = array(
'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $term->taxonomy ) ),
);

return $links;
}

/**
* Attempts to detect the route to access a single item.
*
* @since 5.0.0
*
* @param WP_Post $post Term object.
* @return string REST route relative to the REST base URI, or empty string if unknown.
*/
protected function detect_rest_item_route( $term ) {
$taxonomy = get_taxonomy( $term->taxonomy );
if ( ! $taxonomy ) {
return '';
}

// It's currently impossible to detect the REST URL from a custom controller.
if ( ! empty( $taxonomy->rest_controller_class ) && 'WP_REST_Terms_Controller' !== $taxonomy->rest_controller_class ) {
return '';
}

$namespace = 'wp/v2';
$rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

return sprintf( '%s/%s/%d', $namespace, $rest_base, $term->term_id );
}

}

0 comments on commit 029c768

Please sign in to comment.