wp/wp-includes/js/api-request.js
changeset 18 be944660c56a
parent 16 a86126ab1dd4
--- a/wp/wp-includes/js/api-request.js	Tue Dec 15 15:52:01 2020 +0100
+++ b/wp/wp-includes/js/api-request.js	Wed Sep 21 18:19:35 2022 +0200
@@ -9,6 +9,8 @@
  * - Allows specifying only an endpoint namespace/path instead of a full URL.
  *
  * @since 4.9.0
+ * @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST".
+ *              Added an "application/json" Accept header to all requests.
  * @output wp-includes/js/api-request.js
  */
 
@@ -23,8 +25,9 @@
 	apiRequest.buildAjaxOptions = function( options ) {
 		var url = options.url;
 		var path = options.path;
+		var method = options.method;
 		var namespaceTrimmed, endpointTrimmed, apiRoot;
-		var headers, addNonceHeader, headerName;
+		var headers, addNonceHeader, addAcceptHeader, headerName;
 
 		if (
 			typeof options.namespace === 'string' &&
@@ -53,19 +56,24 @@
 
 		// If ?_wpnonce=... is present, no need to add a nonce header.
 		addNonceHeader = ! ( options.data && options.data._wpnonce );
+		addAcceptHeader = true;
 
 		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;
-					}
-				}
+		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;
 			}
 		}
 
@@ -76,10 +84,29 @@
 			}, headers );
 		}
 
+		if ( addAcceptHeader ) {
+			headers = $.extend( {
+				'Accept': 'application/json, */*;q=0.1'
+			}, headers );
+		}
+
+		if ( typeof method === 'string' ) {
+			method = method.toUpperCase();
+
+			if ( 'PUT' === method || 'DELETE' === method ) {
+				headers = $.extend( {
+					'X-HTTP-Method-Override': method
+				}, headers );
+
+				method = 'POST';
+			}
+		}
+
 		// Do not mutate the original options object.
 		options = $.extend( {}, options, {
 			headers: headers,
-			url: url
+			url: url,
+			method: method
 		} );
 
 		delete options.path;