prefix . 'aish_messages'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query("TRUNCATE TABLE {$table_name}"); } function aish_create_db_table() { global $wpdb; $table_name = $wpdb->prefix . 'aish_messages'; $charset_collate = "CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange $sql = "CREATE TABLE $table_name (id mediumint(9) NOT NULL AUTO_INCREMENT, created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, sender_name tinytext NOT NULL, message_text text NOT NULL, message_type varchar(10) DEFAULT 'user' NOT NULL, PRIMARY KEY (id)) {$charset_collate};"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } add_action('widgets_init', function() { register_widget('AISH_Widget'); }); add_shortcode('aish_shoutbox', 'aish_shortcode_render'); function aish_shortcode_render() { ob_start(); aish_render_html(); return ob_get_clean(); } function aish_render_html() { $options = get_option('aish_options', []); $text = [ 'join_heading' => !empty($options['join_heading']) ? $options['join_heading'] : 'Join the conversation', 'name_placeholder' => !empty($options['name_placeholder']) ? $options['name_placeholder'] : 'Enter your name', 'join_button' => !empty($options['join_button']) ? $options['join_button'] : 'Join', 'write_placeholder' => !empty($options['write_placeholder']) ? $options['write_placeholder'] : 'Write a message...', 'send_button' => !empty($options['send_button']) ? $options['send_button'] : 'Send', 'ask_ai_title' => !empty($options['ask_ai_placeholder']) ? $options['ask_ai_placeholder'] : 'Ask AI a question' ]; ?>

', '', '', ''); ?>
Shoutbox by kaipkada.lt
post_content, 'aish_shoutbox'))) { $options = get_option('aish_options', []); $recaptcha_site_key = $options['recaptcha_site_key'] ?? ''; wp_enqueue_style('aish-style', plugin_dir_url(__FILE__) . 'css/style.css', [], AISH_VERSION); if (!empty($recaptcha_site_key)) { wp_enqueue_script('google-recaptcha-v3', 'https://www.google.com/recaptcha/api.js?render=' . esc_js($recaptcha_site_key), [], AISH_VERSION, true); } wp_enqueue_script('aish-script', plugin_dir_url(__FILE__) . 'js/script.js', ['google-recaptcha-v3'], AISH_VERSION, true); wp_localize_script('aish-script', 'aish_settings', [ 'ajax_url' => admin_url('admin-ajax.php'), 'recaptcha_site_key' => $recaptcha_site_key, 'nonce' => wp_create_nonce('aish_public_nonce'), 'text' => [ 'enter_name' => !empty($options['alert_enter_name']) ? $options['alert_enter_name'] : 'Please enter a name.', 'checking' => !empty($options['checking_text']) ? $options['checking_text'] : 'Checking...', 'join' => !empty($options['join_button']) ? $options['join_button'] : 'Join', 'login_error' => !empty($options['alert_unexpected_error']) ? $options['alert_unexpected_error'] : 'An unexpected error occurred. Please try again.', 'confirmation_failed' => !empty($options['alert_confirmation_failed']) ? $options['alert_confirmation_failed'] : 'Confirmation failed.', 'sending_error' => !empty($options['alert_sending_error']) ? $options['alert_sending_error'] : 'Error sending message.', 'ask_ai' => !empty($options['ask_ai_placeholder']) ? $options['ask_ai_placeholder'] : 'Ask AI a question...', 'write_message' => !empty($options['write_placeholder']) ? $options['write_placeholder'] : 'Write a message...', ] ]); } } add_action('admin_enqueue_scripts', 'aish_enqueue_admin_scripts'); function aish_enqueue_admin_scripts($hook) { if ('widgets.php' !== $hook && 'settings_page_aish_settings' !== $hook) return; wp_enqueue_script('aish-admin-script', plugin_dir_url(__FILE__) . 'js/admin.js', ['jquery'], AISH_VERSION, true); wp_localize_script('aish-admin-script', 'aish_admin', [ 'ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aish_clear_nonce'), 'text' => [ 'confirm_clear' => __('Are you sure you want to delete ALL shoutbox messages? This action cannot be undone.', 'ai-shoutbox'), 'clearing' => __('Clearing...', 'ai-shoutbox'), 'clear_success' => __('Chat history has been successfully cleared.', 'ai-shoutbox'), 'clear_error' => __('An error occurred:', 'ai-shoutbox'), 'unknown_error' => __('Unknown error.', 'ai-shoutbox') ] ]); } // --- AJAX FUNKCIJOS --- add_action('wp_ajax_aish_clear_chat', 'aish_handle_clear_chat'); function aish_handle_clear_chat() { check_ajax_referer('aish_clear_nonce', '_ajax_nonce'); if (!current_user_can('manage_options')) wp_send_json_error(['message' => 'You do not have permission.'], 403); global $wpdb; $table_name = $wpdb->prefix . 'aish_messages'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query("TRUNCATE TABLE {$table_name}"); wp_send_json_success(); } add_action('wp_ajax_aish_login', 'aish_handle_login'); add_action('wp_ajax_nopriv_aish_login', 'aish_handle_login'); function aish_handle_login() { check_ajax_referer('aish_public_nonce', 'security'); $options = get_option('aish_options', []); $secret_key = $options['recaptcha_secret_key'] ?? ''; if (empty($secret_key)) { wp_send_json_error(['message' => 'reCAPTCHA secret key is not set.']); return; } $token = isset($_POST['token']) ? sanitize_text_field(wp_unslash($_POST['token'])) : ''; if (empty($token)) { wp_send_json_error(['message' => 'reCAPTCHA token is missing.']); return; } $remote_ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; $response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', ['body' => ['secret' => $secret_key, 'response' => $token, 'remoteip' => $remote_ip]]); if (is_wp_error($response)) { wp_send_json_error(['message' => 'reCAPTCHA server error.']); return; } $body = json_decode(wp_remote_retrieve_body($response), true); if ($body && !empty($body['success']) && !empty($body['score']) && $body['score'] > 0.5 && !empty($body['action']) && $body['action'] == 'login') { wp_send_json_success(['message' => 'Confirmation successful.']); } else { wp_send_json_error(['message' => 'reCAPTCHA confirmation failed.']); } } add_action('wp_ajax_aish_get_messages', 'aish_get_messages'); add_action('wp_ajax_nopriv_aish_get_messages', 'aish_get_messages'); function aish_get_messages() { check_ajax_referer('aish_public_nonce', 'security'); global $wpdb; $table_name = $wpdb->prefix . 'aish_messages'; $last_id = isset($_POST['last_id']) ? intval($_POST['last_id']) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $messages = $wpdb->get_results($wpdb->prepare("SELECT id, sender_name, message_text, message_type FROM {$table_name} WHERE id > %d ORDER BY id ASC", $last_id)); wp_send_json_success($messages); } add_action('wp_ajax_aish_post_message', 'aish_post_message'); add_action('wp_ajax_nopriv_aish_post_message', 'aish_post_message'); function aish_post_message() { check_ajax_referer('aish_public_nonce', 'security'); global $wpdb; $table_name = $wpdb->prefix . 'aish_messages'; $sender = isset($_POST['sender']) ? sanitize_text_field(wp_unslash($_POST['sender'])) : ''; $text = isset($_POST['text']) ? sanitize_textarea_field(wp_unslash($_POST['text'])) : ''; $isAI = isset($_POST['isAI']) ? filter_var(wp_unslash($_POST['isAI']), FILTER_VALIDATE_BOOLEAN) : false; if (empty($sender) || empty($text)) { wp_send_json_error(['message' => 'Empty sender or text.']); return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert($table_name, ['created_at' => current_time('mysql'), 'sender_name' => $sender, 'message_text' => $text, 'message_type' => 'user']); if ($isAI) { $ai_response_text = aish_get_openai_response($text); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert($table_name, ['created_at' => current_time('mysql'), 'sender_name' => 'AI', 'message_text' => $ai_response_text, 'message_type' => 'ai']); } wp_send_json_success(); } function aish_get_openai_response($prompt) { $options = get_option('aish_options', []); $api_key = $options['openai_key'] ?? ''; if (empty($api_key)) return 'OpenAI API key is not set in settings.'; $max_tokens = !empty($options['openai_max_tokens']) ? absint($options['openai_max_tokens']) : 150; $api_url = 'https://api.openai.com/v1/chat/completions'; $response = wp_remote_post($api_url, ['headers' => ['Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key], 'body' => json_encode(['model' => 'gpt-3.5-turbo', 'messages' => [['role' => 'system', 'content' => 'You are a helpful assistant.'], ['role' => 'user', 'content' => $prompt]], 'max_tokens' => $max_tokens]), 'timeout' => 30]); if (is_wp_error($response)) return 'Error connecting to OpenAI.'; $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['choices'][0]['message']['content'])) return trim($body['choices'][0]['message']['content']); return 'Could not get a response from AI.'; }