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