2017-08-02 18:45:44 +02:00
|
|
|
/**
|
|
|
|
* Thin jQuery.ajax wrapper for WP REST API requests.
|
|
|
|
*
|
|
|
|
* Currently only applies to requests that do not use the `wp-api.js` Backbone
|
|
|
|
* client library, though this may change. Serves several purposes:
|
|
|
|
*
|
|
|
|
* - Allows overriding these requests as needed by customized WP installations.
|
|
|
|
* - Sends the REST API nonce as a request header.
|
|
|
|
* - Allows specifying only an endpoint namespace/path instead of a full URL.
|
|
|
|
*
|
2020-01-29 01:45:18 +01:00
|
|
|
* @since 4.9.0
|
2020-10-12 22:11:06 +02:00
|
|
|
* @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST".
|
2020-12-01 04:44:05 +01:00
|
|
|
* Added an "application/json" Accept header to all requests.
|
2018-06-28 04:30:15 +02:00
|
|
|
* @output wp-includes/js/api-request.js
|
2017-08-02 18:45:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
( function( $ ) {
|
|
|
|
var wpApiSettings = window.wpApiSettings;
|
|
|
|
|
|
|
|
function apiRequest( options ) {
|
|
|
|
options = apiRequest.buildAjaxOptions( options );
|
|
|
|
return apiRequest.transport( options );
|
2017-08-02 19:16:44 +02:00
|
|
|
}
|
2017-08-02 18:45:44 +02:00
|
|
|
|
|
|
|
apiRequest.buildAjaxOptions = function( options ) {
|
|
|
|
var url = options.url;
|
|
|
|
var path = options.path;
|
2020-10-12 22:11:06 +02:00
|
|
|
var method = options.method;
|
2018-04-09 15:10:31 +02:00
|
|
|
var namespaceTrimmed, endpointTrimmed, apiRoot;
|
2020-12-01 04:44:05 +01:00
|
|
|
var headers, addNonceHeader, addAcceptHeader, headerName;
|
2017-08-02 18:45:44 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
typeof options.namespace === 'string' &&
|
|
|
|
typeof options.endpoint === 'string'
|
|
|
|
) {
|
|
|
|
namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' );
|
|
|
|
endpointTrimmed = options.endpoint.replace( /^\//, '' );
|
|
|
|
if ( endpointTrimmed ) {
|
|
|
|
path = namespaceTrimmed + '/' + endpointTrimmed;
|
|
|
|
} else {
|
|
|
|
path = namespaceTrimmed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( typeof path === 'string' ) {
|
2018-04-09 15:10:31 +02:00
|
|
|
apiRoot = wpApiSettings.root;
|
|
|
|
path = path.replace( /^\//, '' );
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// API root may already include query parameter prefix
|
|
|
|
// if site is configured to use plain permalinks.
|
2018-04-09 15:10:31 +02:00
|
|
|
if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) {
|
|
|
|
path = path.replace( '?', '&' );
|
|
|
|
}
|
|
|
|
|
|
|
|
url = apiRoot + path;
|
2017-08-02 18:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If ?_wpnonce=... is present, no need to add a nonce header.
|
|
|
|
addNonceHeader = ! ( options.data && options.data._wpnonce );
|
2020-12-01 04:44:05 +01:00
|
|
|
addAcceptHeader = true;
|
2017-08-02 18:45:44 +02:00
|
|
|
|
|
|
|
headers = options.headers || {};
|
|
|
|
|
2020-12-01 04:44:05 +01:00
|
|
|
for ( headerName in headers ) {
|
|
|
|
if ( ! headers.hasOwnProperty( headerName ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an 'X-WP-Nonce' or 'Accept' header (or any case-insensitive variation
|
|
|
|
// thereof) was specified, no need to add the header again.
|
|
|
|
switch ( headerName.toLowerCase() ) {
|
|
|
|
case 'x-wp-nonce':
|
|
|
|
addNonceHeader = false;
|
|
|
|
break;
|
|
|
|
case 'accept':
|
|
|
|
addAcceptHeader = false;
|
|
|
|
break;
|
2017-08-02 18:45:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( addNonceHeader ) {
|
|
|
|
// Do not mutate the original headers object, if any.
|
|
|
|
headers = $.extend( {
|
|
|
|
'X-WP-Nonce': wpApiSettings.nonce
|
|
|
|
}, headers );
|
|
|
|
}
|
|
|
|
|
2020-12-01 04:44:05 +01:00
|
|
|
if ( addAcceptHeader ) {
|
|
|
|
headers = $.extend( {
|
|
|
|
'Accept': 'application/json, */*;q=0.1'
|
|
|
|
}, headers );
|
|
|
|
}
|
|
|
|
|
2020-10-12 22:11:06 +02:00
|
|
|
if ( typeof method === 'string' ) {
|
|
|
|
method = method.toUpperCase();
|
|
|
|
|
|
|
|
if ( 'PUT' === method || 'DELETE' === method ) {
|
|
|
|
headers = $.extend( {
|
|
|
|
'X-HTTP-Method-Override': method
|
|
|
|
}, headers );
|
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:45:44 +02:00
|
|
|
// Do not mutate the original options object.
|
|
|
|
options = $.extend( {}, options, {
|
|
|
|
headers: headers,
|
2020-10-12 22:11:06 +02:00
|
|
|
url: url,
|
|
|
|
method: method
|
2017-08-02 18:45:44 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
delete options.path;
|
|
|
|
delete options.namespace;
|
|
|
|
delete options.endpoint;
|
|
|
|
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
apiRequest.transport = $.ajax;
|
|
|
|
|
2017-09-08 20:42:49 +02:00
|
|
|
/** @namespace wp */
|
2017-08-02 18:45:44 +02:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.apiRequest = apiRequest;
|
|
|
|
} )( jQuery );
|