sanitize_key($screen), 'type' => in_array($type, self::TYPES, true) ? $type : 'info', 'message' => wp_strip_all_tags($message), 'id' => sanitize_key($test_id), ]; set_transient(self::PREFIX . $user, array_slice($queue, -10), self::TTL); } /** Takes (and forgets) the current user's queued notices for `$screen`. */ public static function take(string $screen): array { $user = (int) get_current_user_id(); if ($user <= 0) { return []; } $queue = get_transient(self::PREFIX . $user); if (!is_array($queue) || !$queue) { return []; } $screen = sanitize_key($screen); $mine = []; $rest = []; foreach ($queue as $notice) { if (is_array($notice) && ($notice['screen'] ?? '') === $screen) { $mine[] = $notice; } else { $rest[] = $notice; } } if ($mine) { if ($rest) { set_transient(self::PREFIX . $user, $rest, self::TTL); } else { delete_transient(self::PREFIX . $user); } } return $mine; } /** Prints (once) the current user's queued notices for `$screen`. Called from inside that screen's markup. */ public static function render(string $screen): void { foreach (self::take($screen) as $notice) { $type = in_array($notice['type'] ?? '', self::TYPES, true) ? $notice['type'] : 'info'; $id = (string) ($notice['id'] ?? ''); printf( '

%3$s

', esc_attr($type), $id !== '' ? ' data-testid="' . esc_attr($id) . '"' : '', esc_html((string) ($notice['message'] ?? '')) ); } } }