Major enhancement to workflow forwarding that solves voicemail issues and adds agent call control capabilities. Key improvements: - Converted direct forwarding to bridged forwarding to avoid self-call voicemail issues - Added DTMF-based agent features during calls: * *9 - Hold/Unhold customer * *0 - Start/Stop call recording * *5 - Transfer to another extension * *1 - Mute/Unmute (placeholder for future conference mode) - Added webhook handlers for agent features and forward results - Agent features URL attached to Number elements for DTMF detection - Continuous DTMF listening during active calls - Proper call leg detection for hold/resume operations Technical details: - Added /agent-features webhook for DTMF capture - Added /agent-action webhook for processing commands - Added /forward-result webhook for handling dial outcomes - Modified append_twiml_element to preserve Number attributes (url, method) - Enhanced logging throughout for debugging This eliminates the issue where calling yourself would go straight to voicemail and provides professional call control features for agents. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
		
			
				
	
	
		
			142 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * Plugin Name: Twilio WP Plugin
 | 
						|
 * Plugin URI: https://repo.anhonesthost.net/wp-plugins/twilio-wp-plugin
 | 
						|
 * Description: WordPress plugin for Twilio integration with phone scheduling, call forwarding, queue management, and Eleven Labs TTS
 | 
						|
 * Version: 2.8.9
 | 
						|
 * Author: Josh Knapp
 | 
						|
 * License: GPL v2 or later
 | 
						|
 * Text Domain: twilio-wp-plugin
 | 
						|
 */
 | 
						|
 | 
						|
// If this file is called directly, abort.
 | 
						|
if (!defined('WPINC')) {
 | 
						|
    die;
 | 
						|
}
 | 
						|
 | 
						|
// Plugin constants
 | 
						|
define('TWP_VERSION', '2.8.9');
 | 
						|
define('TWP_DB_VERSION', '1.6.2'); // Track database version separately
 | 
						|
define('TWP_PLUGIN_DIR', plugin_dir_path(__FILE__));
 | 
						|
define('TWP_PLUGIN_URL', plugin_dir_url(__FILE__));
 | 
						|
define('TWP_PLUGIN_BASENAME', plugin_basename(__FILE__));
 | 
						|
 | 
						|
/**
 | 
						|
 * Plugin activation hook
 | 
						|
 */
 | 
						|
function twp_activate() {
 | 
						|
    require_once TWP_PLUGIN_DIR . 'includes/class-twp-activator.php';
 | 
						|
    TWP_Activator::activate();
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Check if Twilio SDK is installed and show admin notice if not
 | 
						|
 */
 | 
						|
function twp_check_sdk_installation() {
 | 
						|
    $autoloader_path = TWP_PLUGIN_DIR . 'vendor/autoload.php';
 | 
						|
    $sdk_installed = false;
 | 
						|
    
 | 
						|
    if (file_exists($autoloader_path)) {
 | 
						|
        // Try to load autoloader and check for classes
 | 
						|
        require_once $autoloader_path;
 | 
						|
        $sdk_installed = class_exists('Twilio\Rest\Client');
 | 
						|
    }
 | 
						|
    
 | 
						|
    if (!$sdk_installed) {
 | 
						|
        add_action('admin_notices', 'twp_sdk_missing_notice');
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Display admin notice for missing SDK
 | 
						|
 */
 | 
						|
function twp_sdk_missing_notice() {
 | 
						|
    ?>
 | 
						|
    <div class="notice notice-error is-dismissible">
 | 
						|
        <h3>Twilio WordPress Plugin - SDK Required</h3>
 | 
						|
        <p><strong>The Twilio PHP SDK is required for this plugin to work.</strong></p>
 | 
						|
        <p>To install the SDK, run this command in your plugin directory:</p>
 | 
						|
        <code>chmod +x install-twilio-sdk.sh && ./install-twilio-sdk.sh</code>
 | 
						|
        <p>Or install via Composer: <code>composer install</code></p>
 | 
						|
        <p><em>Plugin path: <?php echo TWP_PLUGIN_DIR; ?></em></p>
 | 
						|
    </div>
 | 
						|
    <?php
 | 
						|
}
 | 
						|
 | 
						|
// Check SDK installation and database updates on admin pages
 | 
						|
if (is_admin()) {
 | 
						|
    add_action('admin_init', 'twp_check_sdk_installation');
 | 
						|
    add_action('admin_init', 'twp_check_database_updates');
 | 
						|
    add_action('admin_init', 'twp_setup_auto_revert_cron');
 | 
						|
}
 | 
						|
 | 
						|
// Hook up cron functions
 | 
						|
add_filter('cron_schedules', 'twp_add_cron_interval');
 | 
						|
add_action('twp_auto_revert_agents', 'twp_handle_auto_revert_agents');
 | 
						|
 | 
						|
/**
 | 
						|
 * Check and perform database updates if needed
 | 
						|
 */
 | 
						|
function twp_check_database_updates() {
 | 
						|
    $current_db_version = get_option('twp_db_version', '1.0.0');
 | 
						|
    
 | 
						|
    if (version_compare($current_db_version, TWP_DB_VERSION, '<')) {
 | 
						|
        require_once TWP_PLUGIN_DIR . 'includes/class-twp-activator.php';
 | 
						|
        TWP_Activator::ensure_tables_exist();
 | 
						|
        update_option('twp_db_version', TWP_DB_VERSION);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Setup auto-revert cron job for agent status
 | 
						|
 */
 | 
						|
function twp_setup_auto_revert_cron() {
 | 
						|
    if (!wp_next_scheduled('twp_auto_revert_agents')) {
 | 
						|
        wp_schedule_event(time(), 'twp_every_minute', 'twp_auto_revert_agents');
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Handle auto-revert cron job
 | 
						|
 */
 | 
						|
function twp_handle_auto_revert_agents() {
 | 
						|
    require_once TWP_PLUGIN_DIR . 'includes/class-twp-agent-manager.php';
 | 
						|
    TWP_Agent_Manager::revert_auto_busy_agents();
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Add custom cron schedule
 | 
						|
 */
 | 
						|
function twp_add_cron_interval($schedules) {
 | 
						|
    $schedules['twp_every_minute'] = array(
 | 
						|
        'interval' => 60, // Every 60 seconds
 | 
						|
        'display'  => esc_html__('Every Minute (TWP)', 'twilio-wp-plugin')
 | 
						|
    );
 | 
						|
    return $schedules;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Plugin deactivation hook
 | 
						|
 */
 | 
						|
function twp_deactivate() {
 | 
						|
    require_once TWP_PLUGIN_DIR . 'includes/class-twp-deactivator.php';
 | 
						|
    TWP_Deactivator::deactivate();
 | 
						|
}
 | 
						|
 | 
						|
register_activation_hook(__FILE__, 'twp_activate');
 | 
						|
register_deactivation_hook(__FILE__, 'twp_deactivate');
 | 
						|
 | 
						|
/**
 | 
						|
 * Core plugin class
 | 
						|
 */
 | 
						|
require plugin_dir_path(__FILE__) . 'includes/class-twp-core.php';
 | 
						|
 | 
						|
/**
 | 
						|
 * Begin execution of the plugin
 | 
						|
 */
 | 
						|
function run_twilio_wp_plugin() {
 | 
						|
    $plugin = new TWP_Core();
 | 
						|
    $plugin->run();
 | 
						|
}
 | 
						|
 | 
						|
run_twilio_wp_plugin();
 |