Per-request cache, keyed by user id. */ private static $cache = []; private static function key(): string { return Plugin::PREFIX_ . 'flash_' . get_current_user_id(); } public static function all(): array { $userId = get_current_user_id(); if (!isset(self::$cache[$userId])) { $stored = get_transient(self::key()); self::$cache[$userId] = is_array($stored) ? $stored : []; } return self::$cache[$userId]; } public static function get($item) { $all = self::all(); return array_key_exists($item, $all) ? $all[$item] : false; } /** * Replaces everything that was flashed before. */ public static function add(array $items): void { self::$cache[get_current_user_id()] = $items; set_transient(self::key(), $items, self::TTL); } public static function delete($item): void { $all = self::all(); unset($all[$item]); self::add($all); } public static function destroy(): void { self::$cache[get_current_user_id()] = []; delete_transient(self::key()); } /** * Uninstall: remove the flash data of every user and the old global session option. */ public static function purgeAll(): void { global $wpdb; $prefix = '_transient_' . Plugin::PREFIX_ . 'flash_'; $names = $wpdb->get_col( $wpdb->prepare("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like($prefix) . '%') ); // delete_transient() also clears the timeout row and the object cache. foreach ($names as $name) { delete_transient(substr($name, strlen('_transient_'))); } delete_option(Plugin::PREFIX_ . 'session_handler'); } }