`;
});
$queueList.html(html);
// Show user extension in queue global actions if we found it
if (userExtension) {
const $globalActions = $('#twp-queue-global-actions .global-queue-actions');
if ($globalActions.find('.user-extension-display').length === 0) {
$globalActions.prepend(`
π Your Extension: ${userExtension}
`);
}
}
// Auto-select first queue with calls, or first personal queue, or first queue
const firstQueueWithCalls = userQueues.find(q => parseInt(q.current_waiting) > 0);
const firstPersonalQueue = userQueues.find(q => q.queue_type === 'personal');
const queueToSelect = firstQueueWithCalls || firstPersonalQueue || userQueues[0];
if (queueToSelect) {
selectQueue(queueToSelect.id);
}
}
/**
* Select a queue
*/
function selectQueue(queueId) {
selectedQueue = userQueues.find(q => q.id == queueId);
if (!selectedQueue) return;
// Update UI selection
$('.queue-item').removeClass('selected');
$(`.queue-item[data-queue-id="${queueId}"]`).addClass('selected');
// Update queue controls
$('#selected-queue-name').text(selectedQueue.queue_name);
$('#twp-waiting-count').text(selectedQueue.current_waiting || 0);
$('#twp-queue-max-size').text(selectedQueue.max_size);
// Show queue controls if there are waiting calls
if (parseInt(selectedQueue.current_waiting) > 0) {
$('#twp-queue-controls').show();
} else {
$('#twp-queue-controls').hide();
}
}
/**
* Accept next call from selected queue
*/
function acceptQueueCall() {
if (!selectedQueue) {
showMessage('Please select a queue first', 'error');
return;
}
$.ajax({
url: twp_frontend_ajax.ajax_url,
method: 'POST',
data: {
action: 'twp_accept_next_queue_call',
queue_id: selectedQueue.id,
nonce: twp_frontend_ajax.nonce
},
success: function(response) {
if (response.success) {
showMessage('Connecting to next caller...', 'info');
// Refresh queue data after accepting call
setTimeout(loadUserQueues, 1000);
} else {
showMessage(response.data || 'No calls waiting in this queue', 'info');
}
},
error: function() {
showMessage('Failed to accept queue call', 'error');
}
});
}
/**
* Update call state UI
*/
function updateCallState(state) {
const $callBtn = $('#twp-call-btn');
const $hangupBtn = $('#twp-hangup-btn');
const $resumeBtn = $('#twp-resume-btn');
const $controlsPanel = $('#twp-call-controls-panel');
switch (state) {
case 'idle':
$callBtn.show().prop('disabled', false);
$hangupBtn.hide();
$resumeBtn.hide();
$controlsPanel.hide();
break;
case 'connecting':
case 'ringing':
$callBtn.hide();
$hangupBtn.show();
$resumeBtn.hide();
$controlsPanel.hide();
break;
case 'connected':
$callBtn.hide();
$hangupBtn.show();
$resumeBtn.hide(); // Will be shown by hold logic when needed
$controlsPanel.show();
break;
}
}
/**
* Show call info panel
*/
function showCallInfo(status) {
$('#twp-call-info').show();
$('#twp-call-status').text(status);
}
/**
* Hide call info panel
*/
function hideCallInfo() {
$('#twp-call-info').hide();
$('#twp-call-timer').text('00:00');
$('#twp-call-status').text('');
}
/**
* Start call timer
*/
function startCallTimer() {
callStartTime = new Date();
callTimer = setInterval(updateCallTimer, 1000);
updateCallTimer();
}
/**
* Stop call timer
*/
function stopCallTimer() {
if (callTimer) {
clearInterval(callTimer);
callTimer = null;
}
callStartTime = null;
}
/**
* Update call timer display
*/
function updateCallTimer() {
if (!callStartTime) return;
const elapsed = Math.floor((new Date() - callStartTime) / 1000);
const minutes = Math.floor(elapsed / 60);
const seconds = elapsed % 60;
const timeString = String(minutes).padStart(2, '0') + ':' + String(seconds).padStart(2, '0');
$('#twp-call-timer').text(timeString);
}
/**
* Update connection status
*/
function updateStatus(status, text) {
const $indicator = $('#twp-status-indicator');
const $text = $('#twp-status-text');
$indicator.removeClass('offline connecting online').addClass(status);
$text.text(text);
}
/**
* Show message to user
*/
function showMessage(message, type) {
const $messages = $('#twp-messages');
const $message = $('
').addClass('twp-' + type).text(message);
$messages.empty().append($message);
// Auto-hide success and info messages
if (type === 'success' || type === 'info') {
setTimeout(function() {
$message.fadeOut(function() {
$message.remove();
});
}, 5000);
}
}
// Start queue polling with faster interval
startQueuePolling();
/**
* Start polling for queue updates
*/
function startQueuePolling() {
// Clear any existing timer
if (queuePollingTimer) {
clearInterval(queuePollingTimer);
}
// Poll every 5 seconds for real-time updates
queuePollingTimer = setInterval(function() {
if (isConnected) {
loadUserQueues(true); // Silent update
}
}, 5000); // Every 5 seconds
}
/**
* Schedule token refresh
* Refreshes token 10 minutes before expiry for safety
*/
function scheduleTokenRefresh() {
// Clear any existing timer
if (tokenRefreshTimer) {
clearTimeout(tokenRefreshTimer);
}
if (!tokenExpiry) {
console.error('Token expiry time not set');
// Retry in 30 seconds if token expiry not set
setTimeout(function() {
if (tokenExpiry) {
scheduleTokenRefresh();
}
}, 30000);
return;
}
// Calculate time until refresh (10 minutes before expiry for extra safety)
const refreshBuffer = 10 * 60 * 1000; // 10 minutes in milliseconds
const timeUntilRefresh = tokenExpiry - Date.now() - refreshBuffer;
if (timeUntilRefresh <= 0) {
// Token needs refresh immediately
refreshToken();
} else {
// Schedule refresh
console.log('Scheduling token refresh in', Math.round(timeUntilRefresh / 1000), 'seconds');
tokenRefreshTimer = setTimeout(refreshToken, timeUntilRefresh);
}
}
/**
* Refresh the capability token
*/
function refreshToken() {
console.log('Refreshing capability token...');
// Don't refresh if currently in a call
if (currentCall) {
console.log('Currently in call, postponing token refresh');
// Retry in 1 minute
setTimeout(refreshToken, 60000);
return;
}
$.ajax({
url: twp_frontend_ajax.ajax_url,
method: 'POST',
data: {
action: 'twp_generate_capability_token',
nonce: twp_frontend_ajax.nonce
},
success: function(response) {
if (response.success) {
console.log('Token refreshed successfully');
// Update token expiry
tokenExpiry = Date.now() + (response.data.expires_in || 3600) * 1000;
// Update device with new token
setupTwilioDevice(response.data.token);
// Schedule next refresh
scheduleTokenRefresh();
} else {
console.error('Failed to refresh token:', response.data);
updateStatus('offline', 'Token refresh failed');
showMessage('Failed to refresh connection. Please refresh the page.', 'error');
}
},
error: function() {
console.error('Failed to refresh token - network error');
updateStatus('offline', 'Connection lost');
// Retry in 30 seconds
setTimeout(refreshToken, 30000);
}
});
}
/**
* Initialize alert sound
*/
function initAlertSound() {
// Create audio element for alert sound
alertSound = new Audio();
alertSound.src = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQAAAAA='; // Simple beep sound
// Use Web Audio API for better sound
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Create a simple beep sound
function playBeep() {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.value = 800; // Frequency in Hz
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.5);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.5);
}
return playBeep;
}
const playAlertSound = initAlertSound();
/**
* Start alert for new calls
*/
function startAlert() {
if (!alertEnabled || alertInterval) return;
// Check if there are actually waiting calls before starting alert
const hasWaitingCalls = userQueues.some(q => parseInt(q.current_waiting) > 0);
if (!hasWaitingCalls) {
console.log('No waiting calls found, not starting alert');
return;
}
// Play initial alert
playAlertSound();
// Repeat every 30 seconds
alertInterval = setInterval(function() {
if (alertEnabled && !currentCall) {
// Check if there are still waiting calls
const stillHasWaitingCalls = userQueues.some(q => parseInt(q.current_waiting) > 0);
if (stillHasWaitingCalls) {
playAlertSound();
} else {
console.log('No more waiting calls, stopping alert');
stopAlert();
}
} else {
stopAlert();
}
}, 30000);
}
/**
* Stop alert
*/
function stopAlert() {
if (alertInterval) {
clearInterval(alertInterval);
alertInterval = null;
}
}
/**
* Toggle alert on/off
*/
function toggleAlert() {
alertEnabled = !alertEnabled;
localStorage.setItem('twp_alert_enabled', alertEnabled);
// Update button state
updateAlertButton();
if (!alertEnabled) {
stopAlert();
showMessage('Queue alerts disabled', 'info');
} else {
showMessage('Queue alerts enabled', 'success');
// Check if there are waiting calls
const hasWaitingCalls = userQueues.some(q => parseInt(q.current_waiting) > 0);
if (hasWaitingCalls && !currentCall) {
startAlert();
}
}
}
/**
* Update alert button UI
*/
function updateAlertButton() {
const $btn = $('#twp-alert-toggle');
if (alertEnabled) {
$btn.removeClass('alert-off').addClass('alert-on').html('π Alerts ON');
} else {
$btn.removeClass('alert-on').addClass('alert-off').html('π Alerts OFF');
}
}
/**
* Load alert preference from localStorage
*/
function loadAlertPreference() {
const saved = localStorage.getItem('twp_alert_enabled');
alertEnabled = saved === null ? true : saved === 'true';
updateAlertButton();
}
// Clean up on page unload
$(window).on('beforeunload', function() {
if (tokenRefreshTimer) {
clearTimeout(tokenRefreshTimer);
}
if (queuePollingTimer) {
clearInterval(queuePollingTimer);
}
if (alertInterval) {
clearInterval(alertInterval);
}
if (backgroundAlertInterval) {
clearInterval(backgroundAlertInterval);
}
if (personalQueueTimer) {
clearInterval(personalQueueTimer);
}
if (twilioDevice) {
twilioDevice.destroy();
}
});
/**
* Load user's voicemails
*/
function loadUserVoicemails(silent = false) {
$.ajax({
url: twp_frontend_ajax.ajax_url,
method: 'POST',
data: {
action: 'twp_get_user_voicemails',
nonce: twp_frontend_ajax.nonce
},
success: function(response) {
if (response.success) {
displayVoicemails(response.data);
} else if (!silent) {
showMessage('Failed to load voicemails: ' + (response.data || 'Unknown error'), 'error');
}
},
error: function() {
if (!silent) {
showMessage('Failed to load voicemails', 'error');
}
}
});
}
/**
* Toggle voicemail section visibility
*/
function toggleVoicemailSection() {
const $content = $('#twp-voicemail-content');
const $toggle = $('#twp-voicemail-toggle .toggle-icon');
const isVisible = $content.is(':visible');
if (isVisible) {
$content.slideUp(300);
$toggle.text('βΌ');
localStorage.setItem('twp_voicemail_collapsed', 'true');
} else {
$content.slideDown(300);
$toggle.text('β²');
localStorage.setItem('twp_voicemail_collapsed', 'false');
// Load voicemails when expanding if not already loaded
if ($('#twp-voicemail-list').children('.voicemail-loading').length > 0) {
loadUserVoicemails();
}
}
}
/**
* Initialize voicemail section state
*/
function initVoicemailSection() {
const isCollapsed = localStorage.getItem('twp_voicemail_collapsed') === 'true';
const $content = $('#twp-voicemail-content');
const $toggle = $('#twp-voicemail-toggle .toggle-icon');
if (isCollapsed) {
$content.hide();
$toggle.text('βΌ');
} else {
$content.show();
$toggle.text('β²');
// Load voicemails immediately if expanded
loadUserVoicemails();
}
}
/**
* Display voicemails in the UI
*/
function displayVoicemails(data) {
const $voicemailList = $('#twp-voicemail-list');
// Update stats
$('#twp-total-voicemails').text(data.total_count || 0);
$('#twp-today-voicemails').text(data.today_count || 0);
if (!data.voicemails || data.voicemails.length === 0) {
$voicemailList.html('
No voicemails found.
');
return;
}
let html = '';
data.voicemails.forEach(function(voicemail) {
const hasTranscription = voicemail.transcription && voicemail.transcription !== 'No transcription';
const hasRecording = voicemail.has_recording;
html += `
π${voicemail.from_number}
${voicemail.time_ago}
β±οΈ${formatDuration(voicemail.duration)}
${hasRecording ? 'π΅ Recording' : ''}
${hasTranscription ? `
${voicemail.transcription}
` : ''}
`;
});
$voicemailList.html(html);
}
/**
* Format duration in seconds to mm:ss
*/
function formatDuration(seconds) {
if (!seconds || seconds === 0) return '0:00';
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return minutes + ':' + String(remainingSeconds).padStart(2, '0');
}
/**
* Play voicemail audio
*/
function playVoicemail(voicemailId) {
if (!voicemailId) return;
// Get voicemail audio URL and play it
$.ajax({
url: twp_frontend_ajax.ajax_url,
method: 'POST',
data: {
action: 'twp_get_voicemail_audio',
voicemail_id: voicemailId,
nonce: twp_frontend_ajax.nonce
},
success: function(response) {
if (response.success && response.data.audio_url) {
// Create and play audio element
const audio = new Audio(response.data.audio_url);
audio.play().catch(function(error) {
showMessage('Failed to play voicemail: ' + error.message, 'error');
});
showMessage('Playing voicemail...', 'info');
} else {
showMessage('No audio available for this voicemail', 'error');
}
},
error: function() {
showMessage('Failed to load voicemail audio', 'error');
}
});
}
/**
* Initialize browser notifications
*/
function initializeNotifications() {
// Register service worker for background notifications
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/wp-content/plugins/twilio-wp-plugin/assets/js/twp-service-worker.js')
.then(function(registration) {
console.log('Service Worker registered:', registration);
})
.catch(function(error) {
console.log('Service Worker registration failed:', error);
});
}
// Check if browser supports notifications
if (!('Notification' in window)) {
console.log('This browser does not support notifications');
return;
}
// Check current permission status
notificationPermission = Notification.permission;
// Request permission if not already granted or denied
if (notificationPermission === 'default') {
// Add a button to request permission
if ($('#twp-queue-global-actions').length > 0) {
const $notificationBtn = $('