diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index 2c23fbc..173fd2d 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -6771,7 +6771,25 @@ class TWP_Admin { } function adminStartRecording() { - var callSid = currentCall.parameters.CallSid || currentCall.customParameters.CallSid; + if (!currentCall) { + showNotice('No active call to record', 'error'); + return; + } + + // Try multiple ways to get the call SID for browser phone calls + var callSid = currentCall.parameters.CallSid || + currentCall.customParameters.CallSid || + currentCall.outgoingConnectionId || + currentCall.sid; + + console.log('Current call object:', currentCall); + console.log('Attempting to record call SID:', callSid); + + if (!callSid) { + showNotice('Could not determine call SID for recording', 'error'); + return; + } + var $recordBtn = $('#admin-record-btn'); $.post(ajaxurl, { @@ -6787,8 +6805,9 @@ class TWP_Admin { } else { showNotice('Failed to start recording: ' + (response.data || 'Unknown error'), 'error'); } - }).fail(function() { - showNotice('Failed to start recording', 'error'); + }).fail(function(xhr, status, error) { + console.error('Recording start failed:', xhr.responseText); + showNotice('Failed to start recording: ' + error, 'error'); }); } @@ -7034,10 +7053,32 @@ class TWP_Admin { $call_sid = sanitize_text_field($_POST['call_sid']); $user_id = get_current_user_id(); + if (empty($call_sid)) { + wp_send_json_error('Call SID is required for recording'); + return; + } + + error_log("TWP: Starting recording for call SID: $call_sid"); + try { $twilio = new TWP_Twilio_API(); $client = $twilio->get_client(); + // First, verify the call exists and is in progress + try { + $call = $client->calls($call_sid)->fetch(); + error_log("TWP: Call found - Status: {$call->status}, From: {$call->from}, To: {$call->to}"); + + if (!in_array($call->status, ['in-progress', 'ringing'])) { + wp_send_json_error("Cannot record call in status: {$call->status}. Call must be in-progress."); + return; + } + } catch (Exception $call_error) { + error_log("TWP: Error fetching call details: " . $call_error->getMessage()); + wp_send_json_error("Call not found or not accessible: " . $call_error->getMessage()); + return; + } + // Start recording the call $recording = $client->calls($call_sid)->recordings->create([ 'recordingStatusCallback' => home_url('/wp-json/twilio-webhook/v1/recording-status'), @@ -7045,14 +7086,13 @@ class TWP_Admin { 'recordingChannels' => 'dual' ]); + error_log("TWP: Recording created with SID: {$recording->sid}"); + // Store recording info in database global $wpdb; $recordings_table = $wpdb->prefix . 'twp_call_recordings'; - // Get call details - $call = $client->calls($call_sid)->fetch(); - - $wpdb->insert($recordings_table, [ + $insert_result = $wpdb->insert($recordings_table, [ 'call_sid' => $call_sid, 'recording_sid' => $recording->sid, 'from_number' => $call->from, @@ -7062,11 +7102,16 @@ class TWP_Admin { 'started_at' => current_time('mysql') ]); + if ($insert_result === false) { + error_log("TWP: Database insert failed: " . $wpdb->last_error); + } + wp_send_json_success([ 'message' => 'Recording started', 'recording_sid' => $recording->sid ]); } catch (Exception $e) { + error_log("TWP: Recording start error: " . $e->getMessage()); wp_send_json_error('Failed to start recording: ' . $e->getMessage()); } } diff --git a/assets/js/browser-phone-frontend.js b/assets/js/browser-phone-frontend.js index 187ef6d..19b40fd 100644 --- a/assets/js/browser-phone-frontend.js +++ b/assets/js/browser-phone-frontend.js @@ -1668,6 +1668,25 @@ * Start recording the current call */ function startRecording() { + if (!currentCall || currentCall.status() !== 'open') { + showMessage('No active call to record', 'error'); + return; + } + + // Try multiple ways to get the call SID + const callSid = currentCall.parameters.CallSid || + currentCall.customParameters.CallSid || + currentCall.outgoingConnectionId || + currentCall.sid; + + console.log('Frontend currentCall object:', currentCall); + console.log('Frontend attempting to record call SID:', callSid); + + if (!callSid) { + showMessage('Could not determine call SID for recording', 'error'); + return; + } + const $recordBtn = $('#twp-record-btn'); $.ajax({ @@ -1675,7 +1694,7 @@ method: 'POST', data: { action: 'twp_start_recording', - call_sid: currentCall.parameters.CallSid, + call_sid: callSid, nonce: twp_frontend_ajax.nonce }, success: function(response) { @@ -1688,8 +1707,9 @@ showMessage('Failed to start recording: ' + (response.data || 'Unknown error'), 'error'); } }, - error: function() { - showMessage('Failed to start recording', 'error'); + error: function(xhr, status, error) { + console.error('Frontend recording start failed:', xhr.responseText); + showMessage('Failed to start recording: ' + error, 'error'); } }); }