260 lines
12 KiB
PHP
260 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: WP Business Forum
|
|
* Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Business-Forum
|
|
* Description: Professionelles Forum mit eigenem Login, Rollen, Signaturen, Hierarchie und Moderations-Tools.
|
|
* Version: 1.0.2
|
|
* Author: M_Viper
|
|
* Author URI: https://m-viper.de
|
|
* Text Domain: wp-business-forum
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit;
|
|
|
|
define( 'WBF_PATH', plugin_dir_path( __FILE__ ) );
|
|
define( 'WBF_URL', plugin_dir_url( __FILE__ ) );
|
|
define( 'WBF_VERSION', '1.0.2' );
|
|
|
|
require_once WBF_PATH . 'includes/class-forum-db.php';
|
|
require_once WBF_PATH . 'includes/class-forum-roles.php';
|
|
require_once WBF_PATH . 'includes/class-forum-levels.php';
|
|
require_once WBF_PATH . 'includes/class-forum-bbcode.php';
|
|
require_once WBF_PATH . 'includes/class-forum-auth.php';
|
|
require_once WBF_PATH . 'includes/class-forum-shortcodes.php';
|
|
require_once WBF_PATH . 'includes/class-forum-ajax.php';
|
|
require_once WBF_PATH . 'includes/class-forum-export.php';
|
|
require_once WBF_PATH . 'admin/forum-admin.php';
|
|
require_once WBF_PATH . 'admin/forum-settings.php';
|
|
require_once WBF_PATH . 'admin/forum-setup.php';
|
|
|
|
// ── Aktivierung ────────────────────────────────────────────────────────────────
|
|
register_activation_hook( __FILE__, function() {
|
|
WBF_DB::install();
|
|
// Transient für einmalige Weiterleitung zum Setup-Wizard setzen
|
|
set_transient( 'wbf_activation_redirect', true, 30 );
|
|
});
|
|
|
|
// ── Export / Import Hooks ─────────────────────────────────────────────────────
|
|
add_action( 'plugins_loaded', function() {
|
|
WBF_Export::hooks();
|
|
}, 5 );
|
|
|
|
// ── Superadmin-Sync ───────────────────────────────────────────────────────────
|
|
add_action( 'wp_login', function() { WBF_Roles::sync_superadmin(); } );
|
|
add_action( 'init', function() { WBF_Roles::sync_superadmin(); } );
|
|
|
|
// ── Body-Klasse ───────────────────────────────────────────────────────────────
|
|
add_filter( 'body_class', function( $classes ) {
|
|
global $post;
|
|
if ( $post && has_shortcode( $post->post_content, 'business_forum' ) ) {
|
|
$classes[] = 'wbf-forum-page';
|
|
}
|
|
return $classes;
|
|
});
|
|
|
|
// ── Cron: Abgelaufene Sperren aufheben ────────────────────────────────────────
|
|
add_action( 'wbf_check_expired_bans', function() {
|
|
WBF_DB::check_expired_bans();
|
|
} );
|
|
|
|
if ( ! wp_next_scheduled( 'wbf_check_expired_bans' ) ) {
|
|
wp_schedule_event( time(), 'hourly', 'wbf_check_expired_bans' );
|
|
}
|
|
|
|
register_deactivation_hook( __FILE__, function() {
|
|
wp_clear_scheduled_hook( 'wbf_check_expired_bans' );
|
|
wp_clear_scheduled_hook( 'wbf_check_for_updates' );
|
|
} );
|
|
|
|
|
|
// ── Forum-URL Hilfsfunktion ───────────────────────────────────────────────────
|
|
function wbf_get_forum_url() {
|
|
// 1. Gespeicherte Seite aus dem Setup-Wizard
|
|
$page_id = get_option('wbf_forum_page_id');
|
|
if ( $page_id ) {
|
|
$url = get_permalink( $page_id );
|
|
if ( $url ) return $url;
|
|
}
|
|
// 2. Fallback: Seite mit [business_forum] Shortcode suchen
|
|
$pages = get_posts([
|
|
'post_type' => 'page',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => 1,
|
|
's' => 'business_forum',
|
|
]);
|
|
if ( $pages ) return get_permalink( $pages[0]->ID );
|
|
// 3. Letzter Fallback: aktuelle Seite
|
|
return home_url('/');
|
|
}
|
|
|
|
// ── Assets ────────────────────────────────────────────────────────────────────
|
|
add_action( 'wp_enqueue_scripts', function() {
|
|
wp_enqueue_style( 'wbf-style', WBF_URL . 'assets/css/forum-style.css', [], WBF_VERSION );
|
|
wp_enqueue_script( 'wbf-script', WBF_URL . 'assets/js/forum-script.js', ['jquery'], WBF_VERSION, true );
|
|
$wbf_user = WBF_Auth::get_current_user();
|
|
if ( $wbf_user ) {
|
|
WBF_DB::touch_last_active( $wbf_user->id );
|
|
}
|
|
wp_localize_script( 'wbf-script', 'WBF', [
|
|
'ajax_url' => admin_url('admin-ajax.php'),
|
|
'nonce' => wp_create_nonce('wbf_nonce'),
|
|
'logged_in' => WBF_Auth::is_forum_logged_in() ? 'yes' : 'no',
|
|
'auto_logout_minutes' => (int)( wbf_get_settings()['auto_logout_minutes'] ?? 30 ),
|
|
'my_id' => $wbf_user ? (int)$wbf_user->id : 0,
|
|
'unread_dm' => $wbf_user ? WBF_DB::count_unread_messages($wbf_user->id) : 0,
|
|
'forum_url' => wbf_get_forum_url(),
|
|
'reactions' => WBF_DB::get_allowed_reactions(),
|
|
]);
|
|
});
|
|
|
|
|
|
// ══════════════════════════════════════════════════════════════════════════════
|
|
// ── Update-Checker ────────────────────────────────────────────────────────────
|
|
// Prüft täglich gegen die Gitea-Releases-API ob eine neue Version verfügbar ist.
|
|
// Releases-URL: https://git.viper.ipv64.net/M_Viper/WP-Business-Forum/releases
|
|
// ══════════════════════════════════════════════════════════════════════════════
|
|
|
|
define( 'WBF_UPDATE_API', 'https://git.viper.ipv64.net/api/v1/repos/M_Viper/WP-Business-Forum/releases?limit=1&page=1' );
|
|
define( 'WBF_RELEASES_PAGE', 'https://git.viper.ipv64.net/M_Viper/WP-Business-Forum/releases' );
|
|
define( 'WBF_UPDATE_TRANSIENT','wbf_update_check' );
|
|
|
|
/**
|
|
* Holt die neueste Release-Info von Gitea (gecacht per Transient, 12h).
|
|
* Gibt null zurück wenn kein Update verfügbar oder API nicht erreichbar.
|
|
*
|
|
* @return array|null ['version'=>string, 'url'=>string, 'name'=>string, 'published'=>string, 'body'=>string]
|
|
*/
|
|
function wbf_get_latest_release() {
|
|
$cached = get_transient( WBF_UPDATE_TRANSIENT );
|
|
if ( $cached !== false ) {
|
|
return $cached ?: null; // false = noch nie gecacht, '' = kein Update
|
|
}
|
|
|
|
$response = wp_remote_get( WBF_UPDATE_API, [
|
|
'timeout' => 8,
|
|
'user-agent' => 'WP-Business-Forum/' . WBF_VERSION . '; ' . get_bloginfo('url'),
|
|
'sslverify' => true,
|
|
] );
|
|
|
|
if ( is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200 ) {
|
|
// Bei Fehler 1h warten bevor erneut versucht
|
|
set_transient( WBF_UPDATE_TRANSIENT, '', HOUR_IN_SECONDS );
|
|
return null;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body( $response );
|
|
$releases = json_decode( $body, true );
|
|
|
|
if ( empty($releases) || ! is_array($releases) || empty($releases[0]) ) {
|
|
set_transient( WBF_UPDATE_TRANSIENT, '', 12 * HOUR_IN_SECONDS );
|
|
return null;
|
|
}
|
|
|
|
$latest = $releases[0];
|
|
$version = ltrim( $latest['tag_name'] ?? '', 'v' ); // "v1.2.0" → "1.2.0"
|
|
|
|
$info = [
|
|
'version' => $version,
|
|
'url' => $latest['html_url'] ?? WBF_RELEASES_PAGE,
|
|
'name' => $latest['name'] ?? $latest['tag_name'] ?? $version,
|
|
'published' => $latest['published_at'] ?? '',
|
|
'body' => wp_strip_all_tags( $latest['body'] ?? '' ),
|
|
];
|
|
|
|
// 12 Stunden cachen
|
|
set_transient( WBF_UPDATE_TRANSIENT, $info, 12 * HOUR_IN_SECONDS );
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* Prüft ob ein Update verfügbar ist.
|
|
* Gibt die Release-Info zurück wenn Gitea-Version > installierte Version.
|
|
*/
|
|
function wbf_update_available() {
|
|
$latest = wbf_get_latest_release();
|
|
if ( ! $latest || empty($latest['version']) ) return null;
|
|
if ( version_compare( $latest['version'], WBF_VERSION, '>' ) ) {
|
|
return $latest;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// ── Cron: täglich Update prüfen (Cache warm halten) ──────────────────────────
|
|
add_action( 'wbf_check_for_updates', function() {
|
|
delete_transient( WBF_UPDATE_TRANSIENT );
|
|
wbf_get_latest_release();
|
|
} );
|
|
|
|
if ( ! wp_next_scheduled( 'wbf_check_for_updates' ) ) {
|
|
wp_schedule_event( time(), 'twicedaily', 'wbf_check_for_updates' );
|
|
}
|
|
|
|
// ── Admin-Notice wenn Update verfügbar ───────────────────────────────────────
|
|
add_action( 'admin_notices', function() {
|
|
if ( ! current_user_can('manage_options') ) return;
|
|
|
|
$update = wbf_update_available();
|
|
if ( ! $update ) return;
|
|
|
|
// Notice ausblenden wenn der User sie weggeklickt hat (per GET-Parameter)
|
|
if ( isset($_GET['wbf_dismiss_update']) && check_admin_referer('wbf_dismiss_update') ) {
|
|
set_transient( 'wbf_update_dismissed_' . WBF_VERSION, $update['version'], 7 * DAY_IN_SECONDS );
|
|
wp_safe_redirect( remove_query_arg(['wbf_dismiss_update','_wpnonce']) );
|
|
exit;
|
|
}
|
|
|
|
$dismissed = get_transient( 'wbf_update_dismissed_' . WBF_VERSION );
|
|
if ( $dismissed === $update['version'] ) return;
|
|
|
|
$dismiss_url = wp_nonce_url(
|
|
add_query_arg('wbf_dismiss_update', '1'),
|
|
'wbf_dismiss_update'
|
|
);
|
|
$changelog_url = esc_url( $update['url'] );
|
|
$new_ver = esc_html( $update['version'] );
|
|
$cur_ver = esc_html( WBF_VERSION );
|
|
|
|
echo "
|
|
<div class=\"notice notice-warning is-dismissible\" style=\"border-left-color:#f59e0b;padding:12px 15px\">
|
|
<div style=\"display:flex;align-items:center;gap:14px;flex-wrap:wrap\">
|
|
<span style=\"font-size:1.6rem\">🔔</span>
|
|
<div>
|
|
<strong style=\"font-size:.95rem\">WP Business Forum — Update verfügbar!</strong>
|
|
<p style=\"margin:.3rem 0 0;color:#374151\">
|
|
Version <strong>{$new_ver}</strong> ist verfügbar. Du verwendest <strong>{$cur_ver}</strong>.
|
|
</p>
|
|
</div>
|
|
<div style=\"display:flex;gap:8px;margin-left:auto\">
|
|
<a href=\"{$changelog_url}\" target=\"_blank\" rel=\"noopener\"
|
|
class=\"button button-primary\" style=\"background:#f59e0b;border-color:#d97706\">
|
|
📋 Changelog & Download
|
|
</a>
|
|
<a href=\"" . esc_url($dismiss_url) . "\" class=\"button\">Später erinnern</a>
|
|
</div>
|
|
</div>
|
|
</div>";
|
|
} );
|
|
|
|
// ── Update-Badge im WP-Admin-Menü ─────────────────────────────────────────────
|
|
add_action( 'admin_menu', function() {
|
|
$update = wbf_update_available();
|
|
if ( ! $update ) return;
|
|
global $menu;
|
|
if ( ! is_array($menu) ) return;
|
|
foreach ( $menu as &$item ) {
|
|
if ( isset($item[2]) && $item[2] === 'wbf-admin' ) {
|
|
$item[0] .= ' <span class="update-plugins"><span class="plugin-count">1</span></span>';
|
|
break;
|
|
}
|
|
}
|
|
}, 999 );
|
|
|
|
// ── Manuellen Cache-Reset erlauben (für die Admin-UI) ─────────────────────────
|
|
add_action( 'admin_init', function() {
|
|
if ( ! isset($_GET['wbf_refresh_update']) ) return;
|
|
if ( ! current_user_can('manage_options') ) return;
|
|
if ( ! check_admin_referer('wbf_refresh_update') ) return;
|
|
delete_transient( WBF_UPDATE_TRANSIENT );
|
|
wp_safe_redirect( remove_query_arg(['wbf_refresh_update','_wpnonce']) );
|
|
exit;
|
|
} ); |