prefix = $prefix; add_action("admin_menu", [$this, "acachat_add_settings_page"]); add_action("admin_init", [$this, "acachat_register_settings"]); add_action("wp_ajax_save_acachat_api_key", [$this, "acachat_save_api_key"]); add_action("admin_enqueue_scripts", [$this, "acachat_enqueue_scripts"]); add_action("wp_footer", [$this, "acachat_enqueue_footer_html_design"]); add_action("wp_enqueue_scripts", [$this, "acachat_enqueue_bot_scripts"]); } /** * Enqueue the footer script for the chatbot if the API key is set * * @return void */ public function acachat_enqueue_bot_scripts() { $acachat_api_key = get_option($this->prefix . '_api_key'); if (!empty($acachat_api_key)) { wp_enqueue_style( 'ai-assist-bot-styles', "https://cdn.jsdelivr.net/gh/Open-infotech/AiAssistJsLibForWeb@feature/version2.0/src/v1/30_css/styles.css", [], '1.2.0', 'all' ); wp_enqueue_script( 'ai-assist-bot-script', 'https://cdn.jsdelivr.net/gh/Open-infotech/AiAssistJsLibForWeb@feature/version2.0/src/v1/31_prod_aichatassist/bundle.js?key=' . urlencode($acachat_api_key), [], '1.2.0', true ); } } /** * Add the HTML tag to the footer of the page * * The ai-assist-bot tag is used by the JavaScript library to render the chatbot. * This function is hooked into the wp_footer action which is triggered by WordPress * in the footer of the page * * @return void */ public function acachat_enqueue_footer_html_design() { echo ''; } /** * Add the settings page for the plugin * * Uses the add_menu_page function to add a new admin menu page * * @return void */ public function acachat_add_settings_page() { $icon_url = plugins_url("assets/images/logo.png", __DIR__); add_menu_page( "AI Chat Assist", "AI Chat Assist", "manage_options", $this->prefix . "-settings", [$this, "acachat_render_settings_page"], $icon_url ); } /** * Render the settings page for the plugin * * This function will output the HTML for the settings page * * @return void */ public function acachat_render_settings_page() { $image_url1 = plugin_dir_url(__DIR__) . 'assets/images/star1.png'; $image_url3 = plugin_dir_url(__DIR__) . 'assets/images/star3.png'; $image_url4 = plugin_dir_url(__DIR__) . 'assets/images/cloud1.png'; $image_url5 = plugin_dir_url(__DIR__) . 'assets/images/navbar.png'; ?>

?
" />

prefix . "_options", $this->prefix . "_options", [ $this, "acachat_validate_options", ]); add_settings_section("acachat_api_settings", "", "", $this->prefix); add_settings_field( $this->prefix . "_api_key", "API Key :", [$this, "acachat_setting_api_key"], $this->prefix, "acachat_api_settings" ); } /** * Validate the options for the settings page * * @param array $input The input values from the settings page * * @return array The validated and sanitized options */ public function acachat_validate_options($input) { $newinput = []; if (isset($input["api_key"])) { $newinput["api_key"] = sanitize_text_field(trim($input["api_key"]) ); } else { $newinput["api_key"] = ''; } return $newinput; } /** * Renders the input field for the API key in the settings page. * * Retrieves the stored API key from the database and displays it * in a text input field with a tooltip for user guidance. */ public function acachat_setting_api_key() { $acachat_api_key = get_option($this->prefix . "_api_key"); ?>
?
prefix . '_api_key') . "' name='" . esc_attr($this->prefix . '_api_key') . "' type='text' value='" . esc_attr($api_key) . "' />" ); settings_errors($this->prefix . "_api_key"); ?>
id === "toplevel_page_acachat-settings") { wp_enqueue_style( "chatassist-admin-style", plugin_dir_url(__DIR__) . "assets/css/acachat-admin-style.css", [], "1.0.0" ); wp_enqueue_script( "chatassist-admin-script", plugin_dir_url(__DIR__) . "assets/js/acachat-admin-script.js", ["jquery"], "1.0.0", true ); wp_enqueue_style( "bootstrap-css", plugin_dir_url(__DIR__) . "assets/css/bootstrap.min.css", [], "4.0.0" ); wp_enqueue_script( "bootstrap-js", plugin_dir_url(__DIR__) . "assets/js/bootstrap.bundle.min.js", ["jquery"], "4.0.0", true ); wp_localize_script("chatassist-admin-script", "chatAssist", [ "ajax_url" => admin_url("admin-ajax.php"), ]); } } /** * Validates a given API key by making a POST request to the ChatAssist * API. The request includes the current website URL and the path '/', * and the API key is passed as a query parameter. If the request is * successful (200 status code), the API key is valid. * * @param string $acachat_api_key The API key to validate. * * @return bool True if the API key is valid, false otherwise. */ public function acachat_validate_api_key($acachat_api_key) { $response = wp_remote_get( ACACHAT_CHAT_ASSIST_API_URL . "?secret_key=" . $acachat_api_key, [ "method" => "POST", "body" => [ "website" => preg_replace("/^https?:\/\//","", get_site_url()), "path" => "/", ], ] ); if (is_wp_error($response)) { return false; } $http_code = wp_remote_retrieve_response_code($response); if ($http_code == 200) { return true; } return false; } /** * Save an API key for the plugin. This function is called via an AJAX request * and expects an 'api_key' parameter to be passed in the $_POST array. If the * API key is valid, it will be saved to the database and a JSON success message * will be returned. If the API key is invalid, a JSON error message will be * returned. If the user does not have permission to perform this action, a JSON * error message will be returned. * * @uses acachat_validate_api_key * @uses update_option * @uses wp_send_json_success * @uses wp_send_json_error * @uses wp_die */ public function acachat_save_api_key() { $nonce = isset($_POST["acachat_api_key_nonce"]) ? sanitize_text_field(wp_unslash($_POST["acachat_api_key_nonce"])) : ""; if ( empty($nonce) || !wp_verify_nonce( $nonce, $this->prefix . "_api_key_nonce" ) ) { wp_send_json_error(esc_html__("Nonce verification failed. Please try again.", 'ai-chat-assist')); } if (current_user_can("manage_options")) { if (isset($_POST["acachat_api_key"])) { $acachat_api_key = sanitize_text_field(wp_unslash($_POST["acachat_api_key"])); if ($this->acachat_validate_api_key($acachat_api_key)) { update_option($this->prefix . "_api_key", $acachat_api_key); wp_send_json_success(esc_html__("API Key saved successfully!", 'ai-chat-assist')); } else { wp_send_json_error(esc_html__("Your API key is invalid.", 'ai-chat-assist')); } } else { wp_send_json_error(esc_html__("API key not provided.", 'ai-chat-assist')); } } else { wp_send_json_error( esc_html__("You do not have permission to perform this action.", 'ai-chat-assist') ); } wp_die(); } } } new ACACHAT_AiChatAssistAdmin("acachat");