mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-18 16:45:31 +01:00
8a23b80b56
Improve JS parsing of our inline JSDocs by introducing `@namespace`, `@lends` and `@memberOf`. Helps set the way for showing our JavaScript documentation on developer.wordpress.org, see https://meta.trac.wordpress.org/ticket/3063. * Define all used namespaces using @namespace. * Correctly specify in which namespace each class is using @memberOf. * Define each usage of the extend function as a prototype assignment using @lends. * Some comment blocks were moved to correct the parsing of certain definitions. Props herregroen, atimmer, netweb, SergeyBiryukov. Fixes #41682. Built from https://develop.svn.wordpress.org/trunk@41351 git-svn-id: http://core.svn.wordpress.org/trunk@41184 1a063a9b-81f0-0310-95a4-ce76da25c4cd
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
/**
|
|
* 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.
|
|
*
|
|
* @since 4.9.0
|
|
*/
|
|
|
|
( function( $ ) {
|
|
var wpApiSettings = window.wpApiSettings;
|
|
|
|
function apiRequest( options ) {
|
|
options = apiRequest.buildAjaxOptions( options );
|
|
return apiRequest.transport( options );
|
|
}
|
|
|
|
apiRequest.buildAjaxOptions = function( options ) {
|
|
var url = options.url;
|
|
var path = options.path;
|
|
var namespaceTrimmed, endpointTrimmed;
|
|
var headers, addNonceHeader, headerName;
|
|
|
|
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' ) {
|
|
url = wpApiSettings.root + path.replace( /^\//, '' );
|
|
}
|
|
|
|
// If ?_wpnonce=... is present, no need to add a nonce header.
|
|
addNonceHeader = ! ( options.data && options.data._wpnonce );
|
|
|
|
headers = options.headers || {};
|
|
|
|
// If an 'X-WP-Nonce' header (or any case-insensitive variation
|
|
// thereof) was specified, no need to add a nonce header.
|
|
if ( addNonceHeader ) {
|
|
for ( headerName in headers ) {
|
|
if ( headers.hasOwnProperty( headerName ) ) {
|
|
if ( headerName.toLowerCase() === 'x-wp-nonce' ) {
|
|
addNonceHeader = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( addNonceHeader ) {
|
|
// Do not mutate the original headers object, if any.
|
|
headers = $.extend( {
|
|
'X-WP-Nonce': wpApiSettings.nonce
|
|
}, headers );
|
|
}
|
|
|
|
// Do not mutate the original options object.
|
|
options = $.extend( {}, options, {
|
|
headers: headers,
|
|
url: url
|
|
} );
|
|
|
|
delete options.path;
|
|
delete options.namespace;
|
|
delete options.endpoint;
|
|
|
|
return options;
|
|
};
|
|
|
|
apiRequest.transport = $.ajax;
|
|
|
|
/** @namespace wp */
|
|
window.wp = window.wp || {};
|
|
window.wp.apiRequest = apiRequest;
|
|
} )( jQuery );
|