orphan-comments.js
2.57 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*jslint browser: true, nomen: true, laxbreak: true*/
/*global ajaxurl */
"use strict";
jQuery(function ($) {
var deleted = 0;
var deleting = false;
var initial_orphans_count = 0;
var orphansCount = $('#wpml_orphans_count');
var orphansCheckCount = orphansCount.find('.check-orphans');
var orphansCountResults = orphansCount.find('.orphans-check-results');
var orphansCountProgress = orphansCount.find('.count-in-progress');
var deletingProgress = orphansCount.find('.delete-in-progress');
var deletedOrphans = orphansCount.find('.deleted');
var cleanOrphans = orphansCount.find('.clean-orphans');
var noOrphans = orphansCount.find('.no_orphans');
var orphansCheckLoader = orphansCount.find('.check_loader');
deletedOrphans.hide();
noOrphans.hide();
orphansCountProgress.hide();
orphansCountResults.hide();
orphansCheckLoader.hide();
orphansCount.show();
orphansCheckCount.on('click', count_orphans);
cleanOrphans.on('click', delete_orphans);
var nonce = orphansCount.find('#wpml_orphan_comment_nonce').val();
function resetDeletion() {
deletingProgress.fadeOut();
deleted = 0;
deleting = 0;
}
function count_orphans() {
if(!deleting) {
orphansCheckCount.fadeOut();
orphansCountProgress.fadeIn();
}
var data = {
action : 'wpml_count_orphans',
_icl_nonce: nonce
};
$.post(
ajaxurl, data, function (res) {
orphansCountProgress.fadeOut();
var orphansCountResult = parseInt(res.success ? res.data : 0);
orphansCheckLoader.fadeOut();
orphansCountResults.find('.count').html(orphansCountResult);
if (orphansCountResult > 0) {
if (initial_orphans_count == 0) {
initial_orphans_count = orphansCountResult;
cleanOrphans.fadeIn();
}
noOrphans.fadeOut();
orphansCountResults.fadeIn();
if (deleting) {
delete_orphans();
} else {
resetDeletion();
}
} else {
resetDeletion();
noOrphans.fadeIn();
orphansCountResults.fadeOut();
orphansCheckCount.fadeIn();
}
}
);
}
function delete_orphans() {
cleanOrphans.fadeOut();
deletingProgress.fadeIn();
deleting = true;
deletedOrphans.fadeIn();
var data = {
'action' : 'wpml_delete_orphans',
'data' : {how_many: Math.max(10, initial_orphans_count / 10)},
_icl_nonce: nonce
};
$.post(
ajaxurl, data, function (res) {
var deletedComments = res.success ? res.data : 0;
deleted += parseInt(deletedComments);
deletedOrphans.html(deleted);
count_orphans();
}
);
}
}
);