From 495ac15555f569b92354a200f324d82001b3d062 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 9 Apr 2018 13:09:41 +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. Fixes #42382. git-svn-id: https://develop.svn.wordpress.org/trunk@42965 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/api-request.js | 13 +++++++++++-- tests/qunit/wp-includes/js/api-request.js | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/js/api-request.js b/src/wp-includes/js/api-request.js index f0a84ef48f..4b299beb69 100644 --- a/src/wp-includes/js/api-request.js +++ b/src/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/tests/qunit/wp-includes/js/api-request.js b/tests/qunit/wp-includes/js/api-request.js index 99a1213ea3..86d0d72cda 100644 --- a/tests/qunit/wp-includes/js/api-request.js +++ b/tests/qunit/wp-includes/js/api-request.js @@ -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 } ); }