| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client2/web20/web/wp-content/mu-plugins/ |
Upload File : |
<?php
/**
* DGT Minify – Asset- und HTML-Minifizierung für Gäste.
*
* - Tauscht registrierte CSS/JS-Handles gegen .min-Varianten
* - Entfernt unnötige WordPress-Core-Styles (Block-Library, Global Styles)
* - Minifiziert HTML, inline <style> und inline <script>
* - Greift NUR für nicht eingeloggte Besucher
*
* @package drogentest-online
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class DGT_Minify {
public static function init() {
if ( is_admin() || wp_doing_cron() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return;
}
add_action( 'template_redirect', array( __CLASS__, 'maybe_start_buffer' ), 0 );
add_filter( 'style_loader_src', array( __CLASS__, 'swap_asset' ), 999 );
add_filter( 'script_loader_src', array( __CLASS__, 'swap_asset' ), 999 );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'remove_core_bloat' ), 100 );
}
public static function is_guest() {
return ! is_user_logged_in();
}
public static function remove_core_bloat() {
if ( ! self::is_guest() ) {
return;
}
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
wp_dequeue_style( 'classic-theme-styles' );
wp_dequeue_style( 'global-styles' );
wp_dequeue_style( 'core-block-supports' );
}
public static function swap_asset( $src ) {
if ( ! self::is_guest() || ! $src ) {
return $src;
}
if ( strpos( $src, '.min.' ) !== false ) {
return $src;
}
$content_url = content_url();
if ( strpos( $src, $content_url ) === false ) {
return $src;
}
$rel = str_replace( $content_url, '', strtok( $src, '?' ) );
$path = WP_CONTENT_DIR . $rel;
if ( preg_match( '/\.(css|js)$/', $path, $m ) ) {
$min_path = substr( $path, 0, -strlen( $m[0] ) ) . '.min.' . $m[1];
if ( file_exists( $min_path ) ) {
$min_url = $content_url . substr( $rel, 0, -strlen( $m[0] ) ) . '.min.' . $m[1];
$ver = filemtime( $min_path );
return $min_url . '?ver=' . $ver;
}
}
return $src;
}
public static function maybe_start_buffer() {
if ( ! self::is_guest() ) {
return;
}
ob_start( array( __CLASS__, 'minify_html' ) );
}
public static function minify_html( $html ) {
if ( strlen( $html ) < 255 ) {
return $html;
}
$placeholders = array();
$idx = 0;
// <pre>, <textarea>, <!--[if → Platzhalter (unverändert lassen).
$html = preg_replace_callback(
'#(<(?:pre|textarea)\b[^>]*>.*?</(?:pre|textarea)>|<!--\[if\b.*?<!\[endif\]-->)#si',
function ( $m ) use ( &$placeholders, &$idx ) {
$key = "\x1A" . $idx++ . "\x1A";
$placeholders[ $key ] = $m[0];
return $key;
},
$html
);
// Inline <style> → minifizieren, dann Platzhalter.
$html = preg_replace_callback(
'#(<style\b[^>]*>)(.*?)(</style>)#si',
function ( $m ) use ( &$placeholders, &$idx ) {
$css = $m[2];
$css = preg_replace( '#/\*.*?\*/#s', '', $css );
$css = preg_replace( '/\s*([{};:,>~+])\s*/', '$1', $css );
$css = preg_replace( '/\s{2,}/', ' ', $css );
$css = trim( $css );
$key = "\x1A" . $idx++ . "\x1A";
$placeholders[ $key ] = $m[1] . $css . $m[3];
return $key;
},
$html
);
// Inline <script> (ohne src) → minifizieren, dann Platzhalter.
$html = preg_replace_callback(
'#(<script\b(?![^>]*\bsrc\b)[^>]*>)(.*?)(</script>)#si',
function ( $m ) use ( &$placeholders, &$idx ) {
$js = $m[2];
$js = preg_replace( '#^\s*//[^\n]*$#m', '', $js );
$js = preg_replace( '#/\*.*?\*/#s', '', $js );
$js = preg_replace( '/\n{2,}/', "\n", $js );
$js = preg_replace( '/^\s+/m', '', $js );
$js = trim( $js );
$key = "\x1A" . $idx++ . "\x1A";
$placeholders[ $key ] = $m[1] . $js . $m[3];
return $key;
},
$html
);
// <script src="..."> → Platzhalter (unverändert).
$html = preg_replace_callback(
'#<script\b[^>]*\bsrc\b[^>]*>.*?</script>#si',
function ( $m ) use ( &$placeholders, &$idx ) {
$key = "\x1A" . $idx++ . "\x1A";
$placeholders[ $key ] = $m[0];
return $key;
},
$html
);
// HTML-Kommentare entfernen.
$html = preg_replace( '/<!--(?!\s*noindex).*?-->/s', '', $html );
// Whitespace zwischen Tags.
$html = preg_replace( '/>\s+</', '> <', $html );
// Mehrfach-Whitespace.
$html = preg_replace( '/\s{2,}/', ' ', $html );
// Führende Leerzeichen pro Zeile.
$html = preg_replace( '/^ +/m', '', $html );
// Leere Zeilen.
$html = preg_replace( '/\n{2,}/', "\n", $html );
// Platzhalter zurückschreiben.
$html = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $html );
// Finale Bereinigung nach Platzhalter-Restore.
$html = preg_replace( '/>\s+</', '><', $html );
$html = preg_replace( '/\s{2,}/', ' ', $html );
$html = trim( $html );
return $html;
}
}
DGT_Minify::init();