7 * - Allows overriding these requests as needed by customized WP installations. |
7 * - Allows overriding these requests as needed by customized WP installations. |
8 * - Sends the REST API nonce as a request header. |
8 * - Sends the REST API nonce as a request header. |
9 * - Allows specifying only an endpoint namespace/path instead of a full URL. |
9 * - Allows specifying only an endpoint namespace/path instead of a full URL. |
10 * |
10 * |
11 * @since 4.9.0 |
11 * @since 4.9.0 |
|
12 * @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST". |
|
13 * Added an "application/json" Accept header to all requests. |
12 * @output wp-includes/js/api-request.js |
14 * @output wp-includes/js/api-request.js |
13 */ |
15 */ |
14 |
16 |
15 ( function( $ ) { |
17 ( function( $ ) { |
16 var wpApiSettings = window.wpApiSettings; |
18 var wpApiSettings = window.wpApiSettings; |
21 } |
23 } |
22 |
24 |
23 apiRequest.buildAjaxOptions = function( options ) { |
25 apiRequest.buildAjaxOptions = function( options ) { |
24 var url = options.url; |
26 var url = options.url; |
25 var path = options.path; |
27 var path = options.path; |
|
28 var method = options.method; |
26 var namespaceTrimmed, endpointTrimmed, apiRoot; |
29 var namespaceTrimmed, endpointTrimmed, apiRoot; |
27 var headers, addNonceHeader, headerName; |
30 var headers, addNonceHeader, addAcceptHeader, headerName; |
28 |
31 |
29 if ( |
32 if ( |
30 typeof options.namespace === 'string' && |
33 typeof options.namespace === 'string' && |
31 typeof options.endpoint === 'string' |
34 typeof options.endpoint === 'string' |
32 ) { |
35 ) { |
51 url = apiRoot + path; |
54 url = apiRoot + path; |
52 } |
55 } |
53 |
56 |
54 // If ?_wpnonce=... is present, no need to add a nonce header. |
57 // If ?_wpnonce=... is present, no need to add a nonce header. |
55 addNonceHeader = ! ( options.data && options.data._wpnonce ); |
58 addNonceHeader = ! ( options.data && options.data._wpnonce ); |
|
59 addAcceptHeader = true; |
56 |
60 |
57 headers = options.headers || {}; |
61 headers = options.headers || {}; |
58 |
62 |
59 // If an 'X-WP-Nonce' header (or any case-insensitive variation |
63 for ( headerName in headers ) { |
60 // thereof) was specified, no need to add a nonce header. |
64 if ( ! headers.hasOwnProperty( headerName ) ) { |
61 if ( addNonceHeader ) { |
65 continue; |
62 for ( headerName in headers ) { |
66 } |
63 if ( headers.hasOwnProperty( headerName ) ) { |
67 |
64 if ( headerName.toLowerCase() === 'x-wp-nonce' ) { |
68 // If an 'X-WP-Nonce' or 'Accept' header (or any case-insensitive variation |
65 addNonceHeader = false; |
69 // thereof) was specified, no need to add the header again. |
66 break; |
70 switch ( headerName.toLowerCase() ) { |
67 } |
71 case 'x-wp-nonce': |
68 } |
72 addNonceHeader = false; |
|
73 break; |
|
74 case 'accept': |
|
75 addAcceptHeader = false; |
|
76 break; |
69 } |
77 } |
70 } |
78 } |
71 |
79 |
72 if ( addNonceHeader ) { |
80 if ( addNonceHeader ) { |
73 // Do not mutate the original headers object, if any. |
81 // Do not mutate the original headers object, if any. |
74 headers = $.extend( { |
82 headers = $.extend( { |
75 'X-WP-Nonce': wpApiSettings.nonce |
83 'X-WP-Nonce': wpApiSettings.nonce |
76 }, headers ); |
84 }, headers ); |
77 } |
85 } |
78 |
86 |
|
87 if ( addAcceptHeader ) { |
|
88 headers = $.extend( { |
|
89 'Accept': 'application/json, */*;q=0.1' |
|
90 }, headers ); |
|
91 } |
|
92 |
|
93 if ( typeof method === 'string' ) { |
|
94 method = method.toUpperCase(); |
|
95 |
|
96 if ( 'PUT' === method || 'DELETE' === method ) { |
|
97 headers = $.extend( { |
|
98 'X-HTTP-Method-Override': method |
|
99 }, headers ); |
|
100 |
|
101 method = 'POST'; |
|
102 } |
|
103 } |
|
104 |
79 // Do not mutate the original options object. |
105 // Do not mutate the original options object. |
80 options = $.extend( {}, options, { |
106 options = $.extend( {}, options, { |
81 headers: headers, |
107 headers: headers, |
82 url: url |
108 url: url, |
|
109 method: method |
83 } ); |
110 } ); |
84 |
111 |
85 delete options.path; |
112 delete options.path; |
86 delete options.namespace; |
113 delete options.namespace; |
87 delete options.endpoint; |
114 delete options.endpoint; |