Advertisement
Guest User

Untitled

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