Open AI API Key is not set, the chat app will not load on the front end until API Key is set.
Please visit → Open AI to create an api key."; aichwp_unschedule_initial_embeddings(); } else { try { $openAi = new OpenAIChat(['temperature' => 0.8]); $response = $openAi->generateResultString(['This is a test. Reply and nothing else.']); $is_stale = get_option('aichwp_post_embeddings_are_stale'); $is_running = get_option('aichwp_create_initial_embeddings_running'); if ($is_stale == 1 && $is_running == 0) { aichwp_create_initial_embeddings(); sleep(0.2); } } catch (Exception $e) { echo "

Open AI Key is not functioning correctly, please check the key for validity.

"; aichwp_unschedule_initial_embeddings(); } } ?>
" . esc_html($section['title']) . "\n"; } if ($section['callback']) { call_user_func($section['callback'], $section); } if (!isset($wp_settings_fields) || !isset($wp_settings_fields['aichwp']) || !isset($wp_settings_fields['aichwp'][$section['id']])) { continue; } echo '
'; do_settings_fields('aichwp', $section['id']); echo '
'; } ?>
'#f3f4f6', 'aichwpAIChatMessageBgColor' => '#3c82f6', 'aichwpAIChatMessageTextColor' => '#ffffff', 'aichwpUserChatMessageBgColor' => '#ffffff', 'aichwpUserChatMessageTextColor' => '#001827', 'aichwpChatClearChatTextColor' => '#4b5563', 'aichwpUserAvatarColor' => '#001827', 'aichwpLoadingIconColor' => '#3c82f6', 'aichwpSendButtonColor' => '#3c82f6', 'aichwpSendButtonTextColor' => '#ffffff', 'aichwpChatOpenButtonColor' => '#3c82f6', ); // Check each default color to see if it's already set foreach ($default_colors as $color => $default_value) { if (empty($options[$color])) { $options[$color] = $default_value; $options_update_needed = true; } } // Add settings fields for each color add_settings_field('aichwp_bg_color', 'Bg Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpBgColor']); add_settings_field('aichwp_ai_chat_message_bg_color', 'AI Chat Message Bg Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpAIChatMessageBgColor']); add_settings_field('aichwp_ai_chat_message_text_color', 'AI Chat Message Text Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpAIChatMessageTextColor']); add_settings_field('aichwp_user_chat_message_bg_color', 'User Chat Message Bg Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpUserChatMessageBgColor']); add_settings_field('aichwp_user_chat_message_text_color', 'User Chat Message Text Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpUserChatMessageTextColor']); add_settings_field('aichwp_clear_chat_text_color', 'Clear Chat Text Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpChatClearChatTextColor']); add_settings_field('aichwp_user_avatar_color', 'User Avatar Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpUserAvatarColor']); add_settings_field('aichwp_loading_icon_color', 'Loading Icon Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpLoadingIconColor']); add_settings_field('aichwp_send_button_color', 'Send Button Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpSendButtonColor']); add_settings_field('aichwp_send_button_text_color', 'Send Button Text Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpSendButtonTextColor']); add_settings_field('aichwp_chat_open_button_color', 'Chat Open Button Color', 'aichwp_color_field', 'aichwp', 'aichwp_colors', ['color' => 'aichwpChatOpenButtonColor']); // Add section for post types add_settings_section('aichwp_post_types', 'Post Types', 'aichwp_post_types_section_text', 'aichwp'); $post_types = aichwp_get_post_types(); foreach ($post_types as $post_type) { $post_type_object = get_post_type_object($post_type); add_settings_field( 'aichwp_post_type_' . $post_type, $post_type_object->label, 'aichwp_post_type_field', 'aichwp', 'aichwp_post_types', ['post_type' => $post_type] ); add_settings_field( 'aichwp_post_meta_fields_' . $post_type, '', 'aichwp_post_meta_fields_field', 'aichwp', 'aichwp_post_types', ['post_type' => $post_type] ); } // Add settings section for chat options add_settings_section('aichwp_chat_options', 'Chat Options', 'aichwp_chat_options_section_text', 'aichwp'); // Add settings field for chat welcome message add_settings_field('aichwp_chat_welcome_message', 'Chat Welcome Message', 'aichwp_chat_welcome_message_field', 'aichwp', 'aichwp_chat_options'); // Add settings fields for initial suggested questions add_settings_field('aichwp_initial_suggested_question_1', 'Initial Suggested Question 1', 'aichwp_initial_suggested_question_field', 'aichwp', 'aichwp_chat_options', ['question_number' => 1]); add_settings_field('aichwp_initial_suggested_question_2', 'Initial Suggested Question 2', 'aichwp_initial_suggested_question_field', 'aichwp', 'aichwp_chat_options', ['question_number' => 2]); add_settings_field('aichwp_initial_suggested_question_3', 'Initial Suggested Question 3', 'aichwp_initial_suggested_question_field', 'aichwp', 'aichwp_chat_options', ['question_number' => 3]); // Set default chat welcome message $default_chat_welcome_message = "Hi, I am an artificial intelligence assistant who can answer any queries you may have about content on this site. Type your question in the area below to begin."; if (empty($options['chat_welcome_message'])) { $options['chat_welcome_message'] = $default_chat_welcome_message; $options_update_needed = true; } if ($options_update_needed) { update_option('aichwp_settings', $options); } } // Section text function aichwp_section_text() { echo 'Configure the main AI Chat With Pages settings below:'; } // Indexing progress section text function aichwp_indexing_progress_section_text() { echo 'Site content indexing:'; } // Output indexing progress indicator field function aichwp_indexing_progress_indicator_field() { $options = get_option('aichwp_settings', array()); if (!isset($options['openai_api_key']) || empty($options['openai_api_key'])) { echo ' Please set your OpenAI API Key above.'; } else { echo ''. esc_html(aichwp_get_total_indexed_documents()) .' documents indexed.'; } } // Get the total number of indexed documents function aichwp_get_total_indexed_documents() { global $wpdb; $total_indexed = $wpdb->get_var("SELECT COUNT(DISTINCT post_id) FROM {$wpdb->prefix}aichat_post_embeddings WHERE is_active = 1"); return intval($total_indexed); } // Output messages per hour limit field function aichwp_messages_per_hour_limit_field() { $options = get_option('aichwp_settings', array()); $messages_per_hour_limit = isset($options['messages_per_hour_limit']) ? intval($options['messages_per_hour_limit']) : 50; echo ''; } // Output OpenAI chat model field function aichwp_openai_chat_model_field() { $options = get_option('aichwp_settings', array()); $openai_chat_model = isset($options['openai_chat_model']) ? esc_attr($options['openai_chat_model']) : 'gpt-3.5-turbo'; echo ''; echo 'Reset to default'; } // Output API key field function aichwp_openai_key_field() { $options = get_option('aichwp_settings', array()); if (!isset($options['openai_api_key'])) { $options['openai_api_key'] = ''; } echo ''; } // Post types section text function aichwp_post_types_section_text() { echo 'Select the post types to include in the search:'; } // Get all post types function aichwp_get_post_types() { $post_types = get_post_types(['public' => true], 'names'); $post_types[] = 'wp_template'; unset($post_types['attachment']); return $post_types; } // Output post type checkbox field function aichwp_post_type_field($args) { $options = get_option('aichwp_settings', array()); $post_type = $args['post_type']; $checked = isset($options['post_types'][$post_type]) ? checked($options['post_types'][$post_type], 1, false) : 'checked'; echo ''; } // Chat options section text function aichwp_chat_options_section_text() { echo 'Configure the chat options below:'; } // Output chat welcome message field function aichwp_chat_welcome_message_field() { $options = get_option('aichwp_settings', array()); $chat_welcome_message = isset($options['chat_welcome_message']) ? esc_textarea($options['chat_welcome_message']) : ''; echo ''; } // Output initial suggested question field function aichwp_initial_suggested_question_field($args) { $options = get_option('aichwp_settings', array()); $question_number = $args['question_number']; $initial_suggested_question = isset($options['initial_suggested_question_' . $question_number]) ? esc_textarea($options['initial_suggested_question_' . $question_number]) : ''; echo ''; } // Output post meta fields checkbox field function aichwp_post_meta_fields_field($args) { $options = get_option('aichwp_settings', array()); $post_type = $args['post_type']; $sample_post = get_posts([ 'post_type' => $post_type, 'posts_per_page' => 1, ]); if (!empty($sample_post)) { $meta_fields = get_post_custom($sample_post[0]->ID); $post_type_object = get_post_type_object($post_type); echo '
'; echo 'Select ' .esc_attr($post_type_object->label). ' meta fields +'; echo ''; echo '
'; } } // Validate input function aichwp_validate_settings($input) { $output = array(); // Validate OpenAI api key if (isset($input['openai_api_key'])) { $output['openai_api_key'] = sanitize_text_field($input['openai_api_key']); } // Validate messages per hour limit $input['messages_per_hour_limit'] = intval($input['messages_per_hour_limit']); if ($input['messages_per_hour_limit'] < 0) { $input['messages_per_hour_limit'] = 50; } $output['messages_per_hour_limit'] = sanitize_text_field($input['messages_per_hour_limit']); // Validate OpenAI chat model $valid_models = array('gpt-3.5-turbo', 'gpt-4-turbo', 'gpt-4o'); if (!in_array($input['openai_chat_model'], $valid_models)) { $input['openai_chat_model'] = 'gpt-3.5-turbo'; } $output['openai_chat_model'] = sanitize_text_field($input['openai_chat_model']); // Validate post types $post_types = aichwp_get_post_types(); foreach ($post_types as $post_type) { if (isset($input['post_types'][$post_type])) { $output['post_types'][$post_type] = 1; } else { // If the post type is not in the input array, set it as unchecked $output['post_types'][$post_type] = 0; } // Validate post meta fields for each post type if (isset($input['post_meta_fields'][$post_type])) { foreach ($input['post_meta_fields'][$post_type] as $meta_key => $meta_value) { $sanitized_meta_key = sanitize_key($meta_key); $output['post_meta_fields'][$post_type][$sanitized_meta_key] = 1; } } } // Validate colors $output['aichwpBgColor'] = sanitize_text_field($input['aichwpBgColor']); $output['aichwpAIChatMessageBgColor'] = sanitize_text_field($input['aichwpAIChatMessageBgColor']); $output['aichwpAIChatMessageTextColor'] = sanitize_text_field($input['aichwpAIChatMessageTextColor']); $output['aichwpUserChatMessageBgColor'] = sanitize_text_field($input['aichwpUserChatMessageBgColor']); $output['aichwpUserChatMessageTextColor'] = sanitize_text_field($input['aichwpUserChatMessageTextColor']); $output['aichwpChatClearChatTextColor'] = sanitize_text_field($input['aichwpChatClearChatTextColor']); $output['aichwpUserAvatarColor'] = sanitize_text_field($input['aichwpUserAvatarColor']); $output['aichwpLoadingIconColor'] = sanitize_text_field($input['aichwpLoadingIconColor']); $output['aichwpSendButtonColor'] = sanitize_text_field($input['aichwpSendButtonColor']); $output['aichwpSendButtonTextColor'] = sanitize_text_field($input['aichwpSendButtonTextColor']); $output['aichwpChatOpenButtonColor'] = sanitize_text_field($input['aichwpChatOpenButtonColor']); // Validate chat welcome message $output['chat_welcome_message'] = sanitize_textarea_field($input['chat_welcome_message']); // Validate initial suggested questions if (isset($input['initial_suggested_question_1'])) { $output['initial_suggested_question_1'] = sanitize_textarea_field($input['initial_suggested_question_1']); } if (isset($input['initial_suggested_question_2'])) { $output['initial_suggested_question_2'] = sanitize_textarea_field($input['initial_suggested_question_2']); } if (isset($input['initial_suggested_question_3'])) { $output['initial_suggested_question_3'] = sanitize_textarea_field($input['initial_suggested_question_3']); } return $output; } // Enqueue scripts and styles for the plugin's admin page add_action('admin_enqueue_scripts', 'aichwp_admin_enqueue_scripts'); function aichwp_admin_enqueue_scripts($hook) { if ('toplevel_page_aichwp' != $hook) { return; } wp_enqueue_style('wp-color-picker'); wp_enqueue_script('wp-color-picker'); wp_enqueue_script('aichwp-admin-script', AICHWP_PLUGIN_URL . 'core/js/admin-script.js', ['jquery', 'wp-color-picker'], '1.0', true); wp_enqueue_style('aichwp-chat-style', AICHWP_PLUGIN_URL . 'core/js/chat-app/build/static/css/aichwp.css', [], '1.0'); wp_enqueue_script('aichwp-chat-script', AICHWP_PLUGIN_URL . 'core/js/chat-app/build/static/js/aichwp.js', ['jquery'], '1.0', true); $options = get_option('aichwp_settings', array()); $color_vars = [ 'aichwpBgColor' => $options['aichwpBgColor'] ?? '#f3f4f6', 'aichwpAIChatMessageBgColor' => $options['aichwpAIChatMessageBgColor'] ?? '#3c82f6', 'aichwpAIChatMessageTextColor' => $options['aichwpAIChatMessageTextColor'] ?? '#ffffff', 'aichwpUserChatMessageBgColor' => $options['aichwpUserChatMessageBgColor'] ?? '#ffffff', 'aichwpUserChatMessageTextColor' => $options['aichwpUserChatMessageTextColor'] ?? '#001827', 'aichwpChatClearChatTextColor' => $options['aichwpChatClearChatTextColor'] ?? '#4b5563', 'aichwpUserAvatarColor' => $options['aichwpUserAvatarColor'] ?? '#001827', 'aichwpLoadingIconColor' => $options['aichwpLoadingIconColor'] ?? '#3c82f6', 'aichwpSendButtonColor' => $options['aichwpSendButtonColor'] ?? '#3c82f6', 'aichwpSendButtonTextColor' => $options['aichwpSendButtonTextColor'] ?? '#ffffff', 'aichwpChatOpenButtonColor' => $options['aichwpChatOpenButtonColor'] ?? '#3c82f6', ]; wp_localize_script('aichwp-chat-script', 'aichwp_color_vars', $color_vars); $chat_options = [ 'chat_welcome_message' => $options['chat_welcome_message'] ?? '', 'initial_suggested_question_1' => $options['initial_suggested_question_1'] ?? '', 'initial_suggested_question_2' => $options['initial_suggested_question_2'] ?? '', 'initial_suggested_question_3' => $options['initial_suggested_question_3'] ?? '', ]; wp_localize_script('aichwp-chat-script', 'aichwp_chat_vars', $chat_options); $aichwp_chat_nonce = wp_create_nonce('aichwp_chat_nonce'); wp_localize_script('aichwp-chat-script', 'aichwp_chat_nonce', $aichwp_chat_nonce); wp_localize_script('aichwp-admin-script', 'aichwp_ajax', [ 'ajax_url' => admin_url('admin-ajax.php'), 'plugin_url' => AICHWP_PLUGIN_URL ]); }