Completed
Push — master ( ae592b...8d63b6 )
by frank
03:07
created

autoptimizeSpeedupper.php ➔ ao_speedup_warmcache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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
	// load speedupper conditionally (true by default?)
8
	if ( apply_filters('autoptimize_filter_speedupper', true) ) {
9
        if (strpos($scriptname,"min.js")===false) {
10
                $md5hash = "snippet_".md5($jsin);
11
                $ccheck = new autoptimizeCache($md5hash,'js');
12
                if($ccheck->check()) {
13
                        $scriptsrc = $ccheck->retrieve();
14
                } else {
15
                        if(class_exists('JSMin')) {
16
                                $tmp_jscode = trim(JSMin::minify($jsin));
17
                                if (!empty($tmp_jscode)) {
18
                                        $scriptsrc = $tmp_jscode;
19
                                        unset($tmp_jscode);
20
                                        $ccheck->cache($scriptsrc,'text/javascript');
21
                                } else {
22
                                        $scriptsrc=$jsin;
23
                                }
24
                        } else {
25
                                $scriptsrc=$jsin;
26
                        }
27
                }
28
                unset($ccheck);
29
        } else {
30
            // do some housekeeping here to remove comments & linebreaks and stuff
31
            $scriptsrc=preg_replace("#^\s*\/\/.*$#Um","",$jsin);
32
            $scriptsrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$scriptsrc);
33
			$scriptsrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $scriptsrc);
34
35
			if ((substr($scriptsrc,-1,1)!==";")&&(substr($scriptsrc,-1,1)!=="}")) {
36
				$scriptsrc.=";";
37
			}
38
						
39
			if (get_option("autoptimize_js_trycatch")==="on") {
40
				$scriptsrc="try{".$scriptsrc."}catch(e){}";
41
			}
42
        }
43
        return $scriptsrc;
44
	}
45
}
46
47
function ao_css_snippetcache($cssin,$filename) {
48
	// load speedupper conditionally (true by default?)
49
	if ( apply_filters('autoptimize_filter_speedupper', true) ) {
50
		$md5hash = "snippet_".md5($cssin);
51
		$ccheck = new autoptimizeCache($md5hash,'css');
52
		if($ccheck->check()) {
53
			$stylesrc = $ccheck->retrieve();
54
		} else {
55
			if (strpos($filename,"min.css")===false) {
56 View Code Duplication
				if (class_exists('Minify_CSS_Compressor')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
57
					$tmp_code = trim(Minify_CSS_Compressor::process($cssin));
58
				} else if(class_exists('CSSmin')) {
59
					$cssmin = new CSSmin();
60
					if (method_exists($cssmin,"run")) {
61
						$tmp_code = trim($cssmin->run($cssin));
62
					} elseif (@is_callable(array($cssmin,"minify"))) {
63
						$tmp_code = trim(CssMin::minify($cssin));
0 ignored issues
show
Bug introduced by
The method minify() cannot be called from this context as it is declared private in class CSSmin.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
Bug introduced by
The call to minify() misses a required argument $linebreak_pos.

This check looks for function calls that miss required arguments.

Loading history...
64
					}
65
				}
66
67
				if (!empty($tmp_code)) {
68
					$stylesrc = $tmp_code;
69
					unset($tmp_code);
70
				} else {
71
					$stylesrc = $cssin;
72
				}
73
			} else {
74
				// .min.css -> no heavy-lifting, just some cleanup
75
				$stylesrc=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$cssin);
76
				$stylesrc=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc);
77
				$stylesrc=autoptimizeStyles::fixurls($filename,$stylesrc);
78
			}
79
			if (!empty($filename)) {
80
				// don't cache inline CSS to avoid risk of cache-explosion
81
				$ccheck->cache($stylesrc,'text/css');
82
			}
83
			unset($ccheck);
84
		}
85
		return $stylesrc;
86
	}
87
}
88
89
// try to warm (snippet) cache when AO cache is cleared
90
function ao_speedup_warmcache() {
91
	// load speedupper conditionally (true by default?)
92
	if ( apply_filters('autoptimize_filter_speedupper', true) ) {
93
		error_log("speedupper triggered");
94
		$warmCacheUrl = site_url()."/?ao_speedup_cachebuster=".rand(1,100000);
95
		$warmCache = @wp_remote_get($warmCacheUrl);
96
		unset($warmCache);
97
	}
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_action('autoptimize_action_cachepurged','ao_speedup_warmcache');
103