Completed
Push — master ( 8d63b6...90face )
by frank
02:42
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
    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')) {
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...
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));
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...
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