table = \AKSRT\Core\DBMigrator::table('identity_event'); } /** @param array $event */ public function record(array $event): bool { global $wpdb; $summary = is_array($event['summary'] ?? null) ? $event['summary'] : []; $row = [ 'application_id' => (int) ($event['application_id'] ?? 0), 'event_uuid' => wp_generate_uuid4(), 'source' => sanitize_key((string) ($event['source'] ?? 'system')), 'event_type' => sanitize_key((string) ($event['event_type'] ?? 'status_changed')), 'external_event_id' => sanitize_text_field((string) ($event['external_event_id'] ?? '')), 'dedupe_key' => (string) ($event['dedupe_key'] ?? hash('sha256', wp_generate_uuid4())), 'from_status' => sanitize_key((string) ($event['from_status'] ?? '')), 'to_status' => sanitize_key((string) ($event['to_status'] ?? '')), 'result_code' => sanitize_text_field((string) ($event['result_code'] ?? '')), 'summary_json' => $summary !== [] ? wp_json_encode($summary) : null, 'actor_user_id' => !empty($event['actor_user_id']) ? (int) $event['actor_user_id'] : null, 'created_at' => current_time('mysql', true), ]; $inserted = $wpdb->insert($this->table, $row); if ($inserted === 1) { return true; } return str_contains(strtolower((string) $wpdb->last_error), 'duplicate'); } /** @return array> */ public function for_application(int $application_id): array { global $wpdb; $rows = $wpdb->get_results($wpdb->prepare( "SELECT * FROM {$this->table} WHERE application_id = %d ORDER BY id DESC", $application_id ), ARRAY_A); return is_array($rows) ? $rows : []; } }