Ticket #23220: disable_save_while_disconnected.2.diff

File disable_save_while_disconnected.2.diff, 3.6 KB (added by dh-shredder, 2 months ago)

Only disabling buttons during after autosave if wp.heartbeat.connectionLost

Line 
1diff --git wp-admin/edit-form-advanced.php wp-admin/edit-form-advanced.php
2index 6df9234..770c851 100644
3--- wp-admin/edit-form-advanced.php
4+++ wp-admin/edit-form-advanced.php
5@@ -329,6 +329,12 @@ wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
6 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
7 ?>
8 
9+<div id="lost-connection-notice" class="error hidden">
10+       <p id="lost-connection-message">
11+       <?php _e("You have lost your connection with the server, and saving has been disabled. This message will vanish once you've reconnected."); ?>
12+       </p>
13+</div>
14+
15 <div id="poststuff">
16 
17 <?php
18diff --git wp-includes/js/autosave.js wp-includes/js/autosave.js
19index d1914d1..5e1c3f1 100644
20--- wp-includes/js/autosave.js
21+++ wp-includes/js/autosave.js
22@@ -180,7 +180,9 @@ function autosave_parse_response(response) {
23 function autosave_saved(response) {
24        blockSave = false;
25        autosave_parse_response(response); // parse the ajax response
26-       autosave_enable_buttons(); // re-enable disabled form buttons
27+
28+       if ( ! wp.heartbeat.connectionLost )
29+               autosave_enable_buttons(); // re-enable disabled form buttons
30 }
31 
32 // called when autosaving new post
33@@ -200,7 +202,7 @@ function autosave_saved_new(response) {
34                        autosaveDelayPreview = false;
35                        doPreview();
36                }
37-       } else {
38+       } else if ( ! wp.heartbeat.connectionLost ) {
39                autosave_enable_buttons(); // re-enable disabled form buttons
40        }
41 }
42@@ -238,8 +240,11 @@ function autosave_enable_buttons() {
43 
44 function autosave_disable_buttons() {
45        jQuery(':button, :submit', '#submitpost').prop('disabled', true);
46-       // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
47-       setTimeout(autosave_enable_buttons, 5000);
48+
49+       if ( ! wp.heartbeat.connectionLost ) {
50+               // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
51+               setTimeout(autosave_enable_buttons, 5000);
52+       }
53 }
54 
55 function delayed_autosave() {
56@@ -256,6 +261,7 @@ autosave = function() {
57        var rich = (typeof tinymce != "undefined") && tinymce.activeEditor && !tinymce.activeEditor.isHidden(),
58                post_data, doAutoSave, ed, origStatus, successCallback;
59 
60+       // Disable buttons until we know the save completed.
61        autosave_disable_buttons();
62 
63        post_data = wp.autosave.getPostData();
64@@ -668,3 +674,16 @@ wp.autosave.local = {
65 wp.autosave.local.init();
66 
67 }(jQuery));
68+
69+
70+// When connection is lost, keep user from submitting changes.
71+jQuery(document).on('heartbeat-connection-lost', function() {
72+       autosave_disable_buttons();
73+       jQuery('#lost-connection-notice').show();
74+});
75+
76+// When connection returns, make work all the things.
77+jQuery(document).on('heartbeat-connection-restored', function() {
78+       autosave_enable_buttons();
79+       jQuery('#lost-connection-notice').hide();
80+});
81diff --git wp-includes/js/heartbeat.js wp-includes/js/heartbeat.js
82index a382599..1060786 100644
83--- wp-includes/js/heartbeat.js
84+++ wp-includes/js/heartbeat.js
85@@ -28,6 +28,7 @@ window.wp = window.wp || {};
86 
87                this.url = typeof ajaxurl != 'undefined' ? ajaxurl : 'wp-admin/admin-ajax.php';
88                this.autostart = true;
89+               this.connectionLost = false;
90 
91                if ( typeof( window.heartbeatSettings != 'undefined' ) ) {
92                        settings = $.extend( {}, window.heartbeatSettings );
93@@ -171,7 +172,7 @@ window.wp = window.wp || {};
94 
95                        // temp debug
96                        if ( self.debug )
97-                               console.log('### blurred(), slow down...')
98+                               console.log('### blurred(), slow down...');
99                }
100 
101                function focused() {
102@@ -192,7 +193,7 @@ window.wp = window.wp || {};
103 
104                        // temp debug
105                        if ( self.debug )
106-                               console.log('### focused(), speed up... ')
107+                               console.log('### focused(), speed up... ');
108                }
109 
110                function setFrameEvents() {