PluginProbe
Autoptimize / 2.3.4
Autoptimize v2.3.4
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classlesses / autoptimizeSpeedupper.php

autoptimizeSpeedupper.php in Autoptimize 2.3.4, at classlesses/autoptimizeSpeedupper.php

106 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Autoptimize SpeedUp; minify & cache each JS/ CSS separately
4 * new in Autoptimize 2.2
5 */
6
7 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
8
9 function ao_js_snippetcacher($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
34 if ( (substr($scriptsrc,-1,1)!==";") && (substr($scriptsrc,-1,1)!=="}") ) {
35 $scriptsrc.=";";
36 }
37
38 if ( !empty($jsfilename) && str_replace( apply_filters('autoptimize_filter_js_speedup_cache',false), '', $jsfilename ) === $jsfilename ) {
39 // don't cache inline CSS or if filter says no
40 $ccheck->cache($scriptsrc,'text/javascript');
41 }
42 }
43 unset($ccheck);
44
45 return $scriptsrc;
46 }
47
48 function ao_css_snippetcacher($cssin,$cssfilename) {
49 $md5hash = "snippet_".md5($cssin);
50 $ccheck = new autoptimizeCache($md5hash,'css');
51 if($ccheck->check()) {
52 $stylesrc = $ccheck->retrieve();
53 } else {
54 if ( ( strpos($cssfilename,"min.css") === false ) && ( str_replace( apply_filters('autoptimize_filter_css_consider_minified',false), '', $cssfilename ) === $cssfilename ) ) {
55 if (class_exists('Minify_CSS_Compressor')) {
56 $tmp_code = trim(Minify_CSS_Compressor::process($cssin));
57 } else if(class_exists('CSSmin')) {
58 $cssmin = new CSSmin();
59 if (method_exists($cssmin,"run")) {
60 $tmp_code = trim($cssmin->run($cssin));
61 } elseif (@is_callable(array($cssmin,"minify"))) {
62 $tmp_code = trim(CssMin::minify($cssin));
63 }
64 }
65
66 if (!empty($tmp_code)) {
67 $stylesrc = $tmp_code;
68 unset($tmp_code);
69 } else {
70 $stylesrc = $cssin;
71 }
72 } else {
73 // .min.css -> no heavy-lifting, just some cleanup
74 $stylesrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$cssin);
75 $stylesrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc);
76 $stylesrc=autoptimizeStyles::fixurls($cssfilename,$stylesrc);
77 }
78 if ( !empty($cssfilename) && ( str_replace( apply_filters('autoptimize_filter_css_speedup_cache',false), '', $cssfilename ) === $cssfilename ) ) {
79 // only cache CSS if not inline and allowed by filter
80 $ccheck->cache($stylesrc,'text/css');
81 }
82 }
83 unset($ccheck);
84 return $stylesrc;
85 }
86
87 function ao_css_speedup_cleanup($cssin) {
88 // speedupper results in aggregated CSS not being minified, so the filestart-marker AO adds when aggregating need to be removed
89 return trim(str_replace(array('/*FILESTART*/','/*FILESTART2*/'),'',$cssin));
90 }
91
92 function ao_js_speedup_cleanup($jsin) {
93 // cleanup
94 return trim($jsin);
95 }
96
97 // conditionally attach filters
98 if ( apply_filters('autoptimize_css_do_minify',true) ) {
99 add_filter('autoptimize_css_individual_style','ao_css_snippetcacher',10,2);
100 add_filter('autoptimize_css_after_minify','ao_css_speedup_cleanup',10,1);
101 }
102 if ( apply_filters('autoptimize_js_do_minify',true) ) {
103 add_filter('autoptimize_js_individual_script','ao_js_snippetcacher',10,2);
104 add_filter('autoptimize_js_after_minify','ao_js_speedup_cleanup',10,1);
105 }
106