-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
242 lines (226 loc) · 6.39 KB
/
scripts.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(function( $ ){
// declaring variables at initialization
var $convertAllBtn = $('#vo_convert_filter'),
$output = $('#vo-output'),
$singleConvertLinks = $('.vo-single-convert'),
$doactionTopBtn = $('#doaction'),
$doactionBottomBtn = $('#doaction2'),
convertQueue = [],
doingAjax = false;
// creating hidden Blocks editor
$('<div />').attr('id', 'vo-editor').attr('style', 'display: none').appendTo('body');
wp.editPost.initializeEditor('vo-editor');
// "Bulk Convert All" button handler
$convertAllBtn.click(function(e){
e.preventDefault();
if( ! confirm( voObj.confirmConvertAllMessage ) ) return;
$convertAllBtn.prop("disabled", true);
bulkConvertPosts();
});
// table "Convert" link handler
$singleConvertLinks.click(function(e){
e.preventDefault();
var postID = $(this).data('json').post;
convertQueue.push( postID );
convertPosts();
});
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null) {
return null;
}
return decodeURI(results[1]) || 0;
}
// bulk posts converting via ajax
function bulkConvertPosts( offset = 0, total = -1 ){
if ( doingAjax ) return;
doingAjax = true;
var nonce = $convertAllBtn.data('nonce');
$output.html( voObj.bulkConvertingMessage );
$.ajax({
method: "GET",
url: voObj.ajaxUrl,
data: {
action : "vo_bulk_convert",
offset : offset,
total : total,
vo_post_type : $.urlParam('vo_post_type'),
vo_category : $.urlParam('vo_category'),
vo_from_date : $.urlParam('vo_from_date'),
vo_to_date : $.urlParam('vo_to_date'),
_wpnonce : nonce
}
})
.done(function( data ){
doingAjax = false;
if ( typeof data !== "object" ) {
$output.html( voObj.serverErrorMessage );
return;
}
if ( data.error ) {
$output.html( data.message );
return;
}
var convertedData = [];
var arrayLength = data.postsData.length;
for (var i = 0; i < arrayLength; i++) {
var convertedPost = {
id : data.postsData[i].id,
content : convertToBlocks( data.postsData[i].content )
};
convertedData.push( convertedPost );
}
bulkSaveConverted( convertedData, data.offset, data.total, data.message );
return;
})
.fail(function(){
doingAjax = false;
$output.html( voObj.serverErrorMessage );
});
}
// bulk saving converted posts via ajax
function bulkSaveConverted( convertedData, offset, total, message ){
if ( doingAjax ) return;
doingAjax = true;
var nonce = $convertAllBtn.data('nonce');
var jsonData = {
action : "vo_bulk_convert",
offset : offset,
total : total,
postsData : convertedData,
_wpnonce : nonce
};
$.ajax({
method: "POST",
url: voObj.ajaxUrl,
data: jsonData
})
.done(function( data ){
doingAjax = false;
if ( typeof data !== "object" ) {
$output.html( voObj.serverErrorMessage );
return;
}
if ( data.error ) {
$output.html( data.message );
return;
}
if ( data.offset >= data.total ) {
$convertAllBtn.prop("disabled", false);
$output.html( voObj.bulkConvertingSuccessMessage );
return;
}
bulkConvertPosts( offset, total );
$output.html( message );
return;
})
.fail(function(){
doingAjax = false;
$output.html( voObj.serverErrorMessage );
});
}
// single or group posts converting via ajax
function convertPosts(){
if( convertQueue.length == 0 ){
return;
}
if ( doingAjax ) return;
doingAjax = true;
var postID = convertQueue.shift();
var $linkObject = $('#vo-single-convert-' + postID);
$linkObject.hide().after( voObj.convertingSingleMessage );
$.ajax({
method: "GET",
url: voObj.ajaxUrl,
data: $linkObject.data('json')
})
.done(function( data ){
doingAjax = false;
if ( typeof data !== "object" ) {
$linkObject.parent().html( voObj.failedMessage );
return;
}
if ( data.error ) {
$linkObject.parent().html( voObj.failedMessage );
return;
}
var content = convertToBlocks( data.message );
saveConverted( content, $linkObject );
return;
})
.fail(function(){
doingAjax = false;
$linkObject.parent().html( voObj.failedMessage );
});
}
// posts converting using built in Wordpress library
function convertToBlocks( content ){
var blocks = wp.blocks.rawHandler({
HTML: content
});
return wp.blocks.serialize(blocks);
}
// single or group saving of converted posts via ajax
function saveConverted( content, $linkObject ){
if ( doingAjax ) return;
doingAjax = true;
var jsonData = $linkObject.data('json');
jsonData.content = content;
$.ajax({
method: "POST",
url: voObj.ajaxUrl,
data: jsonData
})
.done(function( data ){
doingAjax = false;
$("#vo-convert-checkbox-"+jsonData.post).prop("checked", false);
$("#vo-convert-checkbox-"+jsonData.post).prop("disabled", true);
if ( typeof data !== "object" ) {
$linkObject.parent().html( voObj.failedMessage );
return;
}
if ( data.error ) {
$linkObject.parent().html( voObj.failedMessage );
return;
}
if (content.includes('<!-- wp:html -->')) {
$linkObject.parent().html( voObj.convertedSingleHTMLWarning );
} else {
$linkObject.parent().html( voObj.convertedSingleMessage );
}
convertPosts();
return;
})
.fail(function(){
doingAjax = false;
$("#vo-convert-checkbox-"+jsonData.post).prop("checked", false);
$("#vo-convert-checkbox-"+jsonData.post).prop("disabled", true);
$linkObject.parent().html( voObj.failedMessage );
});
}
// top action button handler
$doactionTopBtn.click(function(e){
e.preventDefault();
if( $('select[name="action"]').val() === 'bulk-convert' ){
convertChecked();
}
});
// bottom action button handler
$doactionBottomBtn.click(function(e){
e.preventDefault();
if( $('select[name="action2"]').val() === 'bulk-convert' ){
convertChecked();
}
});
// add checked posts to converting queue and run converting process
function convertChecked(){
$('input[name="bulk-convert[]"]').each(function( index ){
if( $(this).prop("checked") == true ){
convertQueue.push( $(this).val() );
}
});
$doactionTopBtn.prop("disabled", true);
$doactionBottomBtn.prop("disabled", true);
convertPosts();
}
})( jQuery );