From 71bd267227633f8fc3346c55f940c232539c0eb0 Mon Sep 17 00:00:00 2001 From: Rajan Vijayan Date: Mon, 25 Nov 2024 09:50:44 +0530 Subject: [PATCH] format all functions --- src/Spam/EmailValidator.php | 96 ++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 29 deletions(-) diff --git a/src/Spam/EmailValidator.php b/src/Spam/EmailValidator.php index 12a929e..8a3b902 100644 --- a/src/Spam/EmailValidator.php +++ b/src/Spam/EmailValidator.php @@ -1,61 +1,99 @@ -isSpam($message)) { - error_log("Invalid email content detected. Email not sent."); - return false; // Optionally, stop the email from sending by modifying the workflow - } else { - error_log("Email content is valid. Proceeding with sending the email."); + return false; // Stop the email from being sent } } + /** + * Checks if the email message is spam. + * + * @param string $message The email message to check. + * @return bool Returns true if the message is spam, false otherwise. + */ private function isSpam($message) { $api_key = get_option('api_key', ''); if (empty($api_key)) { - error_log('API key is missing in the AI Comment Moderator plugin settings.'); - return false; // Consider email as spam if the API key is missing + error_log('API key is missing for spam detection.'); + return true; // Assume spam if the API key is missing } - // Initialize AI engine with API key + // Initialize AI engine with API key and check for spam $ai_client = new AIEngine($api_key); - $response_mode = get_option('response_mode', 'professional'); - - // Assuming the AIEngine class has a method `generateContent` that returns boolean - $prompt = "This is post email content: ".$message." Check it is spam or not. If it is spam, return true; otherwise, return false."; - + $prompt = "Check if this message is spam: $message"; $response_data = $ai_client->generateContent($prompt); - - return $response_data === "true"; // Adjust based on the actual return type from AIEngine + + // Assume the response_data is returned as a boolean + return $response_data; } } -// Ensure the overridden wp_mail function is declared globally and not inside the class +// Overridden wp_mail function with spam check if (!function_exists('wp_mail')) { + /** + * Custom wp_mail function. + * + * Integrates spam checking before sending an email. + * + * @param string $to Recipient email address. + * @param string $subject Email subject. + * @param string $message Email body/message. + * @param mixed $headers Email headers. + * @param mixed $attachments Email attachments. + * @return bool Returns false if the email is flagged as spam, otherwise sends the email. + */ function wp_mail($to, $subject, $message, $headers = '', $attachments = array()) { - // Trigger the pre_wp_mail action before the actual mail sending - do_action('pre_wp_mail', $to, $subject, $message, $headers, $attachments); - - // Call the actual wp_mail function logic - // Assuming _wp_mail is your actual mail function that sends the email + // Trigger spam check + if (!apply_filters('pre_wp_mail', true, $to, $subject, $message, $headers, $attachments)) { + return false; // Stop email if flagged as spam + } + // Send email if not spam return _wp_mail($to, $subject, $message, $headers, $attachments); } } -// Dummy _wp_mail function to simulate WordPress' native wp_mail behavior +// Placeholder for the real wp_mail function if (!function_exists('_wp_mail')) { + /** + * Simulates sending an email. + * + * This is a placeholder function meant to simulate the actual email sending function. + * + * @param string $to Recipient email address. + * @param string $subject Email subject. + * @param string $message Email body/message. + * @param mixed $headers Email headers. + * @param mixed $attachments Email attachments. + * @return bool Simulates successful email send. + */ function _wp_mail($to, $subject, $message, $headers = '', $attachments = array()) { - // This function would normally handle sending the mail - return true; // Simulating a successful mail send + return true; // Simulating email sending } } \ No newline at end of file