@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for CSS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -48,53 +48,53 @@ discard block |
||
48 | 48 | // public $cdn_url; // Used all over the place implicitly, so will have to be either public or protected :/ . |
49 | 49 | |
50 | 50 | // Reads the page and collects style tags. |
51 | - public function read( $options ) |
|
51 | + public function read($options) |
|
52 | 52 | { |
53 | - $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ); |
|
54 | - if ( $noptimizeCSS ) { |
|
53 | + $noptimizeCSS = apply_filters('autoptimize_filter_css_noptimize', false, $this->content); |
|
54 | + if ($noptimizeCSS) { |
|
55 | 55 | return false; |
56 | 56 | } |
57 | 57 | |
58 | - $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '', $this->content ); |
|
59 | - if ( ! empty( $whitelistCSS ) ) { |
|
60 | - $this->whitelist = array_filter( array_map( 'trim', explode( ',', $whitelistCSS ) ) ); |
|
58 | + $whitelistCSS = apply_filters('autoptimize_filter_css_whitelist', '', $this->content); |
|
59 | + if (!empty($whitelistCSS)) { |
|
60 | + $this->whitelist = array_filter(array_map('trim', explode(',', $whitelistCSS))); |
|
61 | 61 | } |
62 | 62 | |
63 | - $removableCSS = apply_filters( 'autoptimize_filter_css_removables', '' ); |
|
64 | - if ( ! empty( $removableCSS ) ) { |
|
65 | - $this->cssremovables = array_filter( array_map( 'trim', explode( ',', $removableCSS ) ) ); |
|
63 | + $removableCSS = apply_filters('autoptimize_filter_css_removables', ''); |
|
64 | + if (!empty($removableCSS)) { |
|
65 | + $this->cssremovables = array_filter(array_map('trim', explode(',', $removableCSS))); |
|
66 | 66 | } |
67 | 67 | |
68 | - $this->cssinlinesize = apply_filters( 'autoptimize_filter_css_inlinesize', 256 ); |
|
68 | + $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256); |
|
69 | 69 | |
70 | 70 | // filter to "late inject minified CSS", default to true for now (it is faster). |
71 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_css_inject_min_late', true ); |
|
71 | + $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true); |
|
72 | 72 | |
73 | 73 | // Remove everything that's not the header. |
74 | - if ( apply_filters( 'autoptimize_filter_css_justhead', $options['justhead'] ) ) { |
|
75 | - $content = explode( '</head>', $this->content, 2 ); |
|
76 | - $this->content = $content[0] . '</head>'; |
|
74 | + if (apply_filters('autoptimize_filter_css_justhead', $options['justhead'])) { |
|
75 | + $content = explode('</head>', $this->content, 2); |
|
76 | + $this->content = $content[0].'</head>'; |
|
77 | 77 | $this->restofcontent = $content[1]; |
78 | 78 | } |
79 | 79 | |
80 | 80 | // Determine whether we're doing CSS-files aggregation or not. |
81 | - if ( isset( $options['aggregate'] ) && ! $options['aggregate'] ) { |
|
81 | + if (isset($options['aggregate']) && !$options['aggregate']) { |
|
82 | 82 | $this->aggregate = false; |
83 | 83 | } |
84 | 84 | // Returning true for "dontaggregate" turns off aggregation. |
85 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_css_dontaggregate', false ) ) { |
|
85 | + if ($this->aggregate && apply_filters('autoptimize_filter_css_dontaggregate', false)) { |
|
86 | 86 | $this->aggregate = false; |
87 | 87 | } |
88 | 88 | |
89 | 89 | // include inline? |
90 | - if ( apply_filters( 'autoptimize_css_include_inline', $options['include_inline'] ) ) { |
|
90 | + if (apply_filters('autoptimize_css_include_inline', $options['include_inline'])) { |
|
91 | 91 | $this->include_inline = true; |
92 | 92 | } |
93 | 93 | |
94 | 94 | // List of CSS strings which are excluded from autoptimization. |
95 | - $excludeCSS = apply_filters( 'autoptimize_filter_css_exclude', $options['css_exclude'], $this->content ); |
|
96 | - if ( '' !== $excludeCSS ) { |
|
97 | - $this->dontmove = array_filter( array_map( 'trim', explode( ',', $excludeCSS ) ) ); |
|
95 | + $excludeCSS = apply_filters('autoptimize_filter_css_exclude', $options['css_exclude'], $this->content); |
|
96 | + if ('' !== $excludeCSS) { |
|
97 | + $this->dontmove = array_filter(array_map('trim', explode(',', $excludeCSS))); |
|
98 | 98 | } else { |
99 | 99 | $this->dontmove = array(); |
100 | 100 | } |
@@ -105,16 +105,16 @@ discard block |
||
105 | 105 | // Should we defer css? |
106 | 106 | // value: true / false. |
107 | 107 | $this->defer = $options['defer']; |
108 | - $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer, $this->content ); |
|
108 | + $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer, $this->content); |
|
109 | 109 | |
110 | 110 | // Should we inline while deferring? |
111 | 111 | // value: inlined CSS. |
112 | - $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content ); |
|
112 | + $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content); |
|
113 | 113 | |
114 | 114 | // Should we inline? |
115 | 115 | // value: true / false. |
116 | 116 | $this->inline = $options['inline']; |
117 | - $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline, $this->content ); |
|
117 | + $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline, $this->content); |
|
118 | 118 | |
119 | 119 | // Store cdn url. |
120 | 120 | $this->cdn_url = $options['cdn_url']; |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $this->datauris = $options['datauris']; |
124 | 124 | |
125 | 125 | // noptimize me. |
126 | - $this->content = $this->hide_noptimize( $this->content ); |
|
126 | + $this->content = $this->hide_noptimize($this->content); |
|
127 | 127 | |
128 | 128 | // Exclude (no)script, as those may contain CSS which should be left as is. |
129 | 129 | $this->content = $this->replace_contents_with_marker_if_exists( |
@@ -134,26 +134,26 @@ discard block |
||
134 | 134 | ); |
135 | 135 | |
136 | 136 | // Save IE hacks. |
137 | - $this->content = $this->hide_iehacks( $this->content ); |
|
137 | + $this->content = $this->hide_iehacks($this->content); |
|
138 | 138 | |
139 | 139 | // Hide HTML comments. |
140 | - $this->content = $this->hide_comments( $this->content ); |
|
140 | + $this->content = $this->hide_comments($this->content); |
|
141 | 141 | |
142 | 142 | // Get <style> and <link>. |
143 | - if ( preg_match_all( '#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches ) ) { |
|
143 | + if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) { |
|
144 | 144 | |
145 | - foreach ( $matches[0] as $tag ) { |
|
146 | - if ( $this->isremovable( $tag, $this->cssremovables ) ) { |
|
147 | - $this->content = str_replace( $tag, '', $this->content ); |
|
148 | - } elseif ( $this->ismovable( $tag ) ) { |
|
145 | + foreach ($matches[0] as $tag) { |
|
146 | + if ($this->isremovable($tag, $this->cssremovables)) { |
|
147 | + $this->content = str_replace($tag, '', $this->content); |
|
148 | + } elseif ($this->ismovable($tag)) { |
|
149 | 149 | // Get the media. |
150 | - if ( false !== strpos( $tag, 'media=' ) ) { |
|
151 | - preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias ); |
|
152 | - $medias = explode( ',', $medias[1] ); |
|
150 | + if (false !== strpos($tag, 'media=')) { |
|
151 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias); |
|
152 | + $medias = explode(',', $medias[1]); |
|
153 | 153 | $media = array(); |
154 | - foreach ( $medias as $elem ) { |
|
154 | + foreach ($medias as $elem) { |
|
155 | 155 | /* $media[] = current(explode(' ',trim($elem),2)); */ |
156 | - if ( empty( $elem ) ) { |
|
156 | + if (empty($elem)) { |
|
157 | 157 | $elem = 'all'; |
158 | 158 | } |
159 | 159 | |
@@ -161,65 +161,65 @@ discard block |
||
161 | 161 | } |
162 | 162 | } else { |
163 | 163 | // No media specified - applies to all. |
164 | - $media = array( 'all' ); |
|
164 | + $media = array('all'); |
|
165 | 165 | } |
166 | 166 | |
167 | - $media = apply_filters( 'autoptimize_filter_css_tagmedia', $media, $tag ); |
|
167 | + $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag); |
|
168 | 168 | |
169 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
169 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
170 | 170 | // <link>. |
171 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
172 | - $path = $this->getpath( $url ); |
|
171 | + $url = current(explode('?', $source[2], 2)); |
|
172 | + $path = $this->getpath($url); |
|
173 | 173 | |
174 | - if ( false !== $path && preg_match( '#\.css$#', $path ) ) { |
|
174 | + if (false !== $path && preg_match('#\.css$#', $path)) { |
|
175 | 175 | // Good link. |
176 | - $this->css[] = array( $media, $path ); |
|
176 | + $this->css[] = array($media, $path); |
|
177 | 177 | } else { |
178 | 178 | // Link is dynamic (.php etc). |
179 | - $new_tag = $this->optionally_defer_excluded( $tag, 'none' ); |
|
180 | - if ( $new_tag !== $tag ) { |
|
181 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
179 | + $new_tag = $this->optionally_defer_excluded($tag, 'none'); |
|
180 | + if ($new_tag !== $tag) { |
|
181 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
182 | 182 | } |
183 | 183 | $tag = ''; |
184 | 184 | } |
185 | 185 | } else { |
186 | 186 | // Inline css in style tags can be wrapped in comment tags, so restore comments. |
187 | - $tag = $this->restore_comments( $tag ); |
|
188 | - preg_match( '#<style.*>(.*)</style>#Usmi', $tag, $code ); |
|
187 | + $tag = $this->restore_comments($tag); |
|
188 | + preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code); |
|
189 | 189 | |
190 | 190 | // And re-hide them to be able to to the removal based on tag. |
191 | - $tag = $this->hide_comments( $tag ); |
|
191 | + $tag = $this->hide_comments($tag); |
|
192 | 192 | |
193 | - if ( $this->include_inline ) { |
|
194 | - $code = preg_replace( '#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1] ); |
|
195 | - $this->css[] = array( $media, 'INLINE;' . $code ); |
|
193 | + if ($this->include_inline) { |
|
194 | + $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]); |
|
195 | + $this->css[] = array($media, 'INLINE;'.$code); |
|
196 | 196 | } else { |
197 | 197 | $tag = ''; |
198 | 198 | } |
199 | 199 | } |
200 | 200 | |
201 | 201 | // Remove the original style tag. |
202 | - $this->content = str_replace( $tag, '', $this->content ); |
|
202 | + $this->content = str_replace($tag, '', $this->content); |
|
203 | 203 | } else { |
204 | 204 | // Excluded CSS, minify if getpath and filter says so... |
205 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
206 | - $exploded_url = explode( '?', $source[2], 2 ); |
|
205 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
206 | + $exploded_url = explode('?', $source[2], 2); |
|
207 | 207 | $url = $exploded_url[0]; |
208 | - $path = $this->getpath( $url ); |
|
208 | + $path = $this->getpath($url); |
|
209 | 209 | |
210 | - if ( $path && apply_filters( 'autoptimize_filter_css_minify_excluded', true, $url ) ) { |
|
211 | - $minified_url = $this->minify_single( $path ); |
|
212 | - if ( ! empty( $minified_url ) ) { |
|
210 | + if ($path && apply_filters('autoptimize_filter_css_minify_excluded', true, $url)) { |
|
211 | + $minified_url = $this->minify_single($path); |
|
212 | + if (!empty($minified_url)) { |
|
213 | 213 | // Replace orig URL with cached minified URL. |
214 | - $new_tag = str_replace( $url, $minified_url, $tag ); |
|
214 | + $new_tag = str_replace($url, $minified_url, $tag); |
|
215 | 215 | } else { |
216 | 216 | $new_tag = $tag; |
217 | 217 | } |
218 | 218 | |
219 | - $new_tag = $this->optionally_defer_excluded( $new_tag, $url ); |
|
219 | + $new_tag = $this->optionally_defer_excluded($new_tag, $url); |
|
220 | 220 | |
221 | 221 | // And replace! |
222 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
222 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
223 | 223 | } |
224 | 224 | } |
225 | 225 | } |
@@ -240,10 +240,10 @@ discard block |
||
240 | 240 | * |
241 | 241 | * @return string $new_tag |
242 | 242 | */ |
243 | - private function optionally_defer_excluded( $new_tag, $url = '' ) |
|
243 | + private function optionally_defer_excluded($new_tag, $url = '') |
|
244 | 244 | { |
245 | 245 | // Defer single CSS if "inline & defer" is ON and there is inline CSS. |
246 | - if ( $this->defer && ! empty( $this->defer_inline ) ) { |
|
246 | + if ($this->defer && !empty($this->defer_inline)) { |
|
247 | 247 | // Get/ set (via filter) the JS to be triggers onload of the preloaded CSS. |
248 | 248 | $_preload_onload = apply_filters( |
249 | 249 | 'autoptimize_filter_css_preload_onload', |
@@ -251,12 +251,12 @@ discard block |
||
251 | 251 | $url |
252 | 252 | ); |
253 | 253 | // Adapt original <link> element for CSS to be preloaded and add <noscript>-version for fallback. |
254 | - $new_tag = '<noscript>' . $new_tag . '</noscript>' . str_replace( |
|
254 | + $new_tag = '<noscript>'.$new_tag.'</noscript>'.str_replace( |
|
255 | 255 | array( |
256 | 256 | "rel='stylesheet'", |
257 | 257 | 'rel="stylesheet"', |
258 | 258 | ), |
259 | - "rel='preload' as='style' onload=\"" . $_preload_onload . "\"", |
|
259 | + "rel='preload' as='style' onload=\"".$_preload_onload."\"", |
|
260 | 260 | $new_tag |
261 | 261 | ); |
262 | 262 | } |
@@ -270,22 +270,22 @@ discard block |
||
270 | 270 | * @param string $path |
271 | 271 | * @return boolean |
272 | 272 | */ |
273 | - private function is_datauri_candidate( $path ) |
|
273 | + private function is_datauri_candidate($path) |
|
274 | 274 | { |
275 | 275 | // Call only once since it's called from a loop. |
276 | 276 | static $max_size = null; |
277 | - if ( null === $max_size ) { |
|
277 | + if (null === $max_size) { |
|
278 | 278 | $max_size = $this->get_datauri_maxsize(); |
279 | 279 | } |
280 | 280 | |
281 | - if ( $path && preg_match( '#\.(jpe?g|png|gif|webp|bmp)$#i', $path ) && |
|
282 | - file_exists( $path ) && is_readable( $path ) && filesize( $path ) <= $max_size ) { |
|
281 | + if ($path && preg_match('#\.(jpe?g|png|gif|webp|bmp)$#i', $path) && |
|
282 | + file_exists($path) && is_readable($path) && filesize($path) <= $max_size) { |
|
283 | 283 | |
284 | 284 | // Seems we have a candidate. |
285 | 285 | $is_candidate = true; |
286 | 286 | } else { |
287 | 287 | // Filter allows overriding default decision (which checks for local file existence). |
288 | - $is_candidate = apply_filters( 'autoptimize_filter_css_is_datauri_candidate', false, $path ); |
|
288 | + $is_candidate = apply_filters('autoptimize_filter_css_is_datauri_candidate', false, $path); |
|
289 | 289 | } |
290 | 290 | |
291 | 291 | return $is_candidate; |
@@ -310,29 +310,29 @@ discard block |
||
310 | 310 | * of thing you're probably better of building assets completely |
311 | 311 | * outside of WordPress anyway. |
312 | 312 | */ |
313 | - if ( null === $max_size ) { |
|
314 | - $max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', 4096 ); |
|
313 | + if (null === $max_size) { |
|
314 | + $max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', 4096); |
|
315 | 315 | } |
316 | 316 | |
317 | 317 | return $max_size; |
318 | 318 | } |
319 | 319 | |
320 | - private function check_datauri_exclude_list( $url ) |
|
320 | + private function check_datauri_exclude_list($url) |
|
321 | 321 | { |
322 | 322 | static $exclude_list = null; |
323 | 323 | $no_datauris = array(); |
324 | 324 | |
325 | 325 | // Again, skip doing certain stuff repeatedly when loop-called. |
326 | - if ( null === $exclude_list ) { |
|
327 | - $exclude_list = apply_filters( 'autoptimize_filter_css_datauri_exclude', '' ); |
|
328 | - $no_datauris = array_filter( array_map( 'trim', explode( ',', $exclude_list ) ) ); |
|
326 | + if (null === $exclude_list) { |
|
327 | + $exclude_list = apply_filters('autoptimize_filter_css_datauri_exclude', ''); |
|
328 | + $no_datauris = array_filter(array_map('trim', explode(',', $exclude_list))); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | $matched = false; |
332 | 332 | |
333 | - if ( ! empty( $exclude_list ) ) { |
|
334 | - foreach ( $no_datauris as $no_datauri ) { |
|
335 | - if ( false !== strpos( $url, $no_datauri ) ) { |
|
333 | + if (!empty($exclude_list)) { |
|
334 | + foreach ($no_datauris as $no_datauri) { |
|
335 | + if (false !== strpos($url, $no_datauri)) { |
|
336 | 336 | $matched = true; |
337 | 337 | break; |
338 | 338 | } |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | return $matched; |
343 | 343 | } |
344 | 344 | |
345 | - private function build_or_get_datauri_image( $path ) |
|
345 | + private function build_or_get_datauri_image($path) |
|
346 | 346 | { |
347 | 347 | /** |
348 | 348 | * TODO/FIXME: document the required return array format, or better yet, |
@@ -351,27 +351,27 @@ discard block |
||
351 | 351 | */ |
352 | 352 | |
353 | 353 | // Allows short-circuiting datauri generation for an image. |
354 | - $result = apply_filters( 'autoptimize_filter_css_datauri_image', array(), $path ); |
|
355 | - if ( ! empty( $result ) ) { |
|
356 | - if ( is_array( $result ) && isset( $result['full'] ) && isset( $result['base64data'] ) ) { |
|
354 | + $result = apply_filters('autoptimize_filter_css_datauri_image', array(), $path); |
|
355 | + if (!empty($result)) { |
|
356 | + if (is_array($result) && isset($result['full']) && isset($result['base64data'])) { |
|
357 | 357 | return $result; |
358 | 358 | } |
359 | 359 | } |
360 | 360 | |
361 | - $hash = md5( $path ); |
|
362 | - $check = new autoptimizeCache( $hash, 'img' ); |
|
363 | - if ( $check->check() ) { |
|
361 | + $hash = md5($path); |
|
362 | + $check = new autoptimizeCache($hash, 'img'); |
|
363 | + if ($check->check()) { |
|
364 | 364 | // we have the base64 image in cache. |
365 | 365 | $headAndData = $check->retrieve(); |
366 | - $_base64data = explode( ';base64,', $headAndData ); |
|
366 | + $_base64data = explode(';base64,', $headAndData); |
|
367 | 367 | $base64data = $_base64data[1]; |
368 | - unset( $_base64data ); |
|
368 | + unset($_base64data); |
|
369 | 369 | } else { |
370 | 370 | // It's an image and we don't have it in cache, get the type by extension. |
371 | - $exploded_path = explode( '.', $path ); |
|
372 | - $type = end( $exploded_path ); |
|
371 | + $exploded_path = explode('.', $path); |
|
372 | + $type = end($exploded_path); |
|
373 | 373 | |
374 | - switch ( $type ) { |
|
374 | + switch ($type) { |
|
375 | 375 | case 'jpg': |
376 | 376 | case 'jpeg': |
377 | 377 | $dataurihead = 'data:image/jpeg;base64,'; |
@@ -393,15 +393,15 @@ discard block |
||
393 | 393 | } |
394 | 394 | |
395 | 395 | // Encode the data. |
396 | - $base64data = base64_encode( file_get_contents( $path ) ); |
|
397 | - $headAndData = $dataurihead . $base64data; |
|
396 | + $base64data = base64_encode(file_get_contents($path)); |
|
397 | + $headAndData = $dataurihead.$base64data; |
|
398 | 398 | |
399 | 399 | // Save in cache. |
400 | - $check->cache( $headAndData, 'text/plain' ); |
|
400 | + $check->cache($headAndData, 'text/plain'); |
|
401 | 401 | } |
402 | - unset( $check ); |
|
402 | + unset($check); |
|
403 | 403 | |
404 | - return array( 'full' => $headAndData, 'base64data' => $base64data ); |
|
404 | + return array('full' => $headAndData, 'base64data' => $base64data); |
|
405 | 405 | } |
406 | 406 | |
407 | 407 | /** |
@@ -413,13 +413,13 @@ discard block |
||
413 | 413 | * |
414 | 414 | * @return string |
415 | 415 | */ |
416 | - protected static function replace_longest_matches_first( $string, $replacements = array() ) |
|
416 | + protected static function replace_longest_matches_first($string, $replacements = array()) |
|
417 | 417 | { |
418 | - if ( ! empty( $replacements ) ) { |
|
418 | + if (!empty($replacements)) { |
|
419 | 419 | // Sort the replacements array by key length in desc order (so that the longest strings are replaced first). |
420 | - $keys = array_map( 'strlen', array_keys( $replacements ) ); |
|
421 | - array_multisort( $keys, SORT_DESC, $replacements ); |
|
422 | - $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string ); |
|
420 | + $keys = array_map('strlen', array_keys($replacements)); |
|
421 | + array_multisort($keys, SORT_DESC, $replacements); |
|
422 | + $string = str_replace(array_keys($replacements), array_values($replacements), $string); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | return $string; |
@@ -435,15 +435,15 @@ discard block |
||
435 | 435 | * |
436 | 436 | * @return string |
437 | 437 | */ |
438 | - public function replace_urls( $code = '' ) |
|
438 | + public function replace_urls($code = '') |
|
439 | 439 | { |
440 | 440 | $replacements = array(); |
441 | 441 | |
442 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
443 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
444 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
442 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
443 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
444 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
445 | 445 | // Removes quotes and other cruft. |
446 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
446 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
447 | 447 | |
448 | 448 | /** |
449 | 449 | * TODO/FIXME: Add a way for other code / callable to be called here |
@@ -461,17 +461,17 @@ discard block |
||
461 | 461 | */ |
462 | 462 | |
463 | 463 | // Do CDN replacement if needed. |
464 | - if ( ! empty( $this->cdn_url ) ) { |
|
465 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
464 | + if (!empty($this->cdn_url)) { |
|
465 | + $replacement_url = $this->url_replace_cdn($url); |
|
466 | 466 | // Prepare replacements array. |
467 | - $replacements[ $url_src_matches[1][ $count ] ] = str_replace( |
|
467 | + $replacements[$url_src_matches[1][$count]] = str_replace( |
|
468 | 468 | $original_url, $replacement_url, $url_src_matches[1][$count] |
469 | 469 | ); |
470 | 470 | } |
471 | 471 | } |
472 | 472 | } |
473 | 473 | |
474 | - $code = self::replace_longest_matches_first( $code, $replacements ); |
|
474 | + $code = self::replace_longest_matches_first($code, $replacements); |
|
475 | 475 | |
476 | 476 | return $code; |
477 | 477 | } |
@@ -484,26 +484,26 @@ discard block |
||
484 | 484 | * @param string $code |
485 | 485 | * @return string |
486 | 486 | */ |
487 | - public function hide_fontface_and_maybe_cdn( $code ) |
|
487 | + public function hide_fontface_and_maybe_cdn($code) |
|
488 | 488 | { |
489 | 489 | // Proceed only if @font-face declarations exist within $code. |
490 | - preg_match_all( self::FONT_FACE_REGEX, $code, $fontfaces ); |
|
491 | - if ( isset( $fontfaces[0] ) ) { |
|
490 | + preg_match_all(self::FONT_FACE_REGEX, $code, $fontfaces); |
|
491 | + if (isset($fontfaces[0])) { |
|
492 | 492 | // Check if we need to cdn fonts or not. |
493 | - $do_font_cdn = apply_filters( 'autoptimize_filter_css_fonts_cdn', false ); |
|
493 | + $do_font_cdn = apply_filters('autoptimize_filter_css_fonts_cdn', false); |
|
494 | 494 | |
495 | - foreach ( $fontfaces[0] as $full_match ) { |
|
495 | + foreach ($fontfaces[0] as $full_match) { |
|
496 | 496 | // Keep original match so we can search/replace it. |
497 | 497 | $match_search = $full_match; |
498 | 498 | |
499 | 499 | // Do font cdn if needed. |
500 | - if ( $do_font_cdn ) { |
|
501 | - $full_match = $this->replace_urls( $full_match ); |
|
500 | + if ($do_font_cdn) { |
|
501 | + $full_match = $this->replace_urls($full_match); |
|
502 | 502 | } |
503 | 503 | |
504 | 504 | // Replace declaration with its base64 encoded string. |
505 | - $replacement = self::build_marker( 'FONTFACE', $full_match ); |
|
506 | - $code = str_replace( $match_search, $replacement, $code ); |
|
505 | + $replacement = self::build_marker('FONTFACE', $full_match); |
|
506 | + $code = str_replace($match_search, $replacement, $code); |
|
507 | 507 | } |
508 | 508 | } |
509 | 509 | |
@@ -517,16 +517,16 @@ discard block |
||
517 | 517 | * @param string $code |
518 | 518 | * @return string |
519 | 519 | */ |
520 | - public function restore_fontface( $code ) |
|
520 | + public function restore_fontface($code) |
|
521 | 521 | { |
522 | - return $this->restore_marked_content( 'FONTFACE', $code ); |
|
522 | + return $this->restore_marked_content('FONTFACE', $code); |
|
523 | 523 | } |
524 | 524 | |
525 | 525 | // Re-write (and/or inline) referenced assets. |
526 | - public function rewrite_assets( $code ) |
|
526 | + public function rewrite_assets($code) |
|
527 | 527 | { |
528 | 528 | // Handle @font-face rules by hiding and processing them separately. |
529 | - $code = $this->hide_fontface_and_maybe_cdn( $code ); |
|
529 | + $code = $this->hide_fontface_and_maybe_cdn($code); |
|
530 | 530 | |
531 | 531 | /** |
532 | 532 | * TODO/FIXME: |
@@ -542,30 +542,30 @@ discard block |
||
542 | 542 | $url_src_matches = array(); |
543 | 543 | $imgreplace = array(); |
544 | 544 | // Matches and captures anything specified within the literal `url()` and excludes those containing data: URIs. |
545 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
546 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
547 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
545 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
546 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
547 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
548 | 548 | // Removes quotes and other cruft. |
549 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
549 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
550 | 550 | |
551 | 551 | // If datauri inlining is turned on, do it. |
552 | 552 | $inlined = false; |
553 | - if ( $this->datauris ) { |
|
553 | + if ($this->datauris) { |
|
554 | 554 | $iurl = $url; |
555 | - if ( false !== strpos( $iurl, '?' ) ) { |
|
556 | - $iurl = strtok( $iurl, '?' ); |
|
555 | + if (false !== strpos($iurl, '?')) { |
|
556 | + $iurl = strtok($iurl, '?'); |
|
557 | 557 | } |
558 | 558 | |
559 | - $ipath = $this->getpath( $iurl ); |
|
559 | + $ipath = $this->getpath($iurl); |
|
560 | 560 | |
561 | - $excluded = $this->check_datauri_exclude_list( $ipath ); |
|
562 | - if ( ! $excluded ) { |
|
563 | - $is_datauri_candidate = $this->is_datauri_candidate( $ipath ); |
|
564 | - if ( $is_datauri_candidate ) { |
|
565 | - $datauri = $this->build_or_get_datauri_image( $ipath ); |
|
561 | + $excluded = $this->check_datauri_exclude_list($ipath); |
|
562 | + if (!$excluded) { |
|
563 | + $is_datauri_candidate = $this->is_datauri_candidate($ipath); |
|
564 | + if ($is_datauri_candidate) { |
|
565 | + $datauri = $this->build_or_get_datauri_image($ipath); |
|
566 | 566 | $base64data = $datauri['base64data']; |
567 | 567 | // Add it to the list for replacement. |
568 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
568 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
569 | 569 | $original_url, |
570 | 570 | $datauri['full'], |
571 | 571 | $url_src_matches[1][$count] |
@@ -581,20 +581,20 @@ discard block |
||
581 | 581 | * inlining isn't turned on, or if a resource is skipped from |
582 | 582 | * being inlined for whatever reason above. |
583 | 583 | */ |
584 | - if ( ! $inlined && ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) ) { |
|
584 | + if (!$inlined && (!empty($this->cdn_url) || has_filter('autoptimize_filter_base_replace_cdn'))) { |
|
585 | 585 | // Just do the "simple" CDN replacement. |
586 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
587 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
586 | + $replacement_url = $this->url_replace_cdn($url); |
|
587 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
588 | 588 | $original_url, $replacement_url, $url_src_matches[1][$count] |
589 | 589 | ); |
590 | 590 | } |
591 | 591 | } |
592 | 592 | } |
593 | 593 | |
594 | - $code = self::replace_longest_matches_first( $code, $imgreplace ); |
|
594 | + $code = self::replace_longest_matches_first($code, $imgreplace); |
|
595 | 595 | |
596 | 596 | // Replace back font-face markers with actual font-face declarations. |
597 | - $code = $this->restore_fontface( $code ); |
|
597 | + $code = $this->restore_fontface($code); |
|
598 | 598 | |
599 | 599 | return $code; |
600 | 600 | } |
@@ -602,29 +602,29 @@ discard block |
||
602 | 602 | // Joins and optimizes CSS. |
603 | 603 | public function minify() |
604 | 604 | { |
605 | - foreach ( $this->css as $group ) { |
|
606 | - list( $media, $css ) = $group; |
|
607 | - if ( preg_match( '#^INLINE;#', $css ) ) { |
|
605 | + foreach ($this->css as $group) { |
|
606 | + list($media, $css) = $group; |
|
607 | + if (preg_match('#^INLINE;#', $css)) { |
|
608 | 608 | // <style>. |
609 | - $css = preg_replace( '#^INLINE;#', '', $css ); |
|
610 | - $css = self::fixurls( ABSPATH . 'index.php', $css ); // ABSPATH already contains a trailing slash. |
|
611 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, '' ); |
|
612 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
609 | + $css = preg_replace('#^INLINE;#', '', $css); |
|
610 | + $css = self::fixurls(ABSPATH.'index.php', $css); // ABSPATH already contains a trailing slash. |
|
611 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, ''); |
|
612 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
613 | 613 | $css = $tmpstyle; |
614 | 614 | $this->alreadyminified = true; |
615 | 615 | } |
616 | 616 | } else { |
617 | 617 | // <link> |
618 | - if ( false !== $css && file_exists( $css ) && is_readable( $css ) ) { |
|
618 | + if (false !== $css && file_exists($css) && is_readable($css)) { |
|
619 | 619 | $cssPath = $css; |
620 | - $css = self::fixurls( $cssPath, file_get_contents( $cssPath ) ); |
|
621 | - $css = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $css ); |
|
622 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath ); |
|
623 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
620 | + $css = self::fixurls($cssPath, file_get_contents($cssPath)); |
|
621 | + $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css); |
|
622 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath); |
|
623 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
624 | 624 | $css = $tmpstyle; |
625 | 625 | $this->alreadyminified = true; |
626 | - } elseif ( $this->can_inject_late( $cssPath, $css ) ) { |
|
627 | - $css = self::build_injectlater_marker( $cssPath, md5( $css ) ); |
|
626 | + } elseif ($this->can_inject_late($cssPath, $css)) { |
|
627 | + $css = self::build_injectlater_marker($cssPath, md5($css)); |
|
628 | 628 | } |
629 | 629 | } else { |
630 | 630 | // Couldn't read CSS. Maybe getpath isn't working? |
@@ -632,12 +632,12 @@ discard block |
||
632 | 632 | } |
633 | 633 | } |
634 | 634 | |
635 | - foreach ( $media as $elem ) { |
|
636 | - if ( ! empty( $css ) ) { |
|
637 | - if ( ! isset( $this->csscode[$elem] ) ) { |
|
635 | + foreach ($media as $elem) { |
|
636 | + if (!empty($css)) { |
|
637 | + if (!isset($this->csscode[$elem])) { |
|
638 | 638 | $this->csscode[$elem] = ''; |
639 | 639 | } |
640 | - $this->csscode[$elem] .= "\n/*FILESTART*/" . $css; |
|
640 | + $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
|
641 | 641 | } |
642 | 642 | } |
643 | 643 | } |
@@ -645,133 +645,133 @@ discard block |
||
645 | 645 | // Check for duplicate code. |
646 | 646 | $md5list = array(); |
647 | 647 | $tmpcss = $this->csscode; |
648 | - foreach ( $tmpcss as $media => $code ) { |
|
649 | - $md5sum = md5( $code ); |
|
648 | + foreach ($tmpcss as $media => $code) { |
|
649 | + $md5sum = md5($code); |
|
650 | 650 | $medianame = $media; |
651 | - foreach ( $md5list as $med => $sum ) { |
|
651 | + foreach ($md5list as $med => $sum) { |
|
652 | 652 | // If same code. |
653 | - if ( $sum === $md5sum ) { |
|
653 | + if ($sum === $md5sum) { |
|
654 | 654 | // Add the merged code. |
655 | - $medianame = $med . ', ' . $media; |
|
655 | + $medianame = $med.', '.$media; |
|
656 | 656 | $this->csscode[$medianame] = $code; |
657 | 657 | $md5list[$medianame] = $md5list[$med]; |
658 | - unset( $this->csscode[$med], $this->csscode[$media], $md5list[$med] ); |
|
658 | + unset($this->csscode[$med], $this->csscode[$media], $md5list[$med]); |
|
659 | 659 | } |
660 | 660 | } |
661 | 661 | $md5list[$medianame] = $md5sum; |
662 | 662 | } |
663 | - unset( $tmpcss ); |
|
663 | + unset($tmpcss); |
|
664 | 664 | |
665 | 665 | // Manage @imports, while is for recursive import management. |
666 | - foreach ( $this->csscode as &$thiscss ) { |
|
666 | + foreach ($this->csscode as &$thiscss) { |
|
667 | 667 | // Flag to trigger import reconstitution and var to hold external imports. |
668 | 668 | $fiximports = false; |
669 | 669 | $external_imports = ''; |
670 | 670 | |
671 | 671 | // remove comments to avoid importing commented-out imports. |
672 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
673 | - while ( preg_match_all( '#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches ) ) { |
|
674 | - foreach ( $matches[0] as $import ) { |
|
675 | - if ( $this->isremovable( $import, $this->cssremovables ) ) { |
|
676 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
672 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
673 | + while (preg_match_all('#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches)) { |
|
674 | + foreach ($matches[0] as $import) { |
|
675 | + if ($this->isremovable($import, $this->cssremovables)) { |
|
676 | + $thiscss = str_replace($import, '', $thiscss); |
|
677 | 677 | $import_ok = true; |
678 | 678 | } else { |
679 | - $url = trim( preg_replace( '#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim( $import ) ), " \t\n\r\0\x0B\"'" ); |
|
680 | - $path = $this->getpath( $url ); |
|
679 | + $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'"); |
|
680 | + $path = $this->getpath($url); |
|
681 | 681 | $import_ok = false; |
682 | - if ( file_exists( $path ) && is_readable( $path ) ) { |
|
683 | - $code = addcslashes( self::fixurls( $path, file_get_contents( $path ) ), "\\" ); |
|
684 | - $code = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $code ); |
|
685 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, '' ); |
|
686 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
682 | + if (file_exists($path) && is_readable($path)) { |
|
683 | + $code = addcslashes(self::fixurls($path, file_get_contents($path)), "\\"); |
|
684 | + $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code); |
|
685 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, ''); |
|
686 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
687 | 687 | $code = $tmpstyle; |
688 | 688 | $this->alreadyminified = true; |
689 | - } elseif ( $this->can_inject_late( $path, $code ) ) { |
|
690 | - $code = self::build_injectlater_marker( $path, md5( $code ) ); |
|
689 | + } elseif ($this->can_inject_late($path, $code)) { |
|
690 | + $code = self::build_injectlater_marker($path, md5($code)); |
|
691 | 691 | } |
692 | 692 | |
693 | - if ( ! empty( $code ) ) { |
|
694 | - $tmp_thiscss = preg_replace( '#(/\*FILESTART\*/.*)' . preg_quote( $import, '#' ) . '#Us', '/*FILESTART2*/' . $code . '$1', $thiscss ); |
|
695 | - if ( ! empty( $tmp_thiscss ) ) { |
|
693 | + if (!empty($code)) { |
|
694 | + $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss); |
|
695 | + if (!empty($tmp_thiscss)) { |
|
696 | 696 | $thiscss = $tmp_thiscss; |
697 | 697 | $import_ok = true; |
698 | - unset( $tmp_thiscss ); |
|
698 | + unset($tmp_thiscss); |
|
699 | 699 | } |
700 | 700 | } |
701 | - unset( $code ); |
|
701 | + unset($code); |
|
702 | 702 | } |
703 | 703 | } |
704 | - if ( ! $import_ok ) { |
|
704 | + if (!$import_ok) { |
|
705 | 705 | // External imports and general fall-back. |
706 | 706 | $external_imports .= $import; |
707 | 707 | |
708 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
708 | + $thiscss = str_replace($import, '', $thiscss); |
|
709 | 709 | $fiximports = true; |
710 | 710 | } |
711 | 711 | } |
712 | - $thiscss = preg_replace( '#/\*FILESTART\*/#', '', $thiscss ); |
|
713 | - $thiscss = preg_replace( '#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss ); |
|
712 | + $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss); |
|
713 | + $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss); |
|
714 | 714 | |
715 | 715 | // and update $thiscss_nocomments before going into next iteration in while loop. |
716 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
716 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
717 | 717 | } |
718 | - unset( $thiscss_nocomments ); |
|
718 | + unset($thiscss_nocomments); |
|
719 | 719 | |
720 | 720 | // Add external imports to top of aggregated CSS. |
721 | - if ( $fiximports ) { |
|
722 | - $thiscss = $external_imports . $thiscss; |
|
721 | + if ($fiximports) { |
|
722 | + $thiscss = $external_imports.$thiscss; |
|
723 | 723 | } |
724 | 724 | } |
725 | - unset( $thiscss ); |
|
725 | + unset($thiscss); |
|
726 | 726 | |
727 | 727 | // $this->csscode has all the uncompressed code now. |
728 | - foreach ( $this->csscode as &$code ) { |
|
728 | + foreach ($this->csscode as &$code) { |
|
729 | 729 | // Check for already-minified code. |
730 | - $hash = md5( $code ); |
|
731 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
732 | - $ccheck = new autoptimizeCache( $hash, 'css' ); |
|
733 | - if ( $ccheck->check() ) { |
|
730 | + $hash = md5($code); |
|
731 | + do_action('autoptimize_action_css_hash', $hash); |
|
732 | + $ccheck = new autoptimizeCache($hash, 'css'); |
|
733 | + if ($ccheck->check()) { |
|
734 | 734 | $code = $ccheck->retrieve(); |
735 | - $this->hashmap[md5( $code )] = $hash; |
|
735 | + $this->hashmap[md5($code)] = $hash; |
|
736 | 736 | continue; |
737 | 737 | } |
738 | - unset( $ccheck ); |
|
738 | + unset($ccheck); |
|
739 | 739 | |
740 | 740 | // Rewrite and/or inline referenced assets. |
741 | - $code = $this->rewrite_assets( $code ); |
|
741 | + $code = $this->rewrite_assets($code); |
|
742 | 742 | |
743 | 743 | // Minify. |
744 | - $code = $this->run_minifier_on( $code ); |
|
744 | + $code = $this->run_minifier_on($code); |
|
745 | 745 | |
746 | 746 | // Bring back INJECTLATER stuff. |
747 | - $code = $this->inject_minified( $code ); |
|
747 | + $code = $this->inject_minified($code); |
|
748 | 748 | |
749 | 749 | // Filter results. |
750 | - $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code ); |
|
751 | - if ( ! empty( $tmp_code ) ) { |
|
750 | + $tmp_code = apply_filters('autoptimize_css_after_minify', $code); |
|
751 | + if (!empty($tmp_code)) { |
|
752 | 752 | $code = $tmp_code; |
753 | - unset( $tmp_code ); |
|
753 | + unset($tmp_code); |
|
754 | 754 | } |
755 | 755 | |
756 | - $this->hashmap[md5( $code )] = $hash; |
|
756 | + $this->hashmap[md5($code)] = $hash; |
|
757 | 757 | } |
758 | 758 | |
759 | - unset( $code ); |
|
759 | + unset($code); |
|
760 | 760 | return true; |
761 | 761 | } |
762 | 762 | |
763 | - public function run_minifier_on( $code ) |
|
763 | + public function run_minifier_on($code) |
|
764 | 764 | { |
765 | - if ( ! $this->alreadyminified ) { |
|
766 | - $do_minify = apply_filters( 'autoptimize_css_do_minify', true ); |
|
765 | + if (!$this->alreadyminified) { |
|
766 | + $do_minify = apply_filters('autoptimize_css_do_minify', true); |
|
767 | 767 | |
768 | - if ( $do_minify ) { |
|
768 | + if ($do_minify) { |
|
769 | 769 | $cssmin = new autoptimizeCSSmin(); |
770 | - $tmp_code = trim( $cssmin->run( $code ) ); |
|
770 | + $tmp_code = trim($cssmin->run($code)); |
|
771 | 771 | |
772 | - if ( ! empty( $tmp_code ) ) { |
|
772 | + if (!empty($tmp_code)) { |
|
773 | 773 | $code = $tmp_code; |
774 | - unset( $tmp_code ); |
|
774 | + unset($tmp_code); |
|
775 | 775 | } |
776 | 776 | } |
777 | 777 | } |
@@ -783,14 +783,14 @@ discard block |
||
783 | 783 | public function cache() |
784 | 784 | { |
785 | 785 | // CSS cache. |
786 | - foreach ( $this->csscode as $media => $code ) { |
|
787 | - $md5 = $this->hashmap[md5( $code )]; |
|
788 | - $cache = new autoptimizeCache( $md5, 'css' ); |
|
789 | - if ( ! $cache->check() ) { |
|
786 | + foreach ($this->csscode as $media => $code) { |
|
787 | + $md5 = $this->hashmap[md5($code)]; |
|
788 | + $cache = new autoptimizeCache($md5, 'css'); |
|
789 | + if (!$cache->check()) { |
|
790 | 790 | // Cache our code. |
791 | - $cache->cache( $code, 'text/css' ); |
|
791 | + $cache->cache($code, 'text/css'); |
|
792 | 792 | } |
793 | - $this->url[$media] = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
793 | + $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
794 | 794 | } |
795 | 795 | } |
796 | 796 | |
@@ -798,88 +798,88 @@ discard block |
||
798 | 798 | public function getcontent() |
799 | 799 | { |
800 | 800 | // restore comments. |
801 | - $this->content = $this->restore_comments( $this->content ); |
|
801 | + $this->content = $this->restore_comments($this->content); |
|
802 | 802 | |
803 | 803 | // restore IE hacks. |
804 | - $this->content = $this->restore_iehacks( $this->content ); |
|
804 | + $this->content = $this->restore_iehacks($this->content); |
|
805 | 805 | |
806 | 806 | // restore (no)script. |
807 | - $this->content = $this->restore_marked_content( 'SCRIPT', $this->content ); |
|
807 | + $this->content = $this->restore_marked_content('SCRIPT', $this->content); |
|
808 | 808 | |
809 | 809 | // Restore noptimize. |
810 | - $this->content = $this->restore_noptimize( $this->content ); |
|
810 | + $this->content = $this->restore_noptimize($this->content); |
|
811 | 811 | |
812 | 812 | // Restore the full content. |
813 | - if ( ! empty( $this->restofcontent ) ) { |
|
813 | + if (!empty($this->restofcontent)) { |
|
814 | 814 | $this->content .= $this->restofcontent; |
815 | 815 | $this->restofcontent = ''; |
816 | 816 | } |
817 | 817 | |
818 | 818 | // Inject the new stylesheets. |
819 | - $replaceTag = array( '<title', 'before' ); |
|
820 | - $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag, $this->content ); |
|
819 | + $replaceTag = array('<title', 'before'); |
|
820 | + $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag, $this->content); |
|
821 | 821 | |
822 | - if ( $this->inline ) { |
|
823 | - foreach ( $this->csscode as $media => $code ) { |
|
824 | - $this->inject_in_html( '<style type="text/css" media="' . $media . '">' . $code . '</style>', $replaceTag ); |
|
822 | + if ($this->inline) { |
|
823 | + foreach ($this->csscode as $media => $code) { |
|
824 | + $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag); |
|
825 | 825 | } |
826 | 826 | } else { |
827 | - if ( $this->defer ) { |
|
827 | + if ($this->defer) { |
|
828 | 828 | $preloadCssBlock = ''; |
829 | 829 | $noScriptCssBlock = "<noscript id=\"aonoscrcss\">"; |
830 | 830 | |
831 | 831 | $defer_inline_code = $this->defer_inline; |
832 | - if ( ! empty( $defer_inline_code ) ) { |
|
833 | - if ( apply_filters( 'autoptimize_filter_css_critcss_minify', true ) ) { |
|
834 | - $iCssHash = md5( $defer_inline_code ); |
|
835 | - $iCssCache = new autoptimizeCache( $iCssHash, 'css' ); |
|
836 | - if ( $iCssCache->check() ) { |
|
832 | + if (!empty($defer_inline_code)) { |
|
833 | + if (apply_filters('autoptimize_filter_css_critcss_minify', true)) { |
|
834 | + $iCssHash = md5($defer_inline_code); |
|
835 | + $iCssCache = new autoptimizeCache($iCssHash, 'css'); |
|
836 | + if ($iCssCache->check()) { |
|
837 | 837 | // we have the optimized inline CSS in cache. |
838 | 838 | $defer_inline_code = $iCssCache->retrieve(); |
839 | 839 | } else { |
840 | 840 | $cssmin = new autoptimizeCSSmin(); |
841 | - $tmp_code = trim( $cssmin->run( $defer_inline_code ) ); |
|
841 | + $tmp_code = trim($cssmin->run($defer_inline_code)); |
|
842 | 842 | |
843 | - if ( ! empty( $tmp_code ) ) { |
|
843 | + if (!empty($tmp_code)) { |
|
844 | 844 | $defer_inline_code = $tmp_code; |
845 | - $iCssCache->cache( $defer_inline_code, 'text/css' ); |
|
846 | - unset( $tmp_code ); |
|
845 | + $iCssCache->cache($defer_inline_code, 'text/css'); |
|
846 | + unset($tmp_code); |
|
847 | 847 | } |
848 | 848 | } |
849 | 849 | } |
850 | - $code_out = '<style type="text/css" id="aoatfcss" media="all">' . $defer_inline_code . '</style>'; |
|
851 | - $this->inject_in_html( $code_out, $replaceTag ); |
|
850 | + $code_out = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
851 | + $this->inject_in_html($code_out, $replaceTag); |
|
852 | 852 | } |
853 | 853 | } |
854 | 854 | |
855 | - foreach ( $this->url as $media => $url ) { |
|
856 | - $url = $this->url_replace_cdn( $url ); |
|
855 | + foreach ($this->url as $media => $url) { |
|
856 | + $url = $this->url_replace_cdn($url); |
|
857 | 857 | |
858 | 858 | // Add the stylesheet either deferred (import at bottom) or normal links in head. |
859 | - if ( $this->defer ) { |
|
859 | + if ($this->defer) { |
|
860 | 860 | $preloadOnLoad = autoptimizeConfig::get_ao_css_preload_onload(); |
861 | 861 | |
862 | - $preloadCssBlock .= '<link rel="preload" as="style" media="' . $media . '" href="' . $url . '" onload="' . $preloadOnLoad . '" />'; |
|
863 | - $noScriptCssBlock .= '<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />'; |
|
862 | + $preloadCssBlock .= '<link rel="preload" as="style" media="'.$media.'" href="'.$url.'" onload="'.$preloadOnLoad.'" />'; |
|
863 | + $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />'; |
|
864 | 864 | } else { |
865 | 865 | // $this->inject_in_html('<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />', $replaceTag); |
866 | - if ( strlen( $this->csscode[$media] ) > $this->cssinlinesize ) { |
|
867 | - $this->inject_in_html( '<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />', $replaceTag ); |
|
868 | - } elseif ( strlen( $this->csscode[$media] ) > 0 ) { |
|
869 | - $this->inject_in_html( '<style type="text/css" media="' . $media . '">' . $this->csscode[$media] . '</style>', $replaceTag ); |
|
866 | + if (strlen($this->csscode[$media]) > $this->cssinlinesize) { |
|
867 | + $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag); |
|
868 | + } elseif (strlen($this->csscode[$media]) > 0) { |
|
869 | + $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag); |
|
870 | 870 | } |
871 | 871 | } |
872 | 872 | } |
873 | 873 | |
874 | - if ( $this->defer ) { |
|
874 | + if ($this->defer) { |
|
875 | 875 | $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill(); |
876 | 876 | $noScriptCssBlock .= '</noscript>'; |
877 | - $this->inject_in_html( $preloadCssBlock . $noScriptCssBlock, $replaceTag ); |
|
877 | + $this->inject_in_html($preloadCssBlock.$noScriptCssBlock, $replaceTag); |
|
878 | 878 | |
879 | 879 | // Adds preload polyfill at end of body tag. |
880 | 880 | $this->inject_in_html( |
881 | - apply_filters( 'autoptimize_css_preload_polyfill', $preload_polyfill ), |
|
882 | - array( '</body>', 'before' ) |
|
881 | + apply_filters('autoptimize_css_preload_polyfill', $preload_polyfill), |
|
882 | + array('</body>', 'before') |
|
883 | 883 | ); |
884 | 884 | } |
885 | 885 | } |
@@ -888,44 +888,44 @@ discard block |
||
888 | 888 | return $this->content; |
889 | 889 | } |
890 | 890 | |
891 | - static function fixurls( $file, $code ) |
|
891 | + static function fixurls($file, $code) |
|
892 | 892 | { |
893 | 893 | // Switch all imports to the url() syntax. |
894 | - $code = preg_replace( '#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code ); |
|
894 | + $code = preg_replace('#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code); |
|
895 | 895 | |
896 | - if ( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) { |
|
897 | - $file = str_replace( WP_ROOT_DIR, '/', $file ); |
|
896 | + if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) { |
|
897 | + $file = str_replace(WP_ROOT_DIR, '/', $file); |
|
898 | 898 | /** |
899 | 899 | * rollback as per https://github.com/futtta/autoptimize/issues/94 |
900 | 900 | * $file = str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', $file ); |
901 | 901 | */ |
902 | - $dir = dirname( $file ); // Like /themes/expound/css. |
|
902 | + $dir = dirname($file); // Like /themes/expound/css. |
|
903 | 903 | |
904 | 904 | /** |
905 | 905 | * $dir should not contain backslashes, since it's used to replace |
906 | 906 | * urls, but it can contain them when running on Windows because |
907 | 907 | * fixurls() is sometimes called with `ABSPATH . 'index.php'` |
908 | 908 | */ |
909 | - $dir = str_replace( '\\', '/', $dir ); |
|
910 | - unset( $file ); // not used below at all. |
|
909 | + $dir = str_replace('\\', '/', $dir); |
|
910 | + unset($file); // not used below at all. |
|
911 | 911 | |
912 | 912 | $replace = array(); |
913 | - foreach ( $matches[1] as $k => $url ) { |
|
913 | + foreach ($matches[1] as $k => $url) { |
|
914 | 914 | // Remove quotes. |
915 | - $url = trim( $url, " \t\n\r\0\x0B\"'" ); |
|
916 | - $noQurl = trim( $url, "\"'" ); |
|
917 | - if ( $url !== $noQurl ) { |
|
915 | + $url = trim($url, " \t\n\r\0\x0B\"'"); |
|
916 | + $noQurl = trim($url, "\"'"); |
|
917 | + if ($url !== $noQurl) { |
|
918 | 918 | $removedQuotes = true; |
919 | 919 | } else { |
920 | 920 | $removedQuotes = false; |
921 | 921 | } |
922 | 922 | |
923 | - if ( '' === $noQurl ) { |
|
923 | + if ('' === $noQurl) { |
|
924 | 924 | continue; |
925 | 925 | } |
926 | 926 | |
927 | 927 | $url = $noQurl; |
928 | - if ( '/' === $url{0} || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) { |
|
928 | + if ('/' === $url{0} || preg_match('#^(https?://|ftp://|data:)#i', $url)) { |
|
929 | 929 | // URL is protocol-relative, host-relative or something we don't touch. |
930 | 930 | continue; |
931 | 931 | } else { |
@@ -934,49 +934,49 @@ discard block |
||
934 | 934 | * rollback as per https://github.com/futtta/autoptimize/issues/94 |
935 | 935 | * $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_CONTENT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
936 | 936 | */ |
937 | - $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
|
938 | - $newurl = apply_filters( 'autoptimize_filter_css_fixurl_newurl', $newurl ); |
|
937 | + $newurl = preg_replace('/https?:/', '', str_replace(' ', '%20', AUTOPTIMIZE_WP_ROOT_URL.str_replace('//', '/', $dir.'/'.$url))); |
|
938 | + $newurl = apply_filters('autoptimize_filter_css_fixurl_newurl', $newurl); |
|
939 | 939 | |
940 | 940 | /** |
941 | 941 | * Hash the url + whatever was behind potentially for replacement |
942 | 942 | * We must do this, or different css classes referencing the same bg image (but |
943 | 943 | * different parts of it, say, in sprites and such) loose their stuff... |
944 | 944 | */ |
945 | - $hash = md5( $url . $matches[2][$k] ); |
|
946 | - $code = str_replace( $matches[0][$k], $hash, $code ); |
|
945 | + $hash = md5($url.$matches[2][$k]); |
|
946 | + $code = str_replace($matches[0][$k], $hash, $code); |
|
947 | 947 | |
948 | - if ( $removedQuotes ) { |
|
949 | - $replace[$hash] = "url('" . $newurl . "')" . $matches[2][$k]; |
|
948 | + if ($removedQuotes) { |
|
949 | + $replace[$hash] = "url('".$newurl."')".$matches[2][$k]; |
|
950 | 950 | } else { |
951 | - $replace[$hash] = 'url(' . $newurl . ')' . $matches[2][$k]; |
|
951 | + $replace[$hash] = 'url('.$newurl.')'.$matches[2][$k]; |
|
952 | 952 | } |
953 | 953 | } |
954 | 954 | } |
955 | 955 | |
956 | - $code = self::replace_longest_matches_first( $code, $replace ); |
|
956 | + $code = self::replace_longest_matches_first($code, $replace); |
|
957 | 957 | } |
958 | 958 | |
959 | 959 | return $code; |
960 | 960 | } |
961 | 961 | |
962 | - private function ismovable( $tag ) |
|
962 | + private function ismovable($tag) |
|
963 | 963 | { |
964 | - if ( ! $this->aggregate ) { |
|
964 | + if (!$this->aggregate) { |
|
965 | 965 | return false; |
966 | 966 | } |
967 | 967 | |
968 | - if ( ! empty( $this->whitelist ) ) { |
|
969 | - foreach ( $this->whitelist as $match ) { |
|
970 | - if ( false !== strpos( $tag, $match ) ) { |
|
968 | + if (!empty($this->whitelist)) { |
|
969 | + foreach ($this->whitelist as $match) { |
|
970 | + if (false !== strpos($tag, $match)) { |
|
971 | 971 | return true; |
972 | 972 | } |
973 | 973 | } |
974 | 974 | // no match with whitelist. |
975 | 975 | return false; |
976 | 976 | } else { |
977 | - if ( is_array( $this->dontmove ) && ! empty( $this->dontmove ) ) { |
|
978 | - foreach ( $this->dontmove as $match ) { |
|
979 | - if ( false !== strpos( $tag, $match ) ) { |
|
977 | + if (is_array($this->dontmove) && !empty($this->dontmove)) { |
|
978 | + foreach ($this->dontmove as $match) { |
|
979 | + if (false !== strpos($tag, $match)) { |
|
980 | 980 | // Matched something. |
981 | 981 | return false; |
982 | 982 | } |
@@ -988,22 +988,22 @@ discard block |
||
988 | 988 | } |
989 | 989 | } |
990 | 990 | |
991 | - private function can_inject_late( $cssPath, $css ) |
|
991 | + private function can_inject_late($cssPath, $css) |
|
992 | 992 | { |
993 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $cssPath ); |
|
994 | - if ( true !== $this->inject_min_late ) { |
|
993 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $cssPath); |
|
994 | + if (true !== $this->inject_min_late) { |
|
995 | 995 | // late-inject turned off. |
996 | 996 | return false; |
997 | - } elseif ( ( false === strpos( $cssPath, 'min.css' ) ) && ( str_replace( $consider_minified_array, '', $cssPath ) === $cssPath ) ) { |
|
997 | + } elseif ((false === strpos($cssPath, 'min.css')) && (str_replace($consider_minified_array, '', $cssPath) === $cssPath)) { |
|
998 | 998 | // file not minified based on filename & filter. |
999 | 999 | return false; |
1000 | - } elseif ( false !== strpos( $css, '@import' ) ) { |
|
1000 | + } elseif (false !== strpos($css, '@import')) { |
|
1001 | 1001 | // can't late-inject files with imports as those need to be aggregated. |
1002 | 1002 | return false; |
1003 | - } elseif ( ( false !== strpos( $css, '@font-face' ) ) && ( apply_filters( 'autoptimize_filter_css_fonts_cdn', false ) === true ) && ( ! empty( $this->cdn_url ) ) ) { |
|
1003 | + } elseif ((false !== strpos($css, '@font-face')) && (apply_filters('autoptimize_filter_css_fonts_cdn', false) === true) && (!empty($this->cdn_url))) { |
|
1004 | 1004 | // don't late-inject CSS with font-src's if fonts are set to be CDN'ed. |
1005 | 1005 | return false; |
1006 | - } elseif ( ( ( $this->datauris == true ) || ( ! empty( $this->cdn_url ) ) ) && preg_match( '#background[^;}]*url\(#Ui', $css ) ) { |
|
1006 | + } elseif ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match('#background[^;}]*url\(#Ui', $css)) { |
|
1007 | 1007 | // don't late-inject CSS with images if CDN is set OR if image inlining is on. |
1008 | 1008 | return false; |
1009 | 1009 | } else { |
@@ -1021,32 +1021,32 @@ discard block |
||
1021 | 1021 | * |
1022 | 1022 | * @return bool|string Url pointing to the minified css file or false. |
1023 | 1023 | */ |
1024 | - public function minify_single( $filepath, $cache_miss = false ) |
|
1024 | + public function minify_single($filepath, $cache_miss = false) |
|
1025 | 1025 | { |
1026 | - $contents = $this->prepare_minify_single( $filepath ); |
|
1026 | + $contents = $this->prepare_minify_single($filepath); |
|
1027 | 1027 | |
1028 | - if ( empty( $contents ) ) { |
|
1028 | + if (empty($contents)) { |
|
1029 | 1029 | return false; |
1030 | 1030 | } |
1031 | 1031 | |
1032 | 1032 | // Check cache. |
1033 | - $hash = 'single_' . md5( $contents ); |
|
1034 | - $cache = new autoptimizeCache( $hash, 'css' ); |
|
1033 | + $hash = 'single_'.md5($contents); |
|
1034 | + $cache = new autoptimizeCache($hash, 'css'); |
|
1035 | 1035 | |
1036 | 1036 | // If not in cache already, minify... |
1037 | - if ( ! $cache->check() || $cache_miss ) { |
|
1037 | + if (!$cache->check() || $cache_miss) { |
|
1038 | 1038 | // Fixurls... |
1039 | - $contents = self::fixurls( $filepath, $contents ); |
|
1039 | + $contents = self::fixurls($filepath, $contents); |
|
1040 | 1040 | // CDN-replace any referenced assets if needed... |
1041 | - $contents = $this->replace_urls( $contents ); |
|
1041 | + $contents = $this->replace_urls($contents); |
|
1042 | 1042 | // Now minify... |
1043 | 1043 | $cssmin = new autoptimizeCSSmin(); |
1044 | - $contents = trim( $cssmin->run( $contents ) ); |
|
1044 | + $contents = trim($cssmin->run($contents)); |
|
1045 | 1045 | // Store in cache. |
1046 | - $cache->cache( $contents, 'text/css' ); |
|
1046 | + $cache->cache($contents, 'text/css'); |
|
1047 | 1047 | } |
1048 | 1048 | |
1049 | - $url = $this->build_minify_single_url( $cache ); |
|
1049 | + $url = $this->build_minify_single_url($cache); |
|
1050 | 1050 | |
1051 | 1051 | return $url; |
1052 | 1052 | } |
@@ -1066,18 +1066,18 @@ discard block |
||
1066 | 1066 | return $this->options; |
1067 | 1067 | } |
1068 | 1068 | |
1069 | - public function replaceOptions( $options ) |
|
1069 | + public function replaceOptions($options) |
|
1070 | 1070 | { |
1071 | 1071 | $this->options = $options; |
1072 | 1072 | } |
1073 | 1073 | |
1074 | - public function setOption( $name, $value ) |
|
1074 | + public function setOption($name, $value) |
|
1075 | 1075 | { |
1076 | 1076 | $this->options[$name] = $value; |
1077 | 1077 | $this->$name = $value; |
1078 | 1078 | } |
1079 | 1079 | |
1080 | - public function getOption( $name ) |
|
1080 | + public function getOption($name) |
|
1081 | 1081 | { |
1082 | 1082 | return $this->options[$name]; |
1083 | 1083 | } |