Make WordPress Core


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

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

Props get_dave, spacedmonkey.
Fixes #56472.

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