From eae8fafb326f8fd2f24ca5a9cbba8811b03a643a Mon Sep 17 00:00:00 2001 From: Vladimir Bryzgalov Date: Tue, 21 Apr 2026 15:56:14 +0500 Subject: [PATCH] feat(classifier): has_valid_email_anywhere() helper for drop-logic --- includes/class-classifier.php | 15 +++++++++++++++ tests/ClassifierDropTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/ClassifierDropTest.php diff --git a/includes/class-classifier.php b/includes/class-classifier.php index a6f7986..bcbccc1 100644 --- a/includes/class-classifier.php +++ b/includes/class-classifier.php @@ -215,4 +215,19 @@ final class Classifier } return $hits; } + + public static function has_valid_email_anywhere(array $fields): bool + { + foreach ($fields as $v) { + $values = is_array($v) ? $v : [$v]; + foreach ($values as $val) { + $val = trim((string)$val); + if ($val === '') continue; + if (preg_match('/[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}/', $val, $m)) { + if (is_email($m[0])) return true; + } + } + } + return false; + } } diff --git a/tests/ClassifierDropTest.php b/tests/ClassifierDropTest.php new file mode 100644 index 0000000..7c283cf --- /dev/null +++ b/tests/ClassifierDropTest.php @@ -0,0 +1,32 @@ + 'pasha@gmail.com']; + self::assertTrue(Classifier::has_valid_email_anywhere($fields)); + } + + public function test_has_valid_email_anywhere_finds_in_array_value(): void + { + $fields = ['x' => ['noise', 'info@some-domain.com']]; + self::assertTrue(Classifier::has_valid_email_anywhere($fields)); + } + + public function test_has_valid_email_anywhere_returns_false_for_invalid(): void + { + $fields = ['email' => 'not-an-email', 'x' => 'AB']; + self::assertFalse(Classifier::has_valid_email_anywhere($fields)); + } + + public function test_has_valid_email_anywhere_returns_false_when_empty(): void + { + self::assertFalse(Classifier::has_valid_email_anywhere([])); + } +}