Commit 1ecd380e06 for wordpress.org
commit 1ecd380e062e6336af05b372a22750adb4dc42e7
Author: lancewillett <lancewillett@git.wordpress.org>
Date: Mon Nov 30 20:46:13 2026 +0000
Build/Test Tools: Enable TypeScript checking for `api-request.js`.
Require a REST root for path-based requests so they do not resolve against the current admin URL. Continue to support explicit URLs when `wpApiSettings` is absent.
Developed in: https://github.com/WordPress/wordpress-develop/pull/16331
Props shailu25, mukesh27.
See #66751.
Built from https://develop.svn.wordpress.org/trunk@65832
git-svn-id: http://core.svn.wordpress.org/trunk@64861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/js/api-request.js b/wp-includes/js/api-request.js
index 4762a37f65..2ab0f3160a 100644
--- a/wp-includes/js/api-request.js
+++ b/wp-includes/js/api-request.js
@@ -19,41 +19,41 @@
* @param {JQueryStatic} $ The jQuery object.
*/
( function( $ ) {
- var wpApiSettings = window.wpApiSettings;
-
/**
* Makes an API request.
*
- * @param {Object} options The options for the API request.
- * @param {string} [options.url] The full URL for the request.
- * @param {string} [options.path] The API path (e.g., 'wp/v2/posts'). Used if url is not provided.
- * @param {string} [options.namespace] The API namespace (e.g., 'wp/v2'). Used with endpoint to build path.
- * @param {string} [options.endpoint] The API endpoint (e.g., 'posts'). Used with namespace to build path.
- * @param {string} [options.method] The HTTP method (GET, POST, PUT, DELETE, etc.).
- * @param {Object} [options.data] The request data to send.
- * @param {string} [options.data._wpnonce] Nonce for security.
- * @param {Object} [options.headers] Custom headers to include in the request.
+ * @param {Object} options The options for the API request.
+ * @param {string} [options.url] The full URL for the request.
+ * @param {string} [options.path] The API path (e.g., 'wp/v2/posts'). Used if url is not provided.
+ * @param {string} [options.namespace] The API namespace (e.g., 'wp/v2'). Used with endpoint to build path.
+ * @param {string} [options.endpoint] The API endpoint (e.g., 'posts'). Used with namespace to build path.
+ * @param {string} [options.method] The HTTP method (GET, POST, PUT, DELETE, etc.).
+ * @param {Object} [options.data] The request data to send.
+ * @param {string} [options.data._wpnonce] Nonce for security.
+ * @param {JQuery.PlainObject<string | null | undefined>} [options.headers] Custom headers to include in the request.
* @return {JQueryXHR} The jQuery XHR object.
*/
function apiRequest( options ) {
- options = apiRequest.buildAjaxOptions( options );
- return apiRequest.transport( options );
+ return apiRequest.transport( apiRequest.buildAjaxOptions( options ) );
}
/**
* Builds the options for the jQuery.ajax request.
*
- * @param {Object} options The options for the API request.
- * @param {string} [options.url] The full URL for the request.
- * @param {string} [options.path] The API path (e.g., 'wp/v2/posts'). Used if url is not provided.
- * @param {string} [options.namespace] The API namespace (e.g., 'wp/v2'). Used with endpoint to build path.
- * @param {string} [options.endpoint] The API endpoint (e.g., 'posts'). Used with namespace to build path.
- * @param {string} [options.method] The HTTP method (GET, POST, PUT, DELETE, etc.).
- * @param {Object} [options.data] The request data to send.
- * @param {Object} [options.headers] Custom headers to include in the request.
- * @return {Object} The options for the jQuery.ajax request.
+ * @param {Object} options The options for the API request.
+ * @param {string} [options.url] The full URL for the request.
+ * @param {string} [options.path] The API path (e.g., 'wp/v2/posts'). Used if url is not provided.
+ * @param {string} [options.namespace] The API namespace (e.g., 'wp/v2'). Used with endpoint to build path.
+ * @param {string} [options.endpoint] The API endpoint (e.g., 'posts'). Used with namespace to build path.
+ * @param {string} [options.method] The HTTP method (GET, POST, PUT, DELETE, etc.).
+ * @param {Object} [options.data] The request data to send.
+ * @param {string} [options.data._wpnonce] Nonce for security.
+ * @param {JQuery.PlainObject<string | null | undefined>} [options.headers] Custom headers to include in the request.
+ * @return {JQuery.AjaxSettings} The options for the jQuery.ajax request.
+ * @throws {Error} If `wpApiSettings.root` is missing when `path` or `namespace`/`endpoint` is used.
*/
apiRequest.buildAjaxOptions = function( options ) {
+ var wpApiSettings = window.wpApiSettings;
var url = options.url;
var path = options.path;
var method = options.method;
@@ -73,12 +73,15 @@
}
}
if ( typeof path === 'string' ) {
- apiRoot = wpApiSettings.root;
+ apiRoot = wpApiSettings && typeof wpApiSettings.root === 'string' ? wpApiSettings.root : '';
+ if ( ! apiRoot ) {
+ throw new Error( 'wpApiSettings.root is required when using path or namespace/endpoint.' );
+ }
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( '?' ) ) {
+ if ( -1 !== apiRoot.indexOf( '?' ) ) {
path = path.replace( '?', '&' );
}
@@ -108,7 +111,7 @@
}
}
- if ( addNonceHeader ) {
+ if ( addNonceHeader && wpApiSettings ) {
// Do not mutate the original headers object, if any.
headers = $.extend( {
'X-WP-Nonce': wpApiSettings.nonce
diff --git a/wp-includes/js/api-request.min.js b/wp-includes/js/api-request.min.js
index 71b300a65f..c127f4e8e7 100644
--- a/wp-includes/js/api-request.min.js
+++ b/wp-includes/js/api-request.min.js
@@ -1,2 +1,2 @@
/*! This file is auto-generated */
-!function(c){var w=window.wpApiSettings;function t(e){return e=t.buildAjaxOptions(e),t.transport(e)}t.buildAjaxOptions=function(e){var t,n,a,p,o,r,i=e.url,d=e.path,s=e.method;for(r in"string"==typeof e.namespace&&"string"==typeof e.endpoint&&(t=e.namespace.replace(/^\/|\/$/g,""),d=(n=e.endpoint.replace(/^\//,""))?t+"/"+n:t),"string"==typeof d&&(n=w.root,d=d.replace(/^\//,""),"string"==typeof n&&-1!==n.indexOf("?")&&(d=d.replace("?","&")),i=n+d),p=!(e.data&&e.data._wpnonce),o=!0,a=e.headers||{})if(a.hasOwnProperty(r))switch(r.toLowerCase()){case"x-wp-nonce":p=!1;break;case"accept":o=!1}return p&&(a=c.extend({"X-WP-Nonce":w.nonce},a)),o&&(a=c.extend({Accept:"application/json, */*;q=0.1"},a)),"string"!=typeof s||"PUT"!==(s=s.toUpperCase())&&"DELETE"!==s||(a=c.extend({"X-HTTP-Method-Override":s},a),s="POST"),delete(e=c.extend({},e,{headers:a,url:i,method:s})).path,delete e.namespace,delete e.endpoint,e},t.transport=c.ajax,window.wp=window.wp||{},window.wp.apiRequest=t}(jQuery);
\ No newline at end of file
+!function(w){function t(e){return t.transport(t.buildAjaxOptions(e))}t.buildAjaxOptions=function(e){var t,n,o,p,r,a,i=window.wpApiSettings,d=e.url,s=e.path,c=e.method;if("string"==typeof e.namespace&&"string"==typeof e.endpoint&&(t=e.namespace.replace(/^\/|\/$/g,""),s=(n=e.endpoint.replace(/^\//,""))?t+"/"+n:t),"string"==typeof s){if(!(n=i&&"string"==typeof i.root?i.root:""))throw new Error("wpApiSettings.root is required when using path or namespace/endpoint.");s=s.replace(/^\//,""),-1!==n.indexOf("?")&&(s=s.replace("?","&")),d=n+s}for(a in p=!(e.data&&e.data._wpnonce),r=!0,o=e.headers||{})if(o.hasOwnProperty(a))switch(a.toLowerCase()){case"x-wp-nonce":p=!1;break;case"accept":r=!1}return p&&i&&(o=w.extend({"X-WP-Nonce":i.nonce},o)),r&&(o=w.extend({Accept:"application/json, */*;q=0.1"},o)),"string"!=typeof c||"PUT"!==(c=c.toUpperCase())&&"DELETE"!==c||(o=w.extend({"X-HTTP-Method-Override":c},o),c="POST"),delete(e=w.extend({},e,{headers:o,url:d,method:c})).path,delete e.namespace,delete e.endpoint,e},t.transport=w.ajax,window.wp=window.wp||{},window.wp.apiRequest=t}(jQuery);
\ No newline at end of file
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 66a4e50c46..557fce0bde 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.3-alpha-65831';
+$wp_version = '7.3-alpha-65832';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.