-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
725f21c
commit 71bd267
Showing
1 changed file
with
67 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,99 @@ | ||
<?php | ||
<?php | ||
namespace AISpamShield\Spam; | ||
|
||
use AIEngine\AIEngine; | ||
|
||
/** | ||
* Class EmailValidator | ||
* Handles email validation to detect and log spam activities. | ||
*/ | ||
class EmailValidator { | ||
/** | ||
* Constructor. | ||
* Hooks into the WordPress email sending process to validate emails. | ||
*/ | ||
public function __construct() { | ||
add_action('pre_wp_mail', [$this, 'validateAndLogEmail'], 10, 2); | ||
add_action('pre_wp_mail', [$this, 'validateAndLogEmail'], 10, 5); | ||
} | ||
|
||
public function validateAndLogEmail($to, $mail) { | ||
// Logging email details | ||
$message = $mail['message']; | ||
// Perform spam check | ||
/** | ||
* Validates and logs the email before sending. | ||
* | ||
* @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 detected as spam, halting the send process. | ||
*/ | ||
public function validateAndLogEmail($to, $subject, $message, $headers, $attachments) { | ||
if ($this->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 | ||
} | ||
} |