From 6c9675bf3743e4eba58ab8bddd492320f53279be Mon Sep 17 00:00:00 2001 From: danielbachhuber Date: Fri, 19 Oct 2018 18:33:39 +0000 Subject: [PATCH] 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 --- wp-includes/js/api-request.js | 13 +++++++++++-- wp-includes/js/api-request.min.js | 2 +- wp-includes/version.php | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/wp-includes/js/api-request.js b/wp-includes/js/api-request.js index f0a84ef48f..4b299beb69 100644 --- a/wp-includes/js/api-request.js +++ b/wp-includes/js/api-request.js @@ -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. diff --git a/wp-includes/js/api-request.min.js b/wp-includes/js/api-request.min.js index 1776661e81..527a55fa23 100644 --- a/wp-includes/js/api-request.min.js +++ b/wp-includes/js/api-request.min.js @@ -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); \ No newline at end of file +!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); \ No newline at end of file diff --git a/wp-includes/version.php b/wp-includes/version.php index d4a950a492..0f53aca1dc 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.