0) { $post = get_post($post_id); if ($post) { $context_data['current_article'] = array( 'title' => $post->post_title, 'content' => wp_strip_all_tags($post->post_content), 'excerpt' => $post->post_excerpt ); } } // Get related articles for context $related_articles = $this->get_related_articles($message, $post_id); $context_data['related_articles'] = $related_articles; // Get AI response $response = $ai_service->get_chat_response($message, $context_data); if ($response) { wp_send_json_success(array( 'message' => $response, 'related_articles' => $related_articles )); } else { wp_send_json_error('Failed to get AI response'); } } /** * Handle contact form submission */ public function handle_send_contact() { // Verify nonce $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; if (!wp_verify_nonce($nonce, 'aiblhewi_nonce')) { wp_die('Security check failed'); } $email = isset($_POST['email']) ? sanitize_email(wp_unslash($_POST['email'])) : ''; $message = isset($_POST['message']) ? sanitize_textarea_field(wp_unslash($_POST['message'])) : ''; if (empty($email) || empty($message)) { wp_send_json_error('Email and message are required'); } if (!is_email($email)) { wp_send_json_error('Invalid email address'); } // Get contact email from SMTP username (simplified settings) $options = get_option('aiblhewi_options'); $contact_email = $options['smtp_username'] ?? get_option('admin_email'); // Prepare email $admin_email = get_option('admin_email'); $website_name = get_bloginfo('name'); /* translators: %s: Website name */ $subject = sprintf(esc_html__('New message from %s', 'ai-blog-helper'), $website_name); // Create HTML email body $body = sprintf( '
%s
%s: %s
%s:
%s
%s: %s
', esc_html__('New message from your website', 'ai-blog-helper'), esc_html__('You have received a new message from your website:', 'ai-blog-helper'), esc_html__('From', 'ai-blog-helper'), esc_html($email), esc_html__('Message', 'ai-blog-helper'), nl2br(esc_html($message)), esc_html__('Sent from', 'ai-blog-helper'), home_url() ); // Set proper headers $headers = array( 'From: ' . $website_name . ' <' . $admin_email . '>', 'Content-Type: text/html; charset=UTF-8', 'Reply-To: ' . $email ); // Send email $sent = wp_mail($contact_email, $subject, $body, $headers); if ($sent) { wp_send_json_success('Message sent successfully'); } else { wp_send_json_error('Failed to send message'); } } /** * Handle article summarization */ public function handle_summarize_article() { // Verify nonce $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; if (!wp_verify_nonce($nonce, 'aiblhewi_nonce')) { wp_die('Security check failed'); } $post_id = isset($_POST['post_id']) ? intval(wp_unslash($_POST['post_id'])) : 0; if ($post_id <= 0) { wp_send_json_error('Invalid post ID'); } $post = get_post($post_id); if (!$post) { wp_send_json_error('Post not found'); } // Get AI service instance $ai_service = new AIBLHEWI_AI_Service(); // Prepare article content $article_content = array( 'title' => $post->post_title, 'content' => wp_strip_all_tags($post->post_content), 'excerpt' => $post->post_excerpt ); // Get summary $summary = $ai_service->summarize_article($article_content); if ($summary) { wp_send_json_success(array('summary' => $summary)); } else { // Get detailed error information from the AI service $error_details = $ai_service->get_last_gemini_error(); if ($error_details) { // Log the detailed error // Send detailed error information back to the browser wp_send_json_error(array( 'message' => 'Failed to generate summary', 'error_code' => $error_details['code'], 'error_details' => $error_details['body'] )); } else { wp_send_json_error('Failed to generate summary'); } } } /** * Get related articles based on query */ private function get_related_articles($query, $exclude_post_id = 0) { $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, 's' => $query, 'post__not_in' => array($exclude_post_id) ); $posts = get_posts($args); $related_articles = array(); foreach ($posts as $post) { $related_articles[] = array( 'id' => $post->ID, 'title' => $post->post_title, 'url' => get_permalink($post->ID), 'excerpt' => wp_trim_words($post->post_excerpt ?: wp_strip_all_tags($post->post_content), 20) ); } return $related_articles; } }