diff --git a/wp-content/plugins/download-gateway/includes/class-download-controller.php b/wp-content/plugins/download-gateway/includes/class-download-controller.php index fd0984db..8960cb77 100644 --- a/wp-content/plugins/download-gateway/includes/class-download-controller.php +++ b/wp-content/plugins/download-gateway/includes/class-download-controller.php @@ -202,13 +202,14 @@ private function get_file_url( (int) $event_id, $endpoint, array( - 'type' => 'download', - 'event_id' => (int) $event_id, - 'person_id' => $person_id, - 'post_id' => $post_id, - 'post_type' => $post_type, - 'policy' => PolicyResolver::resolve( $post_id ), - 'created_at' => current_time( 'mysql' ), + 'type' => 'download', + 'event_id' => (int) $event_id, + 'person_id' => $person_id, + 'post_id' => $post_id, + 'post_type' => $post_type, + 'airtable_record_id' => get_post_meta( $post_id, '_airtable_record_id', true ) ?: null, + 'policy' => PolicyResolver::resolve( $post_id ), + 'created_at' => current_time( 'mysql' ), ) ); } diff --git a/wp-content/plugins/download-gateway/includes/class-download-event-repository.php b/wp-content/plugins/download-gateway/includes/class-download-event-repository.php index 13f1a66a..c1e734c2 100644 --- a/wp-content/plugins/download-gateway/includes/class-download-event-repository.php +++ b/wp-content/plugins/download-gateway/includes/class-download-event-repository.php @@ -84,4 +84,34 @@ public static function log( array $data ): int|false { return $wpdb->insert_id; } + + /** + * Return the ID of the most recent redirect event for a person + post. + * + * Used by IntakeController to include the download event ID in the intake + * webhook payload so Make.com can update the correct Downloads record. + * + * @param int $person_id Person ID from wp_gateway_people. + * @param int $post_id Post ID of the downloaded resource. + * @return int|null Row ID, or null if no matching redirect event found. + */ + public static function find_redirect_id( int $person_id, int $post_id ): ?int { + global $wpdb; + + $id = $wpdb->get_var( + $wpdb->prepare( + "SELECT id FROM {$wpdb->prefix}gateway_download_events + WHERE person_id = %d + AND post_id = %d + AND event_type = %s + ORDER BY created_at DESC + LIMIT 1", + $person_id, + $post_id, + self::EVENT_REDIRECT + ) + ); + + return null !== $id ? (int) $id : null; + } } diff --git a/wp-content/plugins/download-gateway/includes/class-intake-controller.php b/wp-content/plugins/download-gateway/includes/class-intake-controller.php index f92a5923..7cbcfe4a 100644 --- a/wp-content/plugins/download-gateway/includes/class-intake-controller.php +++ b/wp-content/plugins/download-gateway/includes/class-intake-controller.php @@ -101,13 +101,15 @@ public function submit( int $post_id, string $person_cookie, string $nonce, arra (int) $intake_id, $endpoint, array( - 'type' => 'intake', - 'person_id' => $person_id, - 'post_id' => $post_id, - 'post_type' => $post_type, - 'intake_set' => $intake_set, - 'responses' => $sanitized, - 'created_at' => current_time( 'mysql' ), + 'type' => 'intake', + 'person_id' => $person_id, + 'download_event_id' => DownloadEventRepository::find_redirect_id( $person_id, $post_id ), + 'post_id' => $post_id, + 'post_type' => $post_type, + 'airtable_record_id' => get_post_meta( $post_id, '_airtable_record_id', true ) ?: null, + 'intake_set' => $intake_set, + 'responses' => $sanitized, + 'created_at' => current_time( 'mysql' ), ) ); }