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.
Merges [42965] to the 5.0 branch.
Fixes #42382.

Built from https://develop.svn.wordpress.org/branches/5.0@43771


git-svn-id: http://core.svn.wordpress.org/branches/5.0@43600 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
danielbachhuber 2018-10-19 18:33:39 +00:00
parent 58ea8c82e4
commit 6c9675bf37
3 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

@ -1 +1 @@
!function(a){function b(a){return a=b.buildAjaxOptions(a),b.transport(a)}var c=window.wpApiSettings;b.buildAjaxOptions=function(b){var d,e,f,g,h,i=b.url,j=b.path;if("string"==typeof b.namespace&&"string"==typeof b.endpoint&&(d=b.namespace.replace(/^\/|\/$/g,""),e=b.endpoint.replace(/^\//,""),j=e?d+"/"+e:d),"string"==typeof j&&(i=c.root+j.replace(/^\//,"")),g=!(b.data&&b.data._wpnonce),f=b.headers||{},g)for(h in f)if(f.hasOwnProperty(h)&&"x-wp-nonce"===h.toLowerCase()){g=!1;break}return g&&(f=a.extend({"X-WP-Nonce":c.nonce},f)),b=a.extend({},b,{headers:f,url:i}),delete b.path,delete b.namespace,delete b.endpoint,b},b.transport=a.ajax,window.wp=window.wp||{},window.wp.apiRequest=b}(jQuery);
!function(a){function b(a){return a=b.buildAjaxOptions(a),b.transport(a)}var c=window.wpApiSettings;b.buildAjaxOptions=function(b){var d,e,f,g,h,i,j=b.url,k=b.path;if("string"==typeof b.namespace&&"string"==typeof b.endpoint&&(d=b.namespace.replace(/^\/|\/$/g,""),e=b.endpoint.replace(/^\//,""),k=e?d+"/"+e:d),"string"==typeof k&&(f=c.root,k=k.replace(/^\//,""),"string"==typeof f&&-1!==f.indexOf("?")&&(k=k.replace("?","&")),j=f+k),h=!(b.data&&b.data._wpnonce),g=b.headers||{},h)for(i in g)if(g.hasOwnProperty(i)&&"x-wp-nonce"===i.toLowerCase()){h=!1;break}return h&&(g=a.extend({"X-WP-Nonce":c.nonce},g)),b=a.extend({},b,{headers:g,url:j}),delete b.path,delete b.namespace,delete b.endpoint,b},b.transport=a.ajax,window.wp=window.wp||{},window.wp.apiRequest=b}(jQuery);

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-43770';
$wp_version = '5.0-alpha-43771';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.