| 1 |
/* global waNotifier */ |
| 2 |
(function($) { |
| 3 |
|
| 4 |
// Show / hide fields as per conditional logic |
| 5 |
function conditionallyShowFields() { |
| 6 |
if ($('.meta-fields').length == 0) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
$('.form-field').each(function() { |
| 11 |
var thisField = $(this).find(':input'); |
| 12 |
|
| 13 |
// Conditionally show/hide fields |
| 14 |
var conditions = $(this).attr('data-conditions') || ''; |
| 15 |
if (conditions !== '') { |
| 16 |
var showField = false; |
| 17 |
var fieldElem = $(this); |
| 18 |
var conditionsArray = JSON.parse(conditions); |
| 19 |
|
| 20 |
conditionsArray.forEach(function(condition) { |
| 21 |
var fieldClass = '.' + condition.field + '_field'; |
| 22 |
var fieldInput = $(fieldClass + ' :input'); |
| 23 |
var fieldInputType = fieldInput.prop('type'); |
| 24 |
|
| 25 |
// Do not fetch value of hidden fields. |
| 26 |
if(!$(fieldClass).is(':visible')){ |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// Get field value |
| 31 |
var fieldVal = fieldInput.val(); |
| 32 |
|
| 33 |
// Get field value if it's radio or checkbox |
| 34 |
if($.inArray(fieldInputType, ['radio', 'checkbox']) !== -1) { |
| 35 |
fieldVal = $(fieldClass + ' :input:checked').val(); |
| 36 |
} |
| 37 |
|
| 38 |
var showThis = false; |
| 39 |
if(condition.operator == '==') { |
| 40 |
showThis = (fieldVal == condition.value) ? true : false; |
| 41 |
} |
| 42 |
else if(condition.operator == '!=') { |
| 43 |
showThis = (fieldVal != condition.value) ? true : false; |
| 44 |
} |
| 45 |
showField = showField || showThis; |
| 46 |
}); |
| 47 |
|
| 48 |
if(showField){ |
| 49 |
fieldElem.show(); |
| 50 |
} |
| 51 |
else{ |
| 52 |
fieldElem.hide(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Apply data limit on fields |
| 57 |
if (thisField.hasClass('force-text-limit')) { |
| 58 |
var content = thisField.val(); |
| 59 |
var contentLength = content.length; |
| 60 |
var limit = thisField.attr('data-limit'); |
| 61 |
if (contentLength >= limit) { |
| 62 |
thisField.siblings('label').find('.limit-used').text(limit); |
| 63 |
thisField.val(content.substr(0, limit)); |
| 64 |
} else { |
| 65 |
thisField.siblings('label').find('.limit-used').text(contentLength); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Enable select2 for notification receiver contact dropdown |
| 70 |
if($('.notifier-recipient-contact').length > 0){ |
| 71 |
$('.notifier-recipient-contact').each(function (i, obj) { |
| 72 |
if($(obj).closest('.row-add-recipient-template').length > 0){ |
| 73 |
return; |
| 74 |
} |
| 75 |
if (!$(obj).data('select2')){ |
| 76 |
$(obj).select2({ |
| 77 |
selectOnClose: true, |
| 78 |
width: '100%', |
| 79 |
placeholder: 'Search contact...', |
| 80 |
ajax: { |
| 81 |
url: waNotifier.ajaxurl, |
| 82 |
dataType: 'json', |
| 83 |
delay: 250, |
| 84 |
method: 'post', |
| 85 |
data: function (params) { |
| 86 |
return { |
| 87 |
s: params.term, |
| 88 |
action: 'get_wa_contacts_data', |
| 89 |
}; |
| 90 |
}, |
| 91 |
processResults: function( data ) { |
| 92 |
var options = []; |
| 93 |
if ( data ) { |
| 94 |
$.each( data, function( index, text ) { |
| 95 |
options.push( { id: index, text: text } ); |
| 96 |
}); |
| 97 |
} |
| 98 |
return { |
| 99 |
results: options, |
| 100 |
}; |
| 101 |
}, |
| 102 |
cache: true, |
| 103 |
}, |
| 104 |
minimumInputLength: 2, |
| 105 |
}); |
| 106 |
} |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
// Fetch message template data and generate preview |
| 114 |
function fetcDataAndPreviewTemplate() { |
| 115 |
let previewData = {}; |
| 116 |
previewData.header_type = $('#notifier_header_type').val(); |
| 117 |
if('text' == previewData.header_type){ |
| 118 |
previewData.header_text = $('#notifier_header_text').val() || 'Header text here'; |
| 119 |
} |
| 120 |
else if('media' == previewData.header_type){ |
| 121 |
previewData.media_type = $('#notifier_media_type').val() || 'image'; |
| 122 |
var media_type = previewData.media_type.toLowerCase(); |
| 123 |
switch(media_type) { |
| 124 |
case 'image': |
| 125 |
previewData.media_url = $('#notifier_media_item_image').attr('data-url') || ''; |
| 126 |
break; |
| 127 |
|
| 128 |
case 'document': |
| 129 |
previewData.media_url = $('#notifier_media_item_document').attr('data-url') || ''; |
| 130 |
break; |
| 131 |
|
| 132 |
case 'video': |
| 133 |
previewData.media_url = $('#notifier_media_item_video').attr('data-url') || ''; |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
previewData.body_text = $('#notifier_body_text').val() || 'Body text here'; |
| 139 |
previewData.footer_text = $('#notifier_footer_text').val() || ''; |
| 140 |
previewData.button_type = $('#notifier_button_type').val(); |
| 141 |
|
| 142 |
previewData.button_num = $('input[name="notifier_button_num"]:checked').val(); |
| 143 |
|
| 144 |
previewData.button_1_type = $('#notifier_button_1_type :selected').val(); |
| 145 |
previewData.button_1_text = $('#notifier_button_1_text').val() || ''; |
| 146 |
previewData.button_2_text = $('#notifier_button_2_text').val() || ''; |
| 147 |
|
| 148 |
// Button |
| 149 |
if (previewData.button_type != 'none') { |
| 150 |
if (previewData.button_1_type == 'URL') { |
| 151 |
if (previewData.button_1_text == '') { |
| 152 |
previewData.button_1_text = 'Visit Website'; |
| 153 |
} |
| 154 |
} else if (previewData.button_1_type == 'PHONE_NUMBER') { |
| 155 |
if (previewData.button_1_text == '') { |
| 156 |
previewData.button_1_text = 'Call'; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if (previewData.button_num == '1') { |
| 161 |
$('#notifier_button_1_type option').prop('disabled', false); |
| 162 |
$('#notifier_button_2_type option').prop('disabled', false); |
| 163 |
} else { |
| 164 |
$('#notifier_button_1_type option').not('option[value="' + previewData.button_1_type + '"]').prop('disabled', true); |
| 165 |
$('#notifier_button_2_type option').not('option[value="' + previewData.button_1_type + '"]').prop('selected', true); |
| 166 |
$('#notifier_button_2_type option[value="' + previewData.button_1_type + '"]').prop('disabled', true); |
| 167 |
previewData.button_2_type = $('#notifier_button_2_type :selected').val(); |
| 168 |
if (previewData.button_2_type == 'URL') { |
| 169 |
if (previewData.button_2_text == '') { |
| 170 |
previewData.button_2_text = 'Visit Website'; |
| 171 |
} |
| 172 |
} else if (previewData.button_2_type == 'PHONE_NUMBER') { |
| 173 |
if (previewData.button_2_text == '') { |
| 174 |
previewData.button_2_text = 'Call'; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// Render message preview from the data |
| 181 |
renderMessagePreview(previewData); |
| 182 |
} |
| 183 |
|
| 184 |
// Render message preview |
| 185 |
function renderMessagePreview(previewData) { |
| 186 |
messageTemplatePreviewData = previewData; |
| 187 |
// Header |
| 188 |
$('.wa-template-preview .message-head').hide(); |
| 189 |
if('text' == previewData.header_type){ |
| 190 |
$('.wa-template-preview .message-head-text').show().text(previewData.header_text); |
| 191 |
} |
| 192 |
else if('media' == previewData.header_type){ |
| 193 |
var media_type = previewData.media_type.toLowerCase(); |
| 194 |
var head_preview_classes = 'message-head message-head-media message-head-'+media_type; |
| 195 |
$('.wa-template-preview .message-head-media').show().attr('class', head_preview_classes); |
| 196 |
$('.message-head-media-inner').removeClass('hide'); |
| 197 |
$('.message-head-media-preview').addClass('hide'); |
| 198 |
$('.message-head-media-preview > *').addClass('hide'); |
| 199 |
switch(media_type) { |
| 200 |
case 'image': |
| 201 |
if(previewData.media_url){ |
| 202 |
$('.message-head-media-inner').addClass('hide'); |
| 203 |
$('.message-head-media-preview').removeClass('hide'); |
| 204 |
$('.message-head-media-preview-image').removeClass('hide').attr('src', previewData.media_url); |
| 205 |
} |
| 206 |
break; |
| 207 |
|
| 208 |
case 'document': |
| 209 |
if(previewData.media_url){ |
| 210 |
$('.message-head-media-inner').addClass('hide'); |
| 211 |
$('.message-head-media-preview').removeClass('hide'); |
| 212 |
$('.message-head-media-preview-image').removeClass('hide').attr('src', previewData.media_url); |
| 213 |
} |
| 214 |
break; |
| 215 |
|
| 216 |
case 'video': |
| 217 |
if(previewData.media_url){ |
| 218 |
$('.message-head-media-inner').addClass('hide'); |
| 219 |
$('.message-head-media-preview').removeClass('hide'); |
| 220 |
$('.message-head-media-preview-video').removeClass('hide').find('source').attr('src', previewData.media_url); |
| 221 |
$('.message-head-media-preview-video').get(0).load(); |
| 222 |
} |
| 223 |
break; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// Body |
| 228 |
previewData.body_text = previewData.body_text.replace(/(<([^>]+)>)/gi, '') |
| 229 |
.replace(/(?:\r\n|\r|\n)/g, '<br>') |
| 230 |
.replace(/(?:\*)(?:(?!\s))((?:(?!\*|\n).)+)(?:\*)/g,'<b>$1</b>') |
| 231 |
.replace(/(?:_)(?:(?!\s))((?:(?!\n|_).)+)(?:_)/g,'<i>$1</i>') |
| 232 |
.replace(/(?:~)(?:(?!\s))((?:(?!\n|~).)+)(?:~)/g,'<s>$1</s>') |
| 233 |
.replace(/(?:--)(?:(?!\s))((?:(?!\n|--).)+)(?:--)/g,'<u>$1</u>') |
| 234 |
.replace(/(?:```)(?:(?!\s))((?:(?!\n|```).)+)(?:```)/g,'<tt>$1</tt>'); |
| 235 |
|
| 236 |
$('.wa-template-preview .message-body').html(previewData.body_text); |
| 237 |
|
| 238 |
// Footer |
| 239 |
if (previewData.footer_text !== '') { |
| 240 |
$('.wa-template-preview .message-footer').show().text(previewData.footer_text); |
| 241 |
} else { |
| 242 |
$('.wa-template-preview .message-footer').hide(); |
| 243 |
} |
| 244 |
|
| 245 |
// Buttons |
| 246 |
if (previewData.button_type == 'none') { |
| 247 |
$('.wa-template-preview .message-buttons').hide(); |
| 248 |
} else { |
| 249 |
$('.wa-template-preview .message-buttons').show(); |
| 250 |
if (previewData.button_1_type == 'URL') { |
| 251 |
$('.wa-template-preview .message-button-1 .message-button-img').removeClass('call').addClass('visit'); |
| 252 |
$('.wa-template-preview .message-button-1 .message-button-text').text(previewData.button_1_text); |
| 253 |
} else if (previewData.button_1_type == 'PHONE_NUMBER') { |
| 254 |
$('.wa-template-preview .message-button-1 .message-button-img').removeClass('visit').addClass('call'); |
| 255 |
$('.wa-template-preview .message-button-1 .message-button-text').text(previewData.button_1_text); |
| 256 |
} |
| 257 |
|
| 258 |
if (previewData.button_num == '1') { |
| 259 |
$('.wa-template-preview .message-button-2').hide(); |
| 260 |
} else { |
| 261 |
$('.wa-template-preview .message-button-2').show(); |
| 262 |
if (previewData.button_2_type == 'URL') { |
| 263 |
$('.wa-template-preview .message-button-2 .message-button-img').removeClass('call').addClass('visit'); |
| 264 |
$('.wa-template-preview .message-button-2 .message-button-text').text(previewData.button_2_text); |
| 265 |
} else if (previewData.button_2_type == 'PHONE_NUMBER') { |
| 266 |
$('.wa-template-preview .message-button-2 .message-button-img').removeClass('visit').addClass('call'); |
| 267 |
$('.wa-template-preview .message-button-2 .message-button-text').text(previewData.button_2_text); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// Fetch and display send to fields |
| 274 |
function fetchAndDisplaySendToFields() { |
| 275 |
const post_id = $('#post_ID').val() || 0; |
| 276 |
const notification_type = $('#notifier_notification_type').val() || ''; |
| 277 |
const trigger = $('#notifier_notification_trigger').val() || ''; |
| 278 |
|
| 279 |
if('marketing' == notification_type) { |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
$('.send-to-fields').html('<div class="loader"></div>'); |
| 284 |
|
| 285 |
$.ajax({ |
| 286 |
type: 'post', |
| 287 |
dataType: 'json', |
| 288 |
url: waNotifier.ajaxurl, |
| 289 |
data: { |
| 290 |
action: 'fetch_send_to_fields', |
| 291 |
trigger: trigger, |
| 292 |
post_id: post_id, |
| 293 |
}, |
| 294 |
success: function(response) { |
| 295 |
if (response.status == 'success') { |
| 296 |
$('.send-to-fields').html(response.html); |
| 297 |
conditionallyShowFields(); |
| 298 |
} |
| 299 |
}, |
| 300 |
}); |
| 301 |
} |
| 302 |
|
| 303 |
// Fetch and display message template |
| 304 |
function fetchAndDisplayMessageTemplate() { |
| 305 |
const template_id = $('#notifier_notification_message_template :selected').val(); |
| 306 |
var wa_preview = $('#notifier-message-template-preview'); |
| 307 |
if (template_id == '') { |
| 308 |
wa_preview.removeClass('d-block').addClass('hide'); |
| 309 |
return false; |
| 310 |
} |
| 311 |
const post_id = $('#post_ID').val() || 0; |
| 312 |
const notification_type = $('#notifier_notification_type').val() || ''; |
| 313 |
const trigger = $('#notifier_notification_trigger').val() || ''; |
| 314 |
$.ajax({ |
| 315 |
type: 'post', |
| 316 |
dataType: 'json', |
| 317 |
url: waNotifier.ajaxurl, |
| 318 |
data: { |
| 319 |
action: 'fetch_message_template_data', |
| 320 |
notification_type: notification_type, |
| 321 |
template_id: template_id, |
| 322 |
post_id: post_id, |
| 323 |
trigger: trigger, |
| 324 |
}, |
| 325 |
success: function(response) { |
| 326 |
if (response.status == 'success') { |
| 327 |
wa_preview.removeClass('hide').addClass('d-block'); |
| 328 |
renderMessagePreview(response.data); |
| 329 |
} |
| 330 |
}, |
| 331 |
}); |
| 332 |
} |
| 333 |
|
| 334 |
// Notifications page - change save button text |
| 335 |
function updateNotificationSaveButtonText() { |
| 336 |
var type = $('#notifier_notification_type :checked').val(); |
| 337 |
var when = $('#notifier_notification_when :checked').val(); |
| 338 |
var btnText = ''; |
| 339 |
if(type == 'transactional'){ |
| 340 |
btnText = 'Save'; |
| 341 |
} |
| 342 |
else { |
| 343 |
btnText = (when == 'now') ? 'Save & Send' : 'Save & Schedule'; |
| 344 |
} |
| 345 |
$('#publish').val(btnText); |
| 346 |
} |
| 347 |
|
| 348 |
// Uploading media to WP using wp.media |
| 349 |
var file_frame; |
| 350 |
function uploadMediaFile( button, preview_media ) { |
| 351 |
var button_id = button.attr('id'); |
| 352 |
var field_id = button_id.replace( '_button', '' ); |
| 353 |
var preview_id = button_id.replace( '_button', '_preview' ); |
| 354 |
|
| 355 |
// Create the media frame. |
| 356 |
file_frame = wp.media.frames.file_frame = wp.media({ |
| 357 |
title: button.data( 'uploader_title' ), |
| 358 |
button: { |
| 359 |
text: button.data( 'uploader_button_text' ), |
| 360 |
}, |
| 361 |
library: { |
| 362 |
type: button.data( 'uploader_supported_file_types' ).split(',') |
| 363 |
}, |
| 364 |
multiple: false |
| 365 |
}); |
| 366 |
|
| 367 |
// When an image is selected, run a callback. |
| 368 |
file_frame.on( 'select', function() { |
| 369 |
attachment = file_frame.state().get('selection').first().toJSON(); |
| 370 |
jQuery("#"+field_id).attr('data-type', attachment.type); |
| 371 |
jQuery("#"+field_id).attr('data-subtype', attachment.subtype); |
| 372 |
jQuery("#"+field_id).siblings('.notifier-media-preview').find('.notifier-media-preview-item').addClass('hide'); |
| 373 |
if( preview_media ) { |
| 374 |
if('image' == attachment.type || ('application' == attachment.type && 'pdf' == attachment.subtype) ) { |
| 375 |
jQuery("#"+field_id).attr('data-url', attachment.sizes.full.url); |
| 376 |
jQuery("#"+preview_id+'_image').removeClass('hide').attr('src', attachment.sizes.thumbnail.url); |
| 377 |
} |
| 378 |
else if ('video' == attachment.type) { |
| 379 |
jQuery("#"+field_id).attr('data-url', attachment.url); |
| 380 |
jQuery("#"+preview_id+'_video').removeClass('hide').find('source').attr('src', attachment.url); |
| 381 |
jQuery("#"+preview_id+'_video')[0].load(); |
| 382 |
} |
| 383 |
} |
| 384 |
jQuery("#"+field_id).val(attachment.id).change(); |
| 385 |
}); |
| 386 |
|
| 387 |
// Finally, open the modal |
| 388 |
file_frame.open(); |
| 389 |
} |
| 390 |
|
| 391 |
$(document).on('ready', function() { |
| 392 |
|
| 393 |
window.messageTemplatePreviewData = {}; |
| 394 |
|
| 395 |
/***************** |
| 396 |
* Dashboard page |
| 397 |
****************/ |
| 398 |
|
| 399 |
// Toggle steps on dashboard page |
| 400 |
$('.toggle-step').on('click', function() { |
| 401 |
$(this).closest('.step').toggleClass('active'); |
| 402 |
$(this).closest('.step').siblings().removeClass('active'); |
| 403 |
}); |
| 404 |
|
| 405 |
// Highlight menu items |
| 406 |
const wan_menu_elem = $('#toplevel_page_notifier'); |
| 407 |
const notifier_cpts = ['wa_message_template', 'wa_contact', 'wa_notification']; |
| 408 |
const current_cpt = $('#notifier-admin-header').data('post-type') || ''; |
| 409 |
if (notifier_cpts.includes(current_cpt)) { |
| 410 |
wan_menu_elem |
| 411 |
.removeClass('wp-not-current-submenu') |
| 412 |
.addClass('wp-has-current-submenu') |
| 413 |
.children('a') |
| 414 |
.removeClass('wp-not-current-submenu') |
| 415 |
.addClass('wp-has-current-submenu'); |
| 416 |
wan_menu_elem.find('a[href*="' + current_cpt + '"]').addClass('current').closest('li').addClass('current'); |
| 417 |
} |
| 418 |
|
| 419 |
/************************* |
| 420 |
* Message Template page |
| 421 |
*************************/ |
| 422 |
|
| 423 |
// Make the WhatsApp preview sidebar sticky |
| 424 |
if ($('#notifier-message-template-preview').length > 0) { |
| 425 |
var wa_preview = $('#notifier-message-template-preview'); |
| 426 |
var wa_preview_top = wa_preview.removeClass('hide-if-js hide').offset().top - 50; |
| 427 |
var wa_preview_width = wa_preview.width(); |
| 428 |
wa_preview.width(wa_preview_width); |
| 429 |
$(window).scroll( function() { |
| 430 |
if (window.pageYOffset > wa_preview_top && window.innerWidth > 850) { |
| 431 |
wa_preview.addClass('sticky'); |
| 432 |
} else { |
| 433 |
wa_preview.removeClass('sticky'); |
| 434 |
} |
| 435 |
}); |
| 436 |
} |
| 437 |
|
| 438 |
// Validate the message template name |
| 439 |
$('#notifier_template_name').on('keyup', function() { |
| 440 |
var value = $(this).val().trim(); |
| 441 |
$(this).val(value.replaceAll(' ', '_').replaceAll(/[^a-zA-Z0-9_]/g, '').toLowerCase()); |
| 442 |
}); |
| 443 |
|
| 444 |
// Trigger conditional logic and WhatsApp message template preview |
| 445 |
if ($('#notifier-message-template-data').length > 0) { |
| 446 |
fetcDataAndPreviewTemplate(); |
| 447 |
$('#notifier-message-template-data :input').on('change keyup', function() { |
| 448 |
fetcDataAndPreviewTemplate(); |
| 449 |
}); |
| 450 |
} |
| 451 |
|
| 452 |
// Update template name as per the title value |
| 453 |
$('.post-type-wa_message_template #title').on('change', function() { |
| 454 |
const title = $(this).val().trim() || ''; |
| 455 |
const template_name = $('#notifier_template_name').val() || ''; |
| 456 |
if('' !== template_name) { |
| 457 |
return; |
| 458 |
} |
| 459 |
$('#notifier_template_name').val(title.replaceAll(' ', '_').replaceAll(/[^a-zA-Z0-9_]/g, '').toLowerCase()); |
| 460 |
}) |
| 461 |
|
| 462 |
// Message template publish confirmation |
| 463 |
$('.post-type-wa_message_template #publish').on('click', function() { |
| 464 |
const template_name = $('#notifier_template_name').val() || ''; |
| 465 |
const body_text = $('#notifier_body_text').val() || ''; |
| 466 |
if ('' === template_name && '' === body_text) { |
| 467 |
alert('Template name and Body text are required fields.'); |
| 468 |
return false; |
| 469 |
} |
| 470 |
if ('' === template_name) { |
| 471 |
alert('Template name is a required field.'); |
| 472 |
return false; |
| 473 |
} |
| 474 |
if ('' === body_text) { |
| 475 |
alert('Body text is a required field.'); |
| 476 |
return false; |
| 477 |
} |
| 478 |
|
| 479 |
var header_text = $('#notifier_header_text').val() || ''; |
| 480 |
if(header_text.match(/{{.*?}}/g) !== null || body_text.match(/{{.*?}}/g) !== null) { |
| 481 |
alert('Free version of the plugin does not support variables. Please remove variables like {{}} and try again.'); |
| 482 |
return false; |
| 483 |
} |
| 484 |
|
| 485 |
return confirm('IMPORTANT NOTE:\n\nClicking "OK" will send your template data to WhatsApp for approval. It might take between 30 minutes to 24 hours for them to review it.\n\nYou\'ll get confirmation email from them after they complete their review. You will be able to send this template to your contacts only after their approval.'); |
| 486 |
}); |
| 487 |
|
| 488 |
// Message template delete confirmation |
| 489 |
$('.post-type-wa_message_template .row-actions .delete').on('click', function() { |
| 490 |
return confirm('Deleting from here will also delete this template from WhatsApp server.'); |
| 491 |
}); |
| 492 |
|
| 493 |
// Add Refresh Status button to the message template lisitng page |
| 494 |
var refresh_mt_status_template = $('#notifier-template-wa-mt-refresh').html().trim(); |
| 495 |
$('.edit-php.post-type-wa_message_template .wrap .page-title-action').after(refresh_mt_status_template); |
| 496 |
|
| 497 |
/*************** |
| 498 |
* Contact page |
| 499 |
**************/ |
| 500 |
|
| 501 |
// Add Import button and HTML to the Contacts lisitng page |
| 502 |
var import_contact_template = $('#notifier-template-import-contacts').html().trim(); |
| 503 |
$('.edit-php.post-type-wa_contact .wrap .page-title-action').after(import_contact_template); |
| 504 |
|
| 505 |
// Show import options |
| 506 |
$(document).on('click', '#import-contacts', function(e) { |
| 507 |
e.preventDefault(); |
| 508 |
$('.contact-import-options').toggleClass('hide'); |
| 509 |
}); |
| 510 |
|
| 511 |
// Select import method |
| 512 |
$(document).on('change', '.csv-import-method', function() { |
| 513 |
var value = $(this).val(); |
| 514 |
$('.col-import').addClass('hide'); |
| 515 |
$('.col-import-' + value).removeClass('hide'); |
| 516 |
}); |
| 517 |
|
| 518 |
// On submission of CSV import form |
| 519 |
$(document).on('submit', '#import-contacts-csv', function() { |
| 520 |
var file = $('#notifier-contacts-csv').val(); |
| 521 |
if (file == '') { |
| 522 |
alert('Please select a file to upload.'); |
| 523 |
return false; |
| 524 |
} else { |
| 525 |
var file_size = $('#notifier-contacts-csv')[0].files[0].size / 1024 / 1024; |
| 526 |
if (file_size > 24) { |
| 527 |
alert('Please select CSV file smaller than 24MB.'); |
| 528 |
return false; |
| 529 |
} |
| 530 |
var allowedExtension = /(\.csv)$/i; |
| 531 |
if (!allowedExtension.exec(file)) { |
| 532 |
alert('Please select a CSV file.'); |
| 533 |
$('#notifier-contacts-csv').val(''); |
| 534 |
return false; |
| 535 |
} |
| 536 |
} |
| 537 |
}); |
| 538 |
|
| 539 |
// On submission of users import form |
| 540 |
$(document).on('submit', '#import-contacts-users', function() { |
| 541 |
const wa_contact_list_name = $('#wa_contact_list_name').val() || ''; |
| 542 |
if (wa_contact_list_name == '') { |
| 543 |
alert('Please enter a Contact List name.'); |
| 544 |
return false; |
| 545 |
} |
| 546 |
}); |
| 547 |
|
| 548 |
// Contact publish confirmation |
| 549 |
$('.post-type-wa_contact #publish').on('click', function() { |
| 550 |
const first_name = $('#notifier_first_name').val() || ''; |
| 551 |
const last_name = $('#notifier_last_name').val() || ''; |
| 552 |
const wa_number = $('#notifier_wa_number').val() || ''; |
| 553 |
|
| 554 |
if (first_name == '' || last_name == '' || wa_number == '') { |
| 555 |
alert('First Name, Last Name and Phone Number are required fields.'); |
| 556 |
return false; |
| 557 |
} |
| 558 |
}); |
| 559 |
|
| 560 |
/******************** |
| 561 |
* Notification page |
| 562 |
********************/ |
| 563 |
|
| 564 |
// Do stuff if on the notification edit page |
| 565 |
if ($('#notifier-notification-data').length > 0) { |
| 566 |
updateNotificationSaveButtonText(); // Update button text as per user selection |
| 567 |
fetchAndDisplaySendToFields(); // Fetch and display send to fields |
| 568 |
fetchAndDisplayMessageTemplate(); // Fetch and display message template preview + variable mapping fields |
| 569 |
$('#notifier-notification-data :input').on('change keyup', function(){ |
| 570 |
updateNotificationSaveButtonText(); |
| 571 |
|
| 572 |
if('notifier_notification_type' == $(this).attr('name')){ |
| 573 |
$('#notifier_notification_trigger').val('').change(); |
| 574 |
$('#notifier_notification_message_template').val('').change(); |
| 575 |
} |
| 576 |
|
| 577 |
if('notifier_notification_trigger' == $(this).attr('name')){ |
| 578 |
fetchAndDisplaySendToFields(); |
| 579 |
fetchAndDisplayMessageTemplate(); |
| 580 |
} |
| 581 |
|
| 582 |
if('notifier_notification_message_template' == $(this).attr('name')){ |
| 583 |
fetchAndDisplayMessageTemplate(); |
| 584 |
} |
| 585 |
}); |
| 586 |
|
| 587 |
var dateToday = new Date(); |
| 588 |
$('#notifier_notification_datetime').datetimepicker({ |
| 589 |
minDate: dateToday, |
| 590 |
defaultDate: 1, |
| 591 |
}); |
| 592 |
} |
| 593 |
|
| 594 |
// Notification publish confirmation |
| 595 |
$('.post-type-wa_notification #publish').on('click', function() { |
| 596 |
const type = $('#notifier_notification_type').val() || ''; |
| 597 |
const template_name = $('#notifier_notification_message_template').val() || ''; |
| 598 |
|
| 599 |
if ('' === type) { |
| 600 |
alert('Please select a notification type.'); |
| 601 |
return false; |
| 602 |
} |
| 603 |
else if ('transactional' == type) { |
| 604 |
const trigger = $('#notifier_notification_trigger :selected').val() || ''; |
| 605 |
if ('' === trigger) { |
| 606 |
alert('Please select a trigger.'); |
| 607 |
return false; |
| 608 |
} |
| 609 |
|
| 610 |
var recipientMissing = false; |
| 611 |
$('.notifier-recipient').each(function(){ |
| 612 |
const recipient = $('option:selected',this).val() || ''; |
| 613 |
if($(this).is(':visible') && '' == recipient){ |
| 614 |
recipientMissing = true; |
| 615 |
} |
| 616 |
}); |
| 617 |
|
| 618 |
if(recipientMissing){ |
| 619 |
alert('Recipient field can not be empty.'); |
| 620 |
return false; |
| 621 |
} |
| 622 |
} |
| 623 |
else if ('marketing' == type) { |
| 624 |
const list = $('#notifier_notification_list :selected').val() || ''; |
| 625 |
if ('' === list) { |
| 626 |
alert('Please select a contact list.'); |
| 627 |
return false; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
if ('' === template_name) { |
| 632 |
alert('Please select a Message Template.'); |
| 633 |
return false; |
| 634 |
} |
| 635 |
|
| 636 |
if( $('.notifier-variable-mapping').length > 0 ){ |
| 637 |
var recipientValue = false; |
| 638 |
$('.notifier-variable-mapping').each(function(){ |
| 639 |
const value = $('option:selected',this).val() || ''; |
| 640 |
if($(this).is(':visible') && '' == value){ |
| 641 |
recipientValue = true; |
| 642 |
} |
| 643 |
}); |
| 644 |
if(recipientValue){ |
| 645 |
alert('Message template variable missing mapped value.'); |
| 646 |
return false; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
var header_text = messageTemplatePreviewData.header_text || ''; |
| 651 |
var body_text = messageTemplatePreviewData.body_text || ''; |
| 652 |
if(header_text.match(/{{.*?}}/g) !== null || body_text.match(/{{.*?}}/g) !== null) { |
| 653 |
alert('Free version of the plugin does not support sending message templates with variables. Please select a message template that does not use variables.'); |
| 654 |
return false; |
| 655 |
} |
| 656 |
}); |
| 657 |
|
| 658 |
// Add new Notification Send To receiver fields row |
| 659 |
$(document).on('click', '.add-recipient', function(e){ |
| 660 |
e.preventDefault(); |
| 661 |
var next = $('.send-to-fields .fields-repeater tr.row').length; |
| 662 |
var nextRowHtml = $('.row-add-recipient-template').get(0).outerHTML.replaceAll('row_num', next).replace('hide row-add-recipient-template', ''); |
| 663 |
$('.send-to-fields .fields-repeater tbody').append(nextRowHtml); |
| 664 |
conditionallyShowFields(); |
| 665 |
}); |
| 666 |
|
| 667 |
$(document).on('click', '.delete-repeater-field span', function(e){ |
| 668 |
e.preventDefault(); |
| 669 |
$(this).closest('tr.row').remove(); |
| 670 |
}); |
| 671 |
|
| 672 |
/***************** |
| 673 |
* Global |
| 674 |
****************/ |
| 675 |
|
| 676 |
// Make the top admin header sticky |
| 677 |
var wpcontent_top = $('#wpcontent').offset().top; |
| 678 |
$(window).scroll( function() { |
| 679 |
if (window.pageYOffset > wpcontent_top) { |
| 680 |
$('#notifier-admin-header').addClass('sticky'); |
| 681 |
} else { |
| 682 |
$('#notifier-admin-header').removeClass('sticky'); |
| 683 |
} |
| 684 |
}); |
| 685 |
|
| 686 |
// Show feilds conditionally |
| 687 |
conditionallyShowFields(); |
| 688 |
$(document).on('change keyup', '.meta-fields :input', function() { |
| 689 |
conditionallyShowFields(); |
| 690 |
}); |
| 691 |
|
| 692 |
/***************** |
| 693 |
* Settings page |
| 694 |
****************/ |
| 695 |
|
| 696 |
$('.notifier-media-upload-button').click(function() { |
| 697 |
uploadMediaFile( $(this), true ); |
| 698 |
}); |
| 699 |
|
| 700 |
$('.notifier-media-delete-button').click(function() { |
| 701 |
$(this).next( '.notifier-media-attachment-id' ).val( '' ).attr('data-tyoe', '').attr('data-subtyoe', '').attr('data-url', '').change(); |
| 702 |
$(this).siblings( '.notifier-media-preview' ).find('img').attr('src', '').addClass('hide'); |
| 703 |
$(this).siblings( '.notifier-media-preview' ).find('source').attr('src', '').parent().addClass('hide'); |
| 704 |
return false; |
| 705 |
}); |
| 706 |
|
| 707 |
|
| 708 |
}); |
| 709 |
|
| 710 |
})(jQuery); |
| 711 |
|