From f44ba53054887f978260ed45ce981fdf79b98e95 Mon Sep 17 00:00:00 2001 From: sarequl Date: Thu, 19 Nov 2026 07:45:12 +0000 Subject: [PATCH 1/3] Docs: Add JSDoc to ajax-response.js Adds a file header, the wpAjax namespace, and JSDoc for unserialize(), parseAjaxResponse(), invalidateForm() and validateForm(), following the JavaScript Documentation Standards. Comments only; no behavior change. Props diegosa. See #66717. --- src/js/_enqueues/lib/ajax-response.js | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/js/_enqueues/lib/ajax-response.js b/src/js/_enqueues/lib/ajax-response.js index d1a05ca52c8b8..13b089862fc67 100644 --- a/src/js/_enqueues/lib/ajax-response.js +++ b/src/js/_enqueues/lib/ajax-response.js @@ -1,10 +1,38 @@ /** + * Handles and displays the responses of admin Ajax requests. + * + * Parses the XML responses created by the WP_Ajax_Response PHP class and + * provides basic form validation helpers. + * + * @since 2.5.0 + * * @output wp-includes/js/wp-ajax-response.js */ /* global wpAjax */ +/** + * Helper functions to handle the responses of admin Ajax requests. + * + * The `noPerm` and `broken` error messages are localized from PHP, with + * English fallbacks used when they are not available. + * + * @since 2.5.0 + * + * @namespace wpAjax + */ window.wpAjax = jQuery.extend( { + /** + * Converts a query string into an object of key/value pairs. + * + * If a full URL is passed, only the part after the question mark is used. + * The values are not URL-decoded. + * + * @since 2.5.0 + * + * @param {string} s The query string or URL to convert. + * @return {Object} The key/value pairs of the query string. + */ unserialize: function( s ) { var r = {}, q, pp, i, p; if ( !s ) { return r; } @@ -17,6 +45,24 @@ window.wpAjax = jQuery.extend( { } return r; }, + /** + * Parses the response of an admin Ajax request and displays its messages. + * + * An XML response created by the WP_Ajax_Response PHP class is parsed into + * an object. Its error or notice messages are shown in the response element + * and announced to screen readers. A numeric response of -1 means the user + * lacks permission, and 0 means the request failed. Any other non-numeric + * response is shown as an error message. + * + * @since 2.5.0 + * + * @param {(XMLDocument|string|number)} x The response of the Ajax request. + * @param {string} r The ID of the element where the messages are displayed. + * @param {string} [e] Optional. The ID of the form whose fields are marked + * as invalid when the response contains errors. + * @return {(Object|boolean)} The parsed response for an XML response. For other responses, + * false when an error was displayed, true otherwise. + */ parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission. var parsed = {}, re = jQuery('#' + r).empty(), err = '', noticeMessage = ''; @@ -76,9 +122,31 @@ window.wpAjax = jQuery.extend( { } return true; }, + /** + * Marks form fields as invalid. + * + * Adds the `form-invalid` class to the matched elements. The class is + * removed as soon as one of their inputs changes. + * + * @since 2.5.0 + * + * @param {(string|jQuery)} selector The form fields to mark as invalid. + * @return {jQuery} The inputs inside the invalid form fields. + */ invalidateForm: function ( selector ) { return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } ); }, + /** + * Validates the required fields of a form. + * + * Required fields (with the `form-required` class) whose visible input is + * empty are marked as invalid. + * + * @since 2.5.0 + * + * @param {(string|jQuery)} selector The form to validate. + * @return {boolean} True if all the required fields have a value, false otherwise. + */ validateForm: function( selector ) { selector = jQuery( selector ); return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length; From 9abeabbe6cd9128383a800497b216a8e37e2ab70 Mon Sep 17 00:00:00 2001 From: sarequl Date: Fri, 20 Nov 2026 01:35:43 +0600 Subject: [PATCH 2/3] Docs: Correct JSDoc in ajax-response.js. Removes the note about English fallbacks for the error messages, which never apply, and describes more precisely the element passed to parseAjaxResponse() and the fields checked by validateForm(). Also removes an inline comment that the docblock now covers. See #66717. --- src/js/_enqueues/lib/ajax-response.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/js/_enqueues/lib/ajax-response.js b/src/js/_enqueues/lib/ajax-response.js index 13b089862fc67..594466fcedf38 100644 --- a/src/js/_enqueues/lib/ajax-response.js +++ b/src/js/_enqueues/lib/ajax-response.js @@ -14,8 +14,7 @@ /** * Helper functions to handle the responses of admin Ajax requests. * - * The `noPerm` and `broken` error messages are localized from PHP, with - * English fallbacks used when they are not available. + * The `noPerm` and `broken` error messages are localized from PHP. * * @since 2.5.0 * @@ -58,12 +57,12 @@ window.wpAjax = jQuery.extend( { * * @param {(XMLDocument|string|number)} x The response of the Ajax request. * @param {string} r The ID of the element where the messages are displayed. - * @param {string} [e] Optional. The ID of the form whose fields are marked - * as invalid when the response contains errors. + * @param {string} [e] Optional. The ID of the element containing the fields + * to mark as invalid when the response contains errors. * @return {(Object|boolean)} The parsed response for an XML response. For other responses, * false when an error was displayed, true otherwise. */ - parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission. + parseAjaxResponse: function( x, r, e ) { var parsed = {}, re = jQuery('#' + r).empty(), err = '', noticeMessage = ''; if ( x && typeof x === 'object' && x.getElementsByTagName('wp_ajax') ) { @@ -139,7 +138,7 @@ window.wpAjax = jQuery.extend( { /** * Validates the required fields of a form. * - * Required fields (with the `form-required` class) whose visible input is + * Field wrappers with the `form-required` class whose first visible input is * empty are marked as invalid. * * @since 2.5.0 From c365d35f0e31871f864dd6fce16b225a702d2e8c Mon Sep 17 00:00:00 2001 From: Sarequl Basar <9193780+sarequl@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:07:30 +0600 Subject: [PATCH 3/3] Update src/js/_enqueues/lib/ajax-response.js Co-authored-by: Weston Ruter --- src/js/_enqueues/lib/ajax-response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/_enqueues/lib/ajax-response.js b/src/js/_enqueues/lib/ajax-response.js index 594466fcedf38..d89b4594a36a2 100644 --- a/src/js/_enqueues/lib/ajax-response.js +++ b/src/js/_enqueues/lib/ajax-response.js @@ -25,7 +25,7 @@ window.wpAjax = jQuery.extend( { * Converts a query string into an object of key/value pairs. * * If a full URL is passed, only the part after the question mark is used. - * The values are not URL-decoded. + * Neither the keys nor values are URL-decoded. * * @since 2.5.0 *