PluginProbe
MailerLite – WooCommerce integration / 2.0.9
MailerLite – WooCommerce integration v2.0.9
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.9, at public/js/admin.js

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