Files

92 lines
3.7 KiB
PHP

<?php
namespace AskTamMCP\Admin;
defined('ABSPATH') || exit;
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 {
if (!current_user_can('manage_options')) wp_die('Unauthorized');
check_admin_referer('asktam_mcp_create_key', '_wpnonce_mcp');
$name = sanitize_text_field($_POST['key_name'] ?? '');
$scopes = array_map('sanitize_text_field', (array) ($_POST['scopes'] ?? ['read']));
if (empty($name)) {
set_transient('asktam_mcp_notice', ['type' => 'error', 'message' => 'Key name is required.'], 60);
wp_redirect(admin_url('admin.php?page=asktam-mcp'));
exit;
}
global $wpdb;
$table = $wpdb->prefix . 'asktam_mcp_keys';
$rawKey = \AskTamMCP\Core\Auth::generateKey();
$wpdb->insert($table, [
'name' => $name,
'api_key' => $rawKey,
'scopes' => implode(',', $scopes),
'created_at' => current_time('mysql'),
'is_active' => 1,
]);
// 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;
}
public function handleRevoke(): void {
if (!current_user_can('manage_options')) wp_die('Unauthorized');
$id = (int) ($_POST['key_id'] ?? 0);
check_admin_referer('asktam_mcp_revoke_' . $id, '_wpnonce_mcp');
global $wpdb;
$table = $wpdb->prefix . 'asktam_mcp_keys';
$wpdb->update($table, ['is_active' => 0], ['id' => $id]);
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
}
}
}