feat(source): drop label → skip enqueue (or marked enqueue in dry-run); cf7stg_should_enqueue filter

Also extends test bootstrap autoloader to resolve classes in includes/sources/ and admin/ subdirectories.
This commit is contained in:
Vladimir Bryzgalov
2026-04-21 16:30:07 +05:00
parent 24367c1ccb
commit 42e311c05a
3 changed files with 133 additions and 4 deletions
+43 -1
View File
@@ -35,6 +35,17 @@ final class CF7Source implements Interface_Spam_Source
$reason = $this->detect_reason($submission, $fallback_reason);
$classification = Classifier::classify($fields, ['reason' => $reason]);
$label = $classification['label'];
$decision = self::decide_drop_action($label);
if ($decision['action'] === 'skip') {
Settings::increment_drop_counter($decision['counter_mode']);
return;
}
if (!self::should_enqueue($label, $fields, $submission)) {
return;
}
$post_id = FlamingoHelper::find_post_id_for_submission($submission);
if ($post_id !== null && Queue::exists_for_flamingo($post_id, false)) {
@@ -54,14 +65,19 @@ final class CF7Source implements Interface_Spam_Source
'ip' => $ip,
'user_agent' => $ua,
'is_retro' => false,
'dry_run_drop' => $decision['action'] === 'enqueue_marked',
]);
if ($decision['action'] === 'enqueue_marked') {
Settings::increment_drop_counter($decision['counter_mode']);
}
Queue::enqueue([
'flamingo_post_id' => $post_id,
'form_id' => $form_id,
'source_key' => 'cf7',
'payload_json' => wp_json_encode($payload),
'label' => $classification['label'],
'label' => $label,
'is_retro' => 0,
]);
}
@@ -91,4 +107,30 @@ final class CF7Source implements Interface_Spam_Source
}
return '';
}
/**
* Decides what to do with a classified submission based on its label and the
* dry-run setting. Pure function — no side effects.
*
* @return array{action:'enqueue'|'enqueue_marked'|'skip',counter_mode:?string}
*/
public static function decide_drop_action(string $label): array
{
if ($label !== 'drop') {
return ['action' => 'enqueue', 'counter_mode' => null];
}
if (Settings::is_dry_run_drop()) {
return ['action' => 'enqueue_marked', 'counter_mode' => 'dry_run'];
}
return ['action' => 'skip', 'counter_mode' => 'real'];
}
/**
* Filterable gate — allows external code to block non-drop submissions
* via filter `cf7stg_should_enqueue`.
*/
public static function should_enqueue(string $label, array $fields, $submission): bool
{
return (bool)apply_filters('cf7stg_should_enqueue', true, $label, $fields, $submission);
}
}