progress made
This commit is contained in:
@@ -77,7 +77,10 @@ class TWP_Core {
|
||||
// AJAX handlers
|
||||
$this->loader->add_action('wp_ajax_twp_save_schedule', $plugin_admin, 'ajax_save_schedule');
|
||||
$this->loader->add_action('wp_ajax_twp_delete_schedule', $plugin_admin, 'ajax_delete_schedule');
|
||||
$this->loader->add_action('wp_ajax_twp_get_schedules', $plugin_admin, 'ajax_get_schedules');
|
||||
$this->loader->add_action('wp_ajax_twp_get_schedule', $plugin_admin, 'ajax_get_schedule');
|
||||
$this->loader->add_action('wp_ajax_twp_save_workflow', $plugin_admin, 'ajax_save_workflow');
|
||||
$this->loader->add_action('wp_ajax_twp_update_workflow', $plugin_admin, 'ajax_save_workflow');
|
||||
$this->loader->add_action('wp_ajax_twp_get_workflow', $plugin_admin, 'ajax_get_workflow');
|
||||
$this->loader->add_action('wp_ajax_twp_delete_workflow', $plugin_admin, 'ajax_delete_workflow');
|
||||
$this->loader->add_action('wp_ajax_twp_test_call', $plugin_admin, 'ajax_test_call');
|
||||
@@ -106,6 +109,7 @@ class TWP_Core {
|
||||
$this->loader->add_action('wp_ajax_twp_get_voicemail', $plugin_admin, 'ajax_get_voicemail');
|
||||
$this->loader->add_action('wp_ajax_twp_delete_voicemail', $plugin_admin, 'ajax_delete_voicemail');
|
||||
$this->loader->add_action('wp_ajax_twp_transcribe_voicemail', $plugin_admin, 'ajax_transcribe_voicemail');
|
||||
$this->loader->add_action('wp_ajax_twp_get_voicemail_audio', $plugin_admin, 'ajax_get_voicemail_audio');
|
||||
|
||||
// Agent group management AJAX
|
||||
$this->loader->add_action('wp_ajax_twp_get_all_groups', $plugin_admin, 'ajax_get_all_groups');
|
||||
@@ -120,12 +124,17 @@ class TWP_Core {
|
||||
$this->loader->add_action('wp_ajax_twp_accept_call', $plugin_admin, 'ajax_accept_call');
|
||||
$this->loader->add_action('wp_ajax_twp_get_waiting_calls', $plugin_admin, 'ajax_get_waiting_calls');
|
||||
$this->loader->add_action('wp_ajax_twp_set_agent_status', $plugin_admin, 'ajax_set_agent_status');
|
||||
$this->loader->add_action('wp_ajax_twp_get_call_details', $plugin_admin, 'ajax_get_call_details');
|
||||
|
||||
// Callback and outbound call AJAX
|
||||
$this->loader->add_action('wp_ajax_twp_request_callback', $plugin_admin, 'ajax_request_callback');
|
||||
$this->loader->add_action('wp_ajax_twp_initiate_outbound_call', $plugin_admin, 'ajax_initiate_outbound_call');
|
||||
$this->loader->add_action('wp_ajax_twp_initiate_outbound_call_with_from', $plugin_admin, 'ajax_initiate_outbound_call_with_from');
|
||||
$this->loader->add_action('wp_ajax_twp_get_callbacks', $plugin_admin, 'ajax_get_callbacks');
|
||||
|
||||
// Phone number maintenance
|
||||
$this->loader->add_action('wp_ajax_twp_update_phone_status_callbacks', $plugin_admin, 'ajax_update_phone_status_callbacks');
|
||||
$this->loader->add_action('wp_ajax_twp_toggle_number_status_callback', $plugin_admin, 'ajax_toggle_number_status_callback');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,6 +158,9 @@ class TWP_Core {
|
||||
// Callback processing
|
||||
$this->loader->add_action('twp_process_callbacks', 'TWP_Callback_Manager', 'process_callbacks');
|
||||
|
||||
// Call queue cleanup
|
||||
$this->loader->add_action('twp_cleanup_old_calls', $this, 'cleanup_old_calls');
|
||||
|
||||
// Schedule cron events
|
||||
if (!wp_next_scheduled('twp_check_schedules')) {
|
||||
wp_schedule_event(time(), 'twp_every_minute', 'twp_check_schedules');
|
||||
@@ -161,6 +173,10 @@ class TWP_Core {
|
||||
if (!wp_next_scheduled('twp_process_callbacks')) {
|
||||
wp_schedule_event(time(), 'twp_every_minute', 'twp_process_callbacks');
|
||||
}
|
||||
|
||||
if (!wp_next_scheduled('twp_cleanup_old_calls')) {
|
||||
wp_schedule_event(time(), 'hourly', 'twp_cleanup_old_calls');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,6 +215,10 @@ class TWP_Core {
|
||||
* Run the loader
|
||||
*/
|
||||
public function run() {
|
||||
// Initialize webhooks
|
||||
$webhooks = new TWP_Webhooks();
|
||||
$webhooks->register_endpoints();
|
||||
|
||||
// Add custom cron schedules
|
||||
add_filter('cron_schedules', function($schedules) {
|
||||
$schedules['twp_every_minute'] = array(
|
||||
@@ -228,4 +248,83 @@ class TWP_Core {
|
||||
public function get_version() {
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup old stuck calls
|
||||
*/
|
||||
public function cleanup_old_calls() {
|
||||
global $wpdb;
|
||||
$calls_table = $wpdb->prefix . 'twp_queued_calls';
|
||||
|
||||
// Clean up calls that have been in 'answered' status for more than 2 hours
|
||||
// These are likely stuck due to missed webhooks or other issues
|
||||
$updated = $wpdb->query(
|
||||
"UPDATE $calls_table
|
||||
SET status = 'completed', ended_at = NOW()
|
||||
WHERE status = 'answered'
|
||||
AND joined_at < DATE_SUB(NOW(), INTERVAL 2 HOUR)"
|
||||
);
|
||||
|
||||
if ($updated > 0) {
|
||||
error_log("TWP Cleanup: Updated {$updated} stuck calls from 'answered' to 'completed' status");
|
||||
}
|
||||
|
||||
// Backup check for waiting calls (status callbacks should handle most cases)
|
||||
// Only check older calls that might have missed status callbacks
|
||||
$waiting_calls = $wpdb->get_results(
|
||||
"SELECT call_sid FROM $calls_table
|
||||
WHERE status = 'waiting'
|
||||
AND joined_at < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
|
||||
AND joined_at > DATE_SUB(NOW(), INTERVAL 6 HOUR)
|
||||
LIMIT 5"
|
||||
);
|
||||
|
||||
if (!empty($waiting_calls)) {
|
||||
$twilio = new TWP_Twilio_API();
|
||||
$cleaned_count = 0;
|
||||
|
||||
foreach ($waiting_calls as $call) {
|
||||
try {
|
||||
// Check call status with Twilio
|
||||
$call_info = $twilio->get_call_info($call->call_sid);
|
||||
|
||||
if ($call_info && isset($call_info['status'])) {
|
||||
// If call is completed, busy, failed, or canceled, remove from queue
|
||||
if (in_array($call_info['status'], ['completed', 'busy', 'failed', 'canceled'])) {
|
||||
$wpdb->update(
|
||||
$calls_table,
|
||||
array(
|
||||
'status' => 'hangup',
|
||||
'ended_at' => current_time('mysql')
|
||||
),
|
||||
array('call_sid' => $call->call_sid),
|
||||
array('%s', '%s'),
|
||||
array('%s')
|
||||
);
|
||||
$cleaned_count++;
|
||||
error_log("TWP Cleanup: Detected ended call {$call->call_sid} with status {$call_info['status']}");
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("TWP Cleanup: Error checking call {$call->call_sid}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($cleaned_count > 0) {
|
||||
error_log("TWP Cleanup: Cleaned up {$cleaned_count} ended calls from queue");
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up very old waiting calls (older than 24 hours) - these are likely orphaned
|
||||
$updated_waiting = $wpdb->query(
|
||||
"UPDATE $calls_table
|
||||
SET status = 'timeout', ended_at = NOW()
|
||||
WHERE status = 'waiting'
|
||||
AND joined_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)"
|
||||
);
|
||||
|
||||
if ($updated_waiting > 0) {
|
||||
error_log("TWP Cleanup: Updated {$updated_waiting} old waiting calls to 'timeout' status");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user