diff --git a/src/js/_enqueues/admin/inline-edit-post.js b/src/js/_enqueues/admin/inline-edit-post.js index 36ffaf18ef778..698636fb039cd 100644 --- a/src/js/_enqueues/admin/inline-edit-post.js +++ b/src/js/_enqueues/admin/inline-edit-post.js @@ -20,12 +20,20 @@ window.wp = window.wp || {}; * * @property {string} type The type of inline editor. * @property {string} what The prefix before the post ID. + * @property {boolean} saving Whether a save request is active. * */ ( function( $, wp ) { window.inlineEditPost = { + /** + * Whether a save request is active. + * + * @type {boolean} + */ + saving: false, + /** * Initializes the inline and bulk post editor. * @@ -488,6 +496,11 @@ window.wp = window.wp || {}; save : function(id) { var params, fields, page = $('.post_status_page').val() || ''; + if ( this.saving ) { + return false; + } + this.saving = true; + if ( typeof(id) === 'object' ) { id = this.getId(id); } @@ -536,7 +549,10 @@ window.wp = window.wp || {}; wp.a11y.speak( wp.i18n.__( 'Error while saving the changes.' ) ); } }, - 'html'); + 'html' + ).always( function() { + inlineEditPost.saving = false; + } ); // Prevent submitting the form when pressing Enter on a focused field. return false; diff --git a/src/js/_enqueues/admin/inline-edit-tax.js b/src/js/_enqueues/admin/inline-edit-tax.js index 86e3498cd1eac..ddc91b49036ef 100644 --- a/src/js/_enqueues/admin/inline-edit-tax.js +++ b/src/js/_enqueues/admin/inline-edit-tax.js @@ -20,6 +20,12 @@ window.wp = window.wp || {}; ( function( $, wp ) { window.inlineEditTax = { + /** + * Whether a save request is active. + * + * @type {boolean} + */ + saving: false, /** * Initializes the inline taxonomy editor by adding event handlers to be able to @@ -167,6 +173,11 @@ window.inlineEditTax = { save : function(id) { var params, fields, tax = $('input[name="taxonomy"]').val() || ''; + if ( this.saving ) { + return false; + } + this.saving = true; + // Makes sure we can pass an HTMLElement as the ID. if( typeof(id) === 'object' ) { id = this.getId(id); @@ -242,7 +253,9 @@ window.inlineEditTax = { wp.a11y.speak( wp.i18n.__( 'Error while saving the changes.' ) ); } } - ); + ).always( function() { + inlineEditTax.saving = false; + } ); // Prevent submitting the form when pressing Enter on a focused field. return false; diff --git a/tests/qunit/index.html b/tests/qunit/index.html index d0e81acedb502..6dee28ce4b62d 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -76,6 +76,7 @@
+
@@ -90,6 +91,8 @@ + + @@ -147,6 +150,7 @@ + diff --git a/tests/qunit/wp-admin/js/inline-edit.js b/tests/qunit/wp-admin/js/inline-edit.js new file mode 100644 index 0000000000000..3f968e28b4ce3 --- /dev/null +++ b/tests/qunit/wp-admin/js/inline-edit.js @@ -0,0 +1,19 @@ +/* global QUnit, inlineEditPost, inlineEditTax */ + +( function() { + 'use strict'; + + QUnit.module( 'wp.inlineEdit' ); + + QUnit.test( 'Duplicate post saves are ignored while a request is active', function( assert ) { + inlineEditPost.saving = true; + assert.strictEqual( inlineEditPost.save( 1 ), false, 'A duplicate post save is ignored.' ); + inlineEditPost.saving = false; + } ); + + QUnit.test( 'Duplicate taxonomy saves are ignored while a request is active', function( assert ) { + inlineEditTax.saving = true; + assert.strictEqual( inlineEditTax.save( 1 ), false, 'A duplicate taxonomy save is ignored.' ); + inlineEditTax.saving = false; + } ); +} )();