*/ private array $tabs = []; /** @param list $tabs The first tab is the default. */ public function __construct(array $tabs) { foreach ($tabs as $tab) { $this->tabs[$tab->slug()] = $tab; } } public function register(): void { add_action('admin_menu', [$this, 'addMenu']); add_action('admin_init', [$this, 'registerSettings']); foreach ($this->tabs as $tab) { $tab->registerActions(); } } public function addMenu(): void { add_options_page( __('LIT MCP', 'wp-lit-mcp'), __('LIT MCP', 'wp-lit-mcp'), 'manage_options', self::SLUG, [$this, 'render'] ); } public function registerSettings(): void { foreach ($this->tabs as $tab) { $tab->registerSettings(); } } /** @return list */ public function tabSlugs(): array { return array_keys($this->tabs); } public function currentTab(): string { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab switch. $requested = isset($_GET['tab']) ? sanitize_key(wp_unslash((string) $_GET['tab'])) : ''; return isset($this->tabs[$requested]) ? $requested : (string) array_key_first($this->tabs); } /** @param array $args */ public static function url(string $tab = '', array $args = []): string { $query = array_merge(['page' => self::SLUG], $tab !== '' ? ['tab' => $tab] : [], $args); return add_query_arg($query, admin_url('options-general.php')); } public function render(): void { if (! current_user_can('manage_options')) { return; } $current = $this->currentTab(); echo '

' . esc_html__('LIT MCP', 'wp-lit-mcp') . '

'; $this->renderNotice(); echo ''; $this->tabs[$current]->render(); echo '
'; } private function renderNotice(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. $key = isset($_GET['lit_mcp_notice']) ? sanitize_key(wp_unslash((string) $_GET['lit_mcp_notice'])) : ''; $notice = match ($key) { 'revoked' => ['success', __('Verbindung widerrufen.', 'wp-lit-mcp')], 'revoke_failed' => ['error', __('Die Verbindung konnte nicht widerrufen werden.', 'wp-lit-mcp')], 'cleared' => ['success', __('Protokoll geleert.', 'wp-lit-mcp')], default => null, }; if ($notice !== null) { printf( '

%s

', esc_attr($notice[0]), esc_html($notice[1]) ); } } }