minify.php
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
*
* Website: https://wpraiser.com/
* Author: Raul Peixoto (https://www.upwork.com/fl/raulpeixoto)
* Licensed under GPLv2 (or later)
* Version 1.0
*
* Usage: fvm_raisermin_js($js);
*
*/
# Exit if accessed directly
if (!defined('ABSPATH')){ exit(); }
# minify js, whitespace only
function fvm_raisermin_js($code){
# remove // comments
$code = preg_replace('/(^|\s)\/\/(.*)\n/m', '', $code);
$code = preg_replace('/(\{|\}|\[|\]|\(|\)|\;)\/\/(.*)\n/m', '$1', $code);
# remove /* ... */ comments
$code = preg_replace('/(^|\s)\/\*(.*)\*\//Us', '', $code);
$code = preg_replace('/(\;|\{)\/\*(.*)\*\//Us', '$1', $code);
# remove sourceMappingURL
$code = preg_replace('/(\/\/\s*[#]\s*sourceMappingURL\s*[=]\s*)([a-zA-Z0-9-_\.\/]+)(\.map)/ui', '', $code);
# uniform line endings, make them all line feed
$code = str_replace(array("\r\n", "\r"), "\n", $code);
# collapse all non-line feed whitespace into a single space
$code = preg_replace('/[^\S\n]+/', ' ', $code);
# strip leading & trailing whitespace
$code = str_replace(array(" \n", "\n "), "\n", $code);
# collapse consecutive line feeds into just 1
$code = preg_replace('/\n+/', "\n", $code);
# process horizontal space
$code = preg_replace('/([\[\]\(\)\{\}\;\<\>])(\h+)([\[\]\(\)\{\}\;\<\>])/ui', '$1 $3', $code);
$code = preg_replace('/([\)])(\h?)(\.)/ui', '$1$3', $code);
$code = preg_replace('/([\)\?])(\h?)(\.)/ui', '$1$3', $code);
$code = preg_replace('/(\,)(\h+)/ui', '$1 ', $code);
$code = preg_replace('/(\h+)(\,)/ui', ' $2', $code);
$code = preg_replace('/([if])(\h+)(\()/ui', '$1$3', $code);
# trim whitespace on beginning/end
return trim($code);
}
# remove UTF8 BOM
function fvm_min_remove_utf8_bom($text) {
$bom = pack('H*','EFBBBF');
while (preg_match("/^$bom/", $text)) {
$text = preg_replace("/^$bom/ui", '', $text);
}
return $text;
}
# minify html, don't touch certain tags
function fvm_raisermin_html($html) {
# clone
$content = $html;
# get all scripts
$skp = array();
preg_match_all('/(\<script(.*?)\<(\s*)\/script(\s*)\>|\<noscript(.*?)\<(\s*)\/noscript(\s*)\>|\<code(.*?)\<(\s*)\/code(\s*)\>|\<pre(.*?)\<(\s*)\/pre(\s*)\>)/uis', $html, $skp);
# replace all skippable patterns with comment
if(is_array($skp) && isset($skp[0]) && count($skp[0]) > 0) {
foreach ($skp[0] as $k=>$v) {
$content = str_replace($v, '<!-- SKIP '.$k.' -->', $content);
}
}
# remove line breaks, and colapse two or more white spaces into one
$content = preg_replace('/\s+/u', " ", $content);
# add linebreaks after html and head tags, for readability
$content = str_replace('<head>', PHP_EOL . '<head>' . PHP_EOL, $content);
$content = str_replace('</head>', PHP_EOL . '</head>' . PHP_EOL, $content);
$content = str_replace('<html', PHP_EOL . '<html', $content);
$content = str_replace('</html>', PHP_EOL . '</html>', $content);
# final readability adjustments
$content = str_replace('<meta ', PHP_EOL . '<meta ', $content);
$content = str_replace('<link ', PHP_EOL . '<link ', $content);
$content = str_replace('<style', PHP_EOL . '<style', $content);
$content = str_replace('<noscript', PHP_EOL . '<noscript>', $content);
# replace markers for scripts last
if(is_array($skp) && isset($skp[0]) && count($skp[0]) > 0) {
foreach ($skp[0] as $k=>$v) {
$content = str_replace('<!-- SKIP '.$k.' -->', PHP_EOL . $v . PHP_EOL, $content);
}
}
# no empty lines
$lines = explode(PHP_EOL, $content);
foreach($lines as $k=>$ln) { $ln = ltrim($ln); if(empty($ln)) { unset($lines[$k]); } else { $lines[$k] = $ln; } }
$content = implode(PHP_EOL, $lines);
# save as html, if not empty
if(!empty($content)) {
$html = $content;
}
# return
return $html;
}