A1AI Chatbot Title Test';
// Check if chatbots table exists
$chatbots_table = $wpdb->prefix . 'a1ai_chatbots';
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// Direct database queries are necessary for diagnostic testing
$table_exists = $wpdb->get_var(
$wpdb->prepare('SHOW TABLES LIKE %s', $chatbots_table)
) === $chatbots_table;
// phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery
echo '
Chatbots Table Exists: ' . esc_html($table_exists ? 'Yes' : 'No') . '
';
if ($table_exists) {
// Get all chatbots with their settings
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// Direct database queries are necessary for diagnostic testing
$chatbots = $wpdb->get_results(
'SELECT id, name, settings FROM ' . esc_sql($chatbots_table) . ' ORDER BY id',
ARRAY_A
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery
echo 'Chatbots and Their Settings
';
if (!empty($chatbots)) {
echo '';
echo '| ID | Name | Settings (Raw) | Chatbot Title | Chat Bubble Text |
';
foreach ($chatbots as $chatbot) {
$settings = maybe_unserialize($chatbot['settings']);
$chatbot_title = isset($settings['chatbot_title']) ? $settings['chatbot_title'] : 'Not Set';
$chat_bubble_text = isset($settings['chat_bubble_text']) ? $settings['chat_bubble_text'] : 'Not Set';
echo '';
echo '| ' . esc_html($chatbot['id']) . ' | ';
echo '' . esc_html($chatbot['name']) . ' | ';
echo '' . esc_html(substr($chatbot['settings'], 0, 200)) . (strlen($chatbot['settings']) > 200 ? '...' : '') . ' | ';
echo '' . esc_html($chatbot_title) . ' | ';
echo '' . esc_html($chat_bubble_text) . ' | ';
echo '
';
}
echo '
';
} else {
echo 'No chatbots found in the database.
';
}
}
// Test form to manually set a chatbot title
echo 'Test Chatbot Title Setting
';
echo '';
// Handle form submission with proper security checks
if (
isset($_POST['test_set_title']) &&
isset($_POST['chatbot_id']) &&
isset($_POST['chatbot_title']) &&
isset($_POST['a1ai_test_nonce']) &&
wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['a1ai_test_nonce'])), 'a1ai_test_chatbot_title')
) {
$test_chatbot_id = intval(wp_unslash($_POST['chatbot_id']));
$test_chatbot_title = sanitize_text_field(wp_unslash($_POST['chatbot_title']));
// Get current settings with caching
$cache_key = 'a1ai_chatbot_settings_' . $test_chatbot_id;
$current_settings = wp_cache_get($cache_key);
if (false === $current_settings) {
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// Direct database queries are necessary for diagnostic testing
$current_settings = $wpdb->get_var(
$wpdb->prepare(
'SELECT settings FROM ' . esc_sql($chatbots_table) . ' WHERE id = %d',
$test_chatbot_id
)
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery
// Cache the result for 5 minutes
wp_cache_set($cache_key, $current_settings, '', 300);
}
if ($current_settings) {
$settings = maybe_unserialize($current_settings);
if (!is_array($settings)) {
$settings = array();
}
// Update the chatbot title
$settings['chatbot_title'] = $test_chatbot_title;
$serialized_settings = maybe_serialize($settings);
// Save back to database
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// Direct database query is necessary for diagnostic testing
// Cache is managed manually with wp_cache_delete() after update
$result = $wpdb->update(
$chatbots_table,
array('settings' => $serialized_settings),
array('id' => $test_chatbot_id),
array('%s'),
array('%d')
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery
if ($result !== false) {
// Clear the cache after successful update
wp_cache_delete($cache_key);
echo '';
echo 'Success! Chatbot title updated for ID ' . esc_html($test_chatbot_id) . ' to "' . esc_html($test_chatbot_title) . '"';
echo '
';
} else {
echo '';
echo 'Error! Failed to update chatbot title.';
echo '
';
}
} else {
echo '';
echo 'Error! Chatbot with ID ' . esc_html($test_chatbot_id) . ' not found.';
echo '
';
}
} elseif (isset($_POST['test_set_title'])) {
// Form was submitted but nonce verification failed
echo '';
echo 'Security Error! Invalid nonce. Please try again.';
echo '
';
}
echo '
';
echo 'Back to A1AI Admin
';
?>