Files
twilio-wp-plugin/twilio-wp-plugin.php
jknapp 3e4dff5c4e
All checks were successful
Create Release / build (push) Successful in 4s
Add SDK persistence and configurable edge location
- Add external SDK installation (wp-content/twilio-sdk/) that survives
  WordPress plugin updates
- Add install-twilio-sdk-external.sh script for external SDK setup
- Update SDK loading to check external location first, internal fallback
- Add post-update detection hook to warn if SDK was deleted
- Add configurable Twilio Edge Location setting (default: roaming)
- Fix US calls failing due to hardcoded Sydney edge location

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 18:17:25 -08:00

202 lines
6.7 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: {auto_update_value_on_deploy}
* 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', '{auto_update_value_on_deploy}');
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__));
// External SDK location - survives plugin updates (wp-content/twilio-sdk/)
define('TWP_EXTERNAL_SDK_DIR', dirname(dirname(TWP_PLUGIN_DIR)) . '/twilio-sdk/');
/**
* 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
* Checks external location first (survives plugin updates), then internal fallback
*/
function twp_check_sdk_installation() {
$sdk_installed = false;
// Priority 1: Check external SDK location (survives plugin updates)
$external_autoloader = TWP_EXTERNAL_SDK_DIR . 'autoload.php';
if (file_exists($external_autoloader)) {
require_once $external_autoloader;
$sdk_installed = class_exists('Twilio\Rest\Client');
}
// Priority 2: Fall back to internal vendor directory
if (!$sdk_installed) {
$internal_autoloader = TWP_PLUGIN_DIR . 'vendor/autoload.php';
if (file_exists($internal_autoloader)) {
require_once $internal_autoloader;
$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><strong>Recommended:</strong> Install SDK to external location (survives plugin updates):</p>
<code>chmod +x install-twilio-sdk-external.sh && ./install-twilio-sdk-external.sh</code>
<p style="margin-top: 10px;"><strong>Alternative:</strong> Install SDK inside plugin folder:</p>
<code>chmod +x install-twilio-sdk.sh && ./install-twilio-sdk.sh</code>
<p style="margin-top: 10px;"><em>Plugin path: <?php echo esc_html(TWP_PLUGIN_DIR); ?></em></p>
<p><em>External SDK path: <?php echo esc_html(TWP_EXTERNAL_SDK_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');
/**
* Check SDK status after plugin updates
* Shows warning if SDK was deleted during update and external SDK not available
*/
function twp_check_sdk_after_update($upgrader_object, $options) {
// Only run for plugin updates
if ($options['action'] !== 'update' || $options['type'] !== 'plugin') {
return;
}
// Check if this plugin was updated
$updated_plugins = isset($options['plugins']) ? $options['plugins'] : array();
if (!in_array(TWP_PLUGIN_BASENAME, $updated_plugins)) {
return;
}
// Check if SDK is available
$external_sdk = file_exists(TWP_EXTERNAL_SDK_DIR . 'autoload.php');
$internal_sdk = file_exists(TWP_PLUGIN_DIR . 'vendor/autoload.php');
if (!$external_sdk && !$internal_sdk) {
// Set a transient to show warning on next admin page load
set_transient('twp_sdk_update_warning', true, 60 * 5);
}
}
add_action('upgrader_process_complete', 'twp_check_sdk_after_update', 10, 2);
/**
* Show SDK update warning
*/
function twp_show_sdk_update_warning() {
if (get_transient('twp_sdk_update_warning')) {
delete_transient('twp_sdk_update_warning');
?>
<div class="notice notice-warning is-dismissible">
<h3>Twilio WordPress Plugin - SDK Reinstall Required</h3>
<p><strong>The plugin was updated and the Twilio SDK needs to be reinstalled.</strong></p>
<p>To prevent this in the future, install the SDK to the external location:</p>
<code>cd <?php echo esc_html(TWP_PLUGIN_DIR); ?> && ./install-twilio-sdk-external.sh</code>
<p style="margin-top: 10px;">The external SDK at <code><?php echo esc_html(TWP_EXTERNAL_SDK_DIR); ?></code> survives plugin updates.</p>
</div>
<?php
}
}
add_action('admin_notices', 'twp_show_sdk_update_warning');
/**
* 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();