Files
WP-Business-Forum/admin/forum-setup.php
2026-03-21 00:54:20 +01:00

373 lines
20 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Setup-Wizard — läuft einmalig nach der Aktivierung
* Führt den WP-Admin durch die Superadmin-Erstellung
*/
class WBF_Setup {
const OPTION_DONE = 'wbf_setup_complete';
public static function init() {
// Nach Aktivierung zur Setup-Seite weiterleiten
add_action( 'admin_init', [ __CLASS__, 'maybe_redirect' ] );
add_action( 'admin_menu', [ __CLASS__, 'register_page' ] );
add_action( 'admin_notices',[ __CLASS__, 'setup_notice' ] );
}
/** Einmalige Weiterleitung direkt nach Aktivierung */
public static function maybe_redirect() {
if ( get_transient('wbf_activation_redirect') ) {
delete_transient('wbf_activation_redirect');
if ( ! self::superadmin_exists() && current_user_can('manage_options') ) {
wp_redirect( admin_url('admin.php?page=wbf-setup') );
exit;
}
}
}
/** Prüft ob bereits ein Superadmin im Forum existiert */
public static function superadmin_exists() {
global $wpdb;
$count = $wpdb->get_var(
"SELECT COUNT(*) FROM {$wpdb->prefix}forum_users WHERE role='superadmin'"
);
return (int)$count > 0;
}
/** Hinweisbanner solange kein Superadmin existiert */
public static function setup_notice() {
if ( self::superadmin_exists() ) return;
if ( isset($_GET['page']) && $_GET['page'] === 'wbf-setup' ) return;
if ( ! current_user_can('manage_options') ) return;
echo '<div class="notice notice-warning" style="display:flex;align-items:center;gap:16px;padding:12px 16px">
<span style="font-size:1.5rem">⚙️</span>
<div>
<strong>WP Business Forum</strong> — Einrichtung noch nicht abgeschlossen.
<a href="'.admin_url('admin.php?page=wbf-setup').'" class="button button-primary" style="margin-left:12px">Jetzt einrichten</a>
</div>
</div>';
}
/** Versteckte Admin-Seite für den Wizard */
public static function register_page() {
add_submenu_page(
null, // kein Menüeintrag — nur direkt aufrufbar
'Forum Einrichtung',
'Forum Einrichtung',
'manage_options',
'wbf-setup',
[ __CLASS__, 'render_page' ]
);
}
/** Wizard verarbeiten & anzeigen */
public static function render_page() {
$step = (int)( $_GET['step'] ?? 1 );
$error = '';
$success = '';
// ── Schritt 1: Superadmin-Konto erstellen ─────────────────────────────
if ( $step === 1 && isset($_POST['wbf_setup_step1']) && check_admin_referer('wbf_setup_nonce') ) {
$username = sanitize_user( $_POST['username'] ?? '' );
$display_name = sanitize_text_field( $_POST['display_name'] ?? '' );
$email = sanitize_email( $_POST['email'] ?? '' );
$password = $_POST['password'] ?? '';
$password2 = $_POST['password2'] ?? '';
if ( strlen($username) < 3 ) $error = 'Benutzername mindestens 3 Zeichen.';
elseif ( empty($display_name) ) $error = 'Anzeigename darf nicht leer sein.';
elseif ( ! is_email($email) ) $error = 'Ungültige E-Mail-Adresse.';
elseif ( strlen($password) < 6 ) $error = 'Passwort mindestens 6 Zeichen.';
elseif ( $password !== $password2) $error = 'Passwörter stimmen nicht überein.';
elseif ( WBF_DB::get_user_by('username', $username) ) $error = 'Benutzername bereits vergeben.';
elseif ( WBF_DB::get_user_by('email', $email) ) $error = 'E-Mail bereits registriert. <a href="'.admin_url('admin.php?page=wbf-setup&step=1&use_existing=1').'">Bestehendes Konto als Superadmin setzen?</a>';
if ( ! $error ) {
$avatar = 'https://www.gravatar.com/avatar/' . md5(strtolower($email)) . '?d=identicon&s=120';
$id = WBF_DB::create_user([
'username' => $username,
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'display_name' => $display_name,
'avatar_url' => $avatar,
'role' => 'superadmin',
]);
if ($id) {
update_option('wbf_superadmin_email', $email);
wp_redirect( admin_url('admin.php?page=wbf-setup&step=2') );
exit;
} else {
$error = 'Fehler beim Erstellen des Kontos. Bitte versuche es erneut.';
}
}
}
// ── Bestehendes Konto hochstufen ──────────────────────────────────────
if ( $step === 1 && isset($_POST['wbf_setup_promote']) && check_admin_referer('wbf_setup_nonce') ) {
$email = sanitize_email($_POST['existing_email'] ?? '');
$user = WBF_DB::get_user_by('email', $email);
if ($user) {
WBF_DB::update_user($user->id, ['role' => 'superadmin']);
update_option('wbf_superadmin_email', $email);
wp_redirect( admin_url('admin.php?page=wbf-setup&step=2') );
exit;
} else {
$error = 'Kein Forum-Konto mit dieser E-Mail gefunden.';
}
}
// ── Schritt 2: Forum-Seite erstellen (optional) ───────────────────────
if ( $step === 2 && isset($_POST['wbf_setup_step2']) && check_admin_referer('wbf_setup_nonce') ) {
$create_page = ! empty($_POST['create_forum_page']);
$page_title = sanitize_text_field($_POST['page_title'] ?? 'Forum');
if ($create_page) {
$existing = get_posts(['post_type'=>'page','s'=>$page_title,'posts_per_page'=>1]);
if (empty($existing)) {
$page_id = wp_insert_post([
'post_title' => $page_title,
'post_content' => '[business_forum]',
'post_status' => 'publish',
'post_type' => 'page',
]);
if ($page_id) {
update_option('wbf_forum_page_id', $page_id);
$success = get_permalink($page_id);
}
} else {
$success = get_permalink($existing[0]->ID);
}
}
update_option(self::OPTION_DONE, true);
wp_redirect( admin_url('admin.php?page=wbf-setup&step=3&forum_url='.urlencode($success)) );
exit;
}
// ── Schritt 3: Überspringen ───────────────────────────────────────────
if ( $step === 3 ) {
update_option(self::OPTION_DONE, true);
}
self::render_wizard($step, $error, isset($_GET['use_existing']));
}
private static function render_wizard($step, $error = '', $show_promote = false) {
$wp_user = wp_get_current_user();
$forum_url = urldecode($_GET['forum_url'] ?? '');
$forum_page = get_option('wbf_forum_page_id') ? get_permalink(get_option('wbf_forum_page_id')) : '';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Forum Einrichtung</title>
<style>
* { box-sizing:border-box; margin:0; padding:0; }
body { background:#0f1117; font-family:-apple-system,Inter,sans-serif; min-height:100vh; display:flex; align-items:center; justify-content:center; padding:2rem; }
.wbf-wizard { background:#1e2330; border:1px solid rgba(255,255,255,.08); border-radius:16px; max-width:560px; width:100%; box-shadow:0 24px 60px rgba(0,0,0,.5); overflow:hidden; }
.wbf-wizard__header { background:linear-gradient(135deg,#0d1117,#1a2540); padding:2rem 2.5rem; border-bottom:1px solid rgba(0,180,216,.15); }
.wbf-wizard__logo { display:flex; align-items:center; gap:.75rem; margin-bottom:1.5rem; }
.wbf-wizard__logo-icon { width:44px; height:44px; border-radius:10px; background:rgba(0,180,216,.15); border:1px solid rgba(0,180,216,.3); display:flex; align-items:center; justify-content:center; font-size:1.3rem; color:#00b4d8; }
.wbf-wizard__logo-text { font-size:1.1rem; font-weight:700; color:#fff; }
.wbf-wizard__logo-text small { display:block; font-size:.75rem; font-weight:400; color:rgba(255,255,255,.45); }
.wbf-steps { display:flex; gap:0; }
.wbf-step { flex:1; text-align:center; padding:.5rem; font-size:.72rem; font-weight:600; text-transform:uppercase; letter-spacing:.06em; color:rgba(255,255,255,.3); border-bottom:2px solid transparent; transition:.2s; }
.wbf-step.active { color:#00b4d8; border-color:#00b4d8; }
.wbf-step.done { color:rgba(0,180,216,.5); border-color:rgba(0,180,216,.25); }
.wbf-wizard__body { padding:2rem 2.5rem 2.5rem; }
.wbf-wizard__title { font-size:1.3rem; font-weight:700; color:#fff; margin-bottom:.4rem; }
.wbf-wizard__sub { font-size:.875rem; color:rgba(255,255,255,.5); margin-bottom:1.75rem; line-height:1.6; }
.wbf-field { margin-bottom:1rem; }
.wbf-field label { display:block; font-size:.75rem; font-weight:700; color:rgba(255,255,255,.55); text-transform:uppercase; letter-spacing:.05em; margin-bottom:.4rem; }
.wbf-field input[type=text],
.wbf-field input[type=email],
.wbf-field input[type=password] {
width:100%; background:#111318; border:1.5px solid rgba(255,255,255,.12);
border-radius:8px; padding:.7rem 1rem; color:#e8eaf0;
font-size:.9rem; font-family:inherit; transition:.2s;
}
.wbf-field input:focus { outline:none; border-color:#00b4d8; box-shadow:0 0 0 3px rgba(0,180,216,.1); }
.wbf-field input::placeholder { color:rgba(255,255,255,.25); }
.wbf-field-row { display:grid; grid-template-columns:1fr 1fr; gap:.75rem; }
.wbf-prefill { font-size:.72rem; color:rgba(0,180,216,.7); margin-top:.3rem; }
.wbf-hint { font-size:.75rem; color:rgba(255,255,255,.35); margin-top:.3rem; }
.wbf-error { background:rgba(240,82,82,.1); border:1px solid rgba(240,82,82,.3); border-radius:8px; padding:.75rem 1rem; color:#f87171; font-size:.85rem; margin-bottom:1.25rem; }
.wbf-success-box { background:rgba(86,207,126,.08); border:1px solid rgba(86,207,126,.25); border-radius:8px; padding:.75rem 1rem; color:#56cf7e; font-size:.85rem; margin-bottom:1.25rem; display:flex; align-items:center; gap:.5rem; }
.wbf-btn-primary { width:100%; background:#00b4d8; color:#fff; border:none; border-radius:8px; padding:.85rem; font-size:.95rem; font-weight:700; cursor:pointer; font-family:inherit; margin-top:.5rem; transition:.2s; box-shadow:0 0 20px rgba(0,180,216,.25); }
.wbf-btn-primary:hover { background:#0096c7; box-shadow:0 0 28px rgba(0,180,216,.4); }
.wbf-btn-secondary { width:100%; background:transparent; color:rgba(255,255,255,.4); border:1.5px solid rgba(255,255,255,.1); border-radius:8px; padding:.65rem; font-size:.82rem; cursor:pointer; font-family:inherit; margin-top:.5rem; transition:.2s; }
.wbf-btn-secondary:hover { border-color:rgba(255,255,255,.3); color:rgba(255,255,255,.7); }
.wbf-divider { text-align:center; color:rgba(255,255,255,.2); font-size:.75rem; margin:1.25rem 0; position:relative; }
.wbf-divider::before { content:''; position:absolute; top:50%; left:0; right:0; height:1px; background:rgba(255,255,255,.08); }
.wbf-divider span { background:#1e2330; padding:0 .75rem; position:relative; }
.wbf-checkbox-row { display:flex; align-items:center; gap:.75rem; padding:.85rem 1rem; background:#111318; border:1.5px solid rgba(255,255,255,.1); border-radius:8px; cursor:pointer; margin-bottom:1rem; }
.wbf-checkbox-row input { width:18px; height:18px; accent-color:#00b4d8; flex-shrink:0; }
.wbf-checkbox-row div { flex:1; }
.wbf-checkbox-row strong { display:block; font-size:.875rem; color:#e8eaf0; }
.wbf-checkbox-row span { font-size:.78rem; color:rgba(255,255,255,.4); }
.wbf-complete { text-align:center; padding:1rem 0; }
.wbf-complete__icon { font-size:4rem; margin-bottom:1rem; }
.wbf-complete__title { font-size:1.5rem; font-weight:700; color:#fff; margin-bottom:.5rem; }
.wbf-complete__sub { color:rgba(255,255,255,.5); font-size:.9rem; margin-bottom:2rem; line-height:1.6; }
.wbf-link-box { background:#111318; border:1px solid rgba(0,180,216,.2); border-radius:8px; padding:.85rem 1rem; display:flex; align-items:center; justify-content:space-between; gap:.75rem; margin-bottom:1rem; }
.wbf-link-box a { color:#00b4d8; font-size:.875rem; word-break:break-all; }
.wbf-superadmin-badge { display:inline-flex; align-items:center; gap:.4rem; padding:.3rem .85rem; background:rgba(225,29,72,.15); border:1px solid rgba(225,29,72,.3); border-radius:4px; color:#f87171; font-size:.75rem; font-weight:700; text-transform:uppercase; letter-spacing:.05em; margin-bottom:1.25rem; }
</style>
</head>
<body>
<div class="wbf-wizard">
<div class="wbf-wizard__header">
<div class="wbf-wizard__logo">
<div class="wbf-wizard__logo-icon">💬</div>
<div class="wbf-wizard__logo-text">
WP Business Forum
<small>Einrichtungs-Assistent</small>
</div>
</div>
<div class="wbf-steps">
<div class="wbf-step <?php echo $step>=1?($step>1?'done':'active'):''; ?>">1 · Superadmin</div>
<div class="wbf-step <?php echo $step>=2?($step>2?'done':'active'):''; ?>">2 · Forum-Seite</div>
<div class="wbf-step <?php echo $step>=3?'active':''; ?>">3 · Fertig</div>
</div>
</div>
<div class="wbf-wizard__body">
<?php if ($error): ?>
<div class="wbf-error">⚠️ <?php echo $error; ?></div>
<?php endif; ?>
<?php // ══ SCHRITT 1 ══════════════════════════════════════════════════════
if ($step === 1 && !$show_promote): ?>
<p class="wbf-wizard__title">Dein Superadmin-Konto</p>
<p class="wbf-wizard__sub">Erstelle dein persönliches Forum-Konto. Als WordPress-Administrator bekommst du automatisch die Superadmin-Rolle — dauerhaft und unveränderlich.</p>
<div class="wbf-superadmin-badge"><i>👑</i> Superadmin wird automatisch zugewiesen</div>
<form method="post">
<?php wp_nonce_field('wbf_setup_nonce'); ?>
<div class="wbf-field-row">
<div class="wbf-field">
<label>Benutzername</label>
<input type="text" name="username" value="<?php echo esc_attr($wp_user->user_login); ?>" required>
<p class="wbf-prefill">↑ Aus deinem WP-Konto vorausgefüllt</p>
</div>
<div class="wbf-field">
<label>Anzeigename</label>
<input type="text" name="display_name" value="<?php echo esc_attr($wp_user->display_name); ?>" required>
</div>
</div>
<div class="wbf-field">
<label>E-Mail</label>
<input type="email" name="email" value="<?php echo esc_attr($wp_user->user_email); ?>" required>
<p class="wbf-hint">Nutze deine WP-Admin-E-Mail — das verknüpft dein Forum-Konto dauerhaft mit dem Superadmin-Status.</p>
</div>
<div class="wbf-field-row">
<div class="wbf-field">
<label>Passwort</label>
<input type="password" name="password" placeholder="Min. 6 Zeichen" required>
</div>
<div class="wbf-field">
<label>Passwort wiederholen</label>
<input type="password" name="password2" placeholder="Wiederholen" required>
</div>
</div>
<button type="submit" name="wbf_setup_step1" class="wbf-btn-primary">👑 Superadmin-Konto erstellen →</button>
</form>
<div class="wbf-divider"><span>oder</span></div>
<form method="get">
<input type="hidden" name="page" value="wbf-setup">
<input type="hidden" name="step" value="1">
<input type="hidden" name="use_existing" value="1">
<button type="submit" class="wbf-btn-secondary">Ich habe bereits ein Forum-Konto →</button>
</form>
<?php // ══ BESTEHEND HOCHSTUFEN ═══════════════════════════════════════════
elseif ($step === 1 && $show_promote): ?>
<p class="wbf-wizard__title">Bestehendes Konto hochstufen</p>
<p class="wbf-wizard__sub">Gib die E-Mail-Adresse deines vorhandenen Forum-Kontos ein. Es wird sofort auf Superadmin hochgestuft.</p>
<form method="post">
<?php wp_nonce_field('wbf_setup_nonce'); ?>
<div class="wbf-field">
<label>E-Mail deines Forum-Kontos</label>
<input type="email" name="existing_email" value="<?php echo esc_attr($wp_user->user_email); ?>" required>
</div>
<button type="submit" name="wbf_setup_promote" class="wbf-btn-primary">👑 Auf Superadmin hochstufen →</button>
</form>
<form method="get" style="margin-top:.5rem">
<input type="hidden" name="page" value="wbf-setup">
<input type="hidden" name="step" value="1">
<button type="submit" class="wbf-btn-secondary">← Zurück</button>
</form>
<?php // ══ SCHRITT 2 ══════════════════════════════════════════════════════
elseif ($step === 2): ?>
<div class="wbf-success-box">✓ Superadmin-Konto erstellt!</div>
<p class="wbf-wizard__title">Forum-Seite einrichten</p>
<p class="wbf-wizard__sub">Soll eine WordPress-Seite mit dem Forum-Shortcode automatisch erstellt werden?</p>
<form method="post">
<?php wp_nonce_field('wbf_setup_nonce'); ?>
<label class="wbf-checkbox-row">
<input type="checkbox" name="create_forum_page" value="1" checked>
<div>
<strong>Forum-Seite automatisch erstellen</strong>
<span>Erstellt eine neue Seite mit dem Shortcode [business_forum]</span>
</div>
</label>
<div class="wbf-field">
<label>Seiten-Titel</label>
<input type="text" name="page_title" value="Forum">
</div>
<button type="submit" name="wbf_setup_step2" class="wbf-btn-primary">Weiter →</button>
</form>
<form method="post" style="margin-top:.5rem">
<?php wp_nonce_field('wbf_setup_nonce'); ?>
<button type="submit" name="wbf_setup_step2" class="wbf-btn-secondary">Überspringen — ich erstelle die Seite selbst</button>
</form>
<?php // ══ SCHRITT 3 ══════════════════════════════════════════════════════
elseif ($step === 3): ?>
<div class="wbf-complete">
<div class="wbf-complete__icon">🎉</div>
<p class="wbf-complete__title">Forum ist bereit!</p>
<p class="wbf-complete__sub">Dein Superadmin-Konto ist aktiv. Das Forum ist einsatzbereit — viel Spaß mit deiner Community!</p>
</div>
<?php $url = $forum_url ?: $forum_page; ?>
<?php if ($url): ?>
<div class="wbf-link-box">
<a href="<?php echo esc_url($url); ?>" target="_blank"><?php echo esc_html($url); ?></a>
<a href="<?php echo esc_url($url); ?>" target="_blank" style="white-space:nowrap;color:#00b4d8">→ Öffnen</a>
</div>
<?php endif; ?>
<a href="<?php echo admin_url('admin.php?page=wbf-admin'); ?>" class="wbf-btn-primary" style="display:block;text-align:center;text-decoration:none;padding:.85rem;border-radius:8px;background:#00b4d8;color:#fff;font-weight:700;margin-top:.25rem">
Zum Forum-Dashboard →
</a>
<?php endif; ?>
</div>
</div>
</body>
</html>
<?php
}
}
WBF_Setup::init();