PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.0
EmailKit – Email Customizer for WooCommerce & WP v1.6.0
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / assets / admin / js / popup.js

popup.js in EmailKit – Email Customizer for WooCommerce & WP 1.6.0, at assets/admin/js/popup.js

645 lines 25.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function ($) {
2 'use strict';
3
4 //Getting the localize script data
5 var {emailkit_pro_status} = emailkit;
6
7 // Frequently used elements
8 var emailkitTemplateLists = $('.emailkit-template-item');
9 var emailTypeDropdown = $('#emailkit-add-new-form-model__form-type');
10 var templateTypeDropdown = $('#emailkit-add-new-email__template-type');
11 let emailkitWrapper = jQuery('.emailkit-template-wrapper');
12 var form = $('#emailkit-form');
13 var notice = $('#emailkit-notice');
14 var newFormModal = $('#emailkit_new_form_modal');
15 let responseData = '';
16 emailkitWrapper.hide();
17 // Flag to control form reset
18 var resetFormFlag = true;
19 // Open Template Create Modalg
20 $('.page-title-action').on('click', function (e) {
21 e.preventDefault();
22 newFormModal.modal('show');
23 $('#emailkit-add-new-form-model__form-type').val('wordpress');
24 $('#emailkit-add-new-form-model__form-type').trigger('change');
25 });
26
27 // Modify URL to go to the emailkit builder
28 $('.row-title').each(function (_, item) {
29 item.href = item.href.replace(/(action=)[^&]+/g, '$1' + 'emailkit-builder');
30 });
31
32
33
34
35 // Initialize the Select2 dropdown
36 templateTypeDropdown.select2({
37 dropdownParent: newFormModal,
38 width: '95%',
39 placeholder: "Select email type",
40 containerCssClass: 'custom-dropdown'
41 });
42
43
44 templateTypeDropdown.on('select2:open', function (e) {
45 $('input.select2-search__field').prop('placeholder', 'Search...');
46 $('.select2-container').addClass('emailkit-template-type-dropdown');
47
48 let inputField = $('.emailkit-template-type-dropdown .select2-search__field');
49 inputField.on('input', function(e) {
50 let {value} = e.target || {};
51 if(value !== '') {
52 let searchList = $('#select2-emailkit-add-new-email__template-type-results li')[0];
53 let searchListText = searchList.innerText;
54 if(searchListText !== 'Select Template') {
55 $('.select2-container').removeClass('emailkit-disable-first-item');
56 }
57 } else {
58 $('.select2-container').addClass('emailkit-disable-first-item');
59 }
60 })
61
62 if(emailTypeDropdown.val() == 'woocommerce' || emailTypeDropdown.val() == 'wordpress') $('.select2-container').addClass('emailkit-disable-first-item');
63 else $('.select2-container').removeClass('emailkit-disable-first-item');
64
65 setTimeout(function() {
66 $('.select2-container--open .select2-search__field').blur();
67 }, 0);
68 });
69
70 // Remove the custom class when the dropdown is closed
71 templateTypeDropdown.on('select2:close', function (e) {
72 $('.select2-container').removeClass('emailkit-template-type-dropdown');
73 });
74
75
76 emailTypeDropdown.select2({
77 dropdownParent: newFormModal,
78 width: '95%',
79 placeholder: "Select email type",
80 containerCssClass: 'custom-dropdown'
81 });
82
83
84 emailTypeDropdown.on('select2:open', function (e) {
85 $('input.select2-search__field').prop('placeholder', 'Search...');
86 $('.select2-container').addClass('emailkit-email-type-dropdown');
87 setTimeout(function() {
88 // Find the search input within the dropdown and blur it
89 $('.select2-container--open .select2-search__field').blur();
90 }, 0);
91 });
92
93 // Remove the custom class when the dropdown is closed
94 emailTypeDropdown.on('select2:close', function (e) {
95 $('.select2-container').removeClass('emailkit-email-type-dropdown');
96 });
97
98
99 // Validate if the user selects dropdown items
100 $('.emailkit-open-new-form-editor-modal').on('click', function (e) {
101
102 let _self = this;
103
104 var selectedEmailType = emailTypeDropdown.val();
105 var selectedTemplateType = templateTypeDropdown.val();
106
107 if (!selectedEmailType) {
108 handleValidationFailure('Please select email type');
109 e.preventDefault();
110 return;
111 }
112
113 if (!selectedTemplateType || selectedTemplateType === 'Select Template') {
114 handleValidationFailure('Please select template type');
115 e.preventDefault();
116 return;
117 }
118
119 e.preventDefault();
120
121 // create-from-saved-template
122 var url = $(this).attr('resturl');
123 var editAction = $('#emailkit-add-new-form-model__form-type').val().trim();
124 url += editAction !== 'saved-templates'? "template-data/" : "create-from-saved-template/";
125
126 var emailkitProStatus = checkEmailKitProStatus();
127
128 // show popup for pro
129 if( ('saved-templates' == editAction) && !emailkit_pro_status){
130 return false;
131 }
132 jQuery(this).addClass('emailkit-slider-loader');
133
134 $.ajax({
135 url: url,
136 type: 'post',
137 data: form.serialize(),
138 headers: {
139 'X-WP-Nonce': emailTypeDropdown.data('nonce'),
140 },
141 dataType: 'json',
142 success: function (output) {
143 handleAjaxSuccess(output);
144 },
145
146 beforeSend: function () {
147 // $('.emailkit-open-new-form-editor-modal').attr('disabled',true);
148 },
149 complete: function (jqXHR, textStatus) {
150 // $('.emailkit-open-new-form-editor-modal').attr('disabled',false);
151 jQuery(_self).remove('emailkit-slider-loader');
152
153
154 if (jqXHR.status === 404) {
155 // $('#emailkitproOverlay').fadeIn();
156 // $('#emailkitproOverlay .content').html('Active EmailKit Pro');
157 jQuery('.emailkit-pro-alert-msg-wrapper').show();
158 jQuery('.emailkit-saved-template-alert-msg').hide();
159 }
160 },
161 error: function (jqXHR, textStatus, errorThrown) {
162 jQuery(_self).remove('emailkit-slider-loader');
163 console.log('Status Text: ' + textStatus);
164 console.log('Error Thrown: ' + errorThrown);
165 // $('.emailkit-open-new-form-editor-modal').attr('disabled',false);
166 $('#emailkitproOverlay .content').html('Something went wrong');
167 }
168 });
169
170 });
171
172 /**
173 * Checking EmailKit Pro Status
174 */
175 function checkEmailKitProStatus(){
176 return window.emailkit.emailkit_pro_status.trim() === 'active' ? true : false;
177 }
178
179 // Handle the "Buy Pro" action
180 $('#buyProBtn').on('click', function() {
181 closeProNotice(); // close popup and go to buy
182 window.open("https://wpmet.com/")
183 return false;
184 });
185
186 // Handle the "Close" button click
187 $('#closeProBtn').on('click', function() {
188 closeProNotice();
189 });
190
191 // Close the pro notice on focusout
192 $(document).on('click', function(event) {
193 if (!$(event.target).closest('.pro-notice').length) {
194 closeProNotice();
195 }
196 });
197
198 function closeProNotice() {
199 $('#emailkitproOverlay').fadeOut();
200 }
201
202 // Close popup on click close
203 $('.emailkit-add-new-form-modal-close-btn').on('click', function () {
204 deleteTemplatePopup.hide();
205 resetFormAndSelect2();
206 $('.emailkit-template-item').css('display', 'block'); // visible all templates items
207 });
208
209
210 // Get email template type on change
211 emailTypeDropdown.on('change', function () {
212 var emailType = emailTypeDropdown.val();
213 templateTypeDropdown.empty();
214 updateTemplateDropdown();
215 emailkitEmailFilter(emailType);
216 });
217
218 // Email template type change
219 templateTypeDropdown.on('change', function () {
220 resetFormFlag = false; // Set the flag to false to prevent form reset
221 var templateType = templateTypeDropdown.val();
222 var emailType = emailTypeDropdown.val();
223 if(emailType == 'saved-templates'){
224 let selectedTemplateType = jQuery(this).find('option:selected').text();
225 jQuery('.emailkit-saved-template-name').text(selectedTemplateType);
226 jQuery('.emailkit-template-delete-btn-confirm').attr('postId', templateType);
227 }
228
229 emailkitTemplateFilter(templateType, emailType);
230 resetFormFlag = true; // Set the flag back to true after handling the change event
231 });
232
233
234 /**
235 * @description - show delete saved templates popup
236 */
237
238 let deleteSavedTemplateBtn = jQuery('.emailkit-saved-template-delete');
239 let deleteTemplatePopup = jQuery('.emailkit-template-delete-popover');
240 let closeDeleteTemplatePopup = jQuery('.emailkit-template-delete-btn-cancel');
241
242
243 //Show popup when delete button is clicked ref- https://prnt.sc/RSZCXUn1kTPl
244 if(deleteSavedTemplateBtn.length > 0){
245 deleteSavedTemplateBtn.on('click', function(e){
246 e.preventDefault();
247 deleteTemplatePopup.show();
248 })
249 }
250
251 if(closeDeleteTemplatePopup.length > 0){
252 closeDeleteTemplatePopup.on('click', function(e){
253 e.preventDefault();
254 deleteTemplatePopup.hide();
255 })
256 }
257
258 //Close the popover when clicked outside
259 jQuery(document).on('mouseup', function(e){
260 if(!deleteTemplatePopup.is(e.target) && deleteTemplatePopup.has(e.target).length === 0){
261 deleteTemplatePopup.hide();
262 }
263 })
264
265
266
267 /**
268 * @description - handling Saved Template Delete feature.
269 */
270
271 if(jQuery('.emailkit-template-delete-btn-confirm').length > 0){
272 jQuery('.emailkit-template-delete-btn-confirm').on('click', function(e){
273 e.preventDefault();
274 let postId = jQuery(this).attr('postid');
275 let postText = jQuery(this).attr('posttext')
276
277
278 $.ajax( {
279 url: emailkit?.rest_url + `delete-saved-template/${postId}`,
280 type: 'delete',
281 headers: {
282 'X-WP-Nonce': emailkit.rest_nonce,
283 },
284 dataType: 'json',
285 success: function( response ) {
286 emailTypeDropdown.val('saved-templates').trigger('change');
287 deleteTemplatePopup.hide();
288 jQuery('.emailkit-template-delete-success-notification').show();
289 setTimeout(() => {
290 jQuery('.emailkit-template-delete-success-notification').hide();
291 }, 2000);
292 },
293 error: function(error){
294 console.log(error);
295 deleteTemplatePopup.hide();
296 }
297 } );
298
299 });
300 }
301 // Function to handle validation failure
302 function handleValidationFailure(message) {
303 showNotice(message);
304 }
305
306 // Function to handle AJAX success
307 function handleAjaxSuccess(output) {
308 if (localStorage.getItem('editorState')) {
309 localStorage.removeItem('editorState');
310 }
311 window.location.href = output.data.builder_url;
312 $('#message').css('display', 'block');
313 }
314
315 // Function to show notice
316 function showNotice(message) {
317 notice.text(message);
318 notice.css('background', '#d63638');
319 notice.fadeIn().delay(1000).slideUp();
320 }
321
322 // Function to reset form
323 function resetFormAndSelect2() {
324 form[0].reset();
325 templateTypeDropdown.empty().select2({
326 dropdownParent: newFormModal,
327 width: '95%',
328 placeholder: "Select template",
329 data: [{ id: 'Select Template', text: 'Select Template' }],
330 });
331 }
332
333 /**
334 * Function to filter emails based on the dropdownEmailType.
335 *
336 * @param {type} dropdownEmailType - the type of email to filter
337 * @return {type} - no explicit return value
338 */
339 function emailkitEmailFilter(dropdownEmailType) {
340
341
342 if(dropdownEmailType == 'saved-templates'){
343
344 if(!emailkit_pro_status){
345 jQuery('.emailkit-pro-msg-title').text('Almost There!');
346 jQuery('.emailkit-pro-msg-content').text('We’ve kept your template safe. Go Pro and keep creating!');
347 jQuery('.emailkit-pro-alert-msg-wrapper').show();
348 jQuery('.emailkit-blank-template').hide();
349
350 //disabling the edit with emailkit btn if pro is not active
351 $('#editWithEmailkit').prop('disabled', true);
352 jQuery('.emailkit-edit-template-btn').addClass('emailkit-pro-inactive');
353
354
355 } else if(emailkit_pro_status){
356
357 jQuery('.emailkit-edit-template-btn').prop('disabled',false);
358 jQuery('.emailkit-edit-template-btn').removeClass('emailkit-pro-inactive');
359
360 jQuery('.emailkit-saved-template-alert-msg').show();
361 jQuery('.emailkit-blank-template').hide();
362 }
363 jQuery('.emailkit-select-template-type-msg').hide();
364
365 } else {
366 jQuery('.emailkit-edit-template-btn').prop('disabled',false);
367 jQuery('.emailkit-edit-template-btn').removeClass('emailkit-pro-inactive');
368 jQuery('.emailkit-pro-alert-msg-wrapper').hide();
369 jQuery('.emailkit-saved-template-alert-msg').hide();
370 jQuery('.emailkit-blank-template').show();
371 emailkitTemplateLists.each(function (_, value) {
372 var currentTemplate = $(value);
373 currentTemplate.css('display', dropdownEmailType !== currentTemplate.data('mail-type') ? 'none' : 'block');
374 });
375 }
376 }
377
378 /**
379 * A function to filter emailkit templates based on dropdown selection.
380 *
381 * @param {type} dropdownTemplateType - description of parameter
382 * @param {type} emailType - description of parameter
383 * @return {void}
384 */
385 function emailkitTemplateFilter(dropdownTemplateType, emailType) {
386
387 if( emailType == 'metform' && dropdownTemplateType == 'more-forms'){
388 jQuery('.emailkit-blank-template').hide();
389 jQuery('.emailkit-edit-template-btn').prop('disabled',true);
390 jQuery('.emailkit-pro-msg-title').text('Only One Step Away!');
391 jQuery('.emailkit-pro-msg-content').text('Your MetForm user confirmation email templates are waiting for you. Upgrade and start creating!');
392 jQuery('.emailkit-pro-alert-msg-wrapper').show();
393 jQuery('.emailkit-pro-alert-msg-wrapper').css('height', 'fit-content');
394 }else if( emailType != 'saved-templates' && dropdownTemplateType != 'more-forms' ){
395 jQuery('.emailkit-blank-template').show();
396 jQuery('.emailkit-edit-template-btn').prop('disabled',false);
397 jQuery('.emailkit-pro-alert-msg-wrapper').hide();
398 jQuery('.emailkit-pro-alert-msg-wrapper').css('height', 'unset');
399 }
400
401
402 jQuery('.emailkit-template-loader-wrapper').show();
403 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'hidden');
404
405 if('saved-templates' == emailType){
406 $('.emailkit-templates-list').hide()
407 filterSaveAsTemplate(dropdownTemplateType, emailType);
408 }
409
410
411 var emailkitTemplateLists = $('.emailkit-template-item');
412
413 if (dropdownTemplateType.trim() === 'Select Template') {
414 jQuery('.emailkit-template-loader-wrapper').hide();
415 jQuery('.emailkit-add-new-form-model-contents').css('overflow', 'hidden');
416 jQuery('.emailkit-select-template-type-msg').show();
417 jQuery('.emailkit-templates-list').hide();
418 emailkitTemplateLists.css('display', 'block');
419 return;
420 } else {
421 jQuery('.emailkit-select-template-type-msg').hide();
422 jQuery('.emailkit-templates-list').show();
423 }
424
425 emailkitTemplateLists.each(function (_, value) {
426 var currentTemplate = $(value);
427
428 currentTemplate.css('display', dropdownTemplateType.trim() === currentTemplate.data('template-type').trim() ? 'block' : 'none');
429 });
430 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll');
431 jQuery('.emailkit-template-loader-wrapper').hide();
432 }
433
434 function disableEnableEmailkitBtn(el, value) {
435
436 if(emailkit_pro_status){
437 jQuery(el).prop('disabled', value);
438 } else if(!emailkit_pro_status){
439 jQuery(el).prop('disabled', value);
440 }
441
442 }
443
444 /**
445 * Function for filtering saved templates.
446 *
447 * @param {string} template_id - the ID of the template
448 * @param {string} emailType - the type of email
449 * @return {json} the response from the AJAX call
450 */
451 function filterSaveAsTemplate(template_id, emailType){
452 jQuery('.emailkit-template-loader-wrapper').show();
453 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'hidden');
454 $.ajax( {
455 url: window.ajaxurl,
456 type: 'post',
457 data: {template_id:template_id,email_type:emailType, action: 'emailkit_filter_save_as_template', nonce: emailkit.nonce},
458 dataType: 'json',
459 success: function( response ) {
460 jQuery('.emailkit-template-loader-wrapper').hide();
461 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll');
462 if(response.data.dependency_status === false){
463 $('.emailkit-open-new-form-editor-modal').attr('disabled',true);
464 $('.emailkit-open-new-form-editor-modal').addClass('emailkit-pro-inactive');
465 // $('.emailkit-open-new-form-editor-modal').css('opacity','.5');
466 $('.emailkit-templates-list').hide()
467 emailkitWrapper.show();
468 emailkitWrapper.html("<div><br>" +
469 '<p>Looks like you\'ve forgot to <br><a class="emailkit_missing_plugin" target="_blank" href="'+response.data.need_plugin.url+'">'
470
471 +response.data.need_plugin.label+` <span class="dashicons dashicons-admin-plugins"></span>
472 `+"</a>"+
473 "</p></div>")
474 }else{
475 if(emailkit_pro_status){
476 $('.emailkit-open-new-form-editor-modal').attr('disabled',false);
477 $('.emailkit-open-new-form-editor-modal').removeClass('emailkit-pro-inactive');
478 } else if(!emailkit_pro_status){
479 $('.emailkit-open-new-form-editor-modal').attr('disabled',true);
480 $('.emailkit-open-new-form-editor-modal').addClass('emailkit-pro-inactive');
481 }
482 $('.emailkit-open-new-form-editor-modal').css('opacity','1');
483 $('.emailkit-templates-list').show()
484 emailkitWrapper.hide();
485 $('.emailkit-templates-list .emailkit-template-item').remove()
486 }
487
488 },
489 error: function(error){
490 jQuery('.emailkit-template-loader-wrapper').hide();
491 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll');
492 console.log(error);
493 }
494 } );
495 }
496
497 /**
498 * Updates the template dropdown based on the selected email type.
499 *
500 * @param None
501 * @return None
502 */
503 function updateTemplateDropdown() {
504 jQuery('.emailkit-template-loader-wrapper').show();
505
506 if(emailTypeDropdown.val().trim() == 'saved-templates'){
507 jQuery('.emailkit-templates-list').css('min-height', '250px');
508 }
509
510 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'hidden');
511 $.ajax({
512 url: emailkit.ajaxurl,
513 type: 'post',
514 data: {
515 action: 'emailkit_get_email_template_type',
516 nonce: emailkit.nonce,
517 data: emailTypeDropdown.val(),
518 },
519 dataType: 'json',
520 success: function (response) {
521
522 jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll');
523 if(response.data.dependency_status === false){
524
525 $('.emailkit-open-new-form-editor-modal').attr('disabled',true);
526 $('.emailkit-open-new-form-editor-modal').addClass('emailkit-pro-inactive');
527 // $('.emailkit-open-new-form-editor-modal').css('opacity','.5');
528 $('.emailkit-templates-list').hide()
529 emailkitWrapper.show();
530 emailkitWrapper.html("<div><br>" +
531 '<p>Looks like you\'ve forgot to <br><a class="emailkit_missing_plugin" target="_blank" href="'+response.data.need_plugin.url+'">'
532
533 +response.data.need_plugin.label+` <span class="dashicons dashicons-admin-plugins"></span>
534 `+"</a>"+
535 "</p></div>")
536 // $('.emailkit-template-wrapper').text( response.data.btn )
537 }else{
538 $('.emailkit-open-new-form-editor-modal').attr('disabled',false);
539 $('.emailkit-open-new-form-editor-modal').css('opacity','1');
540 $('.emailkit-templates-list').show()
541 emailkitWrapper.hide();
542 var templates = response.data.templates;
543 var data = response.data.template_name;
544 templateTypeDropdown.select2({
545 data: data,
546 width: '95%',
547 dropdownParent: newFormModal,
548 });
549
550 $('.emailkit-templates-list .emailkit-template-item').remove()
551 $('.emailkit-templates-list').append(...templates);
552 if(response && response?.data?.template_name?.length > 0){
553 templateTypeDropdown.val(response?.data?.template_name[0].id).trigger('change');
554 jQuery('.emailkit-saved-template-name').text(data[0]?.text)
555 jQuery('.emailkit-template-delete-btn-confirm').attr('postId',data[0]?.id);
556 jQuery('.emailkit-template-delete-btn-confirm').attr('postText',data[0]?.text);
557 jQuery('.emailkit-template-not-available').hide();
558 jQuery('.emailkit-edit-template-btn').prop('disabled',false);
559 } else {
560 jQuery('.emailkit-saved-template-info').hide();
561 jQuery('.emailkit-template-not-available').show();
562 jQuery('.emailkit-edit-template-btn').prop('disabled',true);
563 }
564 }
565 jQuery('.emailkit-template-loader-wrapper').hide();
566 if(emailTypeDropdown.val().trim() == 'saved-templates'){
567 jQuery('.emailkit-templates-list').css('min-height', '110px');
568 }
569 },
570
571 error: function (data) {
572 console.log('Error: ' + data);
573 }
574 });
575 }
576
577
578 //Edit popup
579
580 $( '.row-actions .edit a, .emailkit-form-edit-btn, body.post-type-emailkit-form a.row-title' ).on( 'click', function( e ) {
581 e.preventDefault();
582 var id = 0;
583 var modal = $( '#emailkit_form_modal' );
584 $('.emailkit_update_inputs .update_template_name').val('')
585
586 id = (new URL($(this).attr('href')).searchParams.get('post'));
587
588 modal.modal( 'show' );
589
590 $('.emailkit_update_inputs #template_id').val(id)
591
592 $.ajax( {
593 url: window.ajaxurl,
594 type: 'post',
595 data: {ID:id, action: 'emailkit_template_data', nonce: emailkit.nonce},
596 dataType: 'json',
597 success: function( response ) {
598
599 $('.emailkit_update_inputs .update_template_name').val(response.data)
600
601 },
602 error: function(error){
603 console.log(error);
604 }
605 } );
606
607 } );
608
609 $( '.emailkit_update_template' ).on( 'click', function( e ) {
610 e.preventDefault();
611 var id = null;
612 var title = null;
613 var actionType = null;
614 var modal = $( '#emailkit_form_modal' );
615 actionType = $(e.target).attr('data-action-type')
616 id = $('.emailkit_update_inputs #template_id').val();
617 title = $('.emailkit_update_inputs .update_template_name').val();
618
619 if(title.trim() == ''){
620 alert('Please Give a title')
621 return false;
622 }
623
624 $.ajax( {
625 url: window.ajaxurl,
626 type: 'post',
627 data: {id:id, title:title, action_type:actionType, action:'emailkit_update_template_data', nonce: emailkit.nonce},
628 dataType: 'json',
629 success: function( response ) {
630
631 if(response.data.builder_url){
632 location.href = response.data.builder_url;
633 }else{
634 $('.update_close_icon').click()
635 window.location.reload();
636 }
637 },
638 error: function(error){
639 console.log(error);
640 }
641 } );
642
643 } );
644
645 });