| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\GoogleSheets\Hooks; |
| 4 |
|
| 5 |
use IvyForms\Services\GoogleSheets\GoogleSheetsDispatcherService; |
| 6 |
|
| 7 |
/** |
| 8 |
* Listens to form submissions and dispatches Google Sheets sync. |
| 9 |
*/ |
| 10 |
class GoogleSheetsEntryHooks |
| 11 |
{ |
| 12 |
private GoogleSheetsDispatcherService $dispatcher; |
| 13 |
|
| 14 |
public function __construct(GoogleSheetsDispatcherService $dispatcher) |
| 15 |
{ |
| 16 |
$this->dispatcher = $dispatcher; |
| 17 |
} |
| 18 |
|
| 19 |
public function register(): void |
| 20 |
{ |
| 21 |
add_action('ivyforms/form/after_submission', [$this, 'onFormSubmission'], 10, 4); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @param array<string, mixed> $submissionData |
| 26 |
* @param array<mixed> $formFields |
| 27 |
*/ |
| 28 |
public function onFormSubmission(int $formId, array $submissionData, array $formFields, ?int $entryId): void |
| 29 |
{ |
| 30 |
$formattedSubmissionData = $submissionData; |
| 31 |
$formattedSubmissionData['id'] = $entryId; |
| 32 |
|
| 33 |
// Defer dispatch until after the submission response is sent. WP-Cron is |
| 34 |
// unreliable in containerised environments, and serialising form field |
| 35 |
// entities into transients breaks placeholder resolution on replay. |
| 36 |
add_action('shutdown', function () use ($formId, $formattedSubmissionData, $formFields): void { |
| 37 |
if (function_exists('fastcgi_finish_request')) { |
| 38 |
fastcgi_finish_request(); |
| 39 |
} |
| 40 |
|
| 41 |
$this->dispatcher->dispatchForSubmission( |
| 42 |
$formId, |
| 43 |
$formattedSubmissionData, |
| 44 |
$formFields |
| 45 |
); |
| 46 |
}, 999); |
| 47 |
} |
| 48 |
} |
| 49 |
|