Make WordPress Core

Changeset 43766


Ignore:
Timestamp:
10/19/2018 11:13:57 AM (6 years ago)
Author:
danielbachhuber
Message:

REST API: Ensure rest_url() consistently has leading slash.

rest_url() inconsistent addes slashes to the passed path depending on whether the site has pretty permalinks enabled. Apart from being inconsistent, this also caused the unit tests to fail when pretty permalinks are enabled.

Props frank-klein.
Merges [42250] to the 5.0 branch.
Partially reverts [43720].
Fixes #42452. See #41451, #45017.

Location:
branches/5.0
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/src/wp-includes/rest-api.php

    r43763 r43766  
    326326    }
    327327
     328    $path = '/' . ltrim( $path, '/' );
     329
    328330    if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
    329331        global $wp_rewrite;
     
    335337        }
    336338
    337         $url .= '/' . ltrim( $path, '/' );
     339        $url .= $path;
    338340    } else {
    339341        $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
     
    343345            $url .= 'index.php';
    344346        }
    345 
    346         $path = '/' . ltrim( $path, '/' );
    347347
    348348        $url = add_query_arg( 'rest_route', $path, $url );
  • branches/5.0/tests/phpunit/includes/testcase-rest-controller.php

    r43720 r43766  
    4141
    4242    public function filter_rest_url_for_leading_slash( $url, $path ) {
    43         if ( is_multisite() || get_option( 'permalink_structure' ) ) {
     43        if ( is_multisite() ) {
    4444            return $url;
    4545        }
  • branches/5.0/tests/phpunit/tests/rest-api.php

    r43763 r43766  
    487487        set_current_screen( 'front' );
    488488
     489    }
     490
     491    /**
     492     * @ticket 42452
     493     */
     494    public function test_always_prepend_path_with_slash_in_rest_url_filter() {
     495        $filter = new MockAction();
     496        add_filter( 'rest_url', array( $filter, 'filter' ), 10, 2 );
     497
     498        // Passing no path should return a slash.
     499        get_rest_url();
     500        $args = $filter->get_args();
     501        $this->assertEquals( '/', $args[0][1] );
     502        $filter->reset();
     503
     504        // Paths without a prepended slash should have one added.
     505        get_rest_url( null, 'wp/media/' );
     506        $args = $filter->get_args();
     507        $this->assertEquals( '/wp/media/', $args[0][1] );
     508        $filter->reset();
     509
     510        // Do not modify paths with a prepended slash.
     511        get_rest_url( null, '/wp/media/' );
     512        $args = $filter->get_args();
     513        $this->assertEquals( '/wp/media/', $args[0][1] );
     514
     515        unset( $filter );
    489516    }
    490517
Note: See TracChangeset for help on using the changeset viewer.