Skip to content

Commit da03cf1

Browse files
REST API: Use helper functions for building routes in more places.
Props get_dave, spacedmonkey. Fixes #56472. git-svn-id: https://develop.svn.wordpress.org/trunk@54121 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8f533d7 commit da03cf1

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ public function get_items( $request ) {
418418
$response->header( 'X-WP-TotalPages', (int) $max_pages );
419419

420420
$request_params = $request->get_query_params();
421-
$base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
421+
$collection_url = rest_url( rest_get_route_for_post_type_items( $this->post_type ) );
422+
$base = add_query_arg( urlencode_deep( $request_params ), $collection_url );
422423

423424
if ( $page > 1 ) {
424425
$prev_page = $page - 1;
@@ -777,7 +778,7 @@ public function create_item( $request ) {
777778
$response = rest_ensure_response( $response );
778779

779780
$response->set_status( 201 );
780-
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
781+
$response->header( 'Location', rest_url( rest_get_route_for_post( $post ) ) );
781782

782783
return $response;
783784
}
@@ -2030,15 +2031,13 @@ public function protected_title_format() {
20302031
* @return array Links for the given post.
20312032
*/
20322033
protected function prepare_links( $post ) {
2033-
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
2034-
20352034
// Entity meta.
20362035
$links = array(
20372036
'self' => array(
2038-
'href' => rest_url( trailingslashit( $base ) . $post->ID ),
2037+
'href' => rest_url( rest_get_route_for_post( $post->ID ) ),
20392038
),
20402039
'collection' => array(
2041-
'href' => rest_url( $base ),
2040+
'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ),
20422041
),
20432042
'about' => array(
20442043
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
@@ -2066,15 +2065,16 @@ protected function prepare_links( $post ) {
20662065
if ( in_array( $post->post_type, array( 'post', 'page' ), true ) || post_type_supports( $post->post_type, 'revisions' ) ) {
20672066
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
20682067
$revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
2068+
$revisions_base = sprintf( '/%s/%s/%d/revisions', $this->namespace, $this->rest_base, $post->ID );
20692069

20702070
$links['version-history'] = array(
2071-
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions' ),
2071+
'href' => rest_url( $revisions_base ),
20722072
'count' => $revisions_count,
20732073
);
20742074

20752075
if ( $revisions_count > 0 ) {
20762076
$links['predecessor-version'] = array(
2077-
'href' => rest_url( trailingslashit( $base ) . $post->ID . '/revisions/' . $revisions['latest_id'] ),
2077+
'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ),
20782078
'id' => $revisions['latest_id'],
20792079
);
20802080
}

src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ public function get_items( $request ) {
334334
$response->header( 'X-WP-TotalPages', (int) $max_pages );
335335

336336
$request_params = $request->get_query_params();
337-
$base = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ) );
337+
$base_path = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) );
338+
$base = add_query_arg( urlencode_deep( $request_params ), $base_path );
338339

339340
if ( $page > 1 ) {
340341
$prev_page = $page - 1;
@@ -620,7 +621,7 @@ public function prepare_item_for_response( $item, $request ) {
620621
$response = rest_ensure_response( $data );
621622

622623
if ( ! empty( $data['parent'] ) ) {
623-
$response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
624+
$response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) );
624625
}
625626

626627
/**

src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,12 @@ public function prepare_item_for_response( $item, $request ) { // phpcs:ignore V
687687
* @return array Links for the given post.
688688
*/
689689
protected function prepare_links( $id ) {
690-
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
691-
692690
$links = array(
693691
'self' => array(
694-
'href' => rest_url( trailingslashit( $base ) . $id ),
692+
'href' => rest_url( rest_get_route_for_post( $id ) ),
695693
),
696694
'collection' => array(
697-
'href' => rest_url( $base ),
695+
'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ),
698696
),
699697
'about' => array(
700698
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),

src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ public function get_items( $request ) {
303303

304304
$response->header( 'X-WP-TotalPages', (int) $max_pages );
305305

306-
$base = add_query_arg( urlencode_deep( $request->get_query_params() ), rest_url( $this->namespace . '/' . $this->rest_base ) );
306+
$request_params = $request->get_query_params();
307+
$collection_url = rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) );
308+
$base = add_query_arg( urlencode_deep( $request_params ), $collection_url );
309+
307310
if ( $page > 1 ) {
308311
$prev_page = $page - 1;
309312

@@ -893,13 +896,12 @@ public function prepare_item_for_response( $item, $request ) {
893896
* @return array Links for the given term.
894897
*/
895898
protected function prepare_links( $term ) {
896-
$base = $this->namespace . '/' . $this->rest_base;
897899
$links = array(
898900
'self' => array(
899-
'href' => rest_url( trailingslashit( $base ) . $term->term_id ),
901+
'href' => rest_url( rest_get_route_for_term( $term ) ),
900902
),
901903
'collection' => array(
902-
'href' => rest_url( $base ),
904+
'href' => rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) ),
903905
),
904906
'about' => array(
905907
'href' => rest_url( sprintf( 'wp/v2/taxonomies/%s', $this->taxonomy ) ),
@@ -911,7 +913,7 @@ protected function prepare_links( $term ) {
911913

912914
if ( $parent_term ) {
913915
$links['up'] = array(
914-
'href' => rest_url( trailingslashit( $base ) . $parent_term->term_id ),
916+
'href' => rest_url( rest_get_route_for_term( $parent_term ) ),
915917
'embeddable' => true,
916918
);
917919
}

0 commit comments

Comments
 (0)