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

398 lines 11.3 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-2018
5 */
6
7
8 jQuery(document).ready(function($) {
9 // init tabs
10 $('#wp-reset-tabs').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 }).show();
16
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
28 // delete uploads
29 $('.tools_page_wp-reset').on('click', '#delete-uploads', 'click', function(e) {
30 e.preventDefault();
31
32 run_tool(this, 'delete_uploads');
33
34 return false;
35 }); // delete uploads
36
37
38 // delete themes
39 $('.tools_page_wp-reset').on('click', '#delete-themes', 'click', function(e) {
40 e.preventDefault();
41
42 run_tool(this, 'delete_themes');
43
44 return false;
45 }); // delete themes
46
47
48 // delete plugins
49 $('.tools_page_wp-reset').on('click', '#delete-plugins', 'click', function(e) {
50 e.preventDefault();
51
52 run_tool(this, 'delete_plugins');
53
54 return false;
55 }); // delete plugins
56
57
58 // compare snapshot
59 $('#wpr-snapshots').on('click', '.compare-snapshot', 'click', function(e) {
60 e.preventDefault();
61 uid = $(this).data('ss-uid');
62 button = $(this);
63
64 block_ui($(button).data('wait-msg'));
65 $.get({
66 url: ajaxurl,
67 data: {
68 action: 'wp_reset_run_tool',
69 _ajax_nonce: wp_reset.nonce_run_tool,
70 tool: 'compare_snapshots',
71 extra_data: uid
72 }
73 }).always(function(data) {
74 swal.close();
75 }).done(function(data) {
76 if (data.success) {
77 msg = $(button).data('title').replace('%s', $(button).data('name'));
78 swal({
79 width: '90%',
80 title: msg,
81 html: data.data,
82 showConfirmButton: false,
83 allowEnterKey:false,
84 focusConfirm: false,
85 showCloseButton: true,
86 customClass: 'compare-snapshots'
87 });
88 } else {
89 swal({ type: 'error', title: wp_reset.documented_error + ' ' + data.data });
90 }
91 }).fail(function(data) {
92 swal({ type: 'error', title: wp_reset.undocumented_error });
93 });
94
95 return false;
96 }); // compare snapshot
97
98
99 // restore snapshot
100 $('#wpr-snapshots').on('click', '.restore-snapshot', 'click', function(e) {
101 e.preventDefault();
102 uid = $(this).data('ss-uid');
103
104 run_tool(this, 'restore_snapshot', uid);
105
106 return false;
107 }); // restore snapshot
108
109
110 // download snapshot
111 $('#wpr-snapshots').on('click', '.download-snapshot', 'click', function(e) {
112 e.preventDefault();
113 uid = $(this).data('ss-uid');
114 button = this;
115
116 block_ui($(this).data('wait-msg'));
117 $.get({
118 url: ajaxurl,
119 data: {
120 action: 'wp_reset_run_tool',
121 _ajax_nonce: wp_reset.nonce_run_tool,
122 tool: 'download_snapshot',
123 extra_data: uid
124 }
125 }).always(function(data) {
126 swal.close();
127 }).done(function(data) {
128 if (data.success) {
129 msg = $(button).data('success-msg').replace('%s', data.data);
130 swal({ type: 'success', title: msg });
131 } else {
132 swal({ type: 'error', title: wp_reset.documented_error + ' ' + data.data });
133 }
134 }).fail(function(data) {
135 swal({ type: 'error', title: wp_reset.undocumented_error });
136 });
137
138 return false;
139 }); // downlod snapshot
140
141
142 // delete snapshot
143 $('#wpr-snapshots').on('click', '.delete-snapshot', 'click', function(e) {
144 e.preventDefault();
145 uid = $(this).data('ss-uid');
146
147 run_tool(this, 'delete_snapshot', uid);
148
149 return false;
150 }); // delete snapshot
151
152
153 // create snapshot
154 $('.tools_page_wp-reset').on('click', '.create-new-snapshot', 'click', function(e) {
155 e.preventDefault();
156 button = $('#create-new-snapshot-primary');
157
158 swal({ title: $(button).data('title'),
159 type: 'question',
160 text: $(button).data('text'),
161 input: 'text',
162 inputPlaceholder: $(button).data('placeholder'),
163 showCancelButton: true,
164 focusConfirm: false,
165 confirmButtonText: $(button).data('btn-confirm'),
166 cancelButtonText: wp_reset.cancel_button,
167 width: 600
168 }).then((result) => {
169 if (typeof result.value != 'undefined') {
170 block = block_ui($(button).data('msg-wait'));
171 $.get({
172 url: ajaxurl,
173 data: {
174 action: 'wp_reset_run_tool',
175 _ajax_nonce: wp_reset.nonce_run_tool,
176 tool: 'create_snapshot',
177 extra_data: result.value
178 }
179 }).always(function(data) {
180 swal.close();
181 }).done(function(data) {
182 if (data.success) {
183 swal({ type: 'success', title: $(button).data('msg-success') }).then((result) => {
184 location.reload();
185 });
186 } else {
187 swal({ type: 'error', title: wp_reset.documented_error + ' ' + data.data });
188 }
189 }).fail(function(data) {
190 swal({ type: 'error', title: wp_reset.undocumented_error });
191 });
192 } // if confirmed
193 });
194
195 return false;
196 }); // create snapshot
197
198 // show/hide extra table info in snapshot diff
199 $('body.tools_page_wp-reset').on('click', '.header-row', function(e) {
200 e.preventDefault();
201
202 parent = $(this).parents('div.wpr-table-container > table > tbody');
203 $(' > tr:not(.header-row)', parent).toggleClass('hidden');
204
205 $('span.dashicons', parent).toggleClass('dashicons-arrow-down-alt2').toggleClass('dashicons-arrow-up-alt2');
206
207 return false;
208 }); // show hide extra info in diff
209
210
211 // standard way of running a tool, with confirmation, loading and success message
212 function run_tool(button, tool_name, extra_data) {
213 confirm_action(wp_reset.confirm_title, $(button).data('text-confirm'), $(button).data('btn-confirm'), wp_reset.cancel_button)
214 .then((result) => {
215 if (result.value) {
216 block = block_ui($(button).data('text-wait'));
217 $.get({
218 url: ajaxurl,
219 data: {
220 action: 'wp_reset_run_tool',
221 _ajax_nonce: wp_reset.nonce_run_tool,
222 tool: tool_name,
223 extra_data: extra_data
224 }
225 }).always(function(data) {
226 swal.close();
227 }).done(function(data) {
228 if (data.success) {
229 msg = $(button).data('text-done').replace('%n', data.data);
230 swal({ type: 'success', title: msg }).then(() => {
231 if (tool_name == 'restore_snapshot') {
232 location.reload();
233 }
234 });
235 if (tool_name == 'delete_snapshot') {
236 $('#wpr-ss-' + extra_data).remove();
237 if ($('#wpr-snapshots tr').length <= 1) {
238 $('#wpr-snapshots').hide();
239 $('#ss-no-snapshots').show();
240 }
241 }
242 } else {
243 swal({ type: 'error', title: wp_reset.documented_error + ' ' + data.data });
244 }
245 }).fail(function(data) {
246 swal({ type: 'error', title: wp_reset.undocumented_error });
247 });
248 } // if confirmed
249 }
250 );
251 } // run_tool
252
253
254 // display a message while an action is performed
255 function block_ui(message) {
256 tmp = swal({ text: message,
257 type: false,
258 imageUrl: wp_reset.icon_url,
259 onOpen: () => { $(swal.getImage()).addClass('rotating'); },
260 imageWidth: 100,
261 imageHeight: 100,
262 imageAlt: message,
263 allowOutsideClick: false,
264 allowEscapeKey: false,
265 allowEnterKey: false,
266 showConfirmButton: false,
267 });
268
269 return tmp;
270 } // block_ui
271
272
273 // display dialog to confirm action
274 function confirm_action(title, question, btn_confirm, btn_cancel) {
275 tmp = swal({ title: title,
276 type: 'question',
277 html: question,
278 showCancelButton: true,
279 focusConfirm: false,
280 confirmButtonText: btn_confirm,
281 cancelButtonText: btn_cancel,
282 confirmButtonColor: '#dd3036',
283 width: 600
284 });
285
286 return tmp;
287 } // confirm_action
288
289
290 $('#wp_reset_form').on('submit', function(e, confirmed) {
291 if (!confirmed) {
292 $('#wp_reset_submit').trigger('click');
293 e.preventDefault();
294 return false;
295 }
296
297 $(this).off('submit').submit();
298 return true;
299 }); // bypass default submit behaviour
300
301
302 $('#wp_reset_submit').click(function(e) {
303 if ($('#wp_reset_confirm').val() !== 'reset') {
304 swal({ title: wp_reset.invalid_confirmation_title,
305 text: wp_reset.invalid_confirmation,
306 type: 'error',
307 confirmButtonText: wp_reset.ok_button,
308 });
309
310 e.preventDefault();
311 return false;
312 } // wrong confirmation code
313
314 message = wp_reset.confirm1 + '<br>' + wp_reset.confirm2;
315 swal({ title: wp_reset.confirm_title,
316 type: 'question',
317 html: message,
318 showCancelButton: true,
319 focusConfirm: false,
320 confirmButtonText: wp_reset.confirm_button,
321 cancelButtonText: wp_reset.cancel_button,
322 confirmButtonColor: '#dd3036',
323 width: 600
324 }).then((result) => {
325 if (result.value === true) {
326 block_ui(wp_reset.doing_reset);
327 $('#wp_reset_form').trigger('submit', true);
328 }
329 });
330
331 e.preventDefault();
332 return false;
333 }); // reset submit
334
335
336 // collapse / expand card
337 $('.card').on('click', '.toggle-card', function(e) {
338 e.preventDefault();
339
340 card = $(this).parents('.card').toggleClass('collapsed');
341 $('.dashicons', this).toggleClass('dashicons-arrow-up-alt2').toggleClass('dashicons-arrow-down-alt2');
342 $(this).blur();
343
344 cards = localStorage.getItem('wp-reset-cards');
345 if (cards == null) {
346 cards = new Object();
347 } else {
348 cards = JSON.parse(cards);
349 }
350
351 if (card.hasClass('collapsed')) {
352 cards[card.attr('id')] = 'collapsed';
353 } else {
354 cards[card.attr('id')] = 'expanded';
355 }
356 localStorage.setItem('wp-reset-cards', JSON.stringify(cards));
357
358 return false;
359 }); // toggle-card
360
361
362 // init cards; collapse those that need collapsing
363 cards = localStorage.getItem('wp-reset-cards');
364 if (cards != null) {
365 cards = JSON.parse(cards);
366 }
367 $.each(cards, function(card_name, card_value) {
368 if (card_value == 'collapsed') {
369 $('a.toggle-card', '#' + card_name).trigger('click');
370 }
371 });
372
373
374 // dismiss notice / pointer
375 $('.wpr-dismiss-notice').on('click', function(e) {
376 notice_name = $(this).data('notice');
377 if (!notice_name) {
378 return true;
379 }
380
381 if (notice_name == 'geoip_tab') {
382 $('#wp-reset-tabs').tabs('option', 'active', 0).tabs('disable', 3);
383 $('#wp-reset-tabs li:eq(3)').remove();
384 $('#wp-reset-tabs').tabs('refresh');
385 }
386
387 $.get(ajaxurl, { notice_name: notice_name,
388 _ajax_nonce: wp_reset.nonce_dismiss_notice,
389 action: 'wp_reset_dismiss_notice'
390 });
391
392 $(this).parents('.notice-wrapper').fadeOut();
393
394 e.preventDefault();
395 return false;
396 }); // dismiss notice
397 }); // onload
398