Files
cf7-spam-to-telegram/includes/class-flamingo-helper.php
T
Vladimir Bryzgalov 8efb82ca43 fix: phone leak, field-by-value detection, strict Flamingo email fallback
Three pilot-surfaced issues in one pass:

1. PhoneParser regex used '\s' in the candidate character class, so a
   digit on a following flattened line bled into the phone. '+7977
   6277470\n1' (phone + trailing checkbox value) parsed as 12 digits,
   producing '+797762774701'. Replaced '\s' with a literal space so
   newlines/tabs stop the candidate.

2. Custom-themed forms don't use 'your-name' / 'your-email' keys
   (washanyanya.ru has text-211 / text-212 / name_user). Added
   FieldDetector::find_name + find_email as fallbacks. Classifier
   and MessageFormatter now call them if no standard key matches,
   so 'Александр' in text-211 is recognised as a cyrillic name and
   scored / displayed accordingly.

3. FlamingoHelper copied '_from_email' into 'your-email' without
   validating it. Flamingo derives that meta from whatever field it
   considers "first"; on phone-first forms it ends up holding the
   phone number. Now we only copy _from_email if it contains '@',
   and dropped the _from_name shortcut entirely — FieldDetector
   handles the name via the actual field values.

Tests: +1 FieldDetectorTest (13 cases); +2 PhoneParserTest cases for
newline/tab bleeds; +1 ClassifierTest case; +1 MessageFormatterTest
case for custom field keys. Full suite 53/53 green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 00:22:39 +05:00

80 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Cf7stg;
final class FlamingoHelper
{
/**
* Try to find the Flamingo post created by the given CF7 submission.
* Matches by recency (created within the last 30 seconds) and a content digest.
*/
public static function find_post_id_for_submission($submission): ?int
{
if (!class_exists('\\WPCF7_Submission') && !is_object($submission)) {
return null;
}
$posts = get_posts([
'post_type' => 'flamingo_inbound',
'post_status' => ['flamingo-spam', 'flamingo-inbound', 'publish'],
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'date_query' => [[
'after' => gmdate('Y-m-d H:i:s', time() - 30),
'inclusive' => true,
]],
]);
if (!$posts) return null;
// Prefer the first (most recent) — Flamingo writes one row per submission.
return (int)$posts[0]->ID;
}
/**
* Extract fields from a Flamingo inbound post.
*
* Flamingo's '_fields' meta stores only the field *names* with null values;
* the actual values live in per-field meta keys prefixed with '_field_'.
* Collect them directly.
*
* @return array<string,mixed>
*/
public static function extract_fields(\WP_Post $post): array
{
$meta = get_post_meta($post->ID);
$out = [];
foreach ($meta as $key => $values) {
if (strpos($key, '_field_') !== 0) continue;
$name = substr($key, strlen('_field_'));
$raw = $values[0] ?? '';
$val = maybe_unserialize($raw);
if (is_array($val)) {
$val = implode(', ', array_map('strval', $val));
}
if ((string)$val !== '') {
$out[$name] = $val;
}
}
// Flamingo's "_from_name" and "_from_email" are derived heuristically
// from whatever field the plugin considers the "first" — on custom
// forms without dedicated email / name fields this produces garbage
// (e.g. phone number under _from_email). Only trust _from_email if
// it actually looks like an email; _from_name is not trustworthy at
// all, so leave it to FieldDetector (run by Classifier / Message-
// Formatter) to identify a name from any field value.
$from_email = (string)get_post_meta($post->ID, '_from_email', true);
if ($from_email !== '' && strpos($from_email, '@') !== false && !isset($out['your-email'])) {
$out['your-email'] = $from_email;
}
return $out;
}
public static function extract_reason(\WP_Post $post): string
{
$log = (string)get_post_meta($post->ID, '_akismet', true);
if ($log !== '') return 'Akismet';
return 'Неизвестно';
}
}