Make WordPress Core

Changeset 54121


Ignore:
Timestamp:
09/11/2022 06:53:28 PM (18 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Use helper functions for building routes in more places.

Props get_dave, spacedmonkey.
Fixes #56472.

Location:
trunk/src/wp-includes/rest-api/endpoints
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r53841 r54121  
    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 ) {
     
    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;
     
    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(
     
    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            );
     
    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                );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r51962 r54121  
    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 ) {
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r53760 r54121  
    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(
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r53760 r54121  
    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;
     
    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(
     
    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                );
Note: See TracChangeset for help on using the changeset viewer.