mirror of
git://develop.git.wordpress.org/
synced 2025-03-15 09:29:48 +01:00
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. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43766 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
583e4220ee
commit
60b2d59358
@ -325,6 +325,8 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
|
||||
$path = '/';
|
||||
}
|
||||
|
||||
$path = '/' . ltrim( $path, '/' );
|
||||
|
||||
if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
|
||||
global $wp_rewrite;
|
||||
|
||||
@ -334,7 +336,7 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
|
||||
$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
|
||||
}
|
||||
|
||||
$url .= '/' . ltrim( $path, '/' );
|
||||
$url .= $path;
|
||||
} else {
|
||||
$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
|
||||
// nginx only allows HTTP/1.0 methods when redirecting from / to /index.php
|
||||
@ -343,8 +345,6 @@ function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
|
||||
$url .= 'index.php';
|
||||
}
|
||||
|
||||
$path = '/' . ltrim( $path, '/' );
|
||||
|
||||
$url = add_query_arg( 'rest_route', $path, $url );
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ abstract class WP_Test_REST_Controller_Testcase extends WP_Test_REST_TestCase {
|
||||
abstract public function test_get_item_schema();
|
||||
|
||||
public function filter_rest_url_for_leading_slash( $url, $path ) {
|
||||
if ( is_multisite() || get_option( 'permalink_structure' ) ) {
|
||||
if ( is_multisite() ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
|
@ -488,6 +488,33 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 42452
|
||||
*/
|
||||
public function test_always_prepend_path_with_slash_in_rest_url_filter() {
|
||||
$filter = new MockAction();
|
||||
add_filter( 'rest_url', array( $filter, 'filter' ), 10, 2 );
|
||||
|
||||
// Passing no path should return a slash.
|
||||
get_rest_url();
|
||||
$args = $filter->get_args();
|
||||
$this->assertEquals( '/', $args[0][1] );
|
||||
$filter->reset();
|
||||
|
||||
// Paths without a prepended slash should have one added.
|
||||
get_rest_url( null, 'wp/media/' );
|
||||
$args = $filter->get_args();
|
||||
$this->assertEquals( '/wp/media/', $args[0][1] );
|
||||
$filter->reset();
|
||||
|
||||
// Do not modify paths with a prepended slash.
|
||||
get_rest_url( null, '/wp/media/' );
|
||||
$args = $filter->get_args();
|
||||
$this->assertEquals( '/wp/media/', $args[0][1] );
|
||||
|
||||
unset( $filter );
|
||||
}
|
||||
|
||||
public function jsonp_callback_provider() {
|
||||
return array(
|
||||
// Standard names
|
||||
|
Loading…
x
Reference in New Issue
Block a user