progress made

This commit is contained in:
2025-08-11 20:31:48 -07:00
parent 805af2f199
commit 304b5de40b
15 changed files with 4028 additions and 404 deletions

View File

@@ -11,8 +11,12 @@ class TWP_Scheduler {
global $wpdb;
$table_name = $wpdb->prefix . 'twp_phone_schedules';
$current_time = current_time('H:i:s');
$current_day = strtolower(date('l'));
// Use WordPress timezone
$wp_timezone = wp_timezone();
$current_datetime = new DateTime('now', $wp_timezone);
$current_time = $current_datetime->format('H:i:s');
$current_day = strtolower($current_datetime->format('l'));
$schedules = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name
@@ -43,7 +47,7 @@ class TWP_Scheduler {
foreach ($numbers['data']['incoming_phone_numbers'] as $number) {
if ($number['phone_number'] == $schedule->phone_number) {
// Configure webhook based on schedule
$webhook_url = home_url('/twilio-webhook/voice');
$webhook_url = home_url('/wp-json/twilio-webhook/v1/voice');
$webhook_url = add_query_arg('schedule_id', $schedule->id, $webhook_url);
$twilio->configure_phone_number(
@@ -64,16 +68,39 @@ class TWP_Scheduler {
global $wpdb;
$table_name = $wpdb->prefix . 'twp_phone_schedules';
// Debug logging - ensure tables exist and columns are correct
TWP_Activator::ensure_tables_exist();
// Force column update check
global $wpdb;
$table_schedules = $wpdb->prefix . 'twp_phone_schedules';
$column_info = $wpdb->get_results("SHOW COLUMNS FROM $table_schedules LIKE 'days_of_week'");
if (!empty($column_info)) {
error_log('TWP Create Schedule: Current days_of_week column type: ' . $column_info[0]->Type);
if ($column_info[0]->Type === 'varchar(20)') {
error_log('TWP Create Schedule: Expanding days_of_week column to varchar(100)');
$wpdb->query("ALTER TABLE $table_schedules MODIFY COLUMN days_of_week varchar(100) NOT NULL");
}
}
error_log('TWP Create Schedule: Input data: ' . print_r($data, true));
error_log('TWP Create Schedule: Table name: ' . $table_name);
$insert_data = array(
'schedule_name' => sanitize_text_field($data['schedule_name']),
'days_of_week' => sanitize_text_field($data['days_of_week']),
'start_time' => sanitize_text_field($data['start_time']),
'end_time' => sanitize_text_field($data['end_time']),
'workflow_id' => sanitize_text_field($data['workflow_id']),
'is_active' => isset($data['is_active']) ? 1 : 0
);
$format = array('%s', '%s', '%s', '%s', '%s', '%d');
$format = array('%s', '%s', '%s', '%s', '%d');
// Add workflow_id if provided
if ($data['workflow_id']) {
$insert_data['workflow_id'] = sanitize_text_field($data['workflow_id']);
$format[] = '%s';
}
// Add optional fields if provided
if (!empty($data['phone_number'])) {
@@ -101,8 +128,24 @@ class TWP_Scheduler {
$format[] = '%s';
}
if (isset($data['holiday_dates'])) {
$insert_data['holiday_dates'] = sanitize_textarea_field($data['holiday_dates']);
$format[] = '%s';
}
error_log('TWP Create Schedule: Insert data: ' . print_r($insert_data, true));
error_log('TWP Create Schedule: Insert format: ' . print_r($format, true));
$result = $wpdb->insert($table_name, $insert_data, $format);
error_log('TWP Create Schedule: Insert result: ' . ($result ? 'true' : 'false'));
if ($result === false) {
error_log('TWP Create Schedule: Database error: ' . $wpdb->last_error);
} else {
$new_id = $wpdb->insert_id;
error_log('TWP Create Schedule: New schedule ID: ' . $new_id);
}
return $result !== false;
}
@@ -166,6 +209,11 @@ class TWP_Scheduler {
$update_format[] = '%s';
}
if (isset($data['holiday_dates'])) {
$update_data['holiday_dates'] = sanitize_textarea_field($data['holiday_dates']);
$update_format[] = '%s';
}
if (isset($data['is_active'])) {
$update_data['is_active'] = $data['is_active'] ? 1 : 0;
$update_format[] = '%d';
@@ -233,19 +281,58 @@ class TWP_Scheduler {
$schedule = self::get_schedule($schedule_id);
if (!$schedule || !$schedule->is_active) {
error_log('TWP Schedule: Schedule not found or inactive - ID: ' . $schedule_id);
return false;
}
$current_time = current_time('H:i:s');
$current_day = strtolower(date('l'));
// Use WordPress timezone for all date/time operations
$wp_timezone = wp_timezone();
$current_datetime = new DateTime('now', $wp_timezone);
$current_time = $current_datetime->format('H:i:s');
$current_day = strtolower($current_datetime->format('l')); // Monday, Tuesday, etc.
$current_date = $current_datetime->format('Y-m-d');
error_log('TWP Schedule: Checking schedule "' . $schedule->schedule_name . '" - Current time: ' . $current_time . ', Current day: ' . $current_day . ', WP Timezone: ' . $wp_timezone->getName());
error_log('TWP Schedule: Schedule days: ' . $schedule->days_of_week . ', Schedule time: ' . $schedule->start_time . ' - ' . $schedule->end_time);
// Check if today is a holiday
if (self::is_holiday($schedule, $current_date)) {
error_log('TWP Schedule: Today is a holiday - treating as after-hours');
return false; // Treat holidays as after-hours
}
// Check if current day is in schedule
if (strpos($schedule->days_of_week, $current_day) === false) {
error_log('TWP Schedule: Current day (' . $current_day . ') not in schedule days (' . $schedule->days_of_week . ')');
return false;
}
// Check if current time is within schedule
return $current_time >= $schedule->start_time && $current_time <= $schedule->end_time;
$is_within_hours = $current_time >= $schedule->start_time && $current_time <= $schedule->end_time;
error_log('TWP Schedule: Time check - Current: ' . $current_time . ', Start: ' . $schedule->start_time . ', End: ' . $schedule->end_time . ', Within hours: ' . ($is_within_hours ? 'YES' : 'NO'));
return $is_within_hours;
}
/**
* Check if today is a holiday for this schedule
*/
private static function is_holiday($schedule, $current_date = null) {
if (empty($schedule->holiday_dates)) {
return false;
}
if ($current_date === null) {
// Use WordPress timezone
$wp_timezone = wp_timezone();
$current_datetime = new DateTime('now', $wp_timezone);
$current_date = $current_datetime->format('Y-m-d');
}
$holidays = array_map('trim', explode(',', $schedule->holiday_dates));
return in_array($current_date, $holidays);
}
/**