From 2d8420c7a2f3d69832efc87bee090a26c2950b28 Mon Sep 17 00:00:00 2001 From: jknapp Date: Tue, 12 Aug 2025 07:18:25 -0700 Subject: [PATCH] testing progress --- admin/class-twp-admin.php | 24 ++++++++++------ includes/class-twp-agent-manager.php | 43 ++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index 2a73718..67682f3 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -3738,19 +3738,24 @@ class TWP_Admin { global $wpdb; $calls_table = $wpdb->prefix . 'twp_queued_calls'; $queues_table = $wpdb->prefix . 'twp_call_queues'; + $groups_table = $wpdb->prefix . 'twp_group_members'; - $waiting_calls = $wpdb->get_results(" + $user_id = get_current_user_id(); + + // Get waiting calls only from queues the user is a member of + $waiting_calls = $wpdb->get_results($wpdb->prepare(" SELECT c.*, q.queue_name, TIMESTAMPDIFF(SECOND, c.joined_at, NOW()) as wait_seconds FROM $calls_table c JOIN $queues_table q ON c.queue_id = q.id - WHERE c.status = 'waiting' + JOIN $groups_table gm ON gm.group_id = q.agent_group_id + WHERE c.status = 'waiting' AND gm.user_id = %d ORDER BY c.position ASC - "); + ", $user_id)); - wp_send_json_success($waiting_calls); + wp_send_json_success(['waiting_calls' => $waiting_calls]); } /** @@ -5430,12 +5435,13 @@ class TWP_Admin { codecPreferences: ['opus', 'pcmu'] }); - Twilio.Device.ready(function(device) { + // Use modern EventEmitter interface instead of deprecated callbacks + Twilio.Device.on('ready', function(device) { $('#phone-status').text('Ready').css('color', '#4CAF50'); $('#call-btn').prop('disabled', false); }); - Twilio.Device.error(function(error) { + Twilio.Device.on('error', function(error) { console.error('Twilio Device Error:', error); var errorMsg = error.message; @@ -5452,7 +5458,7 @@ class TWP_Admin { showError(errorMsg); }); - Twilio.Device.connect(function(conn) { + Twilio.Device.on('connect', function(conn) { currentConnection = conn; $('#phone-status').text('Connected').css('color', '#2196F3'); $('#call-btn').hide(); @@ -5462,7 +5468,7 @@ class TWP_Admin { startCallTimer(); }); - Twilio.Device.disconnect(function(conn) { + Twilio.Device.on('disconnect', function(conn) { currentConnection = null; $('#phone-status').text('Ready').css('color', '#4CAF50'); $('#hangup-btn').hide(); @@ -5474,7 +5480,7 @@ class TWP_Admin { stopCallTimer(); }); - Twilio.Device.incoming(function(conn) { + Twilio.Device.on('incoming', function(conn) { currentConnection = conn; $('#phone-status').text('Incoming Call').css('color', '#FF9800'); $('#phone-number-display').text(conn.parameters.From || 'Unknown Number'); diff --git a/includes/class-twp-agent-manager.php b/includes/class-twp-agent-manager.php index 9599004..905f39c 100644 --- a/includes/class-twp-agent-manager.php +++ b/includes/class-twp-agent-manager.php @@ -212,10 +212,16 @@ class TWP_Agent_Manager { return array('success' => false, 'error' => 'Call not found or already answered'); } - // Get user's phone number + // Check user's call mode + $call_mode = get_user_meta($user_id, 'twp_call_mode', true); + if (empty($call_mode)) { + $call_mode = 'cell'; // Default to cell phone + } + + // Get user's phone number (needed for cell mode and as fallback) $phone_number = get_user_meta($user_id, 'twp_phone_number', true); - if (!$phone_number) { + if ($call_mode === 'cell' && !$phone_number) { return array('success' => false, 'error' => 'No phone number configured for user'); } @@ -273,13 +279,32 @@ class TWP_Agent_Manager { 'original_call_sid' => $call->call_sid ), $status_callback_url); - // Make call to agent with proper workflow number as caller ID and status tracking - $result = $twilio->make_call( - $phone_number, - $connect_url, - $status_callback_url, // Track call status for voicemail detection - $workflow_number // Use queue's phone number as caller ID - ); + // Handle different call modes + if ($call_mode === 'browser') { + // For browser mode, redirect the existing call to the browser client + $current_user = get_userdata($user_id); + $client_name = 'agent_' . $user_id . '_' . sanitize_title($current_user->display_name); + + // Create TwiML to redirect call to browser client + $twiml = new \Twilio\TwiML\VoiceResponse(); + $twiml->say('Connecting you to an agent.', ['voice' => 'alice']); + $dial = $twiml->dial(['timeout' => 30]); + $dial->client($client_name); + + // Update the existing call to redirect to browser + $result = $twilio->update_call($call->call_sid, [ + 'twiml' => $twiml->asXML() + ]); + + } else { + // For cell mode, make call to agent's phone number + $result = $twilio->make_call( + $phone_number, + $connect_url, + $status_callback_url, // Track call status for voicemail detection + $workflow_number // Use queue's phone number as caller ID + ); + } if ($result['success']) { // Log the call acceptance