feat(retro-importer): queue last-N Flamingo spam posts with dedup
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cf7stg;
|
||||
|
||||
final class RetroImporter
|
||||
{
|
||||
public const MAX_COUNT = 50;
|
||||
|
||||
/**
|
||||
* @return array{added:int,skipped:int}
|
||||
*/
|
||||
public static function import(int $count, int $days, bool $allow_repeat): array
|
||||
{
|
||||
$count = max(1, min(self::MAX_COUNT, $count));
|
||||
$args = [
|
||||
'post_type' => 'flamingo_inbound',
|
||||
'post_status' => 'flamingo-spam',
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'posts_per_page' => $count,
|
||||
];
|
||||
if ($days > 0) {
|
||||
$args['date_query'] = [[
|
||||
'after' => gmdate('Y-m-d H:i:s', time() - $days * 86400),
|
||||
'inclusive' => true,
|
||||
]];
|
||||
}
|
||||
$posts = get_posts($args);
|
||||
|
||||
$added = 0; $skipped = 0;
|
||||
foreach ($posts as $post) {
|
||||
if (!$allow_repeat && Queue::exists_for_flamingo((int)$post->ID, true)) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
$fields = FlamingoHelper::extract_fields($post);
|
||||
$reason = FlamingoHelper::extract_reason($post);
|
||||
$classification = Classifier::classify($fields, ['reason' => $reason]);
|
||||
|
||||
$payload = MessageFormatter::build([
|
||||
'fields' => $fields,
|
||||
'classification' => $classification,
|
||||
'site_title' => Settings::get_site_title(),
|
||||
'form_title' => (string)get_post_meta($post->ID, '_subject', true),
|
||||
'submitted_at' => wp_date('d.m.Y H:i', (int)get_post_time('U', true, $post)),
|
||||
'reason' => $reason,
|
||||
'ip' => (string)get_post_meta($post->ID, '_meta_remote_ip', true),
|
||||
'user_agent' => (string)get_post_meta($post->ID, '_meta_user_agent', true),
|
||||
'is_retro' => true,
|
||||
]);
|
||||
|
||||
Queue::enqueue([
|
||||
'flamingo_post_id' => (int)$post->ID,
|
||||
'form_id' => null,
|
||||
'source_key' => 'cf7',
|
||||
'payload_json' => wp_json_encode($payload),
|
||||
'label' => $classification['label'],
|
||||
'is_retro' => 1,
|
||||
]);
|
||||
$added++;
|
||||
}
|
||||
|
||||
return compact('added', 'skipped');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user