From 628bb4a8d22aa3c989d68785e2b19f9b9233db39 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 14 Jan 2026 17:14:12 +0000 Subject: [PATCH] fix: Add type checks before wrapping wp.ajax methods Address PR feedback about potential race condition. The code now checks if `window.wp.ajax.send` and `window.wp.ajax.post` are functions before wrapping them. This prevents TypeError when calling the wrapped function if the original method was undefined during configuration. Update tests to verify that missing methods remain undefined rather than being wrapped with an undefined reference. --- src/utils/ajax.js | 46 +++++++++++++++++++++++------------------- src/utils/ajax.test.js | 6 ++++++ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/utils/ajax.js b/src/utils/ajax.js index e009c881e..ed4df197b 100644 --- a/src/utils/ajax.js +++ b/src/utils/ajax.js @@ -46,35 +46,39 @@ function configureAjaxAuth( authHeader ) { }, } ); - const originalSend = window.wp.ajax.send; - window.wp.ajax.send = function ( options ) { - const originalBeforeSend = options.beforeSend; + if ( typeof window.wp.ajax.send === 'function' ) { + const originalSend = window.wp.ajax.send; + window.wp.ajax.send = function ( options ) { + const originalBeforeSend = options.beforeSend; - options.beforeSend = function ( xhr ) { - xhr.setRequestHeader( 'Authorization', authHeader ); + options.beforeSend = function ( xhr ) { + xhr.setRequestHeader( 'Authorization', authHeader ); - if ( typeof originalBeforeSend === 'function' ) { - originalBeforeSend( xhr ); - } + if ( typeof originalBeforeSend === 'function' ) { + originalBeforeSend( xhr ); + } + }; + + return originalSend.call( this, options ); }; + } - return originalSend.call( this, options ); - }; + if ( typeof window.wp.ajax.post === 'function' ) { + const originalPost = window.wp.ajax.post; + window.wp.ajax.post = function ( options ) { + const originalBeforeSend = options.beforeSend; - const originalPost = window.wp.ajax.post; - window.wp.ajax.post = function ( options ) { - const originalBeforeSend = options.beforeSend; + options.beforeSend = function ( xhr ) { + xhr.setRequestHeader( 'Authorization', authHeader ); - options.beforeSend = function ( xhr ) { - xhr.setRequestHeader( 'Authorization', authHeader ); + if ( typeof originalBeforeSend === 'function' ) { + originalBeforeSend( xhr ); + } + }; - if ( typeof originalBeforeSend === 'function' ) { - originalBeforeSend( xhr ); - } + return originalPost.call( this, options ); }; - - return originalPost.call( this, options ); - }; + } debug( 'AJAX auth configured' ); } diff --git a/src/utils/ajax.test.js b/src/utils/ajax.test.js index a9f988d58..28cf84cd4 100644 --- a/src/utils/ajax.test.js +++ b/src/utils/ajax.test.js @@ -402,6 +402,9 @@ describe( 'configureAjax', () => { expect( () => configureAjax() ).not.toThrow(); + // Should not wrap send (it doesn't exist) + expect( global.window.wp.ajax.send ).toBeUndefined(); + // Should still wrap post expect( global.window.wp.ajax.post ).not.toBe( originalWpAjaxPost ); } ); @@ -421,6 +424,9 @@ describe( 'configureAjax', () => { expect( () => configureAjax() ).not.toThrow(); + // Should not wrap post (it doesn't exist) + expect( global.window.wp.ajax.post ).toBeUndefined(); + // Should still wrap send expect( global.window.wp.ajax.send ).not.toBe( originalWpAjaxSend ); } );