1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* Autoptimize SpeedUp; minify & cache each JS/ CSS separately |
6
|
|
|
* new in Autoptimize 2.2 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
function ao_js_snippetcache($jsin,$jsfilename) { |
10
|
|
|
$md5hash = "snippet_".md5($jsin); |
11
|
|
|
$ccheck = new autoptimizeCache($md5hash,'js'); |
12
|
|
|
if($ccheck->check()) { |
13
|
|
|
$scriptsrc = $ccheck->retrieve(); |
14
|
|
|
} else { |
15
|
|
|
if ( (strpos($jsfilename,"min.js") === false) && ( strpos($jsfilename,"js/jquery/jquery.js") === false ) && ( str_replace(apply_filters('autoptimize_filter_js_consider_minified',false), '', $jsfilename) === $jsfilename ) ) { |
16
|
|
|
if(class_exists('JSMin')) { |
17
|
|
|
$tmp_jscode = trim(JSMin::minify($jsin)); |
18
|
|
|
if (!empty($tmp_jscode)) { |
19
|
|
|
$scriptsrc = $tmp_jscode; |
20
|
|
|
unset($tmp_jscode); |
21
|
|
|
} else { |
22
|
|
|
$scriptsrc=$jsin; |
23
|
|
|
} |
24
|
|
|
} else { |
25
|
|
|
$scriptsrc=$jsin; |
26
|
|
|
} |
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
|
|
View Code Duplication |
if ( !empty($jsfilename) && str_replace( apply_filters('autoptimize_filter_js_speedup_cache',false), '', $jsfilename ) === $jsfilename ) { |
|
|
|
|
38
|
|
|
// don't cache inline CSS or if filter says no |
39
|
|
|
$ccheck->cache($scriptsrc,'text/javascript'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
unset($ccheck); |
43
|
|
|
|
44
|
|
|
if (get_option("autoptimize_js_trycatch")==="on") { |
45
|
|
|
$scriptsrc="try{".$scriptsrc."}catch(e){}"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $scriptsrc; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function ao_css_snippetcache($cssin,$cssfilename) { |
52
|
|
|
$md5hash = "snippet_".md5($cssin); |
53
|
|
|
$ccheck = new autoptimizeCache($md5hash,'css'); |
54
|
|
|
if($ccheck->check()) { |
55
|
|
|
$stylesrc = $ccheck->retrieve(); |
56
|
|
|
} else { |
57
|
|
|
if ( ( strpos($cssfilename,"min.css") === false ) && ( str_replace( apply_filters('autoptimize_filter_css_consider_minified',false), '', $cssfilename ) === $cssfilename ) ) { |
58
|
|
View Code Duplication |
if (class_exists('Minify_CSS_Compressor')) { |
|
|
|
|
59
|
|
|
$tmp_code = trim(Minify_CSS_Compressor::process($cssin)); |
60
|
|
|
} else if(class_exists('CSSmin')) { |
61
|
|
|
$cssmin = new CSSmin(); |
62
|
|
|
if (method_exists($cssmin,"run")) { |
63
|
|
|
$tmp_code = trim($cssmin->run($cssin)); |
64
|
|
|
} elseif (@is_callable(array($cssmin,"minify"))) { |
65
|
|
|
$tmp_code = trim(CssMin::minify($cssin)); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!empty($tmp_code)) { |
70
|
|
|
$stylesrc = $tmp_code; |
71
|
|
|
unset($tmp_code); |
72
|
|
|
} else { |
73
|
|
|
$stylesrc = $cssin; |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
// .min.css -> no heavy-lifting, just some cleanup |
77
|
|
|
$stylesrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$cssin); |
78
|
|
|
$stylesrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc); |
79
|
|
|
$stylesrc=autoptimizeStyles::fixurls($cssfilename,$stylesrc); |
80
|
|
|
} |
81
|
|
View Code Duplication |
if ( !empty($cssfilename) && ( str_replace( apply_filters('autoptimize_filter_css_speedup_cache',false), '', $cssfilename ) === $cssfilename ) ) { |
|
|
|
|
82
|
|
|
// only cache CSS if not inline and allowed by filter |
83
|
|
|
$ccheck->cache($stylesrc,'text/css'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
unset($ccheck); |
87
|
|
|
return $stylesrc; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
function ao_css_speedup_cleanup($cssin) { |
91
|
|
|
// speedupper results in aggregated CSS not being minified, so the filestart-marker AO adds when aggregating need to be removed |
92
|
|
|
return trim(str_replace(array('/*FILESTART*/','/*FILESTART2*/'),'',$cssin)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function ao_js_speedup_cleanup($jsin) { |
96
|
|
|
// cleanup |
97
|
|
|
return trim($jsin); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
add_filter('autoptimize_css_individual_style','ao_css_snippetcache',10,2); |
101
|
|
|
add_filter('autoptimize_js_individual_script','ao_js_snippetcache',10,2); |
102
|
|
|
add_filter('autoptimize_css_after_minify','ao_css_speedup_cleanup',10,1); |
103
|
|
|
add_filter('autoptimize_js_after_minify','ao_js_speedup_cleanup',10,1); |
104
|
|
|
|
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.