| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 /** 2 * @output wp-includes/js/api-request.js 3 */ 4 5 /** 6 * Thin jQuery.ajax wrapper for WP REST API requests. 7 * 8 * Currently only applies to requests that do not use the `wp-api.js` Backbone 9 * client library, though this may change. Serves several purposes: 10 * 11 * - Allows overriding these requests as needed by customized WP installations. 12 * - Sends the REST API nonce as a request header. 13 * - Allows specifying only an endpoint namespace/path instead of a full URL. 14 * 15 * @since 4.9.0 16 * @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST". 17 * Added an "application/json" Accept header to all requests. 18 * 19 * @param {JQueryStatic} $ The jQuery object. 20 */ 21 ( function( $ ) { 22 /** 23 * Makes an API request. 24 * 25 * @param {Object} options The options for the API request. 26 * @param {string} [options.url] The full URL for the request. 27 * @param {string} [options.path] The API path (e.g., 'wp/v2/posts'). Used if url is not provided. 28 * @param {string} [options.namespace] The API namespace (e.g., 'wp/v2'). Used with endpoint to build path. 29 * @param {string} [options.endpoint] The API endpoint (e.g., 'posts'). Used with namespace to build path. 30 * @param {string} [options.method] The HTTP method (GET, POST, PUT, DELETE, etc.). 31 * @param {Object} [options.data] The request data to send. 32 * @param {string} [options.data._wpnonce] Nonce for security. 33 * @param {JQuery.PlainObject<string | null | undefined>} [options.headers] Custom headers to include in the request. 34 * @return {JQueryXHR} The jQuery XHR object. 35 */ 36 function apiRequest( options ) { 37 return apiRequest.transport( apiRequest.buildAjaxOptions( options ) ); 38 } 39 40 /** 41 * Builds the options for the jQuery.ajax request. 42 * 43 * @param {Object} options The options for the API request. 44 * @param {string} [options.url] The full URL for the request. 45 * @param {string} [options.path] The API path (e.g., 'wp/v2/posts'). Used if url is not provided. 46 * @param {string} [options.namespace] The API namespace (e.g., 'wp/v2'). Used with endpoint to build path. 47 * @param {string} [options.endpoint] The API endpoint (e.g., 'posts'). Used with namespace to build path. 48 * @param {string} [options.method] The HTTP method (GET, POST, PUT, DELETE, etc.). 49 * @param {Object} [options.data] The request data to send. 50 * @param {string} [options.data._wpnonce] Nonce for security. 51 * @param {JQuery.PlainObject<string | null | undefined>} [options.headers] Custom headers to include in the request. 52 * @return {JQuery.AjaxSettings} The options for the jQuery.ajax request. 53 * @throws {Error} If `wpApiSettings.root` is missing when `path` or `namespace`/`endpoint` is used. 54 */ 55 apiRequest.buildAjaxOptions = function( options ) { 56 var wpApiSettings = window.wpApiSettings; 57 var url = options.url; 58 var path = options.path; 59 var method = options.method; 60 var namespaceTrimmed, endpointTrimmed, apiRoot; 61 var headers, addNonceHeader, addAcceptHeader, headerName; 62 63 if ( 64 typeof options.namespace === 'string' && 65 typeof options.endpoint === 'string' 66 ) { 67 namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' ); 68 endpointTrimmed = options.endpoint.replace( /^\//, '' ); 69 if ( endpointTrimmed ) { 70 path = namespaceTrimmed + '/' + endpointTrimmed; 71 } else { 72 path = namespaceTrimmed; 73 } 74 } 75 if ( typeof path === 'string' ) { 76 apiRoot = wpApiSettings && typeof wpApiSettings.root === 'string' ? wpApiSettings.root : ''; 77 if ( ! apiRoot ) { 78 throw new Error( 'wpApiSettings.root is required when using path or namespace/endpoint.' ); 79 } 80 path = path.replace( /^\//, '' ); 81 82 // API root may already include query parameter prefix 83 // if site is configured to use plain permalinks. 84 if ( -1 !== apiRoot.indexOf( '?' ) ) { 85 path = path.replace( '?', '&' ); 86 } 87 88 url = apiRoot + path; 89 } 90 91 // If ?_wpnonce=... is present, no need to add a nonce header. 92 addNonceHeader = ! ( options.data && options.data._wpnonce ); 93 addAcceptHeader = true; 94 95 headers = options.headers || {}; 96 97 for ( headerName in headers ) { 98 if ( ! headers.hasOwnProperty( headerName ) ) { 99 continue; 100 } 101 102 // If an 'X-WP-Nonce' or 'Accept' header (or any case-insensitive variation 103 // thereof) was specified, no need to add the header again. 104 switch ( headerName.toLowerCase() ) { 105 case 'x-wp-nonce': 106 addNonceHeader = false; 107 break; 108 case 'accept': 109 addAcceptHeader = false; 110 break; 111 } 112 } 113 114 if ( addNonceHeader && wpApiSettings ) { 115 // Do not mutate the original headers object, if any. 116 headers = $.extend( { 117 'X-WP-Nonce': wpApiSettings.nonce 118 }, headers ); 119 } 120 121 if ( addAcceptHeader ) { 122 headers = $.extend( { 123 'Accept': 'application/json, */*;q=0.1' 124 }, headers ); 125 } 126 127 if ( typeof method === 'string' ) { 128 method = method.toUpperCase(); 129 130 if ( 'PUT' === method || 'DELETE' === method ) { 131 headers = $.extend( { 132 'X-HTTP-Method-Override': method 133 }, headers ); 134 135 method = 'POST'; 136 } 137 } 138 139 // Do not mutate the original options object. 140 options = $.extend( {}, options, { 141 headers: headers, 142 url: url, 143 method: method 144 } ); 145 146 delete options.path; 147 delete options.namespace; 148 delete options.endpoint; 149 150 return options; 151 }; 152 153 apiRequest.transport = $.ajax; 154 155 /** @namespace wp */ 156 window.wp = window.wp || {}; 157 window.wp.apiRequest = apiRequest; 158 } )( jQuery );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Dec 4 08:20:01 2026 | Cross-referenced by PHPXref |