PluginProbe
MailerLite – WooCommerce integration / 2.0.5
MailerLite – WooCommerce integration v2.0.5
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / public / js / admin.js

admin.js in MailerLite – WooCommerce integration 2.0.5, at public/js/admin.js

701 lines 25.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var syncResourcesRequest = null;
2 var groupSaved = false;
3 jQuery(document).ready(function (a) {
4 var c = a("#woocommerce_mailerlite_group");
5
6 if (0 !== c.length) {
7 var d = c.next(".select2-container");
8 a('<span id="woo-ml-refresh-groups" class="woo-ml-icon-refresh" data-woo-ml-refresh-groups="true"></span>').insertAfter(d);
9 }
10
11 var e = !1;
12
13 a(document).on("click", "[data-woo-ml-refresh-groups]", function (b) {
14 if (b.preventDefault(), !e) {
15 refreshGroups();
16 }
17 });
18
19 let i = 0, j = a("#woo-ml-sync-untracked-resources-progress-bar");
20
21 a(document).on("click", "[data-woo-ml-sync-untracked-resources]", function (c) {
22
23 c.preventDefault();
24
25 syncResources(this);
26 });
27
28 var syncResources = function (el) {
29
30 var d = a(el);
31
32 orders_tracked = d.data("woo-ml-untracked-resources-count");
33 all_untracked_orders_left = d.data("woo-ml-untracked-resources-left");
34
35 i = d.data("woo-ml-untracked-resources-cycle");
36
37 fail = a('#woo-ml-sync-untracked-resources-fail');
38 success = a("#woo-ml-sync-untracked-resources-success");
39
40 let r = 0;
41
42 console.log("inside the loop!");
43
44 a.ajax({
45 url: woo_ml_post.ajax_url,
46 type: "post",
47 beforeSend: function () {
48 d.prop("disabled", !0);
49 j.show();
50 r++;
51 },
52 data: {
53 action: "post_woo_ml_sync_untracked_resources"
54 },
55 async: 1,
56 success: function (data) {
57
58 var response = JSON.parse(data);
59
60 if (response.allDone) {
61
62 console.log("done!");
63 console.log("Response: True");
64 d.hide();
65 j.hide();
66 fail.hide();
67 success.show();
68 } else if (response.error) {
69
70 if (response.message) {
71 fail.html(response.message);
72 }
73
74 d.prop('disabled', 0);
75 j.hide();
76 fail.show();
77 } else {
78
79 if (response.completed) {
80 let untracked = parseInt(d.data("woo-ml-untracked-resources-left")) - parseInt(response.completed);
81
82 d.data("woo-ml-untracked-resources-left", untracked);
83 d.html('Synchronize ' + untracked.toString() + ' untracked resources');
84 }
85
86 syncResources(el);
87 }
88 }
89 });
90 };
91
92 a(document).on('click', '[data-woo-ml-reset-resources-sync]', function (event) {
93
94 event.preventDefault();
95
96 a(this).prop('disabled', true);
97 a(this).text("Please wait. Do not close this window until the reset finishes.");
98
99 resetResourcesSync();
100 });
101
102 var resetResourcesSync = function (cancelSync = false) {
103
104 let data = {};
105 data.action = 'post_woo_ml_reset_resources_sync';
106 if(cancelSync) {
107 data.cancelSync = true;
108 }
109 a.ajax({
110 url: woo_ml_post.ajax_url,
111 type: 'post',
112 data: data,
113 async: true,
114 success: function (responseStr) {
115
116 var response = responseStr;
117
118 if (typeof responseStr === "string") {
119 response = parseJSON(responseStr);
120 }
121
122 if (response.allDone) {
123 window.location.reload();
124 } else {
125
126 resetResourcesSync();
127 }
128 }
129 });
130 }
131
132 var field = a("#woocommerce_mailerlite_api_key");
133
134 a('<button id="woo-ml-validate-key" class="button-primary">Validate Key</button>').insertAfter(field);
135
136 a(document).on("click", "#woo-ml-validate-key", function (b) {
137 if (b.preventDefault(), !e) {
138 var key = a("#woocommerce_mailerlite_api_key").val();
139 a.ajax({
140 url: woo_ml_post.ajax_url,
141 type: "post",
142 data: {
143 action: "post_woo_ml_validate_key",
144 key: key
145 },
146 success: function(a) {
147 location.reload()
148 }
149 })
150 }
151 });
152
153 if (a('#woocommerce_mailerlite_group').length > 0) {
154 a('#woocommerce_mailerlite_group').select2();
155 }
156
157 if (a('#woocommerce_mailerlite_ignore_product_list').length > 0) {
158 a('#woocommerce_mailerlite_ignore_product_list').select2();
159 }
160
161 var cs_field = a('#woocommerce_mailerlite_consumer_secret');
162
163 if (0 !== cs_field.length) {
164 var field_desc = cs_field.next(".description");
165 field_desc.closest('tr').after(
166 '<h2>Integration Details</h2>\
167 <p class="section-description">Customize MailerLite integration for WooCommerce</p>');
168 }
169
170 var ml_platform = '';
171
172 if (a('#ml_platform').length > 0) {
173 ml_platform = a('#ml_platform').val();
174 }
175
176 var tracking_field = a('#woocommerce_mailerlite_popups');
177
178 tracking_field.closest('tr').before(
179 '<h2>Popups</h2>\
180 <p class="section-description">Display pop-up subscribe forms created within MailerLite</p>');
181
182 var button = a('[name="save"]');
183
184 if (field.length !== 0 && (cs_field.length === 0 && ml_platform !== '2')) {
185 button.hide();
186 } else {
187 button.show();
188 }
189
190 var ignored_p_field = a('#woocommerce_mailerlite_ignore_product_list');
191
192 if (ignored_p_field.length !== 0) {
193 ignored_p_field.closest('tr').before(
194 '<h2>E-commerce Automations</h2>\
195 <p class="section-description">Customize settings for your e-commerce automations created in MailerLite </p>'
196 )
197 }
198
199 var auto_update_field = a('#woocommerce_mailerlite_auto_update_plugin');
200
201 if (auto_update_field.length !== 0) {
202 auto_update_field.closest('tr').before(
203 '<h2>Plugin Updates</h2>\
204 <p class="section-description">Customize settings for MailerLite plugin </p>'
205 )
206 }
207
208 var refreshGroups = function () {
209 var c = a(this);
210 c.removeClass("error"), c.addClass("running");
211 a.ajax({
212 url: woo_ml_post.ajax_url,
213 type: "post",
214 dataType: 'JSON',
215 data: {
216 action: "post_woo_ml_refresh_groups"
217 },
218 success: function (res) {
219 c.removeClass("running");
220
221 let has_group = false;
222
223 if (res.groups) {
224 a('#woocommerce_mailerlite_group').empty();
225
226 for (const [id, name] of Object.entries(res.groups)) {
227
228 if (res.current && parseInt(res.current) === parseInt(id)) {
229 has_group = true;
230 }
231
232 a('#woocommerce_mailerlite_group, #wooMlSubGroup').append(a('<option>', {
233 value: id,
234 text: name
235 }));
236 }
237 }
238
239 if (res.current && has_group) {
240 a('#woocommerce_mailerlite_group').val(res.current);
241 }
242 },
243 error: function (x, status) {
244 c.addClass("error");
245 }
246 });
247 }
248
249 var parseJSON = function (jsonStr) {
250 try {
251 var parsed = JSON.parse(jsonStr);
252
253 if (parsed && typeof parsed === "object") {
254 return parsed;
255 }
256 } catch (e) {
257 }
258
259 return false;
260 }
261
262 if (a('#woocommerce_mailerlite_api_key').length > 0) {
263 refreshGroups();
264 }
265
266 (function ($) {
267 var selectedGroup = null;
268
269 if($('#selectedGroupValue').val()) {
270 selectedGroup = $('#selectedGroupValue').val();
271 }
272 var errors = document.getElementsByClassName('woo-ml-wizard-error');
273 groupSelectInput();
274 $('.woo-ml-close, .close').on('click', function () {
275 $('.woo-ml-wizard-modal').each(function(index, currentElement) {
276 if(currentElement.style.display == 'block') {
277 currentElement.style.display = 'none';
278 }
279 });
280 });
281
282 $('#wooMlWizardApiKeyBtn').on('click', function (e) {
283
284 let apiKey = $('#wooMlApiKey').val();
285 if (apiKey.length == 0) {
286 if(errors.length == 0 ) {
287 $('.api-key-input').after('<span id="" class="woo-ml-wizard-error">Enter API Key</span>');
288 }
289 } else {
290 $('.woo-ml-wizard-error').remove();
291 buttonLoadingState($(this));
292 $.ajax({
293 url: woo_ml_post.ajax_url,
294 type: "post",
295 data: {
296 action: "post_woo_ml_validate_key",
297 key: apiKey
298 },
299 success: function (a) {
300 buttonLoadingState($('#wooMlWizardApiKeyBtn'), true);
301 window.onbeforeunload = null;
302 window.location.reload();
303 },
304 error: function (b) {
305 buttonLoadingState($('#wooMlWizardApiKeyBtn'), true);
306 console.log(b);
307 },
308 });
309
310 }
311 });
312 $('#createGroupModal').on('click', function () {
313 document.getElementById('wooMlWizardCreateGroupModal').style.display = "block";
314 });
315
316 $('#wooMlResetIntegration').on('click', function () {
317 document.getElementById('wooMlResetIntegrationModal').style.display = "block";
318 });
319
320 $('#resetIntegration').on('submit', function() {
321 buttonLoadingState($('#resetIntegrationBtn'));
322 });
323
324 $('#openDebugLog').on('click', function () {
325 buttonLoadingState($(this));
326 $.ajax({
327 url: woo_ml_post.ajax_url,
328 type: "post",
329 data: {
330 action: "woo_ml_get_debug_log"
331 },
332 success: function (res) {
333
334 if(res.success) {
335 $('#debugLogLines').text(res.log.replaceAll("<br />", "\n"));
336 document.getElementById('openDebugLogModal').style.display = "block";
337 }
338 buttonLoadingState($('#openDebugLog'), true);
339 if(!res.success && res.message) {
340 $('.woo-ml-header').after(`
341 <div class="woo-ml-alert">
342 <span class="woo-ml-closebtn"
343 onClick="this.parentElement.style.display='none';">&times;</span>
344 ${res.message ? res.message : "Something went wrong!"}
345 </div>
346 `);
347 }
348 },
349 error: function (x, status) {
350 buttonLoadingState($('#openDebugLog'), true);
351 console.log(x,status);
352 }
353 });
354
355 });
356
357 $('#createGroup').on('click', function () {
358 $('.woo-ml-wizard-error').remove();
359 if(!$('#wooMlCreateGroup').val()) {
360 $('#wooMlCreateGroup').after(`<span id="" class="woo-ml-wizard-error">Enter group name</span>`);
361 return false;
362 }
363 buttonLoadingState($(this));
364 let modal = document.getElementById('wooMlWizardCreateGroupModal');
365 modal.style.display = "block";
366 $.ajax({
367 url: woo_ml_post.ajax_url,
368 type: "post",
369 data: {
370 action: "woo_ml_create_group",
371 createGroup: $('#wooMlCreateGroup').val()
372 },
373 success: function (res) {
374 if(res.data?.id) {
375 $('#wooMlSubGroup').append($('<option>', {
376 value: res.data.id,
377 text: res.data.name
378 }));
379 removeExistingErrors();
380 $('.woo-ml-header').after(`
381 <div class="woo-ml-alert-success">
382 <span class="woo-ml-closebtn-success"
383 onClick="this.parentElement.style.display='none';">&times;</span>
384 Group created successfully
385 </div>
386 `);
387 groupSelectInput(res.data.id);
388 } else if(res?.id) {
389 $('#wooMlSubGroup').append($('<option>', {
390 value: res.id,
391 text: res.name
392 }));
393 removeExistingErrors();
394 $('.woo-ml-header').after(`
395 <div class="woo-ml-alert-success">
396 <span class="woo-ml-closebtn-success"
397 onClick="this.parentElement.style.display='none';">&times;</span>
398 Group created successfully
399 </div>
400 `);
401 groupSelectInput(res.id);
402 }
403 if(res.error) {
404 if(errors.length > 0 ) {
405 $('.woo-ml-wizard-error').remove();
406 }
407 $('#wooMlCreateGroup').after(`<span id="" class="woo-ml-wizard-error">${res.error}</span>`);
408 } else {
409 modal.style.display = "none";
410 }
411 buttonLoadingState($('#createGroup'), true);
412 },
413 error: function (x, status) {
414 buttonLoadingState($('#createGroup'), true);
415 console.log(x,status);
416 }
417 });
418 });
419 $('#startImport').on('click', function () {
420 if(errors.length > 0 ) {
421 $('.woo-ml-wizard-error').remove();
422 }
423 if(!$('#wooMlSubGroup').val()) {
424 $('.select2-container--mailerlite').parent().after(`<span id="" class="woo-ml-wizard-error">Select group</span>`);
425 return false;
426 } else if($('#consumerKey').length && !$('#consumerKey').val()) {
427 $('#consumerKey').after(`<span id="" class="woo-ml-wizard-error">Enter consumer key</span>`);
428 return false;
429 } else if($('#consumerSecret').length && !$('#consumerSecret').val()) {
430 $('#consumerSecret').after(`<span id="" class="woo-ml-wizard-error">Enter consumer secret</span>`);
431 return false;
432 }
433 importResources();
434 });
435
436 $("input[name='disable_checkout_sync']").change(function() {
437 if(this.checked) {
438 $(this).parent().nextAll('.woo-ml-alert-warning-small').show();
439 } else {
440 $(this).parent().nextAll('.woo-ml-alert-warning-small').hide();
441 }
442 });
443
444 $("input[name='checkout']").change(function() {
445 let block = $(this).closest('.settings-block');
446 let inputs = block.find('input, select');
447
448 if(this.checked) {
449 $('.checkout-settings-group').show();
450 inputs.map(function(key, elem){
451 elem.disabled = false;
452 });
453 } else {
454 inputs.map(function(key, elem){
455 $('.checkout-settings-group').hide();
456 if(elem.name !== 'checkout') {
457 elem.disabled = true;
458 }
459 });
460 }
461
462 });
463
464 $('#woo_ml_wizard_back_step_1').on('click', function(e) {
465 window.location.href = window.location.href + '&step=1';
466 });
467
468 $('#updateSettingsForm').on('submit', function (e) {
469 if(!$('#wooMlSubGroup').val()) {
470 e.preventDefault();
471 if(errors.length > 0 ) {
472 $('.woo-ml-wizard-error').remove();
473 }
474 $('.select2-container--mailerlite').parent().after(`<span id="" class="woo-ml-wizard-error mt-0-ml">Select group</span>`);
475
476 $([document.documentElement, document.body]).animate({
477 scrollTop: $(".select2-container--mailerlite").parent().offset().top
478 }, 1000);
479 return false;
480 }
481 buttonLoadingState($("#updateSettingsBtn"));
482 $(this).unbind('submit').submit();
483
484 });
485 const importResources = function(completed = 0) {
486 let modal = document.getElementById('wooMlSyncModal');
487 modal.style.display = "block";
488 removeExistingErrors();
489 if($('#consumerKey').length) {
490 let error = false;
491 $('.woo-ml-wizard-error').remove();
492 if(!$('#consumerKey').val().startsWith('ck_')) {
493 error = true;
494 $('#consumerKey').after(`<span id="" class="woo-ml-wizard-error">Invalid consumer key</span>`);
495 }
496 if(!$('#consumerSecret').val().startsWith('cs_')) {
497 error = true;
498 $('#consumerSecret').after(`<span id="" class="woo-ml-wizard-error">Invalid consumer secret</span>`);
499 }
500 if(error) {
501 modal.style.display = "none";
502 return false;
503 }
504 }
505 if(((selectedGroup !== $('#wooMlSubGroup').val()) || $('#consumerKey').length) && !groupSaved) {
506 groupSaved = true;
507 let data = {};
508 if($('#consumerKey').length) {
509 data.consumer_key = $('#consumerKey').val();
510 data.consumer_secret = $('#consumerSecret').val();
511 }
512
513 data.action = "woo_ml_save_group";
514 data.section = "mailerlite";
515 data.group = jQuery('#wooMlSubGroup').val();
516 syncResourcesRequest = $.ajax({
517 url: woo_ml_post.ajax_url,
518 type: "post",
519 data: data,
520 success: function (res) {
521 if(res.error) {
522 $('.woo-ml-header').after(`
523 <div class="woo-ml-alert">
524 <span class="woo-ml-closebtn"
525 onClick="this.parentElement.style.display='none';">&times;</span>
526 ${res.message ? res.message : "Something went wrong!"}
527 </div>
528 `);
529 modal.style.display = 'none';
530 return false;
531 }
532 selectedGroup = jQuery('#wooMlSubGroup').val();
533 $.ajax({
534 url: woo_ml_post.ajax_url,
535 type: "post",
536 xhr: function(){ return progressBar(completed)},
537 data: {
538 action: "post_woo_ml_sync_untracked_resources"
539 },
540 success: function (res) {
541 let response = JSON.parse(res);
542
543 if (response.allDone) {
544 console.log("done!");
545 modal.style.display = "none";
546 window.onbeforeunload = null;
547 location.reload();
548 } else if (response.error) {
549 if (response.message) {
550 console.log(response.message);
551 }
552 removeExistingErrors();
553 $('.woo-ml-header').after(`
554 <div class="woo-ml-alert">
555 <span class="woo-ml-closebtn"
556 onClick="this.parentElement.style.display='none';">&times;</span>
557 Something went wrong, please retry.
558 </div>
559 `);
560 modal.style.display = "none";
561 } else {
562 importResources((response.completed ? response.completed : 0) + completed);
563 }
564 },
565 error: function (x) {
566 modal.style.display = "none";
567 console.log(x);
568 }
569 });
570 },
571 error: function (x, status) {
572 console.log(x,status);
573 }
574 });
575 } else {
576 syncResourcesRequest = $.ajax({
577 url: woo_ml_post.ajax_url,
578 type: "post",
579 xhr: function(){ return progressBar(completed)},
580 data: {
581 action : "post_woo_ml_sync_untracked_resources"
582 },
583 success: function (res) {
584 let response = JSON.parse(res);
585 if (response.allDone) {
586 console.log("done!");
587 modal.style.display = "none";
588 window.onbeforeunload = null;
589 location.reload();
590 } else if (response.error) {
591 if (response.message) {
592 console.log(response.message);
593 }
594 removeExistingErrors();
595 $('.woo-ml-header').after(`
596 <div class="woo-ml-alert">
597 <span class="woo-ml-closebtn"
598 onClick="this.parentElement.style.display='none';">&times;</span>
599 Something went wrong, please retry.
600 </div>
601 `);
602 modal.style.display = "none";
603 } else {
604 importResources((response.completed ? response.completed : 0) + completed);
605 }
606 },
607 error: function (x) {
608 modal.style.display = "none";
609 console.log(x);
610 }
611 });
612 }
613
614 };
615
616 $('#confirmCancelSync').on('click', function () {
617 buttonLoadingState($(this));
618 syncResourcesRequest?.abort();
619 window.location.reload();
620 });
621
622 function progressBar(completed = 0)
623 {
624 let xhr = new window.XMLHttpRequest();
625 xhr.upload.addEventListener("progress", function (evt) {
626 let percentage = ((100 * completed) / $('#totalUntrackedResources').val());
627 if(percentage <= 100) {
628 $('#wooMlWizardProgress').css({
629 width: percentage + '%'
630 });
631 $('#progressPercentage').text(Math.round(percentage) + '%');
632 }
633 }, false);
634 return xhr;
635 }
636
637
638
639 function groupSelectInput(selected = false)
640 {
641 $('#wooMlSubGroup').select2({theme: 'mailerlite'});
642 if(selected) {
643 $('#wooMlSubGroup').val(selected).trigger('change');
644 }
645
646 }
647
648 $('#woo-ml-reset-integration').on('click', function () {
649 $.ajax({
650 url: woo_ml_post.ajax_url,
651 type: "post",
652 data: {
653 action: "woo_ml_reset_integration"
654 },
655 success: function (res) {
656 window.onbeforeunload = null;
657 location.reload();
658 },
659 error: function (x) {
660 console.log(x);
661 }
662 });
663 });
664
665 const removeExistingErrors = function () {
666 if (jQuery('.woo-ml-header').next().hasClass('woo-ml-alert')) {
667 jQuery('.woo-ml-header').next().remove();
668 }
669 if (jQuery('.woo-ml-header').next().hasClass('woo-ml-alert-success')) {
670 jQuery('.woo-ml-header').next().remove();
671 }
672 };
673
674 function buttonLoadingState(elem, release = false)
675 {
676 elem.toggleClass('woo-ml-button-loading');
677 if(release)
678 {
679 elem.attr('disabled', false);
680 } else {
681 elem.attr('disabled', true);
682 }
683
684 }
685
686 $("#copyDebugLogToClipboard").click(async function(){
687 $("#copyDebugLogToClipboard .no-icon-tooltip-ml-text").show();
688 try {
689 const text = document.getElementById("debugLogLines").textContent;
690 await navigator.clipboard.writeText(text);
691 console.log('Content copied to clipboard');
692 } catch (err) {
693 console.error('Failed to copy: ', err);
694 }
695 setTimeout(function() { $("#copyDebugLogToClipboard .no-icon-tooltip-ml-text").hide(); }, 2000);
696 });
697 })(jQuery);
698
699 });
700
701