Skip to content

Commit 8e77ffe

Browse files
dcalhounclaude
andcommitted
fix: Add type checks before wrapping wp.ajax methods (#282)
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. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 827204d commit 8e77ffe

2 files changed

Lines changed: 31 additions & 21 deletions

File tree

src/utils/ajax.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,39 @@ function configureAjaxAuth( authHeader ) {
4646
},
4747
} );
4848

49-
const originalSend = window.wp.ajax.send;
50-
window.wp.ajax.send = function ( options ) {
51-
const originalBeforeSend = options.beforeSend;
49+
if ( typeof window.wp.ajax.send === 'function' ) {
50+
const originalSend = window.wp.ajax.send;
51+
window.wp.ajax.send = function ( options ) {
52+
const originalBeforeSend = options.beforeSend;
5253

53-
options.beforeSend = function ( xhr ) {
54-
xhr.setRequestHeader( 'Authorization', authHeader );
54+
options.beforeSend = function ( xhr ) {
55+
xhr.setRequestHeader( 'Authorization', authHeader );
5556

56-
if ( typeof originalBeforeSend === 'function' ) {
57-
originalBeforeSend( xhr );
58-
}
57+
if ( typeof originalBeforeSend === 'function' ) {
58+
originalBeforeSend( xhr );
59+
}
60+
};
61+
62+
return originalSend.call( this, options );
5963
};
64+
}
6065

61-
return originalSend.call( this, options );
62-
};
66+
if ( typeof window.wp.ajax.post === 'function' ) {
67+
const originalPost = window.wp.ajax.post;
68+
window.wp.ajax.post = function ( options ) {
69+
const originalBeforeSend = options.beforeSend;
6370

64-
const originalPost = window.wp.ajax.post;
65-
window.wp.ajax.post = function ( options ) {
66-
const originalBeforeSend = options.beforeSend;
71+
options.beforeSend = function ( xhr ) {
72+
xhr.setRequestHeader( 'Authorization', authHeader );
6773

68-
options.beforeSend = function ( xhr ) {
69-
xhr.setRequestHeader( 'Authorization', authHeader );
74+
if ( typeof originalBeforeSend === 'function' ) {
75+
originalBeforeSend( xhr );
76+
}
77+
};
7078

71-
if ( typeof originalBeforeSend === 'function' ) {
72-
originalBeforeSend( xhr );
73-
}
79+
return originalPost.call( this, options );
7480
};
75-
76-
return originalPost.call( this, options );
77-
};
81+
}
7882

7983
debug( 'AJAX auth configured' );
8084
}

src/utils/ajax.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ describe( 'configureAjax', () => {
402402

403403
expect( () => configureAjax() ).not.toThrow();
404404

405+
// Should not wrap send (it doesn't exist)
406+
expect( global.window.wp.ajax.send ).toBeUndefined();
407+
405408
// Should still wrap post
406409
expect( global.window.wp.ajax.post ).not.toBe( originalWpAjaxPost );
407410
} );
@@ -421,6 +424,9 @@ describe( 'configureAjax', () => {
421424

422425
expect( () => configureAjax() ).not.toThrow();
423426

427+
// Should not wrap post (it doesn't exist)
428+
expect( global.window.wp.ajax.post ).toBeUndefined();
429+
424430
// Should still wrap send
425431
expect( global.window.wp.ajax.send ).not.toBe( originalWpAjaxSend );
426432
} );

0 commit comments

Comments
 (0)