autoptimizeSpeedupper::js_snippetcacher()   B
last analyzed

Complexity

Conditions 10
Paths 13

Size

Total Lines 36

Duplication

Lines 4
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 10
nc 13
nop 2
dl 4
loc 36
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Autoptimize SpeedUp; minify & cache each JS/ CSS separately
4
 * new in Autoptimize 2.2
5
 */
6
7
if ( ! defined( 'ABSPATH' ) ) {
8
    exit;
9
}
10
11
class autoptimizeSpeedupper
12
{
13
    public function __construct()
14
    {
15
        $this->add_hooks();
16
    }
17
18
    public function add_hooks()
19
    {
20
        if ( apply_filters( 'autoptimize_js_do_minify', true ) ) {
21
            add_filter( 'autoptimize_js_individual_script', array( $this, 'js_snippetcacher' ), 10, 2 );
22
            add_filter( 'autoptimize_js_after_minify', array( $this, 'js_cleanup' ), 10, 1 );
23
        }
24
        if ( apply_filters( 'autoptimize_css_do_minify', true ) ) {
25
            add_filter( 'autoptimize_css_individual_style', array( $this, 'css_snippetcacher' ), 10, 2 );
26
            add_filter( 'autoptimize_css_after_minify', array( $this, 'css_cleanup' ), 10, 1 );
27
        }
28
    }
29
30
    public function js_snippetcacher( $jsin, $jsfilename )
31
    {
32
        $md5hash = 'snippet_' . md5( $jsin );
33
        $ccheck  = new autoptimizeCache( $md5hash, 'js' );
34
        if ( $ccheck->check() ) {
35
            $scriptsrc = $ccheck->retrieve();
36
        } else {
37
            if ( false === ( strpos( $jsfilename, 'min.js' ) ) && ( false === strpos( $jsfilename, 'js/jquery/jquery.js' ) ) && ( str_replace( apply_filters( 'autoptimize_filter_js_consider_minified', false ), '', $jsfilename ) === $jsfilename ) ) {
38
                $tmp_jscode = trim( JSMin::minify( $jsin ) );
39
                if ( ! empty( $tmp_jscode ) ) {
40
                    $scriptsrc = $tmp_jscode;
41
                    unset( $tmp_jscode );
42
                } else {
43
                    $scriptsrc = $jsin;
44
                }
45
            } else {
46
                // Removing comments, linebreaks and stuff!
47
                $scriptsrc = preg_replace( '#^\s*\/\/.*$#Um', '', $jsin );
48
                $scriptsrc = preg_replace( '#^\s*\/\*[^!].*\*\/\s?#Us', '', $scriptsrc );
49
                $scriptsrc = preg_replace( "#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $scriptsrc );
50
            }
51
52
            $last_char = substr( $scriptsrc, -1, 1 );
53
            if ( ';' !== $last_char && '}' !== $last_char ) {
54
                $scriptsrc .= ';';
55
            }
56
57 View Code Duplication
            if ( ! empty( $jsfilename ) && str_replace( apply_filters( 'autoptimize_filter_js_speedup_cache', false ), '', $jsfilename ) === $jsfilename ) {
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...
58
                // Don't cache inline CSS or if filter says no!
59
                $ccheck->cache( $scriptsrc, 'text/javascript' );
60
            }
61
        }
62
        unset( $ccheck );
63
64
        return $scriptsrc;
65
    }
66
67
    public function css_snippetcacher( $cssin, $cssfilename )
68
    {
69
        $md5hash = 'snippet_' . md5( $cssin );
70
        $ccheck  = new autoptimizeCache( $md5hash, 'css' );
71
        if ( $ccheck->check() ) {
72
            $stylesrc = $ccheck->retrieve();
73
        } else {
74
            if ( ( false === strpos( $cssfilename, 'min.css' ) ) && ( str_replace( apply_filters( 'autoptimize_filter_css_consider_minified', false ), '', $cssfilename ) === $cssfilename ) ) {
75
                $cssmin   = new autoptimizeCSSmin();
76
                $tmp_code = trim( $cssmin->run( $cssin ) );
77
78
                if ( ! empty( $tmp_code ) ) {
79
                    $stylesrc = $tmp_code;
80
                    unset( $tmp_code );
81
                } else {
82
                    $stylesrc = $cssin;
83
                }
84
            } else {
85
                // .min.css -> no heavy-lifting, just some cleanup!
86
                $stylesrc = preg_replace( '#^\s*\/\*[^!].*\*\/\s?#Us', '', $cssin );
87
                $stylesrc = preg_replace( "#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $stylesrc );
88
                $stylesrc = autoptimizeStyles::fixurls( $cssfilename, $stylesrc );
89
            }
90 View Code Duplication
            if ( ! empty( $cssfilename ) && ( str_replace( apply_filters( 'autoptimize_filter_css_speedup_cache', false ), '', $cssfilename ) === $cssfilename ) ) {
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...
91
                // Only caching CSS if it's not inline and is allowed by filter!
92
                $ccheck->cache( $stylesrc, 'text/css' );
93
            }
94
        }
95
        unset( $ccheck );
96
97
        return $stylesrc;
98
    }
99
100
    public function css_cleanup( $cssin )
101
    {
102
        // Speedupper results in aggregated CSS not being minified, so the filestart-marker AO adds when aggregating needs to be removed.
103
        return trim( str_replace( array( '/*FILESTART*/', '/*FILESTART2*/' ), '', $cssin ) );
104
    }
105
106
    public function js_cleanup( $jsin )
107
    {
108
        return trim( $jsin );
109
    }
110
}
111