aboutsummaryrefslogtreecommitdiff
path: root/srcs/wordpress/wp-includes/js/api-request.js
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-07 13:06:14 +0100
committerCharles <sircharlesaze@gmail.com>2020-01-07 13:06:14 +0100
commit7086111ad4dd997e12a3220e1ee60c9b9bcf0bb8 (patch)
treef7453d7dd5cbaaab246e23810b02d3edf1e451be /srcs/wordpress/wp-includes/js/api-request.js
parentc59bdcf77c50cbe89b4a93782cdd6d9e7532080e (diff)
downloadft_server-7086111ad4dd997e12a3220e1ee60c9b9bcf0bb8.tar.gz
ft_server-7086111ad4dd997e12a3220e1ee60c9b9bcf0bb8.tar.bz2
ft_server-7086111ad4dd997e12a3220e1ee60c9b9bcf0bb8.zip
Added wordpress
Diffstat (limited to 'srcs/wordpress/wp-includes/js/api-request.js')
-rw-r--r--srcs/wordpress/wp-includes/js/api-request.js97
1 files changed, 97 insertions, 0 deletions
diff --git a/srcs/wordpress/wp-includes/js/api-request.js b/srcs/wordpress/wp-includes/js/api-request.js
new file mode 100644
index 0000000..3ed1c68
--- /dev/null
+++ b/srcs/wordpress/wp-includes/js/api-request.js
@@ -0,0 +1,97 @@
+/**
+ * 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
+ * @output wp-includes/js/api-request.js
+ */
+
+( 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, apiRoot;
+ 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' ) {
+ 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.
+ 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 );