Sichtbarer Name „Kompatibilität“ (Slug compatibility bleibt) in Reiter, Hinweisen, Hilfe-Tab, CLI-Hilfe, Guides und readme. Oben eine Übersicht aller Plugins mit Gesamtzustand, Version, Zustand (Wort und Icon), Freigabe-Art in Klartext und Sprunglink; je Plugin erst Zustand und nächster Schritt, dann Aktionen, dann aufklappbare Details (offen bei Handlungsbedarf, ohne JavaScript). Tabellen mit Beschriftung und Kartenansicht bis 375 px, Buttons mit Plugin-Namen im zugänglichen Namen, Kontraste mit Reserve. Doku: Divi leert beim Prüfen den Cache einmal komplett. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
304 lines
12 KiB
PHP
304 lines
12 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace LIT\MCP\Admin;
|
||
|
||
use Closure;
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
/**
|
||
* Settings page "Einstellungen → LIT MCP" with tabs (spec §8.4). Settings API, no JS framework.
|
||
*/
|
||
final class SettingsPage
|
||
{
|
||
public const SLUG = 'lit-mcp';
|
||
|
||
/** Hook suffix WordPress gives this page (add_options_page → "settings_page_<slug>"). */
|
||
public const SCREEN_HOOK = 'settings_page_' . self::SLUG;
|
||
|
||
/**
|
||
* Filter on the tab list (list<AdminTab>): integrations append their own tabs in Bootable::boot() – Divi its
|
||
* tab "Divi". Applied once, on first use of the tabs (after Plugin booted the integrations).
|
||
*/
|
||
public const TABS_FILTER = 'lit_mcp_admin_tabs';
|
||
|
||
/** @var array<string, AdminTab>|null null = TABS_FILTER not applied yet. */
|
||
private ?array $tabs = null;
|
||
|
||
/**
|
||
* @param list<AdminTab> $baseTabs The plugin's own tabs; the first tab is the default.
|
||
* @param string $logoUrl Loosen-IT logo shown top right of the page ('' = none).
|
||
* @param string $assetsUrl URL of the plugin's assets/ directory ('' = no own CSS/JS).
|
||
* @param string $version Asset version for cache busting.
|
||
* @param (Closure(string): ?string)|null $profileLabel Label of a compatibility profile by ID, null = unknown;
|
||
* names the profile in the compat_* notices.
|
||
*/
|
||
public function __construct(
|
||
private array $baseTabs,
|
||
private string $logoUrl = '',
|
||
private string $assetsUrl = '',
|
||
private string $version = '',
|
||
private ?Closure $profileLabel = null
|
||
) {
|
||
}
|
||
|
||
public function register(): void
|
||
{
|
||
add_action('admin_menu', [$this, 'addMenu']);
|
||
add_action('admin_init', [$this, 'registerSettings']);
|
||
add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']);
|
||
add_filter('removable_query_args', [$this, 'removableQueryArgs']);
|
||
foreach ($this->tabs() as $tab) {
|
||
$tab->registerActions();
|
||
}
|
||
}
|
||
|
||
public function addMenu(): void
|
||
{
|
||
$hook = add_options_page(
|
||
__('LIT MCP', 'wp-lit-mcp'),
|
||
__('LIT MCP', 'wp-lit-mcp'),
|
||
'manage_options',
|
||
self::SLUG,
|
||
[$this, 'render']
|
||
);
|
||
if (is_string($hook)) {
|
||
add_action('load-' . $hook, [$this, 'addHelpTab']);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* WordPress removes these from the address bar after the page loaded, so a reload or a copied link
|
||
* does not show a finished action's notice again.
|
||
*
|
||
* @param array<int, string> $args
|
||
* @return array<int, string>
|
||
*/
|
||
public function removableQueryArgs(array $args): array
|
||
{
|
||
$args[] = 'lit_mcp_notice';
|
||
$args[] = 'lit_mcp_profile';
|
||
|
||
return $args;
|
||
}
|
||
|
||
/** Short "Hilfe" panel at the top right; the tabs explain themselves, this only says where to start. */
|
||
public function addHelpTab(): void
|
||
{
|
||
$screen = get_current_screen();
|
||
if ($screen !== null) {
|
||
$this->helpTabFor($screen);
|
||
}
|
||
}
|
||
|
||
/** @param object $screen A WP_Screen. */
|
||
public function helpTabFor(object $screen): void
|
||
{
|
||
$items = [
|
||
__('Verbinden', 'wp-lit-mcp') => __(
|
||
'Tab „Verbindung“: Server-URL kopieren und im KI-Assistenten als MCP-Server hinzufügen.',
|
||
'wp-lit-mcp'
|
||
),
|
||
__('Wer darf?', 'wp-lit-mcp') => __(
|
||
'Tab „Zugriff“: Rollen festlegen und bestehende Verbindungen widerrufen.',
|
||
'wp-lit-mcp'
|
||
),
|
||
__('Was darf der Assistent?', 'wp-lit-mcp') => __(
|
||
'Tab „Tools“: jedes Tool direkt, indirekt oder aus; ganze Gruppen auf „nur lesen“ stellen.',
|
||
'wp-lit-mcp'
|
||
),
|
||
__('Was ist passiert?', 'wp-lit-mcp') => __(
|
||
'Tab „Protokoll“: jede Änderung des Assistenten mit Benutzer, Ergebnis und Zeitpunkt.',
|
||
'wp-lit-mcp'
|
||
),
|
||
__('Passt es zu Divi, Pods & Co.?', 'wp-lit-mcp') => __(
|
||
'Tab „Kompatibilität“: Fremd-Plugins prüfen, Versionen freigeben und sehen, was gesperrt ist.',
|
||
'wp-lit-mcp'
|
||
),
|
||
];
|
||
$content = '';
|
||
foreach ($items as $title => $text) {
|
||
$content .= sprintf('<p><strong>%s</strong> %s</p>', esc_html($title), esc_html($text));
|
||
}
|
||
|
||
$screen->add_help_tab([
|
||
'id' => 'lit-mcp-overview',
|
||
'title' => __('Überblick', 'wp-lit-mcp'),
|
||
'content' => $content,
|
||
]);
|
||
}
|
||
|
||
public function registerSettings(): void
|
||
{
|
||
foreach ($this->tabs() as $tab) {
|
||
$tab->registerSettings();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Own CSS and a small script, only on this page. The script builds on WordPress' own
|
||
* clipboard.js and wp.a11y.speak (as the media modal's "Copy URL") and postbox.js.
|
||
*/
|
||
public function enqueueAssets(string $hook): void
|
||
{
|
||
if ($hook !== self::SCREEN_HOOK || $this->assetsUrl === '') {
|
||
return;
|
||
}
|
||
|
||
$base = rtrim($this->assetsUrl, '/');
|
||
wp_enqueue_style('lit-mcp-admin', $base . '/css/admin.css', [], $this->version);
|
||
$deps = ['clipboard', 'wp-a11y'];
|
||
if ($this->currentTab() === 'tools') {
|
||
// Collapsible tool groups use WordPress' own postbox toggles (remembered per user).
|
||
$deps[] = 'postbox';
|
||
}
|
||
wp_enqueue_script('lit-mcp-admin', $base . '/js/admin.js', $deps, $this->version, true);
|
||
wp_localize_script('lit-mcp-admin', 'litMcpAdmin', [
|
||
'copied' => __('Die URL wurde in die Zwischenablage kopiert.', 'wp-lit-mcp'),
|
||
/* translators: %d: number of tools matching the filter */
|
||
'toolsShown' => __('%d Tools angezeigt', 'wp-lit-mcp'),
|
||
'toolShown' => __('1 Tool angezeigt', 'wp-lit-mcp'),
|
||
'groupReset' => __('Gruppe auf Standard zurückgesetzt. Noch nicht gespeichert.', 'wp-lit-mcp'),
|
||
]);
|
||
}
|
||
|
||
/** @return list<string> */
|
||
public function tabSlugs(): array
|
||
{
|
||
return array_keys($this->tabs());
|
||
}
|
||
|
||
public function currentTab(): string
|
||
{
|
||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab switch.
|
||
$requested = is_string($_GET['tab'] ?? null) ? sanitize_key(wp_unslash($_GET['tab'])) : '';
|
||
|
||
$tabs = $this->tabs();
|
||
|
||
return isset($tabs[$requested]) ? $requested : (string) array_key_first($tabs);
|
||
}
|
||
|
||
/** @param array<string, string> $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 '<div class="wrap lit-mcp">';
|
||
echo '<div class="lit-mcp-header"><h1>' . esc_html__('LIT MCP', 'wp-lit-mcp') . '</h1>';
|
||
$this->renderLogo();
|
||
// Admin notices of other plugins are moved right after this marker – above the tabs.
|
||
echo '</div><hr class="wp-header-end">';
|
||
$this->renderNotice();
|
||
printf('<nav class="nav-tab-wrapper wp-clearfix" aria-label="%s">', esc_attr__('Bereiche', 'wp-lit-mcp'));
|
||
foreach ($this->tabs() as $slug => $tab) {
|
||
printf(
|
||
'<a href="%s" class="nav-tab%s"%s>%s</a>',
|
||
esc_url(self::url($slug)),
|
||
$slug === $current ? ' nav-tab-active' : '',
|
||
$slug === $current ? ' aria-current="page"' : '',
|
||
esc_html($tab->label())
|
||
);
|
||
}
|
||
echo '</nav>';
|
||
$this->tabs()[$current]->render();
|
||
echo '</div>';
|
||
}
|
||
|
||
/** @return array<string, AdminTab> The plugin's tabs and those the integrations add (TABS_FILTER), by slug. */
|
||
private function tabs(): array
|
||
{
|
||
if ($this->tabs === null) {
|
||
$this->tabs = [];
|
||
foreach ((array) apply_filters(self::TABS_FILTER, $this->baseTabs) as $tab) {
|
||
if ($tab instanceof AdminTab) {
|
||
$this->tabs[$tab->slug()] = $tab;
|
||
}
|
||
}
|
||
}
|
||
|
||
return $this->tabs;
|
||
}
|
||
|
||
private function renderLogo(): void
|
||
{
|
||
if ($this->logoUrl === '') {
|
||
return;
|
||
}
|
||
|
||
printf(
|
||
'<a class="lit-mcp-logo-link" href="https://loosen-it.de" target="_blank" rel="noopener">'
|
||
. '<img src="%s" alt="Loosen-IT Logo" class="lit-mcp-logo">'
|
||
. '<span class="screen-reader-text"> %s</span></a>',
|
||
esc_url($this->logoUrl),
|
||
esc_html__('(öffnet in einem neuen Tab)', 'wp-lit-mcp')
|
||
);
|
||
}
|
||
|
||
/** "<Label>: " for a compat_* notice naming a known profile; the ID from the address bar only looks the label up. */
|
||
private function noticeProfile(string $key): string
|
||
{
|
||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only.
|
||
$id = is_string($_GET['lit_mcp_profile'] ?? null) ? sanitize_key(wp_unslash($_GET['lit_mcp_profile'])) : '';
|
||
$label = str_starts_with($key, 'compat_') && $id !== '' && $this->profileLabel !== null
|
||
? ($this->profileLabel)($id)
|
||
: null;
|
||
|
||
return $label === null ? '' : $label . ': ';
|
||
}
|
||
|
||
private function renderNotice(): void
|
||
{
|
||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only.
|
||
$key = is_string($_GET['lit_mcp_notice'] ?? null) ? sanitize_key(wp_unslash($_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')],
|
||
'compat_checked' => ['success', __('Prüfung abgeschlossen; das Ergebnis steht unten.', 'wp-lit-mcp')],
|
||
'compat_check_running' => [
|
||
'warning',
|
||
__('Es läuft bereits eine Prüfung; bitte in ein paar Minuten erneut versuchen.', 'wp-lit-mcp'),
|
||
],
|
||
'compat_check_failed' => [
|
||
'error',
|
||
__('Die Prüfung ist abgebrochen; Details stehen im PHP-Error-Log.', 'wp-lit-mcp'),
|
||
],
|
||
'compat_released' => ['success', __('Die Version ist für LIT MCP freigegeben.', 'wp-lit-mcp')],
|
||
'compat_release_refused' => [
|
||
'error',
|
||
__(
|
||
// phpcs:ignore Generic.Files.LineLength.TooLong -- WordPress.WP.I18n forbids splitting the text argument.
|
||
'Freigabe nicht möglich: Der letzte Prüfbericht ist nicht grün oder passt nicht mehr zur installierten Version oder zur Plugin-Version. Bitte die Prüfung erneut ausführen.',
|
||
'wp-lit-mcp'
|
||
),
|
||
],
|
||
'compat_withdrawn' => ['success', __('Freigabe zurückgezogen.', 'wp-lit-mcp')],
|
||
'compat_withdraw_failed' => [
|
||
'error',
|
||
__('Für die installierte Version gibt es auf dieser Website keine Freigabe.', 'wp-lit-mcp'),
|
||
],
|
||
default => null,
|
||
};
|
||
|
||
if ($notice !== null) {
|
||
printf(
|
||
'<div class="notice notice-%s is-dismissible"><p>%s</p></div>',
|
||
esc_attr($notice[0]),
|
||
esc_html($this->noticeProfile($key) . $notice[1])
|
||
);
|
||
}
|
||
}
|
||
}
|