Advertisement
Guest User

WordPress Minify HTML shippet

a guest
Apr 19th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.32 KB | Source Code | 0 0
  1. <?php
  2.  
  3. /**
  4.  * WordPress HTML Minify
  5.  */
  6. class WP_HTML_Compression {
  7.  
  8.     // Settings
  9.     protected $compress_css = true;
  10.     protected $compress_js = true;
  11.     protected $info_comment = true;
  12.     protected $remove_comments = true;
  13.  
  14.     // Variables
  15.     protected $html;
  16.     public function __construct($html) {
  17.         if (!empty($html)) {
  18.             $this->parseHTML($html);
  19.         }
  20.     }
  21.  
  22.     public function __toString() {
  23.         return $this->html;
  24.     }
  25.  
  26.     protected function bottomComment($raw, $compressed) {
  27.         $raw = strlen($raw);
  28.         $compressed = strlen($compressed);
  29.  
  30.         $savings = ($raw - $compressed) / $raw * 100;
  31.  
  32.         $savings = round($savings, 2);
  33.  
  34.         return '<!--HTML compressed, size saved ' . $savings . '%. From ' . $raw . ' bytes, now ' . $compressed . ' bytes-->';
  35.     }
  36.    
  37.     protected function minifyHTML($html) {
  38.         $pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
  39.         preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  40.         $overriding = false;
  41.         $raw_tag = false;
  42.         // Variable reused for output
  43.         $html = '';
  44.         foreach ($matches as $token) {
  45.             $tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
  46.  
  47.             $content = $token[0];
  48.  
  49.             if (is_null($tag)) {
  50.                 if (!empty($token['script'])) {
  51.                     $strip = $this->compress_js;
  52.                 }
  53.                 else if (!empty($token['style'])) {
  54.                     $strip = $this->compress_css;
  55.                 }
  56.                 else if ($content == '<!--wp-html-compression no compression-->') {
  57.                     $overriding = !$overriding;
  58.  
  59.                     // Don't print the comment
  60.                     continue;
  61.                 }
  62.                 else if ($this->remove_comments) {
  63.                     if (!$overriding && $raw_tag != 'textarea') {
  64.                         // Remove any HTML comments, except MSIE conditional comments
  65.                         $content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
  66.                     }
  67.                 }
  68.             }
  69.             else {
  70.                 if ($tag == 'pre' || $tag == 'textarea') {
  71.                     $raw_tag = $tag;
  72.                 }
  73.                 else if ($tag == '/pre' || $tag == '/textarea') {
  74.                     $raw_tag = false;
  75.                 }
  76.                 else {
  77.                     if ($raw_tag || $overriding) {
  78.                         $strip = false;
  79.                     }
  80.                     else {
  81.                         $strip = true;
  82.  
  83.                         // Remove any empty attributes, except:
  84.                         // action, alt, content, src
  85.                         $content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
  86.  
  87.                         // Remove any space before the end of self-closing XHTML tags
  88.                         // JavaScript excluded
  89.                         $content = str_replace(' />', '/>', $content);
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             if ($strip) {
  95.                 $content = $this->removeWhiteSpace($content);
  96.             }
  97.  
  98.             $html .= $content;
  99.         }
  100.  
  101.         return $html;
  102.     }
  103.  
  104.     public function parseHTML($html) {
  105.         $this->html = $this->minifyHTML($html);
  106.  
  107.         if ($this->info_comment) {
  108.             $this->html .= "\n" . $this->bottomComment($html, $this->html);
  109.         }
  110.     }
  111.  
  112.     protected function removeWhiteSpace($str) {
  113.         $str = str_replace("\t", ' ', $str);
  114.         $str = str_replace("\n", '', $str);
  115.         $str = str_replace("\r", '', $str);
  116.  
  117.         while (stristr($str, '  ')) {
  118.             $str = str_replace('  ', ' ', $str);
  119.         }
  120.  
  121.         return $str;
  122.     }
  123. }
  124.  
  125. function wp_html_compression_finish($html) {
  126.     return new WP_HTML_Compression($html);
  127. }
  128.  
  129. function wp_html_compression_start() {
  130.     ob_start('wp_html_compression_finish');
  131. }
  132. add_action('get_header', 'wp_html_compression_start');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement