admin-settings.js
12.7 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
jQuery(document).ready(function($) {
var working = false;
var message, container, messageBox, deleteInput, deleteMsgs, buttons, confirm, cancel, lineBreak;
container = document.createElement( 'div' );
messageBox = document.createElement( 'p' );
deleteInput = document.createElement( 'input' );
deleteInput.type = 'text';
deleteInput.id = 'confirmDeleteInput';
buttons = document.createElement( 'div' );
buttons.style.marginTop = '10px';
buttons.style.backgroundColor = '#f4f5f6';
confirm = document.createElement( 'div' );
confirm.style.padding = '8px';
confirm.style.cursor = 'default';
confirm.style.backgroundColor = '#d9534f';
confirm.style.borderColor = '#d9534f';
confirm.style.fontSize = '14pt';
confirm.style.fontWeight = 'bold';
confirm.style.color = '#ffffff';
confirm.style.borderRadius = '4px';
cancel = document.createElement( 'div' );
cancel.style.padding = '8px';
cancel.style.cursor = 'default';
cancel.style.backgroundColor = '#5bc0de';
cancel.style.borderColor = '#5bc0de';
cancel.style.fontSize = '14pt';
cancel.style.fontWeight = 'bold';
cancel.style.color = '#ffffff';
cancel.style.borderRadius = '4px';
lineBreak = document.createElement( 'br' );
container.classList.add( 'message' );
messageBox.innerHTML += 'This will DELETE all forms, form submissions,' +
' and deactivate Ninja Forms';
messageBox.appendChild( lineBreak );
messageBox.innerHTML += '<br>Type <span style="color:red;">DELETE</span>' +
' to' +
' confirm';
container.appendChild( messageBox );
container.appendChild( deleteInput );
container.appendChild( lineBreak );
deleteMsgs = document.createElement( 'div' );
deleteMsgs.id = 'progressMsg';
deleteMsgs.style.display = 'none';
deleteMsgs.style.color = 'red';
container.appendChild( deleteMsgs );
confirm.innerHTML = 'Delete';
confirm.classList.add( 'confirm', 'nf-button', 'primary' );
confirm.style.float = 'left';
cancel.innerHTML = 'Cancel';
cancel.classList.add( 'cancel', 'nf-button', 'secondary', 'cancel-delete-all' );
cancel.style.float = 'right';
buttons.appendChild( confirm );
buttons.appendChild( cancel );
buttons.classList.add( 'buttons' );
container.appendChild( buttons );
message = document.createElement( 'div' );
message.appendChild( container );
var downgradeContainer, downgradeTitle, downgradeWarning, downgradeCTA, downgradeInput;
downgradeInput = document.createElement( 'input' );
downgradeInput.setAttribute( 'type', 'text' );
downgradeInput.id = 'downgradeConfirmInput';
downgradeInput.style.marginBottom = '15px';
downgradeInput.style.padding = '7px';
downgradeInput.style.border = '1px solid #D3D3D3';
downgradeInput.style.width = '100%';
downgradeInput.style.height = '3em';
downgradeCTA = document.createElement( 'p' );
downgradeCTA.innerHTML = nfAdmin.i18n.downgradeConfirmMessage;
downgradeWarning = document.createElement( 'p' );
downgradeWarning.innerHTML = nfAdmin.i18n.downgradeWarningMessage;
downgradeWarning.style.color = 'red';
downgradeTitle = document.createElement( 'h3' );
downgradeTitle.innerHTML = nfAdmin.i18n.downgradeMessage;
downgradeContainer = document.createElement( 'div' );
downgradeContainer.appendChild( downgradeTitle );
downgradeContainer.appendChild( downgradeWarning );
downgradeContainer.appendChild( downgradeCTA );
downgradeContainer.appendChild( downgradeInput );
// set up delete model with all the elements created above
deleteAllDataModal = new jBox( 'Modal', {
width: 450,
addClass: 'dashboard-modal',
overlay: true,
closeOnClick: 'body'
} );
deleteAllDataModal.setContent( message.innerHTML );
deleteAllDataModal.setTitle( 'Delete All Ninja Forms Data?' );
// add event listener for cancel button
var btnCancel = deleteAllDataModal.container[0].getElementsByClassName('cancel')[0];
btnCancel.addEventListener('click', function() {
if( ! working ) {
deleteAllDataModal.close();
}
} );
var doAllDataDeletions = function( formIndex ) {
var last_form = 0;
// Gives the user confidence things are happening
$( '#progressMsg' ).html( 'Deleting submissions for '
+ nfAdmin.forms[ formIndex ].title + "" + ' ( ID: '
+ nfAdmin.forms[ formIndex ].id + ' )' );
$( '#progressMsg').show();
// notify php this is the last one so it delete data and deactivate NF
if( formIndex === nfAdmin.forms.length - 1 ) {
last_form = 1;
}
// do this deletion thang
$.post(
nfAdmin.ajax_url,
{
'action': 'nf_delete_all_data',
'form': nfAdmin.forms[ formIndex ].id,
'security': nfAdmin.nonce,
'last_form': last_form
}
).then (function( response ) {
formIndex = formIndex + 1;
response = JSON.parse( response );
if(response.data.hasOwnProperty('errors')) {
var errors = response.data.errors;
var errorMsg = '';
if (Array.isArray(errors)) {
errors.forEach(function(error) {
errors += error + "\n";
})
} else {
errors = errors;
}
console.log('Delete All Data Errors: ', errors);
alert(errors);
return null;
}
// we expect success and then move to the next form
if( response.data.success ) {
if( formIndex < nfAdmin.forms.length ) {
doAllDataDeletions( formIndex )
} else {
// if we're finished deleting data then redirect to plugins
if( response.data.plugin_url ) {
window.location = response.data.plugin_url;
}
}
}
} ).fail( function( xhr, status, error ) {
// writes error messages to console to help us debug
console.log( xhr.status + ' ' + error + '\r\n' +
'There was an error deleting submissions for '
+ nfAdmin.forms[ formIndex ].title );
});
};
// Add event listener for delete button
var btnDelete = deleteAllDataModal.container[0].getElementsByClassName('confirm')[0];
btnDelete.addEventListener('click', function() {
var confirmVal = $('#confirmDeleteInput').val();
if (! working) {
working = true;
// Gotta type DELETE to play
if ('DELETE' === confirmVal) {
this.style.backgroundColor = '#9f9f9f';
this.style.borderColor = '#3f3f3f';
var cancelBtn = $( '.cancel-delete-all' );
cancelBtn.css( 'backgroundColor', '#9f9f9f' );
cancelBtn.css( 'borderColor', '#3f3f3f');
// this is the first one, so we'll start with index 0
doAllDataDeletions(0);
} else {
deleteAllDataModal.close();
working = false;
}
}
} );
$( '.js-delete-saved-field' ).click( function(){
var that = this;
var data = {
'action': 'nf_delete_saved_field',
'field': {
id: $( that ).data( 'id' )
},
'security': nfAdmin.nonce
};
$.post( nfAdmin.ajax_url, data )
.done( function( response ) {
$( that ).closest( 'tr').fadeOut().remove();
});
});
// Start building our downgrade model.
jQuery( '#nfDowngrade' ).click( function( e ) {
var data = {
class: 'nfDowngradeModal',
closeOnClick: 'body',
closeOnEsc: true,
// TODO: Maybe this should be build using DOM node construction?
content: downgradeContainer.innerHTML,
btnPrimary: {
text: nfAdmin.i18n.downgradeButtonPrimary,
class: 'nfDowngradeButtonPrimary',
callback: function( e ) {
// If our "Downgrade" button does not have have an attribute of disabled...
if( 'disabled' !== $('.nfDowngradeButtonPrimary').prop( 'disabled' ) ){
// ...get the url...
var url = window.location.href;
// ...split the url based on the question mark from the query string...
url = url.split( '?' );
// build the downgrade url and redirect the user.
url[0] = url[0] + '?page=ninja-forms&nf-switcher=rollback&security=' + nfAdmin.nonce;
window.location.replace( url[0] );
}
}
},
btnSecondary: {
text: nfAdmin.i18n.downgradeButtonSecondary,
class: 'nfDowngradeButtonSecondary',
callback: function( e ) {
// Close the modal if this button is clicked.
downgradeModel.toggleModal( false );
}
},
useProgressBar: false,
};
var downgradeModel = new NinjaModal( data );
// Style and add the disabled tag by default.
$('.nfDowngradeButtonPrimary').css( 'background', '#ccc' );
$('.nfDowngradeButtonPrimary').css( 'border', '#ccc 1px solid' );
$('.nfDowngradeButtonPrimary').prop( 'disabled', 'disabled' );
// Listen to our input and...
$('#downgradeConfirmInput').on( 'keyup', function(){
// ...if DOWNGRADE is typed then...
if( 'DOWNGRADE' == $('#downgradeConfirmInput').val() ) {
// ...apply our blue styling to button and remove disabled attribute.
$('.nfDowngradeButtonPrimary').css( 'background', '#1EA9EA' );
$('.nfDowngradeButtonPrimary').css( 'border', '#1EA9EA 1px solid' );
$('.nfDowngradeButtonPrimary').prop( 'disabled','' );
}
// ...if DOWNGRADE is not typed then...
if( 'DOWNGRADE' !== $('#downgradeConfirmInput').val() ) {
// ...set styling back to default and reapply the disabled prop.
$('.nfDowngradeButtonPrimary').css( 'background', '#ccc' );
$('.nfDowngradeButtonPrimary').css( 'border', '#ccc 1px solid' );
$('.nfDowngradeButtonPrimary').prop( 'disabled', 'disabled' );
}
})
});
$( document ).on( 'click', '#delete_on_uninstall', function( e ) {
deleteAllDataModal.open();
} );
$( document ).on( 'click', '.nf-delete-on-uninstall-yes', function( e ) {
e.preventDefault();
$( "#delete_on_uninstall" ).attr( 'checked', true );
} );
// If we're allowed to track site data...
if ( '1' == nfAdmin.allow_telemetry ) {
// Show the optout button.
$( '#nfTelOptin' ).addClass( 'hidden' );
$( '#nfTelOptout' ).removeClass( 'hidden' );
} // Otherwise...
else {
// Show the optin button.
$( '#nfTelOptout' ).addClass( 'hidden' );
$( '#nfTelOptin' ).removeClass( 'hidden' );
}
// If optin is clicked...
$( '#nfTelOptin' ).click( function( e ) {
// Hide the button.
$( '#nfTelOptin' ).addClass( 'hidden' );
$( '#nfTelSpinner' ).css( 'display', 'inline-block' );
// Hit AJAX endpoint and opt-in.
$.post( ajaxurl, { action: 'nf_optin', ninja_forms_opt_in: 1 },
function( response ) {
$( '#nfTelOptout' ).removeClass( 'hidden' );
$( '#nfTelSpinner' ).css( 'display', 'none' );
} );
} );
// If optout is clicked...
$( '#nfTelOptout' ).click( function( e ) {
// Hide the button.
$( '#nfTelOptout' ).addClass( 'hidden' );
$( '#nfTelSpinner' ).css( 'display', 'inline-block' );
// Hit AJAX endpoint and opt-out.
$.post( ajaxurl, { action: 'nf_optin', ninja_forms_opt_in: 0 },
function( response ) {
$( '#nfTelOptin' ).removeClass( 'hidden' );
$( '#nfTelSpinner' ).css( 'display', 'none' );
} );
} );
jQuery( '#nfTrashExpiredSubmissions' ).click( function( e ) {
var settings = {
content: '<p>' + nfAdmin.i18n.trashExpiredSubsMessage + '</p>',
btnPrimaryText: nfAdmin.i18n.trashExpiredSubsButtonPrimary,
btnSecondaryText: nfAdmin.i18n.trashExpiredSubsButtonSecondary,
batch_type: 'expired_submission_cleanup',
// extraData: [ 'test1', 'test2', 'test3' ]
}
new NinjaBatchProcessor( settings );
});
jQuery( '#nfRemoveMaintenanceMode' ).click( function( e ) {
var that = this;
jQuery( this ).addClass( 'disabled' ).attr( 'disabled', 'disabled' );
jQuery( '#nf_maintenanceModeProgress' ).html("<strong>Removing Maintenance Mode...</strong>" );
jQuery( '#nf_maintenanceModeProgress' ).fadeIn( 1 );
var data = {
action: 'nf_remove_maintenance_mode',
security: nf_settings.nonce,
};
$.post(
nf_settings.ajax_url,
data
).then (function( response ) {
response = JSON.parse( response );
// if there are errors then, console it out
if( response.data.errors ) {
console.log( response.data.errors );
}
jQuery( that ).removeClass( 'disabled' ).removeAttr( 'disabled' );
jQuery( '#nf_maintenanceModeProgress' ).html("<strong>Done.</strong>" );
jQuery( '#nf_maintenanceModeProgress' ).fadeOut( 600 );
} ).fail( function( xhr, status, error ) {
// writes error messages to console to help us debug
console.log( xhr.status + ' ' + error + '\r\n' +
'There was an error resetting maintenance mode' );
});
} );
});