get_chatbots();
// Get contact forms for the selected chatbot
$selected_chatbot_id = 0;
if ( isset($_GET['chatbot_id'], $_GET['a1ai_select_nonce']) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['a1ai_select_nonce'] ) ), 'a1ai_select_chatbot' ) ) {
$selected_chatbot_id = intval($_GET['chatbot_id']);
}
$contact_forms = array();
if ($selected_chatbot_id > 0) {
// Get contact form configuration for this chatbot
$chatbot = $data_manager->get_chatbot($selected_chatbot_id);
if ($chatbot && isset($chatbot['settings'])) {
// Check if settings is already an array or needs to be unserialized
if (is_array($chatbot['settings'])) {
$settings = $chatbot['settings'];
} else {
$settings = maybe_unserialize($chatbot['settings']);
}
$contact_forms = isset($settings['contact_forms']) ? $settings['contact_forms'] : array();
}
}
// Show success messages from redirects (tab is activated by a1ai-admin-chatbots.js via active_tab param)
if (isset($_GET['form_saved']) && $_GET['form_saved'] === '1') {
echo '
' . esc_html__('Contact form saved successfully!', 'a1ai-chatbot') . '
';
}
if (isset($_GET['form_deleted']) && $_GET['form_deleted'] === '1') {
echo '' . esc_html__('Contact form deleted successfully!', 'a1ai-chatbot') . '
';
}
// Handle form submissions
if ( isset($_SERVER['REQUEST_METHOD']) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
if ( isset($_POST['a1ai_action']) ) {
$a1ai_action = sanitize_key( wp_unslash( $_POST['a1ai_action'] ) );
switch ( $a1ai_action ) {
case 'save_contact_form':
if ( ! current_user_can('manage_options') ) {
echo '' . esc_html__('You do not have permission to perform this action.', 'a1ai-chatbot') . '
';
break;
}
if ( ! isset($_POST['a1ai_contact_form_nonce']) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['a1ai_contact_form_nonce'] ) ), 'a1ai_save_contact_form' ) ) {
echo '' . esc_html__('Security check failed. Please try again.', 'a1ai-chatbot') . '
';
break;
}
if ( isset($_POST['chatbot_id'], $_POST['form_name'], $_POST['form_type']) ) {
$chatbot_id = intval( $_POST['chatbot_id'] );
$form_name = sanitize_text_field( wp_unslash( $_POST['form_name'] ) );
$form_type = sanitize_text_field( wp_unslash( $_POST['form_type'] ) );
$save_submissions = isset($_POST['save_submissions']) ? 1 : 0;
$email_notifications = isset($_POST['email_notifications']) ? 1 : 0;
$notification_email = sanitize_email( wp_unslash( $_POST['notification_email'] ?? '' ) );
// Get form fields
$form_fields = array();
if (isset($_POST['field_name']) && is_array($_POST['field_name'])) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We sanitize after unslashing
$field_names = wp_unslash($_POST['field_name']);
foreach ($field_names as $index => $field_name) {
if (!empty($field_name)) {
$form_fields[] = array(
'name' => sanitize_text_field($field_name),
'label' => sanitize_text_field(wp_unslash($_POST['field_label'][$index] ?? $field_name)),
'type' => sanitize_text_field(wp_unslash($_POST['field_type'][$index] ?? 'text')),
'required' => !empty($_POST['field_required']) && in_array((int) $index, array_map('intval', (array) $_POST['field_required']), true) ? 1 : 0,
'placeholder' => sanitize_text_field(wp_unslash($_POST['field_placeholder'][$index] ?? ''))
);
}
}
}
// Get current chatbot settings
$chatbot = $data_manager->get_chatbot($chatbot_id);
if ($chatbot) {
// Debug logging
if (defined('WP_DEBUG') && WP_DEBUG) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging wrapped in WP_DEBUG check
error_log('A1AI Contact Forms Debug - Form Name: ' . $form_name);
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging wrapped in WP_DEBUG check
error_log('A1AI Contact Forms Debug - Chatbot ID: ' . $chatbot_id);
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging wrapped in WP_DEBUG check
error_log('A1AI Contact Forms Debug - Form Fields Count: ' . count($form_fields));
}
// Check if settings is already an array or needs to be unserialized
if (is_array($chatbot['settings'])) {
$settings = $chatbot['settings'];
} else {
$settings = maybe_unserialize($chatbot['settings']);
}
if (!isset($settings['contact_forms'])) {
$settings['contact_forms'] = array();
}
// Add/update contact form
$form_id = isset($_POST['form_id']) ? intval($_POST['form_id']) : time();
$settings['contact_forms'][$form_id] = array(
'id' => $form_id,
'name' => $form_name,
'type' => $form_type,
'fields' => $form_fields,
'save_submissions' => $save_submissions,
'email_notifications' => $email_notifications,
'notification_email' => $notification_email,
'created_at' => isset($settings['contact_forms'][$form_id]['created_at']) ? $settings['contact_forms'][$form_id]['created_at'] : current_time('mysql'),
'updated_at' => current_time('mysql')
);
// Debug logging before save
if (defined('WP_DEBUG') && WP_DEBUG) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging wrapped in WP_DEBUG check
error_log('A1AI Contact Forms Debug - Form ID: ' . $form_id);
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r -- Debug logging wrapped in WP_DEBUG check
error_log('A1AI Contact Forms Debug - Settings before save: ' . print_r($settings['contact_forms'], true));
}
// Update chatbot settings
$result = $data_manager->save_chatbot($chatbot_id, $chatbot['name'], $settings);
if (defined('WP_DEBUG') && WP_DEBUG) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging wrapped in WP_DEBUG check
error_log('A1AI Contact Forms Debug - Save result: ' . ($result ? 'SUCCESS' : 'FAILED'));
}
if ($result) {
// Force clear cache to ensure form appears immediately
wp_cache_delete('a1ai_chatbot_' . $chatbot_id, 'a1ai');
wp_cache_delete('a1ai_all_chatbots', 'a1ai');
// Redirect to Chatbots list page with Contact Forms tab; use active_tab so tab is correct even if hash is stripped
$redirect_url = admin_url('admin.php');
$params = array(
'page' => 'a1ai-chatbots',
'form_saved' => '1',
'active_tab' => 'contact-forms',
'chatbot_id' => $chatbot_id,
'a1ai_select_nonce' => wp_create_nonce('a1ai_select_chatbot')
);
$redirect_url = add_query_arg($params, $redirect_url) . '#tab-contact-forms';
wp_redirect($redirect_url);
exit;
} else {
echo '' . esc_html__('Failed to save contact form.', 'a1ai-chatbot') . '
';
}
}
}
break;
case 'delete_contact_form':
if ( ! current_user_can('manage_options') ) {
echo '' . esc_html__('You do not have permission to perform this action.', 'a1ai-chatbot') . '
';
break;
}
if ( ! isset($_POST['_wpnonce']) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'a1ai_delete_contact_form' ) ) {
echo '' . esc_html__('Security check failed. Please try again.', 'a1ai-chatbot') . '
';
break;
}
if ( isset($_POST['form_id'], $_POST['chatbot_id']) ) {
$form_id = intval( $_POST['form_id'] );
$chatbot_id = intval( $_POST['chatbot_id'] );
$chatbot = $data_manager->get_chatbot($chatbot_id);
if ($chatbot) {
// Check if settings is already an array or needs to be unserialized
if (is_array($chatbot['settings'])) {
$settings = $chatbot['settings'];
} else {
$settings = maybe_unserialize($chatbot['settings']);
}
if (isset($settings['contact_forms'][$form_id])) {
unset($settings['contact_forms'][$form_id]);
$result = $data_manager->save_chatbot($chatbot_id, $chatbot['name'], $settings);
if ($result) {
// Force clear cache
wp_cache_delete('a1ai_chatbot_' . $chatbot_id, 'a1ai');
wp_cache_delete('a1ai_all_chatbots', 'a1ai');
// Redirect to Chatbots list page with Contact Forms tab
$redirect_url = admin_url('admin.php');
$params = array(
'page' => 'a1ai-chatbots',
'form_deleted' => '1',
'active_tab' => 'contact-forms',
'chatbot_id' => $chatbot_id,
'a1ai_select_nonce' => wp_create_nonce('a1ai_select_chatbot')
);
$redirect_url = add_query_arg($params, $redirect_url) . '#tab-contact-forms';
wp_redirect($redirect_url);
exit;
}
}
}
}
break;
}
}
}
?>