PluginProbe
Autoptimize / 3.1.7
Autoptimize v3.1.7
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 / classes / autoptimizeSpeedupper.php

autoptimizeSpeedupper.php in Autoptimize 3.1.7, at classes/autoptimizeSpeedupper.php

111 lines 4.3 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' ) ) {
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' ) ) && ( 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 if ( ! empty( $jsfilename ) && str_replace( apply_filters( 'autoptimize_filter_js_speedup_cache', false ), '', $jsfilename ) === $jsfilename ) {
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 if ( ! empty( $cssfilename ) && ( str_replace( apply_filters( 'autoptimize_filter_css_speedup_cache', false ), '', $cssfilename ) === $cssfilename ) ) {
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