-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-backdating-posts.js
50 lines (43 loc) · 1.36 KB
/
no-backdating-posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @todo: the script is triggered to frequently, reduce the number of requests.
*/
(function (wp) {
var lastSaveTime = 0;
var checkingNotice = false;
function checkForBackdateNotice() {
if (checkingNotice) return;
checkingNotice = true;
wp.ajax.post(noBackdate.action, {
nonce: noBackdate.nonce
}).then(function (response) {
if (response && response.message && response.type !== 'no-backdate') {
wp.data.dispatch('core/notices').createNotice(
response.type,
response.message,
{
id: 'backdate-notice',
isDismissible: true,
}
);
} else {
console.warn("No backdate notice", response);
}
}).catch(function (error) {
console.error('Error checking for backdate notice:', error);
}).always(function () {
checkingNotice = false;
});
}
wp.data.subscribe(function () {
var isSaving = wp.data.select('core/editor').isSavingPost();
var isAutosaving = wp.data.select('core/editor').isAutosavingPost();
var currentTime = Date.now();
if (!isSaving && !isAutosaving && currentTime - lastSaveTime > 1000) {
// Post has finished saving (and it's not an autosave)
lastSaveTime = currentTime;
setTimeout(checkForBackdateNotice, 500); // Small delay to ensure server-side processing is complete
}
});
// Also check for notice on initial load
wp.domReady(checkForBackdateNotice);
})(window.wp);