wpf-options.js
10.1 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
jQuery(document).ready(function($){
// Settings page specific functions
if($('body').hasClass('settings_page_wpf-settings')) {
$('[data-toggle="tooltip"]').tooltip()
//
// Import Users
//
$( "#import-users-btn" ).on( "click", function() {
if($('[name^="wpf_options[import_users]"] option:selected').length == 0) {
$('[name^="wpf_options[import_users]"]').next().addClass('error');
setTimeout( function() { $('[name^="wpf_options[import_users]"]').next().removeClass('error'); }, 1000 );
return;
}
$(this).html('<i class="fa-li fa fa-spinner fa-spin"></i> Importing');
$(this).attr('disabled', 'disabled');
var button = $(this);
var params = {};
$('[name^="wpf_options[import_users]"]').each(function() {
params[$(this).attr('id')] = $(this).val();
});
var data = {
'action' : 'import_users',
'params' : JSON.stringify(params),
'role' : $('#import_role').val(),
'notify' : $('#email_notifications').is(':checked')
};
$.post(ajaxurl, data, function(response) {
button.html('Import');
button.removeAttr('disabled');
if(response > 0) {
$('#import-output').html('<div class="updated"><p><strong>Success:</strong> ' + response + ' new contacts imported.</p></div>');
} else {
$('#import-output').html('<div class="error"><p><strong>Error:</strong> No new contacts found.</p></div>');
}
});
});
$( ".delete-import-group" ).on( "click", function() {
var button = $(this);
button.attr('disabled', 'disabled');
button.closest('tr').children('.import-date').children('.progress-bar').width('100');
if(confirm("WARNING: All users from this import will be deleted, and any user content will be reassigned to your account.") == true) {
button.closest('tr').children('.import-date').children('.progress-bar').width($('#import-groups').width());
var data = {
'action' : 'delete_import_group',
'group_id' : button.data('delete')
};
$.post(ajaxurl, data, function(response) {
if(response.success == true) {
button.closest('tr').remove();
} else {
button.closest('tr').children('.import-date').children('.progress-bar').width('0');
}
});
} else {
button.closest('tr').children('.import-date').children('.progress-bar').width('0');
button.removeAttr('disabled');
}
});
//
// Export
//
// Process export step
var processExportStep = function(button, actions, index, offset, max) {
// If current objective is complete, move to next stage
if(offset >= max) {
startExport(button, actions, index + 1);
return;
}
//console.log('export step for ' + actions[index].action + ' with offset ' + offset);
var data = {
'action' : 'wpf_batch_step',
'hook' : actions[index].action,
'offset' : offset,
'number' : actions[index].batchSize
};
offset = parseInt(offset) + parseInt(actions[index].batchSize);
// Don't show "processing" counts for more than the maximum
if(offset > max) {
offset = max;
}
button.find('span').html('Processing ' + offset + ' of ' + max + ' ' + actions[index].title);
$.post(ajaxurl, data, function(result) {
processExportStep(button, actions, index, offset, max );
});
}
// Start export stage
var startExport = function(button, actions, index) {
if(index >= actions.length) {
completeExport( button );
return;
}
button.html('<i class="fa-li fa fa-spinner fa-spin"></i><span>Beginning ' + actions[index].title + ' Processing</span>');
var data = {
'action' : 'wpf_batch_init',
'hook' : actions[index].action
};
$.post(ajaxurl, data, function(total) {
if(actions[index].batchSize > total) {
actions[index].batchSize = parseFloat(total);
}
processExportStep(button, actions, index, 0, total);
});
}
// Complete export
var completeExport = function(button) {
button.html('Processing Complete');
window.onbeforeunload = null;
}
// Export button handlers
$( "#export-btn" ).on( "click", function() {
if($('input.export-options:checked').length == 0) {
return
}
if (confirm('Export will now run. Do not close this page until export is complete.') != true) {
return;
}
// Set up warning if they try and leave the page
window.onbeforeunload = function() {
return 'WARNING: Currently processing. Leaving the page will abort the batch process.';
};
$(this).attr('disabled', 'disabled');
var button = $(this);
var actions = [];
$('input.export-options:checked').each(function(i){
actions[i] = { 'action': $(this).val(), 'title': $(this).attr('data-title'), 'batchSize': parseFloat($(this).attr('data-batch-size')) }
});
startExport(button, actions, 0);
});
//
// Test Connection and perform initial sync
//
// Syncs users in batches to avoid timeouts on sites with many users
var processStep = function(offset, number, max, crmContainer) {
var currentSyncNumber = offset + number;
if(currentSyncNumber >= max) {
currentSyncNumber = max;
}
$( "a#test-connection" ).html('<i class="fa-li fa fa-spinner fa-spin"></i><span>Syncing ' + currentSyncNumber + ' of ' + max + ' users</span>');
var data = {
'action' : 'wpf_sync_users',
'offset' : offset,
'number' : number
};
$.post(ajaxurl, data, function(response) {
if(response.success == true) {
if(offset + number >= max) {
// Update button to indicate we're done
$( "a#test-connection" ).html('Complete');
$('#connection_configured').val(true);
$(crmContainer).find('#connection-output').html('<div class="updated"><p><strong>Congratulations:</strong> you\'ve successfully established a connection to ' + $(crmContainer).attr('data-name') + ' and your data has been imported. Press "Save Changes" below to finalize.</p></div>');
} else {
processStep(number + offset, number, max, crmContainer);
}
}
});
}
// Button handler for test connection / resync
$( "a#test-connection" ).on( "click", function() {
$(this).html('<i class="fa-li fa fa-spinner fa-spin"></i> Connecting');
$(this).attr('disabled', 'disabled');
var button = $(this);
var crmContainer = $(this).parents('div.crm-config');
var crm = $(crmContainer).attr('data-crm');
var data = {
'action' : 'wpf_test_connection_' + crm
};
// Add the submitted data
postFields = $(this).attr('data-post-fields').split(',');
$(postFields).each(function(index, el) {
data[el] = $('input#' + el).val();
});
// Test the CRM connection
$.post(ajaxurl, data, function(response) {
if(response.success != true) {
$(crmContainer).find('#connection-output').html('<div class="error"><p><strong>Error: </strong>' + response.data + '</p></div>');
button.html('Retry').removeClass('btn-success').addClass('btn-danger').removeAttr('disabled');
} else {
// Sync tags and custom fields
button.html('<i class="fa-li fa fa-spinner fa-spin"></i><span>Syncing Tags & Fields</span>').addClass('btn-success');
data['action'] = 'wpf_sync';
$.post(ajaxurl, data, function(response) {
if(response.success == true) {
// Process user sync in batches
processStep(0, parseFloat(button.attr('data-batch-size')), parseFloat(button.attr('data-total-users')), $(crmContainer));
}
});
}
});
});
//
// Change CRM
//
$('#wpf-settings select#crm').change(function(event) {
$('#wpf-settings').find('div.crm-active').slideUp().removeClass('crm-active').addClass('hidden');
$('#wpf-settings').find('div#' + $(this).val()).slideDown().addClass('crm-active').removeClass('hidden');
});
//
// Activate / deactivate license
//
$( "#edd-license" ).on( "click", function() {
$(this).html('<i class="fa-li fa fa-spinner fa-spin"></i> Connecting');
$(this).attr('disabled', 'disabled');
var button = $(this);
var data = {
'action' : $(this).attr('data-action'),
'key' : $('#license_key').val()
};
$.post(ajaxurl, data, function(response) {
console.dir(response);
if(response.success == true && response.data == 'activated') {
button.html('Deactivate License').removeAttr('disabled').attr('data-action', 'edd_deactivate');
$('#license_key').attr('disabled', 'disabled');
$('#license_status').val('valid');
} else if(response.success == true && response.data == 'deactivated') {
button.html('Activate License').removeAttr('disabled').attr('data-action', 'edd_activate');
$('#license_key').removeAttr('disabled');
$('#license_status').val('invalid');
} else {
button.html('Retry').addClass('btn-danger').removeAttr('disabled');
$('#connection-output-edd').html('<div class="error validation-error"><p>Error processing request. Debugging info below:</p></div><br/>' + response.data);
}
});
});
$('table#contact-fields select.select4-crm-field').change(function(event) {
if(!$(this).val()) {
$(this).closest('td').siblings().find('input.contact-fields-checkbox').attr('disabled');
$(this).closest('tr').find('input.contact-fields-checkbox').prop('checked', false);
$(this).closest('tr').removeClass('success');
} else {
$(this).closest('td').siblings().find('input.contact-fields-checkbox').removeAttr('disabled');
$(this).closest('tr').find('input.contact-fields-checkbox').prop('checked', true);
$(this).closest('tr').addClass('success');
}
});
// When a checkbox is configured to unlock other options
$('[data-unlock]').click(function() {
var targets = $(this).data('unlock').split(" ");
var ischecked = $(this).prop('checked');
$.each(targets, function( index, target ) {
$('#' + target).closest( 'tr' ).toggleClass('disabled');
$('#' + target).prop('disabled', !ischecked);
});
});
$( ".contact-fields-checkbox" ).on( "click", function() {
$(this).closest( 'tr' ).toggleClass('success');
});
$('form').bind('submit', function() {
$(this).find(':input').removeAttr('disabled');
});
} // end WPF settings page listeners
});