Edit File by line
/home/barbar84/public_h.../wp-inclu.../js
File: api-request.js
/**
[0] Fix | Delete
* Thin jQuery.ajax wrapper for WP REST API requests.
[1] Fix | Delete
*
[2] Fix | Delete
* Currently only applies to requests that do not use the `wp-api.js` Backbone
[3] Fix | Delete
* client library, though this may change. Serves several purposes:
[4] Fix | Delete
*
[5] Fix | Delete
* - Allows overriding these requests as needed by customized WP installations.
[6] Fix | Delete
* - Sends the REST API nonce as a request header.
[7] Fix | Delete
* - Allows specifying only an endpoint namespace/path instead of a full URL.
[8] Fix | Delete
*
[9] Fix | Delete
* @since 4.9.0
[10] Fix | Delete
* @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST".
[11] Fix | Delete
* Added an "application/json" Accept header to all requests.
[12] Fix | Delete
* @output wp-includes/js/api-request.js
[13] Fix | Delete
*/
[14] Fix | Delete
[15] Fix | Delete
( function( $ ) {
[16] Fix | Delete
var wpApiSettings = window.wpApiSettings;
[17] Fix | Delete
[18] Fix | Delete
function apiRequest( options ) {
[19] Fix | Delete
options = apiRequest.buildAjaxOptions( options );
[20] Fix | Delete
return apiRequest.transport( options );
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
apiRequest.buildAjaxOptions = function( options ) {
[24] Fix | Delete
var url = options.url;
[25] Fix | Delete
var path = options.path;
[26] Fix | Delete
var method = options.method;
[27] Fix | Delete
var namespaceTrimmed, endpointTrimmed, apiRoot;
[28] Fix | Delete
var headers, addNonceHeader, addAcceptHeader, headerName;
[29] Fix | Delete
[30] Fix | Delete
if (
[31] Fix | Delete
typeof options.namespace === 'string' &&
[32] Fix | Delete
typeof options.endpoint === 'string'
[33] Fix | Delete
) {
[34] Fix | Delete
namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' );
[35] Fix | Delete
endpointTrimmed = options.endpoint.replace( /^\//, '' );
[36] Fix | Delete
if ( endpointTrimmed ) {
[37] Fix | Delete
path = namespaceTrimmed + '/' + endpointTrimmed;
[38] Fix | Delete
} else {
[39] Fix | Delete
path = namespaceTrimmed;
[40] Fix | Delete
}
[41] Fix | Delete
}
[42] Fix | Delete
if ( typeof path === 'string' ) {
[43] Fix | Delete
apiRoot = wpApiSettings.root;
[44] Fix | Delete
path = path.replace( /^\//, '' );
[45] Fix | Delete
[46] Fix | Delete
// API root may already include query parameter prefix
[47] Fix | Delete
// if site is configured to use plain permalinks.
[48] Fix | Delete
if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) {
[49] Fix | Delete
path = path.replace( '?', '&' );
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
url = apiRoot + path;
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
// If ?_wpnonce=... is present, no need to add a nonce header.
[56] Fix | Delete
addNonceHeader = ! ( options.data && options.data._wpnonce );
[57] Fix | Delete
addAcceptHeader = true;
[58] Fix | Delete
[59] Fix | Delete
headers = options.headers || {};
[60] Fix | Delete
[61] Fix | Delete
for ( headerName in headers ) {
[62] Fix | Delete
if ( ! headers.hasOwnProperty( headerName ) ) {
[63] Fix | Delete
continue;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
// If an 'X-WP-Nonce' or 'Accept' header (or any case-insensitive variation
[67] Fix | Delete
// thereof) was specified, no need to add the header again.
[68] Fix | Delete
switch ( headerName.toLowerCase() ) {
[69] Fix | Delete
case 'x-wp-nonce':
[70] Fix | Delete
addNonceHeader = false;
[71] Fix | Delete
break;
[72] Fix | Delete
case 'accept':
[73] Fix | Delete
addAcceptHeader = false;
[74] Fix | Delete
break;
[75] Fix | Delete
}
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
if ( addNonceHeader ) {
[79] Fix | Delete
// Do not mutate the original headers object, if any.
[80] Fix | Delete
headers = $.extend( {
[81] Fix | Delete
'X-WP-Nonce': wpApiSettings.nonce
[82] Fix | Delete
}, headers );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
if ( addAcceptHeader ) {
[86] Fix | Delete
headers = $.extend( {
[87] Fix | Delete
'Accept': 'application/json, */*;q=0.1'
[88] Fix | Delete
}, headers );
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
if ( typeof method === 'string' ) {
[92] Fix | Delete
method = method.toUpperCase();
[93] Fix | Delete
[94] Fix | Delete
if ( 'PUT' === method || 'DELETE' === method ) {
[95] Fix | Delete
headers = $.extend( {
[96] Fix | Delete
'X-HTTP-Method-Override': method
[97] Fix | Delete
}, headers );
[98] Fix | Delete
[99] Fix | Delete
method = 'POST';
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
// Do not mutate the original options object.
[104] Fix | Delete
options = $.extend( {}, options, {
[105] Fix | Delete
headers: headers,
[106] Fix | Delete
url: url,
[107] Fix | Delete
method: method
[108] Fix | Delete
} );
[109] Fix | Delete
[110] Fix | Delete
delete options.path;
[111] Fix | Delete
delete options.namespace;
[112] Fix | Delete
delete options.endpoint;
[113] Fix | Delete
[114] Fix | Delete
return options;
[115] Fix | Delete
};
[116] Fix | Delete
[117] Fix | Delete
apiRequest.transport = $.ajax;
[118] Fix | Delete
[119] Fix | Delete
/** @namespace wp */
[120] Fix | Delete
window.wp = window.wp || {};
[121] Fix | Delete
window.wp.apiRequest = apiRequest;
[122] Fix | Delete
} )( jQuery );
[123] Fix | Delete
[124] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function