1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Autoptimize SpeedUp; minify & cache each JS/ CSS separately + warm the cache |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
function ao_js_snippetcache($jsin,$scriptname) { |
7
|
|
|
if (strpos($scriptname,"min.js")===false) { |
8
|
|
|
$md5hash = "snippet_".md5($jsin); |
9
|
|
|
$ccheck = new autoptimizeCache($md5hash,'js'); |
10
|
|
|
if($ccheck->check()) { |
11
|
|
|
$scriptsrc = $ccheck->retrieve(); |
12
|
|
|
} else { |
13
|
|
|
if(class_exists('JSMin')) { |
14
|
|
|
$tmp_jscode = trim(JSMin::minify($jsin)); |
15
|
|
|
if (!empty($tmp_jscode)) { |
16
|
|
|
$scriptsrc = $tmp_jscode; |
17
|
|
|
unset($tmp_jscode); |
18
|
|
|
$ccheck->cache($scriptsrc,'text/javascript'); |
19
|
|
|
} else { |
20
|
|
|
$scriptsrc=$jsin; |
21
|
|
|
} |
22
|
|
|
} else { |
23
|
|
|
$scriptsrc=$jsin; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
unset($ccheck); |
27
|
|
|
} else { |
28
|
|
|
// do some housekeeping here to remove comments & linebreaks and stuff |
29
|
|
|
$scriptsrc=preg_replace("#^\s*\/\/.*$#Um","",$jsin); |
30
|
|
|
$scriptsrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$scriptsrc); |
31
|
|
|
$scriptsrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $scriptsrc); |
32
|
|
|
|
33
|
|
|
if ((substr($scriptsrc,-1,1)!==";")&&(substr($scriptsrc,-1,1)!=="}")) { |
34
|
|
|
$scriptsrc.=";"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (get_option("autoptimize_js_trycatch")==="on") { |
38
|
|
|
$scriptsrc="try{".$scriptsrc."}catch(e){}"; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
return $scriptsrc; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function ao_css_snippetcache($cssin,$filename) { |
45
|
|
|
$md5hash = "snippet_".md5($cssin); |
46
|
|
|
$ccheck = new autoptimizeCache($md5hash,'css'); |
47
|
|
|
if($ccheck->check()) { |
48
|
|
|
$stylesrc = $ccheck->retrieve(); |
49
|
|
|
} else { |
50
|
|
|
if (strpos($filename,"min.css")===false) { |
51
|
|
View Code Duplication |
if (class_exists('Minify_CSS_Compressor')) { |
|
|
|
|
52
|
|
|
$tmp_code = trim(Minify_CSS_Compressor::process($cssin)); |
53
|
|
|
} else if(class_exists('CSSmin')) { |
54
|
|
|
$cssmin = new CSSmin(); |
55
|
|
|
if (method_exists($cssmin,"run")) { |
56
|
|
|
$tmp_code = trim($cssmin->run($cssin)); |
57
|
|
|
} elseif (@is_callable(array($cssmin,"minify"))) { |
58
|
|
|
$tmp_code = trim(CssMin::minify($cssin)); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!empty($tmp_code)) { |
63
|
|
|
$stylesrc = $tmp_code; |
64
|
|
|
unset($tmp_code); |
65
|
|
|
} else { |
66
|
|
|
$stylesrc = $cssin; |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
// .min.css -> no heavy-lifting, just some cleanup |
70
|
|
|
$stylesrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$cssin); |
71
|
|
|
$stylesrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc); |
72
|
|
|
$stylesrc=autoptimizeStyles::fixurls($filename,$stylesrc); |
73
|
|
|
} |
74
|
|
|
if (!empty($filename)) { |
75
|
|
|
// don't cache inline CSS to avoid risk of cache-explosion |
76
|
|
|
$ccheck->cache($stylesrc,'text/css'); |
77
|
|
|
} |
78
|
|
|
unset($ccheck); |
79
|
|
|
} |
80
|
|
|
return $stylesrc; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
add_filter('autoptimize_css_individual_style','ao_css_snippetcache',10,2); |
84
|
|
|
add_filter('autoptimize_js_individual_script','ao_js_snippetcache',10,2); |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.