2017-08-02 12:45:44 -04: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-28 19:45:18 -05:00
|
|
|
* @since 4.9.0
|
2020-10-12 16:11:06 -04:00
|
|
|
* @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST".
|
2020-11-30 22:44:05 -05:00
|
|
|
* Added an "application/json" Accept header to all requests.
|
2018-06-27 22:30:15 -04:00
|
|
|
* @output wp-includes/js/api-request.js
|
2017-08-02 12:45:44 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
( function( $ ) {
|
|
|
|
var wpApiSettings = window.wpApiSettings;
|
|
|
|
|
|
|
|
function apiRequest( options ) {
|
|
|
|
options = apiRequest.buildAjaxOptions( options );
|
|
|
|
return apiRequest.transport( options );
|
2017-08-02 13:16:44 -04:00
|
|
|
}
|
2017-08-02 12:45:44 -04:00
|
|
|
|
|
|
|
apiRequest.buildAjaxOptions = function( options ) {
|
|
|
|
var url = options.url;
|
|
|
|
var path = options.path;
|
2020-10-12 16:11:06 -04:00
|
|
|
var method = options.method;
|
2018-04-09 09:10:31 -04:00
|
|
|
var namespaceTrimmed, endpointTrimmed, apiRoot;
|
2020-11-30 22:44:05 -05:00
|
|
|
var headers, addNonceHeader, addAcceptHeader, headerName;
|
2017-08-02 12:45:44 -04: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 09:10:31 -04:00
|
|
|
apiRoot = wpApiSettings.root;
|
|
|
|
path = path.replace( /^\//, '' );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// API root may already include query parameter prefix
|
|
|
|
// if site is configured to use plain permalinks.
|
2018-04-09 09:10:31 -04:00
|
|
|
if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) {
|
|
|
|
path = path.replace( '?', '&' );
|
|
|
|
}
|
|
|
|
|
|
|
|
url = apiRoot + path;
|
2017-08-02 12:45:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If ?_wpnonce=... is present, no need to add a nonce header.
|
|
|
|
addNonceHeader = ! ( options.data && options.data._wpnonce );
|
2020-11-30 22:44:05 -05:00
|
|
|
addAcceptHeader = true;
|
2017-08-02 12:45:44 -04:00
|
|
|
|
|
|
|
headers = options.headers || {};
|
|
|
|
|
2020-11-30 22:44:05 -05: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 12:45:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( addNonceHeader ) {
|
|
|
|
// Do not mutate the original headers object, if any.
|
|
|
|
headers = $.extend( {
|
|
|
|
'X-WP-Nonce': wpApiSettings.nonce
|
|
|
|
}, headers );
|
|
|
|
}
|
|
|
|
|
2020-11-30 22:44:05 -05:00
|
|
|
if ( addAcceptHeader ) {
|
|
|
|
headers = $.extend( {
|
|
|
|
'Accept': 'application/json, */*;q=0.1'
|
|
|
|
}, headers );
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:11:06 -04: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 12:45:44 -04:00
|
|
|
// Do not mutate the original options object.
|
|
|
|
options = $.extend( {}, options, {
|
|
|
|
headers: headers,
|
2020-10-12 16:11:06 -04:00
|
|
|
url: url,
|
|
|
|
method: method
|
2017-08-02 12:45:44 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
delete options.path;
|
|
|
|
delete options.namespace;
|
|
|
|
delete options.endpoint;
|
|
|
|
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
apiRequest.transport = $.ajax;
|
|
|
|
|
2017-09-08 14:42:49 -04:00
|
|
|
/** @namespace wp */
|
2017-08-02 12:45:44 -04:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
window.wp.apiRequest = apiRequest;
|
|
|
|
} )( jQuery );
|