diff --git a/README.md b/README.md index 95288cb..5a7b9ff 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ The settings are saved on the form itself, so they are version controlled and de ## How it works -When a form submission is created, a listener checks the form's MailerLite settings. If syncing is enabled and a field mapping is present, a queued job sends the subscriber — with the mapped fields and selected group — to MailerLite. Make sure a queue worker is running (or use the `sync` queue driver) for subscribers to be created. +When a form submission is created, a listener checks the form's MailerLite settings. If syncing is enabled and a field mapping is present, a queued job sends the subscriber — with the mapped fields, the selected group, and the submitter's IP address (which MailerLite uses as a consent and spam signal) — to MailerLite. Make sure a queue worker is running (or use the `sync` queue driver) for subscribers to be created. ## Testing diff --git a/src/Jobs/CreateSubscriberJob.php b/src/Jobs/CreateSubscriberJob.php index e9dbe38..c981bc9 100644 --- a/src/Jobs/CreateSubscriberJob.php +++ b/src/Jobs/CreateSubscriberJob.php @@ -22,6 +22,7 @@ public function __construct( private array $fieldMapping, private array $subscriberGroups, private string $apiKey, + private ?string $ipAddress = null, ) {} public function handle(): void @@ -49,6 +50,10 @@ public function handle(): void $data = ['email' => $email]; + if (filled($this->ipAddress)) { + $data['ip_address'] = $this->ipAddress; + } + if (! empty($fields)) { $data['fields'] = $fields; } diff --git a/src/Listeners/SubmissionCreatedListener.php b/src/Listeners/SubmissionCreatedListener.php index d278a76..f3b9aaa 100644 --- a/src/Listeners/SubmissionCreatedListener.php +++ b/src/Listeners/SubmissionCreatedListener.php @@ -36,6 +36,7 @@ public function handle(SubmissionCreated $event): void fieldMapping: array_values($fieldMapping), subscriberGroups: filled($group) ? Arr::wrap($group) : [], apiKey: $apiKey, + ipAddress: request()->ip(), )); } } diff --git a/tests/Feature/SubmissionCreatedListenerTest.php b/tests/Feature/SubmissionCreatedListenerTest.php index 69728b8..4a383ea 100644 --- a/tests/Feature/SubmissionCreatedListenerTest.php +++ b/tests/Feature/SubmissionCreatedListenerTest.php @@ -66,6 +66,24 @@ function jobProperty(CreateSubscriberJob $job, string $name): mixed }); }); +it('passes the submitter ip address to the job', function () { + fakeApiKey('key-123'); + + request()->server->set('REMOTE_ADDR', '203.0.113.7'); + + $event = submissionEvent([ + 'mailerlite_enabled' => true, + 'mailerlite_field_mapping' => [['form_field' => 'email', 'mailerlite_field' => 'email']], + ], ['email' => 'a@b.com']); + + (new SubmissionCreatedListener)->handle($event); + + Queue::assertPushed( + CreateSubscriberJob::class, + fn (CreateSubscriberJob $job) => jobProperty($job, 'ipAddress') === '203.0.113.7', + ); +}); + it('wraps a single selected group into an array', function () { fakeApiKey('key-123');