Opened 5 years ago
Last modified 4 weeks ago
#54205 new defect (bug)
jqxhr is undefined inside of deferred.done() when using wp.media to add a custom image upload
| Reported by: | metawebdevelopment | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Media | Version: | 5.8.1 |
| Severity: | major | Keywords: | needs-testing has-test-info has-patch has-unit-tests |
| Cc: | Focuses: | javascript, administration |
Description
I have done all the usual trouble shooting and found this is being cause by a new block of code added to wp-util.js in 5.8.1. Specifically lines 121-134.
The jqXHR property does not exist inside of the done() object and therefor always errors out. This is removing a key functionality of a clients admin that makes it where no new products can be added without a horrible workaround.
Change History (8)
This ticket was mentioned in Slack in #core-test by hellofromtonya. View the logs.
5 years ago
#3
follow-up:
↓ 4
@
5 years ago
@mai21 That will be way too much to add in here, but the gist of it to quickly get to the error would be:
1) Create a new settings/menu page or submenu page in admin with an upload button and 2 hidden inputs. One to store the url and one to store the image id.
2) Enqueue wp_enqueue_media() if did_action check fails
3) add an on click action to the button and when clicked run the following code or similar code that is referenced in the codex and media API docs.
$(document).on('click', '.upload_image_button', function(e){
e.preventDefault();
let button = $(this),
custom_uploader = wp.media({
title: 'Set Image',
library : {
type : 'image'
},
button: {
text: 'Use this image'
},
multiple: false
}).on('select', function(){
let attachment = custom_uploader.state().get('selection').first().toJSON();
let imageObject = {
id: attachment.id,
url: attachment.url,
}
button.siblings('.image_url').val(imageObject.url);
button.siblings('.img_id').val(imageObject.id);
button.next().find('img').attr('src', imageObject.url);
button.next().find('.remove-image').show();
})
.open();
});
You will get a console warning when attempting to click the button or set an image.
#4
in reply to: ↑ 3
@
5 years ago
Replying to metawebdevelopment:
@mai21 That will be way too much to add in here, but the gist of it to quickly get to the error would be:
1) Create a new settings/menu page or submenu page in admin with an upload button and 2 hidden inputs. One to store the url and one to store the image id.
2) Enqueue wp_enqueue_media() if did_action check fails
3) add an on click action to the button and when clicked run the following code or similar code that is referenced in the codex and media API docs.
$(document).on('click', '.upload_image_button', function(e){ e.preventDefault(); let button = $(this), custom_uploader = wp.media({ title: 'Set Image', library : { type : 'image' }, button: { text: 'Use this image' }, multiple: false }).on('select', function(){ let attachment = custom_uploader.state().get('selection').first().toJSON(); let imageObject = { id: attachment.id, url: attachment.url, } button.siblings('.image_url').val(imageObject.url); button.siblings('.img_id').val(imageObject.id); button.next().find('img').attr('src', imageObject.url); button.next().find('.remove-image').show(); }) .open(); });You will get a console warning when attempting to click the button or set an image.
Just an update that something similar is also causing bugs with custom wysiwyg inputs in various plugins that seems to be related. They will load and only run in text mode.
#5
@
4 years ago
- Keywords needs-testing has-testing-info added; reporter-feedback needs-testing-info removed
Updating keywords as reporter has shared testing instructions. Adding needs-testing for a tester to reproduce the reported issue and add a test report.
#7
@
4 weeks ago
Reproduction Report
Description
This report validates that the issue can be reproduced, with a narrower trigger than originally reported.
The error only happens when the query-attachments request in wp.ajax.send() completes synchronously. In that case jQuery runs the .done() callbacks before $.ajax() returns, so deferred.jqXHR hasn't been assigned yet, and the guard deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) throws because it is called on undefined.
With normal async requests, deferred.jqXHR is assigned long before the response comes back. That's why the reporter's snippet on its own does not reproduce on a clean install. Synchronous completion can come from jQuery.ajaxSetup( { async: false } ), a custom ajaxTransport / prefilter, or a plugin that mocks or caches $.ajax. Something in the reporter's stack was likely doing one of these.
The affected code (added in 5.8.1) is unchanged in trunk.
Environment
- WordPress: 7.2-alpha-64315 (trunk)
- PHP: 8.3.6
- Server: PHP built-in server
- Database: SQLite 3.45.1 (SQLite Database Integration drop-in)
- Browser: Chrome 141.0.7390.37
- OS: Linux
- Theme: Twenty Twenty-Five
- MU-Plugins: None activated
- Plugins: None activated
- jQuery: 3.7.1 (core bundled)
Steps to Reproduce
- Upload a few images (6 used here).
- Go to Media > Library and switch to grid view.
- Open the browser console and run:
jQuery.ajaxSetup( { async: false } ); - Type
repro(any term matching the uploads) in the media search box. This fires aquery-attachmentsrequest. - 🐞 Check the console and the grid.
Actual Results
- ✅ Error reproduced:
TypeError: Cannot read properties of undefined (reading 'hasOwnProperty') at wp-includes/js/wp-util.js:130:24 at fire (wp-includes/js/jquery/jquery.js:3223:31) - ✅ Visible side effect: the grid shows every matching attachment twice (12 tiles for 6 unique attachment IDs).
- Without step 3 (default async requests), there is no error and the grid shows 6 tiles.
Expected Results
- The attachments query completes without errors and each attachment is shown once, whether the request is sync or async.
Additional Notes
- Non-attachment actions aren't affected, because the
'query-attachments' === action.data.actioncheck short-circuits beforedeferred.jqXHRis touched. - Suggested fix: use the jqXHR that jQuery passes as the third argument to
.done()instead of reading the outerdeferred.jqXHRassignment:
- deferred.jqXHR = $.ajax( options ).done( function( response ) {
+ deferred.jqXHR = $.ajax( options ).done( function( response, textStatus, jqXHR ) {
...
- deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
- deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
+ jqXHR &&
+ jqXHR.getResponseHeader( 'X-WP-Total' )
) {
- context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
+ context.totalAttachments = parseInt( jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
- With this change applied (SCRIPT_DEBUG on, same steps), there is no error and the grid shows 6 tiles for 6 unique IDs. Default async behavior is unchanged.
This ticket was mentioned in PR #14249 on WordPress/wordpress-develop by @irozum.
4 weeks ago
#8
- Keywords has-patch has-unit-tests added; needs-patch removed
Summary
wp.ajax.send() reads deferred.jqXHR inside the .done() callback to get the X-WP-Total header for query-attachments requests. If the request completes synchronously (for example jQuery.ajaxSetup( { async: false } ), a custom transport, or a plugin that mocks or caches $.ajax), jQuery runs the callback before $.ajax() returns, so deferred.jqXHR is still undefined and the guard throws a TypeError. In the media grid this also leaves every matching attachment duplicated.
This uses the jqXHR jQuery passes as the third argument to .done(), which is always defined. Async behaviour is unchanged.
Testing
Added a QUnit test that stubs $.ajax to resolve synchronously and asserts totalAttachments is read from the X-WP-Total header. It fails on trunk with the TypeError and passes with the fix. Run with npx grunt qunit.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.
---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
@metawebdevelopment Thanks for reporting this issue.
Can you please provide step-by-step instruction on how to reproduce it?