PluginProbe
WP Reset / 1.70
WP Reset v1.70
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.70, at js/wp-reset.js

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