REST API: Use new `rest_get_route_for_post()` in the post search handler.

In [48273] the rest_get_route_for_post function was introduced. This simplifies determining the REST API route for any given post object and adds a filter for custom controllers.

Props ravatparmar.
Fixes #50529.


git-svn-id: https://develop.svn.wordpress.org/trunk@48381 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs 2020-07-07 15:29:22 +00:00
parent f4b381b640
commit 9c4288687e
1 changed files with 5 additions and 14 deletions

View File

@ -148,7 +148,7 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
$links = array();
$item_route = $this->detect_rest_item_route( $post );
$item_route = rest_get_route_for_post( $post );
if ( ! empty( $item_route ) ) {
$links['self'] = array(
'href' => rest_url( $item_route ),
@ -182,25 +182,16 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
* Attempts to detect the route to access a single item.
*
* @since 5.0.0
* @deprecated 5.5.0 Use rest_get_route_for_post()
* @see rest_get_route_for_post()
*
* @param WP_Post $post Post object.
* @return string REST route relative to the REST base URI, or empty string if unknown.
*/
protected function detect_rest_item_route( $post ) {
$post_type = get_post_type_object( $post->post_type );
if ( ! $post_type ) {
return '';
}
_deprecated_function( __METHOD__, '5.5.0', 'rest_get_route_for_post()' );
// It's currently impossible to detect the REST URL from a custom controller.
if ( ! empty( $post_type->rest_controller_class ) && 'WP_REST_Posts_Controller' !== $post_type->rest_controller_class ) {
return '';
}
$namespace = 'wp/v2';
$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
return sprintf( '%s/%s/%d', $namespace, $rest_base, $post->ID );
return rest_get_route_for_post( $post );
}
}