esc_html__('Select or Upload Icon', 'ai24-assistant-integrator'),
'useIconText' => esc_html__('Use this icon', 'ai24-assistant-integrator')
);
wp_localize_script('AI24AI-image-uploader-toggle', 'ai24aiToggleImage', $localize_data);
}
// Conditionally enqueue the toggle-sidebar.js script for the 'styling' tab
if (isset($_GET['tab']) && 'styling' === $_GET['tab']) {
wp_enqueue_script(
'AI24AI-toggle-sidebar',
AI24AI_PLUGIN_URL . 'assets/js/toggle-sidebar.js',
array('jquery'),
false,
true
);
}
// Conditionally enqueue the sliders.css file
if (isset($_GET['tab']) && $_GET['tab'] === 'styling') {
wp_enqueue_style(
'AI24AI-sliders-css',
AI24AI_PLUGIN_URL . 'assets/css/sliders.css',
array(),
false
);
}
// Conditionally enqueue the extra-admin.js file
if (isset($_GET['tab']) && $_GET['tab'] === 'styling') {
wp_enqueue_script(
'AI24AI-extra-admin-js',
AI24AI_PLUGIN_URL . 'assets/js/extra-admin.js',
array('jquery'),
false,
true
);
}
wp_enqueue_style(
'AI24AI-admin-css',
AI24AI_PLUGIN_URL . 'assets/css/admin.css',
array(),
filemtime(plugin_dir_path(__FILE__) . 'assets/css/admin.css')
);
if (isset($_GET['tab']) && $_GET['tab'] === 'main_settings') {
wp_enqueue_script(
'ai24ai-generate-functions',
AI24AI_PLUGIN_URL . 'assets/js/generate-functions.js',
array('jquery'),
filemtime(plugin_dir_path(__FILE__) . 'assets/js/generate-functions.js'),
true
);
wp_enqueue_script(
'ai24ai-regenerate-functions',
AI24AI_PLUGIN_URL . 'assets/js/regenerate-functions.js',
array('jquery'),
filemtime(plugin_dir_path(__FILE__) . 'assets/js/regenerate-functions.js'),
true
);
}
}
add_action('admin_enqueue_scripts', 'AI24AI_enqueue_admin_scripts');
add_action('admin_menu', 'AI24AI_add_admin_menu');
add_action('admin_init', 'AI24AI_register_settings');
function AI24AI_add_admin_menu() {
$icon_path = __DIR__ . '/../assets/images/spy-agent.svg';
$icon_data = file_get_contents($icon_path);
if ($icon_data === false) {
$icon_base64 = '';
} else {
$icon_base64 = base64_encode($icon_data);
}
add_menu_page(
'AI24 Assistant Integrator Settings', // Page title
'AI24 AI', // Menu title
'manage_options', // Capability
'AI24-assistant-integrator', // Menu slug
'AI24AI_settings_page', // Callback function
'data:image/svg+xml;base64,' . $icon_base64 // Icon URL
);
}
function AI24AI_settings_page() {
$default_tab = 'main_settings';
// Sanitize and validate nonce for tab switching
$tab_nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '';
$active_tab = (isset($_GET['tab']) && wp_verify_nonce($tab_nonce, 'ai24ai_tab_change')) ? sanitize_key($_GET['tab']) : $default_tab;
// Prepare base URL for the settings page
$base_url = menu_page_url('AI24-assistant-integrator', false);
// Tabs configuration
$tabs = [
'main_settings' => ['label' => 'API settings', 'nonce_action' => 'ai24ai_tab_change'],
'styling' => ['label' => 'Widget Styling', 'nonce_action' => 'ai24ai_tab_change'],
'assistant_styling' => ['label' => 'Assistant Styling', 'nonce_action' => 'ai24ai_tab_change'],
'tutorials' => ['label' => 'Tutorials', 'nonce_action' => 'ai24ai_tab_change'],
];
?>
AI24 Assistant Integrator Settings
$tab_info):
$tab_url = esc_url(add_query_arg(['tab' => $tab_id, '_wpnonce' => wp_create_nonce($tab_info['nonce_action'])], $base_url));
?>
'AI24AI_functions_generate_button', 'class' => 'AI24AI-class', 'description' => 'Click to generate the custom functions file.'));
add_settings_field('AI24AI_functions_regenerate_button', 'Regenerate Functions File', 'AI24AI_regenerate_button_callback', 'AI24AI-main-settings', 'AI24AI_function_settings', array('label_for' => 'AI24AI_functions_regenerate_button', 'class' => 'AI24AI-class', 'description' => 'Click to regenerate the custom functions file.'));
add_settings_field('AI24AI_functions_edit_button', 'Edit Functions File', 'AI24AI_edit_button_callback', 'AI24AI-main-settings', 'AI24AI_function_settings', array('label_for' => 'AI24AI_functions_edit_button', 'class' => 'AI24AI-class', 'description' => 'Click to edit the custom functions file.'));
// Add settings section and fields for API Settings
add_settings_section('AI24AI_api_settings', 'API Keys', null, 'AI24AI-main-settings');
add_settings_field('AI24AI_api_key', 'OpenAI API Key', 'AI24AI_api_key_callback', 'AI24AI-main-settings', 'AI24AI_api_settings');
add_settings_field('AI24AI_assistant_id', 'Assistant ID', 'AI24AI_assistant_id_callback', 'AI24AI-main-settings', 'AI24AI_api_settings');
add_settings_field('AI24AI_api_keys', 'Secret Keys', 'AI24AI_api_keys_callback', 'AI24AI-main-settings', 'AI24AI_api_settings');
// Register settings for widget styling
register_setting('AI24AI_styling_group', 'AI24AI_enable_widget', 'sanitize_text_field');
register_setting('AI24AI_styling_group', 'AI24AI_widget_pages', 'AI24AI_sanitize_pages');
register_setting('AI24AI_styling_group', 'AI24AI_header_color');
register_setting('AI24AI_styling_group', 'AI24AI_widget_corner_color');
register_setting('AI24AI_styling_group', 'AI24AI_toggle_image');
register_setting('AI24AI_styling_group', 'AI24AI_input_border_glow_color');
register_setting('AI24AI_styling_group', 'AI24AI_icon_color');
register_setting('AI24AI_styling_group', 'AI24AI_widget_position');
register_setting('AI24AI_styling_group', 'AI24AI_sidebar_content');
register_setting('AI24AI_styling_group', 'AI24AI_sidebar_text', 'sanitize_text_field');
register_setting('AI24AI_styling_group', 'AI24AI_text_options', 'AI24AI_sanitize_text_options');
// Add settings section and fields for widget styling
add_settings_section('AI24AI_widget_styling', 'Widget Styling', null, 'AI24AI-styling');
add_settings_field('AI24AI_enable_widget', 'Enable Widget', 'AI24AI_enable_widget_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_widget_pages', 'Select Pages for Widget', 'AI24AI_widget_pages_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_widget_position', 'Widget Position', 'AI24AI_widget_position_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_sidebar_content', 'Sidebar Content', 'AI24AI_sidebar_content_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_header_color', 'Header Color', 'AI24AI_header_color_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_widget_corner_color', 'Toggle Color', 'AI24AI_widget_corner_color_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_icon_color', 'Toggle Icon/Text Color', 'AI24AI_icon_color_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_toggle_image', 'Toggle Icon SVG', 'AI24AI_toggle_image_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
add_settings_field('AI24AI_input_border_glow_color', 'Input Border Glow Color', 'AI24AI_input_border_glow_color_callback', 'AI24AI-styling', 'AI24AI_widget_styling');
// New font settings section and field
//add_settings_section('AI24AI_font_settings', 'Font Settings', 'AI24AI_font_settings_callback', 'AI24AI-styling');
//add_settings_field('AI24AI_font', 'Chatbot Font', 'AI24AI_font_field_callback', 'AI24AI-styling', 'AI24AI_font_settings');
// Add settings section for text options
add_settings_section('AI24AI_text_options', 'Text Options', 'AI24AI_text_options_callback', 'AI24AI-styling');
add_settings_field('AI24AI_markdown_enabled', 'Enable Markdown', 'AI24AI_markdown_enabled_callback', 'AI24AI-styling', 'AI24AI_text_options');
// Tutorials Section
add_settings_section('AI24AI_tutorials_section', 'Tutorials', 'AI24AI_tutorials_section_callback', 'AI24AI-tutorials');
add_settings_field('AI24AI_tutorial_video', 'Tutorial Videos', 'AI24AI_tutorial_video_callback', 'AI24AI-tutorials', 'AI24AI_tutorials_section');
add_settings_field('AI24AI_tutorial_video2', 'Tutorial Video 2', 'AI24AI_tutorial_video2_callback', 'AI24AI-tutorials', 'AI24AI_tutorials_section');
// First, create a new option group for assistant styling settings.
register_setting('AI24AI_assistant_styling_group', 'AI24AI_assistant_name');
register_setting('AI24AI_assistant_styling_group', 'AI24AI_assistant_description');
register_setting('AI24AI_assistant_styling_group', 'AI24AI_assistant_image');
register_setting('AI24AI_assistant_styling_group', 'AI24AI_assistant_context_message');
// Register new settings for up to three context messages and their toggles
for ($i = 1; $i <= 3; $i++) {
register_setting('AI24AI_assistant_styling_group', "AI24AI_assistant_context_message_$i");
register_setting('AI24AI_assistant_styling_group', "AI24AI_assistant_context_toggle_$i");
}
// Add settings section for assistant styling
add_settings_section('AI24AI_assistant_styling_settings', 'Assistant Styling Settings', 'AI24AI_styling_settings_callback', 'AI24AI-assistant-styling');
add_settings_field('AI24AI_assistant_name', 'Assistant Name', 'AI24AI_assistant_name_callback', 'AI24AI-assistant-styling', 'AI24AI_assistant_styling_settings');
add_settings_field('AI24AI_assistant_description', 'Assistant Description', 'AI24AI_assistant_description_callback', 'AI24AI-assistant-styling', 'AI24AI_assistant_styling_settings');
add_settings_field('AI24AI_assistant_image', 'Assistant Image', 'AI24AI_assistant_image_callback', 'AI24AI-assistant-styling', 'AI24AI_assistant_styling_settings');
for ($i = 1; $i <= 3; $i++) {
add_settings_field("AI24AI_assistant_context_message_$i", "Starting Message $i", 'AI24AI_context_message_callback', 'AI24AI-assistant-styling', 'AI24AI_assistant_styling_settings', array('index' => $i));
add_settings_field("AI24AI_assistant_context_toggle_$i", "Enable Message $i", 'AI24AI_context_toggle_callback', 'AI24AI-assistant-styling', 'AI24AI_assistant_styling_settings', array('index' => $i));
}
}
// Generate Function file button callback
function AI24AI_generate_button_callback($args) {
echo 'Generate Functions File ';
echo 'Generate button rendered.
'; // Debugging line
}
// Regenerate Function file button callback
function AI24AI_regenerate_button_callback($args) {
echo 'Regenerate Functions File ';
echo 'Regenerate button rendered.
'; // Debugging line
}
function AI24AI_edit_button_callback($args) {
$plugin_base_dir = basename(WP_PLUGIN_DIR . '/AI24AI-child');
$plugin_file_path = 'functions.php';
$plugin_editor_link = network_admin_url("plugin-editor.php") . '?file=' . urlencode($plugin_base_dir . '/' . $plugin_file_path) . '&plugin=' . urlencode($plugin_base_dir . '/functions.php');
echo 'Edit Functions File ';
}
// AJAX handler for generating the functions file
add_action('wp_ajax_AI24AI_generate_function', function() {
// error_log('AI24AI_generate_function called.'); // Debugging line
$source_file = WP_PLUGIN_DIR . '/ai24-assistant-integrator/includes/functions.php';
$destination_dir = WP_PLUGIN_DIR . '/AI24AI-child';
$destination_file = $destination_dir . '/functions.php';
if (!file_exists($source_file)) {
// error_log('Source file does not exist: ' . $source_file); // Debugging line
wp_send_json_error('Source file does not exist.');
return;
}
if (!file_exists($destination_dir)) {
mkdir($destination_dir, 0755, true);
}
if (!copy($source_file, $destination_file)) {
// error_log('Failed to copy file from ' . $source_file . ' to ' . $destination_file); // Debugging line
wp_send_json_error('Failed to generate functions file.');
} else {
// Activate the child plugin
$plugin = 'AI24AI-child/functions.php';
if (!is_plugin_active($plugin)) {
activate_plugin($plugin);
}
wp_send_json_success('Functions file generated and plugin activated successfully.');
}
});
// AJAX handler for regenerating the functions file
add_action('wp_ajax_AI24AI_regenerate_function', function() {
// error_log('AI24AI_regenerate_function called.'); // Debugging line
$source_file = WP_PLUGIN_DIR . '/ai24-assistant-integrator/includes/functions.php';
$destination_file = WP_PLUGIN_DIR . '/AI24AI-child/functions.php';
if (!file_exists($source_file)) {
// error_log('Source file does not exist: ' . $source_file); // Debugging line
wp_send_json_error('Source file does not exist.');
return;
}
if (file_exists($destination_file)) {
unlink($destination_file);
}
if (!copy($source_file, $destination_file)) {
// error_log('Failed to copy file from ' . $source_file . ' to ' . $destination_file); // Debugging line
wp_send_json_error('Failed to regenerate functions file.');
} else {
// Activate the child plugin
$plugin = 'AI24AI-child/functions.php';
if (!is_plugin_active($plugin)) {
activate_plugin($plugin);
}
wp_send_json_success('Functions file regenerated and plugin activated successfully.');
}
});
// Enable widget and page view handling
// Callback function for the enable/disable widget field
function AI24AI_enable_widget_callback() {
$enable_widget = get_option('AI24AI_enable_widget', 'no');
?>
/>
Enable Widget
Select the pages where the widget should be displayed. Leave all unchecked to display on all pages.
" . esc_textarea($context_message) . "";
}
//function AI24AI_widget_styling_callback() {
// echo '';
//}
// Callback for the 'Widget Position' setting field
function AI24AI_widget_position_callback() {
$widget_position = get_option('AI24AI_widget_position', 'corner');
?>
> Corner
> Sidebar
";
// Optionally, add a hidden field to ensure the value gets posted
echo " ";
} else {
$checked = $context_toggle === 'on' ? 'checked' : '';
echo " ";
}
}
function AI24AI_api_keys_callback() {
$apiKeys = get_option('AI24AI_api_keys');
if (!is_array($apiKeys)) {
$apiKeys = [];
}
echo 'Here you can add as many secret keys as you need.
';
echo '';
foreach ($apiKeys as $index => $details) {
echo '
';
echo '
';
echo 'Identifier: ';
echo ' ';
echo '
';
echo '
';
echo 'Secret: ';
echo ' ';
echo 'Show '; // Show button
echo '
';
echo '
Remove ';
echo '
';
}
echo '
Add More Keys ';
echo '
'; // Close the container
}
function AI24AI_api_key_callback() {
$apiKey = get_option('AI24AI_api_key');
$locked = !empty($apiKey); // Assume it's locked if a key exists
echo '';
}
function AI24AI_assistant_id_callback() {
$assistantId = get_option('AI24AI_assistant_id');
$locked = !empty($assistantId);
echo '';
}
add_action('admin_init', function () {
if (isset($_POST['option_page']) && $_POST['option_page'] == 'AI24AI_options_group') {
// Sanitize and verify the nonce first to ensure the form submission is valid
if (!isset($_POST['AI24AI_settings_nonce'])) {
wp_die(esc_html__('Nonce not set.', 'ai24-assistant-integrator'));
}
$nonce = sanitize_text_field(wp_unslash($_POST['AI24AI_settings_nonce']));
if (!wp_verify_nonce($nonce, 'AI24AI_settings_action')) {
wp_die(esc_html__('Security check failed.', 'ai24-assistant-integrator'));
}
// Process the form since the nonce is verified.
if (isset($_POST['AI24AI_api_key']) && $_POST['AI24AI_api_key'] !== '') {
update_option('AI24AI_api_key', sanitize_text_field($_POST['AI24AI_api_key']));
}
if (isset($_POST['AI24AI_assistant_id']) && $_POST['AI24AI_assistant_id'] !== '') {
update_option('AI24AI_assistant_id', sanitize_text_field($_POST['AI24AI_assistant_id']));
}
}
});
// Display the settings errors
add_action('admin_notices', function() {
settings_errors('AI24AI_messages');
});
// Show a confirmation message when settings are saved
add_action('admin_notices', function () {
if (get_transient('settings_saved')) {
echo '';
delete_transient('settings_saved');
}
});
// Define callback functions for each field
function AI24AI_assistant_name_callback() {
$name = get_option('AI24AI_assistant_name');
echo ' ';
}
function AI24AI_assistant_description_callback() {
$description = get_option('AI24AI_assistant_description');
echo ' ';
}
function AI24AI_assistant_context_message_callback() {
$context_message = get_option('AI24AI_assistant_context_message');
echo '';
}
// Placeholder for settings section callback function
function AI24AI_styling_settings_callback() {
// Output any description or header you want for the Assistant Styling section.
echo 'Customize the appearance and behavior of the virtual assistant here.
';
}
function AI24AI_placement_settings_callback() {
echo 'Select where you want the virtual assistant to appear on your site.
';
}
function AI24AI_placement_options_callback() {
$placement_options = get_option('AI24AI_placement_options');
// Assuming $placement_options is an array with the selected options
?>
>All Pages
>Front Page
Hold down the Ctrl (Windows) or Command (Mac) button to select multiple options.
';
}
// Callback for the 'Widget Corner Color' field
function AI24AI_widget_corner_color_callback() {
$widget_corner_color = get_option('AI24AI_widget_corner_color', '#632289'); // Default color
echo ' ';
}
// Callback for the 'Input Border Glow Color' field
function AI24AI_input_border_glow_color_callback() {
$input_border_glow_color = get_option('AI24AI_input_border_glow_color', '#632289'); // Default color
echo ' ';
}
function AI24AI_icon_color_callback() {
$icon_text_color = get_option('AI24AI_icon_color', '#ffffff'); // Default to white
?>
Icon and Text Color
Choose a color for the icon or text:
>
Black
>
White
';
echo '' . esc_html__('Upload Image', 'ai24-assistant-integrator') . ' ';
echo ' ';
}
function AI24AI_sidebar_content_callback() {
$sidebar_content = get_option('AI24AI_sidebar_content', 'icon');
$sidebar_text = get_option('AI24AI_sidebar_text', '');
$initial_display_style = get_option('AI24AI_widget_position', 'corner') === 'corner' ? 'style="display: none;"' : '';
?>
Select the type of API handler you wish to use.';
}
// Function to get dynamic plugin file path
function get_dynamic_plugin_file_path() {
return 'includes/functions.php';
}
function AI24AI_functions_field_callback() {
$options = get_option('AI24AI_functions_option');
$handler_type = is_array($options) && isset($options['handler_type']) ? $options['handler_type'] : 'non_functions';
?>
>
API handler V1
>
API handler V2
Video tutorials to help you make the most of the AI24 Assistant Integrator.';
}
// Callback for the tutorial video field 1
function AI24AI_tutorial_video_callback() {
// Embed YouTube video
echo 'VIDEO ';
}
// The callback function for the second video 2
function AI24AI_tutorial_video2_callback() {
// Embed YouTube video
echo 'VIDEO ';
}
function AI24AI_sanitize_api_keys($keys) {
$sanitized_keys = [];
if (is_array($keys)) {
foreach ($keys as $key => $details) {
if (is_array($details) && isset($details['name']) && isset($details['key'])) {
$sanitized_key = [
'name' => sanitize_text_field($details['name']),
'key' => sanitize_text_field($details['key']), // Consider encrypting this value
];
$sanitized_keys[$key] = $sanitized_key;
}
}
}
return $sanitized_keys;
}
//SETTINGS FOR TEXT OPTIONS
/*//Settings for all the font selection and forcing
// Font settings section callback
//function AI24AI_font_settings_callback() {
echo '' . __('Configure the font settings for the chatbot.', 'AI24AI') . '
';
}
// Sanitize settings input
function AI24AI_sanitize_settings($input) {
$sanitized_input = array();
$sanitized_input['font'] = sanitize_text_field($input['font']);
error_log('Sanitized font: ' . $sanitized_input['font']); // Debug log
return $sanitized_input;
}
// Font field callback
function AI24AI_font_field_callback() {
$options = get_option('AI24AI_font_settings');
$fonts = array(
'AI24AIKumbhsans' => 'Kumbh Sans',
'Arial' => 'Arial',
'Helvetica' => 'Helvetica',
'Times New Roman' => 'Times New Roman',
'Georgia' => 'Georgia',
'Verdana' => 'Verdana',
'Tahoma' => 'Tahoma',
'Courier New' => 'Courier New',
'Lucida Console' => 'Lucida Console',
'Monaco' => 'Monaco',
'serif' => 'Serif',
'sans-serif' => 'Sans-serif',
'monospace' => 'Monospace',
'cursive' => 'Cursive',
'fantasy' => 'Fantasy',
);
$selected_font = isset($options['font']) ? $options['font'] : 'AI24AIKumbhsans'; // Default to Kumbh Sans
error_log('Current selected font in callback: ' . $selected_font); // Debug log
?>
$value) { ?>
>
Select a font from the available options.
' . __('Configure the text options for the chatbot.', 'AI24AI') . '';
}
function AI24AI_markdown_enabled_callback() {
$options = get_option('AI24AI_text_options');
?>
/>
Enable or disable Markdown processing in the chatbot.