141 lines
5.7 KiB
PHP
141 lines
5.7 KiB
PHP
<?php
|
||
/**
|
||
* Minecraft Modern Theme - Updater & Dashboard Status
|
||
*
|
||
* Diese Datei prüft auf neue Versionen via Gitea API und zeigt den Status im Dashboard an.
|
||
* Aus Sicherheitsgründen ist das automatische Update deaktiviert, um Datenverlust zu vermeiden.
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
class Minecraft_Modern_Theme_Manager {
|
||
|
||
// BUG-FIX: Hardcoded 'Minecraft-Modern-Theme' schlug auf Linux-Servern
|
||
// (case-sensitive Dateisystem) fehl, wenn das Verzeichnis kleingeschrieben ist.
|
||
// get_template() liefert immer den echten Verzeichnisnamen.
|
||
private $theme_slug;
|
||
private $repo = 'M_Viper/Minecraft-Modern-Theme';
|
||
private $transient_key = 'mm_theme_update_check';
|
||
|
||
public function __construct() {
|
||
$this->theme_slug = get_template();
|
||
|
||
add_action( 'admin_notices', [ $this, 'display_update_notice' ] );
|
||
add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] );
|
||
add_action( 'admin_init', [ $this, 'handle_refresh_request' ] );
|
||
}
|
||
|
||
/**
|
||
* Holt die API-Daten von Gitea (mit Transient-Cache).
|
||
*/
|
||
private function get_latest_release() {
|
||
$update_data = get_transient( $this->transient_key );
|
||
|
||
if ( false === $update_data ) {
|
||
$api_url = "https://git.viper.ipv64.net/api/v1/repos/{$this->repo}/releases/latest";
|
||
$response = wp_remote_get( $api_url, [ 'timeout' => 10 ] );
|
||
|
||
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
|
||
set_transient( $this->transient_key, [ 'error' => true ], 2 * HOUR_IN_SECONDS );
|
||
return [ 'error' => true ];
|
||
}
|
||
|
||
$data = json_decode( wp_remote_retrieve_body( $response ) );
|
||
$new_version = ltrim( $data->tag_name, 'vV' );
|
||
|
||
$update_data = [
|
||
'version' => $new_version,
|
||
'url' => "https://git.viper.ipv64.net/{$this->repo}/releases",
|
||
];
|
||
|
||
set_transient( $this->transient_key, $update_data, 12 * HOUR_IN_SECONDS );
|
||
}
|
||
|
||
return $update_data;
|
||
}
|
||
|
||
/**
|
||
* Zeigt die gelbe Info-Box oben im Admin-Bereich an, wenn ein Update verfügbar ist.
|
||
*/
|
||
public function display_update_notice() {
|
||
if ( ! current_user_can( 'update_themes' ) ) return;
|
||
|
||
$latest = $this->get_latest_release();
|
||
if ( isset( $latest['error'] ) ) return;
|
||
|
||
$current_version = wp_get_theme( $this->theme_slug )->get( 'Version' );
|
||
if ( ! $current_version ) return;
|
||
|
||
if ( version_compare( $current_version, $latest['version'], '<' ) ) {
|
||
?>
|
||
<div class="notice notice-warning is-dismissible">
|
||
<p>
|
||
<span class="dashicons dashicons-update" style="color: #dba617; margin-right: 5px;"></span>
|
||
<strong>Minecraft Modern Update verfügbar:</strong>
|
||
Version <strong><?php echo esc_html( $latest['version'] ); ?></strong> ist bereit zum Download.
|
||
<a href="<?php echo esc_url( $latest['url'] ); ?>" target="_blank" style="margin-left: 10px; font-weight: bold;">
|
||
Zum Download →
|
||
</a>
|
||
</p>
|
||
</div>
|
||
<?php
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Fügt das Widget zum WordPress-Dashboard hinzu.
|
||
*/
|
||
public function add_dashboard_widget() {
|
||
wp_add_dashboard_widget(
|
||
'mm_theme_status_widget',
|
||
'Minecraft Modern Theme Status',
|
||
[ $this, 'render_widget_content' ]
|
||
);
|
||
}
|
||
|
||
/**
|
||
* HTML-Inhalt des Dashboard-Widgets.
|
||
*/
|
||
public function render_widget_content() {
|
||
$current_version = wp_get_theme( $this->theme_slug )->get( 'Version' );
|
||
$latest = $this->get_latest_release();
|
||
|
||
echo '<div class="mm-status-widget">';
|
||
echo '<p><span class="dashicons dashicons-admin-appearance"></span> <strong>Installiert:</strong> ' . esc_html( $current_version ?: '–' ) . '</p>';
|
||
|
||
if ( isset( $latest['version'] ) ) {
|
||
echo '<p><span class="dashicons dashicons-cloud"></span> <strong>Aktuellste:</strong> ' . esc_html( $latest['version'] ) . '</p>';
|
||
|
||
if ( $current_version && version_compare( $current_version, $latest['version'], '<' ) ) {
|
||
echo '<div style="background: #fff8e5; border-left: 4px solid #ffb900; padding: 12px; margin: 15px 0;">';
|
||
echo '<p style="margin: 0 0 10px; color: #856404;"><strong>Update verfügbar!</strong></p>';
|
||
echo '<a href="' . esc_url( $latest['url'] ) . '" class="button button-primary" target="_blank">Download ZIP von Gitea</a>';
|
||
echo '</div>';
|
||
} else {
|
||
echo '<p style="color: #46b450; font-weight: bold; margin-top: 15px;">';
|
||
echo '<span class="dashicons dashicons-yes"></span> Theme ist aktuell.</p>';
|
||
}
|
||
} else {
|
||
echo '<p style="color: #d63638;"><span class="dashicons dashicons-warning"></span> Prüfung fehlgeschlagen.</p>';
|
||
}
|
||
|
||
$refresh_url = wp_nonce_url( admin_url( 'index.php?mm_refresh_check=1' ), 'mm_refresh_action' );
|
||
echo '<hr><p><small><a href="' . esc_url( $refresh_url ) . '">Update-Cache jetzt leeren</a></small></p>';
|
||
echo '</div>';
|
||
}
|
||
|
||
/**
|
||
* Verarbeitet den Klick auf "Update-Cache jetzt leeren".
|
||
*/
|
||
public function handle_refresh_request() {
|
||
if ( isset( $_GET['mm_refresh_check'] ) && check_admin_referer( 'mm_refresh_action' ) ) {
|
||
delete_transient( $this->transient_key );
|
||
wp_redirect( admin_url( 'index.php' ) );
|
||
exit;
|
||
}
|
||
}
|
||
}
|
||
|
||
new Minecraft_Modern_Theme_Manager();
|