Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/js/_enqueues/wp/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ window.wp = window.wp || {};
delete options.error;

// Use with PHP's wp_send_json_success() and wp_send_json_error().
deferred.jqXHR = $.ajax( options ).done( function( response ) {
deferred.jqXHR = $.ajax( options ).done( function( response, textStatus, jqXHR ) {
// Treat a response of 1 as successful for backward compatibility with existing handlers.
if ( response === '1' || response === 1 ) {
response = { success: true };
Expand All @@ -127,10 +127,10 @@ window.wp = window.wp || {};
action &&
action.data &&
'query-attachments' === action.data.action &&
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 );
} else {
context.totalAttachments = 0;
}
Expand Down
1 change: 1 addition & 0 deletions tests/qunit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<script src="wp-includes/js/jquery.js"></script>
<script src="wp-includes/js/wp-api.js"></script>
<script src="wp-includes/js/wp-sanitize.js"></script>
<script src="wp-includes/js/wp-util.js"></script>
<script src="wp-includes/js/wp-emoji.js"></script>
<script src="wp-admin/js/customize-controls.js"></script>
<script src="wp-admin/js/customize-controls-utils.js"></script>
Expand Down
26 changes: 26 additions & 0 deletions tests/qunit/wp-includes/js/wp-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* global wp, jQuery */

QUnit.module( 'wp.ajax.send', {
beforeEach: function() {
this.originalAjax = jQuery.ajax;
},
afterEach: function() {
jQuery.ajax = this.originalAjax;
}
} );

QUnit.test( 'query-attachments request that completes synchronously should set totalAttachments', function( assert ) {
const jqXHR = { getResponseHeader: () => '42' };
const context = {};

jQuery.ajax = function( options ) {
const deferred = jQuery.Deferred();
deferred.done( options.success );
deferred.resolveWith( options.context, [ { success: true, data: [] }, 'success', jqXHR ] );
return deferred.promise();
};

wp.ajax.send( { data: { action: 'query-attachments' }, context: context } );

assert.strictEqual( context.totalAttachments, 42, 'totalAttachments is read from the X-WP-Total header' );
} );
Loading