feat: show new API key once via transient after creation
This commit is contained in:
@@ -4,14 +4,12 @@ namespace AskTamMCP\Admin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Handles admin-post.php form submissions for key create/revoke from the WP admin UI.
|
||||
*/
|
||||
class AdminActions {
|
||||
|
||||
public function __construct() {
|
||||
add_action('admin_post_asktam_mcp_create_key', [$this, 'handleCreate']);
|
||||
add_action('admin_post_asktam_mcp_revoke_key', [$this, 'handleRevoke']);
|
||||
add_action('admin_notices', [$this, 'showNotices']);
|
||||
}
|
||||
|
||||
public function handleCreate(): void {
|
||||
@@ -22,7 +20,8 @@ class AdminActions {
|
||||
$scopes = array_map('sanitize_text_field', (array) ($_POST['scopes'] ?? ['read']));
|
||||
|
||||
if (empty($name)) {
|
||||
wp_redirect(add_query_arg(['mcp_error' => 'name_required'], admin_url('admin.php?page=asktam-mcp')));
|
||||
set_transient('asktam_mcp_notice', ['type' => 'error', 'message' => 'Key name is required.'], 60);
|
||||
wp_redirect(admin_url('admin.php?page=asktam-mcp'));
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -38,7 +37,10 @@ class AdminActions {
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
wp_redirect(add_query_arg(['mcp_created' => 1], admin_url('admin.php?page=asktam-mcp')));
|
||||
// Store raw key in transient so we can show it once on next page load
|
||||
set_transient('asktam_mcp_new_key_' . get_current_user_id(), $rawKey, 120);
|
||||
|
||||
wp_redirect(admin_url('admin.php?page=asktam-mcp&mcp_created=1'));
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -51,7 +53,39 @@ class AdminActions {
|
||||
$table = $wpdb->prefix . 'asktam_mcp_keys';
|
||||
$wpdb->update($table, ['is_active' => 0], ['id' => $id]);
|
||||
|
||||
wp_redirect(add_query_arg(['mcp_revoked' => 1], admin_url('admin.php?page=asktam-mcp')));
|
||||
set_transient('asktam_mcp_notice', ['type' => 'success', 'message' => 'API key revoked.'], 60);
|
||||
wp_redirect(admin_url('admin.php?page=asktam-mcp'));
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show new-key banner via admin_notices so the raw key appears once.
|
||||
*/
|
||||
public function showNotices(): void {
|
||||
$screen = get_current_screen();
|
||||
if (!$screen || strpos($screen->id, 'asktam-mcp') === false) return;
|
||||
|
||||
// General notices (errors, simple confirmations)
|
||||
$notice = get_transient('asktam_mcp_notice');
|
||||
if ($notice) {
|
||||
delete_transient('asktam_mcp_notice');
|
||||
$class = $notice['type'] === 'error' ? 'notice-error' : 'notice-success';
|
||||
printf('<div class="notice %s is-dismissible"><p>%s</p></div>', esc_attr($class), esc_html($notice['message']));
|
||||
}
|
||||
|
||||
// New key reveal (shown once)
|
||||
$newKey = get_transient('asktam_mcp_new_key_' . get_current_user_id());
|
||||
if ($newKey && isset($_GET['mcp_created'])) {
|
||||
delete_transient('asktam_mcp_new_key_' . get_current_user_id());
|
||||
?>
|
||||
<div class="notice notice-success mcp-new-key-banner" style="display:block;padding:14px 18px;">
|
||||
<p><strong>✓ API key created. Copy it now — it won't be shown in full again.</strong></p>
|
||||
<p style="font-family:monospace;font-size:14px;background:#f0fdf4;border:1px solid #86efac;padding:10px 14px;border-radius:4px;word-break:break-all;">
|
||||
<?php echo esc_html($newKey); ?>
|
||||
<button onclick="navigator.clipboard.writeText('<?php echo esc_js($newKey); ?>');this.textContent='✓ Copied!'" class="button button-small" style="margin-left:10px;">Copy</button>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user