REST API: Handle api-request query parameters with plain permalinks.

When constructing the request URL, ensure that  `?` is replaced with `&` when the API root  already contains a `?`. Fixes an issue where requests were broken when sites had permalinks set to plain.

Props aduth.
Fixes #42382.



git-svn-id: https://develop.svn.wordpress.org/trunk@42965 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein 2018-04-09 13:09:41 +00:00
parent 531abcbdd4
commit 495ac15555
2 changed files with 13 additions and 4 deletions

View File

@ -22,7 +22,7 @@
apiRequest.buildAjaxOptions = function( options ) {
var url = options.url;
var path = options.path;
var namespaceTrimmed, endpointTrimmed;
var namespaceTrimmed, endpointTrimmed, apiRoot;
var headers, addNonceHeader, headerName;
if (
@ -38,7 +38,16 @@
}
}
if ( typeof path === 'string' ) {
url = wpApiSettings.root + path.replace( /^\//, '' );
apiRoot = wpApiSettings.root;
path = path.replace( /^\//, '' );
// API root may already include query parameter prefix if site is
// configured to use plain permalinks.
if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) {
path = path.replace( '?', '&' );
}
url = apiRoot + path;
}
// If ?_wpnonce=... is present, no need to add a nonce header.

View File

@ -140,9 +140,9 @@
window.wpApiSettings.root = 'http://localhost/index.php?rest_route=/';
assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
namespace: '/wp/v2/',
endpoint: '/posts'
endpoint: '/posts?orderby=title'
} ), {
url: 'http://localhost/index.php?rest_route=/wp/v2/posts',
url: 'http://localhost/index.php?rest_route=/wp/v2/posts&orderby=title',
headers: nonceHeader
} );
}