__('Comment not found.', 'ai-powered-comment-moderator')]; } // 2?? Sanitize content $comment_text = sanitize_text_field($comment->comment_content); // 3?? Retrieve OpenAI API key & settings $api_key = sanitize_text_field(get_option('aicomo_api_key')); $model = sanitize_text_field(get_option('aicomo_model', 'gpt-4-turbo')); $prompt = get_option('aicomo_system_prompt', ''); // 4?? Use default moderation prompt if none is set if (empty($prompt)) { $prompt = 'You are an AI comment moderator. ' . 'Your task is to determine if a comment is meaningful or spam/inappropriate. ' . 'If the comment is nonsensical, offensive, or spam, respond with: ' . 'Decision: No, Category: e.g. "Spam" or "Nonsensical", Confidence: <0-100>. ' . 'If the comment makes sense, respond with: Decision: Yes, Category: e.g. "Legitimate", Confidence: <0-100>. ' . 'Use exactly this format: Decision: [Yes/No]\nCategory: [string]\nConfidence: [integer]. ' . 'No additional text.'; } // 5?? OpenAI Moderation Endpoint Check (Optional) $moderation_response = wp_remote_post('https://api.openai.com/v1/moderations', [ 'body' => wp_json_encode(['input' => $comment_text]), 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ], 'timeout' => 15 ]); if (is_wp_error($moderation_response)) { return ['error' => __('AI Moderation API request failed.', 'ai-powered-comment-moderator')]; } $moderation_body = json_decode(wp_remote_retrieve_body($moderation_response), true); // ?? If OpenAI flags it, assume it's spam if (!empty($moderation_body['results'][0]['flagged'])) { if (function_exists('wp_spam_comment')) { wp_spam_comment($comment_ID); } return [ 'decision' => __('Spam - AI Flagged', 'ai-powered-comment-moderator'), 'category' => 'Flagged', 'confidence' => '99%' ]; } // 6?? ChatGPT AI Review $request_body = [ 'model' => $model, 'messages' => [ [ 'role' => 'system', 'content' => $prompt ], [ 'role' => 'user', 'content' => $comment_text ], ], 'temperature' => 0, // Consistency in responses ]; $response = wp_remote_post('https://api.openai.com/v1/chat/completions', [ 'body' => wp_json_encode($request_body), 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ], 'timeout' => 15 ]); if (is_wp_error($response)) { return ['error' => __('ChatGPT API request failed.', 'ai-powered-comment-moderator')]; } $body = json_decode(wp_remote_retrieve_body($response), true); if (empty($body['choices'][0]['message']['content'])) { return ['error' => __('Invalid ChatGPT API response.', 'ai-powered-comment-moderator')]; } // 7?? Extract AI response $message_content = strtolower(trim($body['choices'][0]['message']['content'])); preg_match('/decision:\\s*(yes|no)/i', $message_content, $decision_match); preg_match('/category:\\s*([a-zA-Z ]+)/i', $message_content, $category_match); preg_match('/confidence:\\s*(\\d+)/i', $message_content, $confidence_match); $decision = (!empty($decision_match[1]) && strtolower($decision_match[1]) === 'yes'); $category = (!empty($category_match[1])) ? trim($category_match[1]) : 'Uncategorized'; $confidence = (!empty($confidence_match[1])) ? intval($confidence_match[1]) : 0; // ? FINAL DECISION LOGIC if ($decision) { // ?? AI says it's a valid comment -> APPROVE IT wp_update_comment([ 'comment_ID' => $comment_ID, 'comment_approved' => 1 ]); return [ /* translators: %s represents the detected category by AI (e.g., "Uncategorized", "Spam", etc.) */ 'decision' => __('Approved', 'ai-powered-comment-moderator'), 'category' => $category, 'confidence' => $confidence . '%' ]; } else { // ? If confidence is HIGH (=70) -> MARK AS SPAM if ($confidence >= 70) { if (function_exists('wp_spam_comment')) { wp_spam_comment($comment_ID); } return [ /* translators: %s represents the detected category by AI (e.g., "Uncategorized", "Spam", etc.) */ 'decision' => sprintf(__('Spam - %s', 'ai-powered-comment-moderator'), $category), 'category' => $category, 'confidence' => $confidence . '%' ]; } else { // ? If confidence is LOW (<70) -> NEEDS REVIEW return [ /* translators: %s represents the detected category by AI (e.g., "Uncategorized", "Spam", etc.) */ 'decision' => sprintf(__('Needs Review - %s', 'ai-powered-comment-moderator'), $category), 'category' => $category, 'confidence' => $confidence . '%' ]; } } } catch (Exception $e) { return ['error' => __('An unexpected error occurred.', 'ai-powered-comment-moderator')]; } }