Implement multiple phone numbers per workflow feature

Database Changes:
- Added twp_workflow_phones junction table for many-to-many relationship
- Updated activator to create and manage new table
- Maintained backward compatibility with existing phone_number field

Workflow Management:
- Added set_workflow_phone_numbers() and get_workflow_phone_numbers() methods
- Updated get_workflow_by_phone_number() to check both old and new structures
- Enhanced save/update handlers to support phone_numbers array
- Added AJAX endpoint for retrieving workflow phone numbers

User Interface:
- Replaced single phone number select with dynamic multi-select interface
- Added "Add Number" and "Remove" buttons for managing multiple numbers
- Updated workflow listing to display all assigned phone numbers
- Enhanced JavaScript for phone number management and validation

The webhook routing already supports multiple numbers since get_workflow_by_phone_number()
was updated to check both structures. This feature allows users to assign multiple
Twilio phone numbers to trigger the same workflow.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-13 10:35:21 -07:00
parent 30b0cd355f
commit aa58b45501
5 changed files with 254 additions and 19 deletions

View File

@@ -37,6 +37,7 @@ class TWP_Activator {
'twp_call_queues',
'twp_queued_calls',
'twp_workflows',
'twp_workflow_phones',
'twp_call_log',
'twp_sms_log',
'twp_voicemails',
@@ -137,7 +138,7 @@ class TWP_Activator {
KEY status (status)
) $charset_collate;";
// Workflows table
// Workflows table (keeping phone_number for backward compatibility, will be deprecated)
$table_workflows = $wpdb->prefix . 'twp_workflows';
$sql_workflows = "CREATE TABLE $table_workflows (
id int(11) NOT NULL AUTO_INCREMENT,
@@ -151,6 +152,19 @@ class TWP_Activator {
KEY phone_number (phone_number)
) $charset_collate;";
// Workflow phone numbers junction table
$table_workflow_phones = $wpdb->prefix . 'twp_workflow_phones';
$sql_workflow_phones = "CREATE TABLE $table_workflow_phones (
id int(11) NOT NULL AUTO_INCREMENT,
workflow_id int(11) NOT NULL,
phone_number varchar(20) NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY workflow_phone (workflow_id, phone_number),
KEY workflow_id (workflow_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 (
@@ -274,6 +288,7 @@ class TWP_Activator {
dbDelta($sql_queues);
dbDelta($sql_queued_calls);
dbDelta($sql_workflows);
dbDelta($sql_workflow_phones);
dbDelta($sql_call_log);
dbDelta($sql_sms_log);
dbDelta($sql_voicemails);

View File

@@ -127,6 +127,7 @@ class TWP_Core {
$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_get_workflow_phone_numbers', $plugin_admin, 'ajax_get_workflow_phone_numbers');
$this->loader->add_action('wp_ajax_twp_delete_workflow', $plugin_admin, 'ajax_delete_workflow');
// Phone number management AJAX

View File

@@ -1084,15 +1084,31 @@ class TWP_Workflow {
/**
* Get workflow by phone number
* Now checks both the legacy phone_number field and the new junction table
*/
public static function get_workflow_by_phone_number($phone_number) {
global $wpdb;
$table_name = $wpdb->prefix . 'twp_workflows';
$workflows_table = $wpdb->prefix . 'twp_workflows';
$phones_table = $wpdb->prefix . 'twp_workflow_phones';
return $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $table_name WHERE phone_number = %s AND is_active = 1 ORDER BY created_at DESC LIMIT 1",
// First check the new junction table
$workflow = $wpdb->get_row($wpdb->prepare(
"SELECT w.* FROM $workflows_table w
INNER JOIN $phones_table p ON w.id = p.workflow_id
WHERE p.phone_number = %s AND w.is_active = 1
ORDER BY w.created_at DESC LIMIT 1",
$phone_number
));
// If not found, fall back to legacy phone_number field
if (!$workflow) {
$workflow = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $workflows_table WHERE phone_number = %s AND is_active = 1 ORDER BY created_at DESC LIMIT 1",
$phone_number
));
}
return $workflow;
}
/**
@@ -1145,11 +1161,71 @@ class TWP_Workflow {
public static function delete_workflow($workflow_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'twp_workflows';
$phones_table = $wpdb->prefix . 'twp_workflow_phones';
// Delete phone numbers first
$wpdb->delete($phones_table, array('workflow_id' => $workflow_id), array('%d'));
// Then delete workflow
return $wpdb->delete(
$table_name,
array('id' => $workflow_id),
array('%d')
);
}
/**
* Set phone numbers for a workflow
*/
public static function set_workflow_phone_numbers($workflow_id, $phone_numbers) {
global $wpdb;
$phones_table = $wpdb->prefix . 'twp_workflow_phones';
// Remove existing phone numbers for this workflow
$wpdb->delete($phones_table, array('workflow_id' => $workflow_id));
// Add new phone numbers
foreach ($phone_numbers as $phone_number) {
if (!empty($phone_number)) {
$wpdb->insert(
$phones_table,
array(
'workflow_id' => $workflow_id,
'phone_number' => $phone_number
)
);
}
}
return true;
}
/**
* Get phone numbers for a workflow
*/
public static function get_workflow_phone_numbers($workflow_id) {
global $wpdb;
$phones_table = $wpdb->prefix . 'twp_workflow_phones';
$numbers = $wpdb->get_col($wpdb->prepare(
"SELECT phone_number FROM $phones_table WHERE workflow_id = %d",
$workflow_id
));
// If no numbers in junction table, check legacy field
if (empty($numbers)) {
$workflows_table = $wpdb->prefix . 'twp_workflows';
$legacy_number = $wpdb->get_var($wpdb->prepare(
"SELECT phone_number FROM $workflows_table WHERE id = %d",
$workflow_id
));
if ($legacy_number) {
$numbers = array($legacy_number);
// Optionally migrate to new structure
self::set_workflow_phone_numbers($workflow_id, $numbers);
}
}
return $numbers;
}
}