feat(flamingo-helper): locate flamingo_inbound posts and extract fields/reason
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?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.
|
||||
*
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public static function extract_fields(\WP_Post $post): array
|
||||
{
|
||||
$fields = get_post_meta($post->ID, '_fields', true);
|
||||
if (is_array($fields)) return $fields;
|
||||
// Fallback: meta stored under Flamingo's structure
|
||||
$meta = get_post_meta($post->ID);
|
||||
$out = [];
|
||||
foreach ($meta as $key => $values) {
|
||||
if (strpos($key, '_field_') === 0) {
|
||||
$out[substr($key, strlen('_field_'))] = maybe_unserialize($values[0] ?? '');
|
||||
}
|
||||
}
|
||||
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 'Неизвестно';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user