getTranslation('slug', $locale, useFallbackLocale: false); // No slug in this language means no address to preview. if (blank($slug)) { return null; } $path = SiteUrls::path($type, $locale, (string) $slug); $token = Crypt::encryptString(json_encode([ 'type' => $type, 'id' => $record->getKey(), 'locale' => $locale, 'expires' => now()->addHours(self::HOURS)->timestamp, ], JSON_THROW_ON_ERROR)); return rtrim((string) config('site.url'), '/').$path.'?preview='.urlencode($token); } /** * What a token says, or null when it is expired, forged or unreadable. * * @return array{type: string, id: int|string, locale: string}|null */ public static function open(string $token): ?array { try { $payload = json_decode(Crypt::decryptString($token), true, flags: JSON_THROW_ON_ERROR); } catch (\Throwable) { return null; } if (! is_array($payload) || ($payload['expires'] ?? 0) < now()->timestamp) { return null; } return [ 'type' => (string) $payload['type'], 'id' => $payload['id'], 'locale' => (string) $payload['locale'], ]; } }