diff --git a/includes/class-twp-agent-manager.php b/includes/class-twp-agent-manager.php index 02cb48c..54ce692 100644 --- a/includes/class-twp-agent-manager.php +++ b/includes/class-twp-agent-manager.php @@ -283,7 +283,12 @@ class TWP_Agent_Manager { 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); + // Twilio requires alphanumeric characters only - must match generate_capability_token + $clean_name = preg_replace('/[^a-zA-Z0-9]/', '', $current_user->display_name); + if (empty($clean_name)) { + $clean_name = 'user'; + } + $client_name = 'agent' . $user_id . $clean_name; // Create TwiML to redirect call to browser client $twiml = new \Twilio\TwiML\VoiceResponse(); diff --git a/includes/class-twp-webhooks.php b/includes/class-twp-webhooks.php index 18a9b7b..1a56897 100644 --- a/includes/class-twp-webhooks.php +++ b/includes/class-twp-webhooks.php @@ -408,7 +408,12 @@ class TWP_Webhooks { $agent_phone = get_user_meta($agent->ID, 'twp_phone_number', true); if ($call_mode === 'browser') { - $client_name = 'agent_' . $agent->ID . '_' . sanitize_title($agent->display_name); + // Twilio requires alphanumeric characters only + $clean_name = preg_replace('/[^a-zA-Z0-9]/', '', $agent->display_name); + if (empty($clean_name)) { + $clean_name = 'user'; + } + $client_name = 'agent' . $agent->ID . $clean_name; $browser_agents[] = $client_name; } elseif (!empty($agent_phone)) { $cell_agents[] = $agent_phone; @@ -485,7 +490,12 @@ class TWP_Webhooks { $agent_phone = get_user_meta($agent->ID, 'twp_phone_number', true); if ($call_mode === 'browser') { - $client_name = 'agent_' . $agent->ID . '_' . sanitize_title($agent->display_name); + // Twilio requires alphanumeric characters only + $clean_name = preg_replace('/[^a-zA-Z0-9]/', '', $agent->display_name); + if (empty($clean_name)) { + $clean_name = 'user'; + } + $client_name = 'agent' . $agent->ID . $clean_name; $browser_agents[] = $client_name; } elseif (!empty($agent_phone)) { $cell_agents[] = $agent_phone; diff --git a/includes/class-twp-workflow.php b/includes/class-twp-workflow.php index 9bdca6d..6ee9d95 100644 --- a/includes/class-twp-workflow.php +++ b/includes/class-twp-workflow.php @@ -458,7 +458,12 @@ class TWP_Workflow { )); foreach ($agents as $agent) { - $client_name = 'agent_' . $agent->ID . '_' . sanitize_title($agent->display_name); + // Twilio requires alphanumeric characters only + $clean_name = preg_replace('/[^a-zA-Z0-9]/', '', $agent->display_name); + if (empty($clean_name)) { + $clean_name = 'user'; + } + $client_name = 'agent' . $agent->ID . $clean_name; $client = $dial->addChild('Client', $client_name); } } else {