code revision
This commit is contained in:
281
includes/class-twp-activator.php
Normal file
281
includes/class-twp-activator.php
Normal file
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* Fired during plugin activation
|
||||
*/
|
||||
class TWP_Activator {
|
||||
|
||||
/**
|
||||
* Run activation tasks
|
||||
*/
|
||||
public static function activate() {
|
||||
// Create database tables
|
||||
self::create_tables();
|
||||
|
||||
// Set default options
|
||||
self::set_default_options();
|
||||
|
||||
// Create webhook endpoints
|
||||
flush_rewrite_rules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tables exist and create them if needed
|
||||
*/
|
||||
public static function ensure_tables_exist() {
|
||||
global $wpdb;
|
||||
|
||||
$required_tables = array(
|
||||
'twp_phone_schedules',
|
||||
'twp_call_queues',
|
||||
'twp_queued_calls',
|
||||
'twp_workflows',
|
||||
'twp_call_log',
|
||||
'twp_sms_log',
|
||||
'twp_voicemails',
|
||||
'twp_agent_groups',
|
||||
'twp_group_members',
|
||||
'twp_agent_status',
|
||||
'twp_callbacks'
|
||||
);
|
||||
|
||||
$missing_tables = array();
|
||||
|
||||
foreach ($required_tables as $table) {
|
||||
$table_name = $wpdb->prefix . $table;
|
||||
$table_exists = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_name));
|
||||
|
||||
if (!$table_exists) {
|
||||
$missing_tables[] = $table;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($missing_tables)) {
|
||||
error_log('TWP Plugin: Missing database tables: ' . implode(', ', $missing_tables) . '. Creating them now.');
|
||||
self::create_tables();
|
||||
return false; // Tables were missing
|
||||
}
|
||||
|
||||
return true; // All tables exist
|
||||
}
|
||||
|
||||
/**
|
||||
* Create plugin database tables
|
||||
*/
|
||||
private static function create_tables() {
|
||||
global $wpdb;
|
||||
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
// Phone schedules table
|
||||
$table_schedules = $wpdb->prefix . 'twp_phone_schedules';
|
||||
$sql_schedules = "CREATE TABLE $table_schedules (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
phone_number varchar(20),
|
||||
schedule_name varchar(100) NOT NULL,
|
||||
days_of_week varchar(20) NOT NULL,
|
||||
start_time time NOT NULL,
|
||||
end_time time NOT NULL,
|
||||
workflow_id varchar(100),
|
||||
forward_number varchar(20),
|
||||
after_hours_action varchar(20) DEFAULT 'workflow',
|
||||
after_hours_workflow_id varchar(100),
|
||||
after_hours_forward_number varchar(20),
|
||||
is_active tinyint(1) DEFAULT 1,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY phone_number (phone_number)
|
||||
) $charset_collate;";
|
||||
|
||||
// Call queues table
|
||||
$table_queues = $wpdb->prefix . 'twp_call_queues';
|
||||
$sql_queues = "CREATE TABLE $table_queues (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
queue_name varchar(100) NOT NULL,
|
||||
max_size int(11) DEFAULT 10,
|
||||
wait_music_url varchar(255),
|
||||
tts_message text,
|
||||
timeout_seconds int(11) DEFAULT 300,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
// Queued calls table
|
||||
$table_queued_calls = $wpdb->prefix . 'twp_queued_calls';
|
||||
$sql_queued_calls = "CREATE TABLE $table_queued_calls (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
queue_id int(11) NOT NULL,
|
||||
call_sid varchar(100) NOT NULL,
|
||||
from_number varchar(20) NOT NULL,
|
||||
to_number varchar(20) NOT NULL,
|
||||
position int(11) NOT NULL,
|
||||
status varchar(20) DEFAULT 'waiting',
|
||||
joined_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
answered_at datetime,
|
||||
ended_at datetime,
|
||||
PRIMARY KEY (id),
|
||||
KEY queue_id (queue_id),
|
||||
KEY call_sid (call_sid)
|
||||
) $charset_collate;";
|
||||
|
||||
// Workflows table
|
||||
$table_workflows = $wpdb->prefix . 'twp_workflows';
|
||||
$sql_workflows = "CREATE TABLE $table_workflows (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
workflow_name varchar(100) NOT NULL,
|
||||
phone_number varchar(20) NOT NULL,
|
||||
workflow_data longtext,
|
||||
is_active tinyint(1) DEFAULT 1,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY phone_number (phone_number)
|
||||
) $charset_collate;";
|
||||
|
||||
// Call log table
|
||||
$table_call_log = $wpdb->prefix . 'twp_call_log';
|
||||
$sql_call_log = "CREATE TABLE $table_call_log (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
call_sid varchar(100) NOT NULL,
|
||||
from_number varchar(20),
|
||||
to_number varchar(20),
|
||||
status varchar(20) NOT NULL,
|
||||
duration int(11) DEFAULT 0,
|
||||
workflow_id int(11),
|
||||
workflow_name varchar(100),
|
||||
queue_time int(11) DEFAULT 0,
|
||||
actions_taken text,
|
||||
call_data longtext,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY call_sid (call_sid),
|
||||
KEY from_number (from_number),
|
||||
KEY status (status)
|
||||
) $charset_collate;";
|
||||
|
||||
// SMS log table
|
||||
$table_sms_log = $wpdb->prefix . 'twp_sms_log';
|
||||
$sql_sms_log = "CREATE TABLE $table_sms_log (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
message_sid varchar(100) NOT NULL,
|
||||
from_number varchar(20) NOT NULL,
|
||||
to_number varchar(20) NOT NULL,
|
||||
body text,
|
||||
received_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY message_sid (message_sid)
|
||||
) $charset_collate;";
|
||||
|
||||
// Voicemails table
|
||||
$table_voicemails = $wpdb->prefix . 'twp_voicemails';
|
||||
$sql_voicemails = "CREATE TABLE $table_voicemails (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
workflow_id int(11),
|
||||
from_number varchar(20) NOT NULL,
|
||||
recording_url varchar(255),
|
||||
duration int(11) DEFAULT 0,
|
||||
transcription text,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY workflow_id (workflow_id)
|
||||
) $charset_collate;";
|
||||
|
||||
// Agent groups table
|
||||
$table_agent_groups = $wpdb->prefix . 'twp_agent_groups';
|
||||
$sql_agent_groups = "CREATE TABLE $table_agent_groups (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
group_name varchar(100) NOT NULL,
|
||||
description text,
|
||||
ring_strategy varchar(20) DEFAULT 'simultaneous',
|
||||
timeout_seconds int(11) DEFAULT 30,
|
||||
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY group_name (group_name)
|
||||
) $charset_collate;";
|
||||
|
||||
// Group members table
|
||||
$table_group_members = $wpdb->prefix . 'twp_group_members';
|
||||
$sql_group_members = "CREATE TABLE $table_group_members (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
group_id int(11) NOT NULL,
|
||||
user_id bigint(20) NOT NULL,
|
||||
priority int(11) DEFAULT 0,
|
||||
is_active tinyint(1) DEFAULT 1,
|
||||
added_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (id),
|
||||
KEY group_id (group_id),
|
||||
KEY user_id (user_id),
|
||||
UNIQUE KEY group_user (group_id, user_id)
|
||||
) $charset_collate;";
|
||||
|
||||
// Agent status table
|
||||
$table_agent_status = $wpdb->prefix . 'twp_agent_status';
|
||||
$sql_agent_status = "CREATE TABLE $table_agent_status (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id bigint(20) NOT NULL,
|
||||
status varchar(20) DEFAULT 'offline',
|
||||
current_call_sid varchar(100),
|
||||
last_activity datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
available_for_queues tinyint(1) DEFAULT 1,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY user_id (user_id)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
dbDelta($sql_schedules);
|
||||
dbDelta($sql_queues);
|
||||
dbDelta($sql_queued_calls);
|
||||
dbDelta($sql_workflows);
|
||||
dbDelta($sql_call_log);
|
||||
dbDelta($sql_sms_log);
|
||||
dbDelta($sql_voicemails);
|
||||
// Callbacks table
|
||||
$table_callbacks = $wpdb->prefix . 'twp_callbacks';
|
||||
$sql_callbacks = "CREATE TABLE $table_callbacks (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
phone_number varchar(20) NOT NULL,
|
||||
requested_at datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
status varchar(20) DEFAULT 'pending',
|
||||
attempts int(11) DEFAULT 0,
|
||||
last_attempt datetime,
|
||||
completed_at datetime,
|
||||
queue_id int(11),
|
||||
original_call_sid varchar(100),
|
||||
callback_call_sid varchar(100),
|
||||
notes text,
|
||||
PRIMARY KEY (id),
|
||||
KEY phone_number (phone_number),
|
||||
KEY status (status),
|
||||
KEY queue_id (queue_id)
|
||||
) $charset_collate;";
|
||||
|
||||
dbDelta($sql_schedules);
|
||||
dbDelta($sql_queues);
|
||||
dbDelta($sql_queued_calls);
|
||||
dbDelta($sql_workflows);
|
||||
dbDelta($sql_call_log);
|
||||
dbDelta($sql_sms_log);
|
||||
dbDelta($sql_voicemails);
|
||||
dbDelta($sql_agent_groups);
|
||||
dbDelta($sql_group_members);
|
||||
dbDelta($sql_agent_status);
|
||||
dbDelta($sql_callbacks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default plugin options
|
||||
*/
|
||||
private static function set_default_options() {
|
||||
add_option('twp_twilio_account_sid', '');
|
||||
add_option('twp_twilio_auth_token', '');
|
||||
add_option('twp_elevenlabs_api_key', '');
|
||||
add_option('twp_elevenlabs_voice_id', '');
|
||||
add_option('twp_elevenlabs_model_id', 'eleven_multilingual_v2');
|
||||
add_option('twp_default_queue_timeout', 300);
|
||||
add_option('twp_default_queue_size', 10);
|
||||
add_option('twp_urgent_keywords', 'urgent,emergency,important,asap,help');
|
||||
add_option('twp_sms_notification_number', '');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user