PluginProbe
WP Reset / 1.55
WP Reset v1.55
trunk 1.0 1.1 1.11 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.77 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.90 All 42 releases
wp-reset / js / wp-reset.js

wp-reset.js in WP Reset 1.55, at js/wp-reset.js

608 lines 16.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WP Reset
3 * https://wpreset.com/
4 * (c) WebFactory Ltd, 2017-2019
5 */
6
7 jQuery(document).ready(function($) {
8 // init tabs
9 $('#wp-reset-tabs')
10 .tabs({
11 activate: function(event, ui) {
12 localStorage.setItem('wp-reset-tabs', $('#wp-reset-tabs').tabs('option', 'active'));
13 },
14 active: localStorage.getItem('wp-reset-tabs') || 0
15 })
16 .show();
17
18 // delete transients
19 $('.tools_page_wp-reset').on('click', '#delete-transients', 'click', function(e) {
20 e.preventDefault();
21
22 run_tool(this, 'delete_transients');
23
24 return false;
25 }); // delete transients
26
27 // delete uploads
28 $('.tools_page_wp-reset').on('click', '#delete-uploads', 'click', function(e) {
29 e.preventDefault();
30
31 run_tool(this, 'delete_uploads');
32
33 return false;
34 }); // delete uploads
35
36 // delete themes
37 $('.tools_page_wp-reset').on('click', '#delete-themes', 'click', function(e) {
38 e.preventDefault();
39
40 run_tool(this, 'delete_themes');
41
42 return false;
43 }); // delete themes
44
45 // delete plugins
46 $('.tools_page_wp-reset').on('click', '#delete-plugins', 'click', function(e) {
47 e.preventDefault();
48
49 run_tool(this, 'delete_plugins');
50
51 return false;
52 }); // delete plugins
53
54 // drop custom tables
55 $('.tools_page_wp-reset').on('click', '#drop-custom-tables', 'click', function(e) {
56 e.preventDefault();
57
58 run_tool(this, 'drop_custom_tables');
59
60 return false;
61 }); // drop custom tables
62
63 // truncate custom tables
64 $('.tools_page_wp-reset').on('click', '#truncate-custom-tables', 'click', function(e) {
65 e.preventDefault();
66
67 run_tool(this, 'truncate_custom_tables');
68
69 return false;
70 }); // truncate custom tables
71
72 // delete htaccess file
73 $('.tools_page_wp-reset').on('click', '#delete-htaccess', 'click', function(e) {
74 e.preventDefault();
75
76 run_tool(this, 'delete_htaccess');
77
78 return false;
79 }); // delete htaccess file
80
81 // compare snapshot
82 $('#wpr-snapshots').on('click', '.compare-snapshot', 'click', function(e) {
83 e.preventDefault();
84 uid = $(this).data('ss-uid');
85 button = $(this);
86
87 block_ui($(button).data('wait-msg'));
88 $.get({
89 url: ajaxurl,
90 data: {
91 action: 'wp_reset_run_tool',
92 _ajax_nonce: wp_reset.nonce_run_tool,
93 tool: 'compare_snapshots',
94 extra_data: uid
95 }
96 })
97 .always(function(data) {
98 swal.close();
99 })
100 .done(function(data) {
101 if (data.success) {
102 msg = $(button)
103 .data('title')
104 .replace('%s', $(button).data('name'));
105 swal({
106 width: '90%',
107 title: msg,
108 html: data.data,
109 showConfirmButton: false,
110 allowEnterKey: false,
111 focusConfirm: false,
112 showCloseButton: true,
113 customClass: 'compare-snapshots'
114 });
115 } else {
116 swal({
117 type: 'error',
118 title: wp_reset.documented_error + ' ' + data.data
119 });
120 }
121 })
122 .fail(function(data) {
123 swal({ type: 'error', title: wp_reset.undocumented_error });
124 });
125
126 return false;
127 }); // compare snapshot
128
129 // restore snapshot
130 $('#wpr-snapshots').on('click', '.restore-snapshot', 'click', function(e) {
131 e.preventDefault();
132 uid = $(this).data('ss-uid');
133
134 run_tool(this, 'restore_snapshot', uid);
135
136 return false;
137 }); // restore snapshot
138
139 // download snapshot
140 $('#wpr-snapshots').on('click', '.download-snapshot', 'click', function(e) {
141 e.preventDefault();
142 uid = $(this).data('ss-uid');
143 button = this;
144
145 block_ui($(this).data('wait-msg'));
146 $.get({
147 url: ajaxurl,
148 data: {
149 action: 'wp_reset_run_tool',
150 _ajax_nonce: wp_reset.nonce_run_tool,
151 tool: 'download_snapshot',
152 extra_data: uid
153 }
154 })
155 .always(function(data) {
156 swal.close();
157 })
158 .done(function(data) {
159 if (data.success) {
160 msg = $(button)
161 .data('success-msg')
162 .replace('%s', data.data);
163 swal({ type: 'success', title: msg });
164 } else {
165 swal({
166 type: 'error',
167 title: wp_reset.documented_error + ' ' + data.data
168 });
169 }
170 })
171 .fail(function(data) {
172 swal({ type: 'error', title: wp_reset.undocumented_error });
173 });
174
175 return false;
176 }); // downlod snapshot
177
178 // delete snapshot
179 $('#wpr-snapshots').on('click', '.delete-snapshot', 'click', function(e) {
180 e.preventDefault();
181 uid = $(this).data('ss-uid');
182
183 run_tool(this, 'delete_snapshot', uid);
184
185 return false;
186 }); // delete snapshot
187
188 // create snapshot
189 $('.tools_page_wp-reset').on('click', '.create-new-snapshot', 'click', function(e) {
190 e.preventDefault();
191 button = $('#create-new-snapshot-primary');
192
193 swal({
194 title: $(button).data('title'),
195 type: 'question',
196 text: $(button).data('text'),
197 input: 'text',
198 inputPlaceholder: $(button).data('placeholder'),
199 showCancelButton: true,
200 focusConfirm: false,
201 confirmButtonText: $(button).data('btn-confirm'),
202 cancelButtonText: wp_reset.cancel_button,
203 width: 600
204 }).then(result => {
205 if (typeof result.value != 'undefined') {
206 block = block_ui($(button).data('msg-wait'));
207 $.get({
208 url: ajaxurl,
209 data: {
210 action: 'wp_reset_run_tool',
211 _ajax_nonce: wp_reset.nonce_run_tool,
212 tool: 'create_snapshot',
213 extra_data: result.value
214 }
215 })
216 .always(function(data) {
217 swal.close();
218 })
219 .done(function(data) {
220 if (data.success) {
221 swal({
222 type: 'success',
223 title: $(button).data('msg-success')
224 }).then(result => {
225 location.reload();
226 });
227 } else {
228 swal({
229 type: 'error',
230 title: wp_reset.documented_error + ' ' + data.data
231 });
232 }
233 })
234 .fail(function(data) {
235 swal({ type: 'error', title: wp_reset.undocumented_error });
236 });
237 } // if confirmed
238 });
239
240 return false;
241 }); // create snapshot
242
243 // show/hide extra table info in snapshot diff
244 $('body.tools_page_wp-reset').on('click', '.header-row', function(e) {
245 e.preventDefault();
246
247 parent = $(this).parents('div.wpr-table-container > table > tbody');
248 $(' > tr:not(.header-row)', parent).toggleClass('hidden');
249
250 $('span.dashicons', parent)
251 .toggleClass('dashicons-arrow-down-alt2')
252 .toggleClass('dashicons-arrow-up-alt2');
253
254 return false;
255 }); // show hide extra info in diff
256
257 // standard way of running a tool, with confirmation, loading and success message
258 function run_tool(button, tool_name, extra_data) {
259 confirm_action(
260 wp_reset.confirm_title,
261 $(button).data('text-confirm'),
262 $(button).data('btn-confirm'),
263 wp_reset.cancel_button
264 ).then(result => {
265 if (result.value) {
266 block = block_ui($(button).data('text-wait'));
267 $.get({
268 url: ajaxurl,
269 data: {
270 action: 'wp_reset_run_tool',
271 _ajax_nonce: wp_reset.nonce_run_tool,
272 tool: tool_name,
273 extra_data: extra_data
274 }
275 })
276 .always(function(data) {
277 swal.close();
278 })
279 .done(function(data) {
280 if (data.success) {
281 if (data.data == 1) {
282 msg = $(button).data('text-done-singular');
283 } else {
284 msg = $(button)
285 .data('text-done')
286 .replace('%n', data.data);
287 }
288 swal({ type: 'success', title: msg }).then(() => {
289 if (tool_name == 'restore_snapshot') {
290 location.reload();
291 }
292 });
293 if (tool_name == 'delete_snapshot') {
294 $('#wpr-ss-' + extra_data).remove();
295 if ($('#wpr-snapshots tr').length <= 1) {
296 $('#wpr-snapshots').hide();
297 $('#ss-no-snapshots').show();
298 }
299 }
300 } else {
301 swal({
302 type: 'error',
303 title: wp_reset.documented_error + ' ' + data.data
304 });
305 }
306 })
307 .fail(function(data) {
308 swal({ type: 'error', title: wp_reset.undocumented_error });
309 });
310 } // if confirmed
311 });
312 } // run_tool
313
314 // display a message while an action is performed
315 function block_ui(message) {
316 tmp = swal({
317 text: message,
318 type: false,
319 imageUrl: wp_reset.icon_url,
320 onOpen: () => {
321 $(swal.getImage()).addClass('rotating');
322 },
323 imageWidth: 100,
324 imageHeight: 100,
325 imageAlt: message,
326 allowOutsideClick: false,
327 allowEscapeKey: false,
328 allowEnterKey: false,
329 showConfirmButton: false
330 });
331
332 return tmp;
333 } // block_ui
334
335 // display dialog to confirm action
336 function confirm_action(title, question, btn_confirm, btn_cancel) {
337 tmp = swal({
338 title: title,
339 type: 'question',
340 html: question,
341 showCancelButton: true,
342 focusConfirm: false,
343 confirmButtonText: btn_confirm,
344 cancelButtonText: btn_cancel,
345 confirmButtonColor: '#dd3036',
346 width: 600
347 });
348
349 return tmp;
350 } // confirm_action
351
352 $('#wp_reset_form').on('submit', function(e, confirmed) {
353 if (!confirmed) {
354 $('#wp_reset_submit').trigger('click');
355 e.preventDefault();
356 return false;
357 }
358
359 $(this)
360 .off('submit')
361 .submit();
362 return true;
363 }); // bypass default submit behaviour
364
365 $('#wp_reset_submit').click(function(e) {
366 if ($('#wp_reset_confirm').val() !== 'reset') {
367 swal({
368 title: wp_reset.invalid_confirmation_title,
369 text: wp_reset.invalid_confirmation,
370 type: 'error',
371 confirmButtonText: wp_reset.ok_button
372 });
373
374 e.preventDefault();
375 return false;
376 } // wrong confirmation code
377
378 message = wp_reset.confirm1 + '<br>' + wp_reset.confirm2;
379 swal({
380 title: wp_reset.confirm_title,
381 type: 'question',
382 html: message,
383 showCancelButton: true,
384 focusConfirm: false,
385 confirmButtonText: wp_reset.confirm_button,
386 cancelButtonText: wp_reset.cancel_button,
387 confirmButtonColor: '#dd3036',
388 width: 600
389 }).then(result => {
390 if (result.value === true) {
391 block_ui(wp_reset.doing_reset);
392 $('#wp_reset_form').trigger('submit', true);
393 }
394 });
395
396 e.preventDefault();
397 return false;
398 }); // reset submit
399
400 // collapse / expand card
401 $('.card').on('click', '.toggle-card', function(e) {
402 e.preventDefault();
403
404 card = $(this)
405 .parents('.card')
406 .toggleClass('collapsed');
407 $('.dashicons', this)
408 .toggleClass('dashicons-arrow-up-alt2')
409 .toggleClass('dashicons-arrow-down-alt2');
410 $(this).blur();
411
412 cards = localStorage.getItem('wp-reset-cards');
413 if (cards == null) {
414 cards = new Object();
415 } else {
416 cards = JSON.parse(cards);
417 }
418
419 if (card.hasClass('collapsed')) {
420 cards[card.attr('id')] = 'collapsed';
421 } else {
422 cards[card.attr('id')] = 'expanded';
423 }
424 localStorage.setItem('wp-reset-cards', JSON.stringify(cards));
425
426 return false;
427 }); // toggle-card
428
429 // init cards; collapse those that need collapsing
430 cards = localStorage.getItem('wp-reset-cards');
431 if (cards != null) {
432 cards = JSON.parse(cards);
433 }
434 $.each(cards, function(card_name, card_value) {
435 if (card_value == 'collapsed') {
436 $('a.toggle-card', '#' + card_name).trigger('click');
437 }
438 });
439
440 // dismiss notice / pointer
441 $('.wpr-dismiss-notice').on('click', function(e) {
442 notice_name = $(this).data('notice');
443 if (!notice_name) {
444 return true;
445 }
446
447 if ($(this).data('survey')) {
448 $('#survey-dialog').dialog('close');
449 }
450
451 $.get(ajaxurl, {
452 notice_name: notice_name,
453 _ajax_nonce: wp_reset.nonce_dismiss_notice,
454 action: 'wp_reset_dismiss_notice'
455 });
456
457 $(this)
458 .parents('.notice-wrapper')
459 .fadeOut();
460
461 e.preventDefault();
462 return false;
463 }); // dismiss notice
464
465 // maybe init survey dialog
466 if (wp_reset.open_survey) {
467 $('#survey-dialog').dialog({
468 dialogClass: 'wp-dialog wpr-dialog wpr-survey-dialog',
469 modal: 1,
470 resizable: false,
471 width: 800,
472 height: 'auto',
473 show: 'fade',
474 hide: 'fade',
475 close: function(event, ui) {},
476 open: function(event, ui) {
477 wpr_fix_dialog_close(event, ui);
478 },
479 autoOpen: true,
480 closeOnEscape: true
481 });
482 }
483
484 // turn questions into checkboxes
485 $('.question-wrapper').on('click', function(e) {
486 if ($(this).hasClass('selected')) {
487 $(this).removeClass('selected');
488 } else {
489 if ($('.question-wrapper.selected').length >= 2) {
490 swal({
491 type: 'error',
492 allowOutsideClick: false,
493 text: 'You can choose only up to 2 features at a time.'
494 });
495 } else {
496 $(this).addClass('selected');
497 }
498 }
499
500 e.preventDefault();
501 return false;
502 });
503
504 // submit and hide survey
505 $('.submit-survey').on('click', function(e) {
506 if ($('.question-wrapper.selected').length != 2 && $('.question-wrapper.selected').length != 1) {
507 swal({
508 type: 'error',
509 allowOutsideClick: false,
510 text: 'Please choose 1 or 2 features you would like us to build next.'
511 });
512 return false;
513 }
514
515 if (
516 $('#survey-dialog .custom-input').val() == '' &&
517 $('#survey-dialog .custom-input')
518 .parents('div.question-wrapper')
519 .hasClass('selected')
520 ) {
521 swal({
522 type: 'error',
523 allowOutsideClick: false,
524 text: 'Please describe the custom feature you need.'
525 });
526 return false;
527 }
528
529 answers = '';
530 $('.question-wrapper.selected').each(function(i, el) {
531 answers += $(el).data('value') + ',';
532 });
533
534 $.post(ajaxurl, {
535 survey: 'features',
536 answers: answers,
537 emailme: $('#survey-dialog #emailme:checked').val(),
538 custom_answer: $('#survey-dialog .custom-input').val(),
539 _ajax_nonce: wp_reset.nonce_submit_survey,
540 action: 'wp_reset_submit_survey'
541 });
542
543 $('#survey-dialog').dialog('close');
544 swal({
545 type: 'success',
546 text: 'Thank you for your time! We appriciate your input!'
547 });
548
549 e.preventDefault();
550 return false;
551 });
552
553 $('.tools_page_wp-reset').on('click', '.open-webhooks-dialog', function(e) {
554 $(this).blur();
555 $('#webhooks-dialog').dialog('open');
556
557 e.preventDefault();
558 return false;
559 });
560
561 // webhooks dialog init
562 $('#webhooks-dialog').dialog({
563 dialogClass: 'wp-dialog wpr-dialog webhooks-dialog',
564 modal: 1,
565 resizable: false,
566 title: 'WP Webhooks - Connect WordPress to any 3rd party system',
567 width: 550,
568 height: 'auto',
569 show: 'fade',
570 hide: 'fade',
571 open: function(event, ui) {
572 wpr_fix_dialog_close(event, ui);
573 $(this)
574 .siblings()
575 .find('span.ui-dialog-title')
576 .html(wp_reset.webhooks_dialog_title);
577 },
578 close: function(event, ui) {},
579 autoOpen: false,
580 closeOnEscape: true
581 });
582 $(window).resize(function(e) {
583 $('#webhooks-dialog').dialog('option', 'position', {
584 my: 'center',
585 at: 'center',
586 of: window
587 });
588 });
589
590 jQuery('#install-webhooks').on('click', function(e) {
591 $('#webhooks-dialog').dialog('close');
592 jQuery('body').append(
593 '<div style="width:550px;height:450px; position:fixed;top:10%;left:50%;margin-left:-275px; color:#444; background-color: #fbfbfb;border:1px solid #DDD; border-radius:4px;box-shadow: 0px 0px 0px 4000px rgba(0, 0, 0, 0.85);z-index: 9999999;"><iframe src="' +
594 wp_reset.webhooks_install_url +
595 '" style="width:100%;height:100%;border:none;" /></div>'
596 );
597 jQuery('#wpwrap').css('pointer-events', 'none');
598 e.preventDefault();
599 return false;
600 });
601 }); // onload
602
603 function wpr_fix_dialog_close(event, ui) {
604 jQuery('.ui-widget-overlay').bind('click', function() {
605 jQuery('#' + event.target.id).dialog('close');
606 });
607 } // wpr_fix_dialog_close
608