@@ -12,29 +12,29 @@ |
||
12 | 12 | .ao_settings_div {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;} |
13 | 13 | .ao_settings_div .form-table th {font-weight: normal;} |
14 | 14 | </style> |
15 | - <script>document.title = "Autoptimize: <?php _e( 'Critical CSS', 'autoptimize' ); ?> " + document.title;</script> |
|
15 | + <script>document.title = "Autoptimize: <?php _e('Critical CSS', 'autoptimize'); ?> " + document.title;</script> |
|
16 | 16 | <ul id="explain-panel"> |
17 | 17 | <div class="ao_settings_div"> |
18 | 18 | <?php |
19 | 19 | $ccss_explanation = ''; |
20 | 20 | |
21 | 21 | // get the HTML with the explanation of what critical CSS is. |
22 | - if ( apply_filters( 'autoptimize_settingsscreen_remotehttp', true ) ) { |
|
23 | - $ccss_explanation = get_transient( 'ao_ccss_explain' ); |
|
24 | - if ( empty( $ccss_explanation ) ) { |
|
25 | - $ccss_expl_resp = wp_remote_get( 'https://misc.optimizingmatters.com/autoptimize_ccss_explain_i18n.html?ao_ver=' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
26 | - if ( ! is_wp_error( $ccss_expl_resp ) ) { |
|
27 | - if ( '200' == wp_remote_retrieve_response_code( $ccss_expl_resp ) ) { |
|
28 | - $ccss_explanation = wp_kses_post( wp_remote_retrieve_body( $ccss_expl_resp ) ); |
|
29 | - set_transient( 'ao_ccss_explain', $ccss_explanation, WEEK_IN_SECONDS ); |
|
22 | + if (apply_filters('autoptimize_settingsscreen_remotehttp', true)) { |
|
23 | + $ccss_explanation = get_transient('ao_ccss_explain'); |
|
24 | + if (empty($ccss_explanation)) { |
|
25 | + $ccss_expl_resp = wp_remote_get('https://misc.optimizingmatters.com/autoptimize_ccss_explain_i18n.html?ao_ver='.AUTOPTIMIZE_PLUGIN_VERSION); |
|
26 | + if (!is_wp_error($ccss_expl_resp)) { |
|
27 | + if ('200' == wp_remote_retrieve_response_code($ccss_expl_resp)) { |
|
28 | + $ccss_explanation = wp_kses_post(wp_remote_retrieve_body($ccss_expl_resp)); |
|
29 | + set_transient('ao_ccss_explain', $ccss_explanation, WEEK_IN_SECONDS); |
|
30 | 30 | } |
31 | 31 | } |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | 35 | // placeholder text in case HTML is empty. |
36 | - if ( empty( $ccss_explanation ) ) { |
|
37 | - $ccss_explanation = __( '<h2>Fix render-blocking CSS!</h2><p>Significantly improve your first-paint times by making CSS non-render-blocking.</p><p>The next step is to sign up at <a href="https://criticalcss.com/?aff=1" target="_blank">https://criticalcss.com</a> (this is a premium service, priced 2 GBP/month for membership and 5 GBP/month per domain) <strong>and get the API key, which you can copy from <a href="https://criticalcss.com/account/api-keys?aff=1" target="_blank">the API-keys page</a></strong> and paste below.</p><p>If you have any questions or need support, head on over to <a href="https://wordpress.org/support/plugin/autoptimize" target="_blank">our support forum</a> and we\'ll help you get up and running in no time!</p>', 'autoptimize' ); |
|
36 | + if (empty($ccss_explanation)) { |
|
37 | + $ccss_explanation = __('<h2>Fix render-blocking CSS!</h2><p>Significantly improve your first-paint times by making CSS non-render-blocking.</p><p>The next step is to sign up at <a href="https://criticalcss.com/?aff=1" target="_blank">https://criticalcss.com</a> (this is a premium service, priced 2 GBP/month for membership and 5 GBP/month per domain) <strong>and get the API key, which you can copy from <a href="https://criticalcss.com/account/api-keys?aff=1" target="_blank">the API-keys page</a></strong> and paste below.</p><p>If you have any questions or need support, head on over to <a href="https://wordpress.org/support/plugin/autoptimize" target="_blank">our support forum</a> and we\'ll help you get up and running in no time!</p>', 'autoptimize'); |
|
38 | 38 | } else { |
39 | 39 | // we were able to fetch the explenation, so add the JS to show correct language. |
40 | 40 | $ccss_explanation .= "<script>jQuery('.ao_i18n').hide();d=document;lang=d.getElementsByTagName('html')[0].getAttribute('lang').substring(0,2);if(d.getElementById(lang)!= null){jQuery('#'+lang).show();}else{jQuery('#default').show();}</script>"; |
@@ -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 | |
@@ -161,54 +161,54 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @param array $options all options. |
163 | 163 | */ |
164 | - public function read( $options ) |
|
164 | + public function read($options) |
|
165 | 165 | { |
166 | - $noptimize_css = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ); |
|
167 | - if ( $noptimize_css ) { |
|
166 | + $noptimize_css = apply_filters('autoptimize_filter_css_noptimize', false, $this->content); |
|
167 | + if ($noptimize_css) { |
|
168 | 168 | return false; |
169 | 169 | } |
170 | 170 | |
171 | - $allowlist_css = apply_filters( 'autoptimize_filter_css_allowlist', '', $this->content ); |
|
172 | - $allowlist_css = apply_filters( 'autoptimize_filter_css_whitelist', $allowlist_css, $this->content ); // fixme: to be removed in next version. |
|
173 | - if ( ! empty( $allowlist_css ) ) { |
|
174 | - $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_css ) ) ); |
|
171 | + $allowlist_css = apply_filters('autoptimize_filter_css_allowlist', '', $this->content); |
|
172 | + $allowlist_css = apply_filters('autoptimize_filter_css_whitelist', $allowlist_css, $this->content); // fixme: to be removed in next version. |
|
173 | + if (!empty($allowlist_css)) { |
|
174 | + $this->allowlist = array_filter(array_map('trim', explode(',', $allowlist_css))); |
|
175 | 175 | } |
176 | 176 | |
177 | - $removable_css = apply_filters( 'autoptimize_filter_css_removables', '' ); |
|
178 | - if ( ! empty( $removable_css ) ) { |
|
179 | - $this->cssremovables = array_filter( array_map( 'trim', explode( ',', $removable_css ) ) ); |
|
177 | + $removable_css = apply_filters('autoptimize_filter_css_removables', ''); |
|
178 | + if (!empty($removable_css)) { |
|
179 | + $this->cssremovables = array_filter(array_map('trim', explode(',', $removable_css))); |
|
180 | 180 | } |
181 | 181 | |
182 | - $this->cssinlinesize = apply_filters( 'autoptimize_filter_css_inlinesize', 256 ); |
|
182 | + $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256); |
|
183 | 183 | |
184 | 184 | // filter to "late inject minified CSS", default to true for now (it is faster). |
185 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_css_inject_min_late', true ); |
|
185 | + $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true); |
|
186 | 186 | |
187 | 187 | // Remove everything that's not the header. |
188 | - if ( apply_filters( 'autoptimize_filter_css_justhead', $options['justhead'] ) ) { |
|
189 | - $content = explode( '</head>', $this->content, 2 ); |
|
190 | - $this->content = $content[0] . '</head>'; |
|
188 | + if (apply_filters('autoptimize_filter_css_justhead', $options['justhead'])) { |
|
189 | + $content = explode('</head>', $this->content, 2); |
|
190 | + $this->content = $content[0].'</head>'; |
|
191 | 191 | $this->restofcontent = $content[1]; |
192 | 192 | } |
193 | 193 | |
194 | 194 | // Determine whether we're doing CSS-files aggregation or not. |
195 | - if ( isset( $options['aggregate'] ) && ! $options['aggregate'] ) { |
|
195 | + if (isset($options['aggregate']) && !$options['aggregate']) { |
|
196 | 196 | $this->aggregate = false; |
197 | 197 | } |
198 | 198 | // Returning true for "dontaggregate" turns off aggregation. |
199 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_css_dontaggregate', false ) ) { |
|
199 | + if ($this->aggregate && apply_filters('autoptimize_filter_css_dontaggregate', false)) { |
|
200 | 200 | $this->aggregate = false; |
201 | 201 | } |
202 | 202 | |
203 | 203 | // include inline? |
204 | - if ( apply_filters( 'autoptimize_css_include_inline', $options['include_inline'] ) ) { |
|
204 | + if (apply_filters('autoptimize_css_include_inline', $options['include_inline'])) { |
|
205 | 205 | $this->include_inline = true; |
206 | 206 | } |
207 | 207 | |
208 | 208 | // List of CSS strings which are excluded from autoptimization. |
209 | - $exclude_css = apply_filters( 'autoptimize_filter_css_exclude', $options['css_exclude'], $this->content ); |
|
210 | - if ( '' !== $exclude_css ) { |
|
211 | - $this->dontmove = array_filter( array_map( 'trim', explode( ',', $exclude_css ) ) ); |
|
209 | + $exclude_css = apply_filters('autoptimize_filter_css_exclude', $options['css_exclude'], $this->content); |
|
210 | + if ('' !== $exclude_css) { |
|
211 | + $this->dontmove = array_filter(array_map('trim', explode(',', $exclude_css))); |
|
212 | 212 | } else { |
213 | 213 | $this->dontmove = array(); |
214 | 214 | } |
@@ -219,16 +219,16 @@ discard block |
||
219 | 219 | // Should we defer css? |
220 | 220 | // value: true / false. |
221 | 221 | $this->defer = $options['defer']; |
222 | - $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer, $this->content ); |
|
222 | + $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer, $this->content); |
|
223 | 223 | |
224 | 224 | // Should we inline while deferring? |
225 | 225 | // value: inlined CSS. |
226 | - $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content ); |
|
226 | + $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content); |
|
227 | 227 | |
228 | 228 | // Should we inline? |
229 | 229 | // value: true / false. |
230 | 230 | $this->inline = $options['inline']; |
231 | - $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline, $this->content ); |
|
231 | + $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline, $this->content); |
|
232 | 232 | |
233 | 233 | // Store cdn url. |
234 | 234 | $this->cdn_url = $options['cdn_url']; |
@@ -237,16 +237,16 @@ discard block |
||
237 | 237 | $this->datauris = $options['datauris']; |
238 | 238 | |
239 | 239 | // Determine whether excluded files should be minified if not yet so. |
240 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
240 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
241 | 241 | $this->minify_excluded = false; |
242 | 242 | } |
243 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_css_minify_excluded', $this->minify_excluded, '' ); |
|
243 | + $this->minify_excluded = apply_filters('autoptimize_filter_css_minify_excluded', $this->minify_excluded, ''); |
|
244 | 244 | |
245 | 245 | // should we force all media-attributes to all? |
246 | - $this->media_force_all = apply_filters( 'autoptimize_filter_css_tagmedia_forceall', false ); |
|
246 | + $this->media_force_all = apply_filters('autoptimize_filter_css_tagmedia_forceall', false); |
|
247 | 247 | |
248 | 248 | // noptimize me. |
249 | - $this->content = $this->hide_noptimize( $this->content ); |
|
249 | + $this->content = $this->hide_noptimize($this->content); |
|
250 | 250 | |
251 | 251 | // Exclude (no)script, as those may contain CSS which should be left as is. |
252 | 252 | $this->content = $this->replace_contents_with_marker_if_exists( |
@@ -257,25 +257,25 @@ discard block |
||
257 | 257 | ); |
258 | 258 | |
259 | 259 | // Save IE hacks. |
260 | - $this->content = $this->hide_iehacks( $this->content ); |
|
260 | + $this->content = $this->hide_iehacks($this->content); |
|
261 | 261 | |
262 | 262 | // Hide HTML comments. |
263 | - $this->content = $this->hide_comments( $this->content ); |
|
263 | + $this->content = $this->hide_comments($this->content); |
|
264 | 264 | |
265 | 265 | // Get <style> and <link>. |
266 | - if ( preg_match_all( '#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches ) ) { |
|
266 | + if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) { |
|
267 | 267 | |
268 | - foreach ( $matches[0] as $tag ) { |
|
269 | - if ( $this->isremovable( $tag, $this->cssremovables ) ) { |
|
270 | - $this->content = str_replace( $tag, '', $this->content ); |
|
271 | - } elseif ( $this->ismovable( $tag ) ) { |
|
268 | + foreach ($matches[0] as $tag) { |
|
269 | + if ($this->isremovable($tag, $this->cssremovables)) { |
|
270 | + $this->content = str_replace($tag, '', $this->content); |
|
271 | + } elseif ($this->ismovable($tag)) { |
|
272 | 272 | // Get the media. |
273 | - if ( false !== strpos( $tag, 'media=' ) ) { |
|
274 | - preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias ); |
|
275 | - $medias = explode( ',', $medias[1] ); |
|
273 | + if (false !== strpos($tag, 'media=')) { |
|
274 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias); |
|
275 | + $medias = explode(',', $medias[1]); |
|
276 | 276 | $media = array(); |
277 | - foreach ( $medias as $elem ) { |
|
278 | - if ( empty( $elem ) ) { |
|
277 | + foreach ($medias as $elem) { |
|
278 | + if (empty($elem)) { |
|
279 | 279 | $elem = 'all'; |
280 | 280 | } |
281 | 281 | |
@@ -283,68 +283,68 @@ discard block |
||
283 | 283 | } |
284 | 284 | } else { |
285 | 285 | // No media specified - applies to all. |
286 | - $media = array( 'all' ); |
|
286 | + $media = array('all'); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | // forcing media attribute to all to merge all in one file. |
290 | - if ( $this->media_force_all ) { |
|
291 | - $media = array( 'all' ); |
|
290 | + if ($this->media_force_all) { |
|
291 | + $media = array('all'); |
|
292 | 292 | } |
293 | 293 | |
294 | - $media = apply_filters( 'autoptimize_filter_css_tagmedia', $media, $tag ); |
|
294 | + $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag); |
|
295 | 295 | |
296 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
296 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
297 | 297 | // <link>. |
298 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
299 | - $path = $this->getpath( $url ); |
|
298 | + $url = current(explode('?', $source[2], 2)); |
|
299 | + $path = $this->getpath($url); |
|
300 | 300 | |
301 | - if ( false !== $path && preg_match( '#\.css$#', $path ) ) { |
|
301 | + if (false !== $path && preg_match('#\.css$#', $path)) { |
|
302 | 302 | // Good link. |
303 | - $this->css[] = array( $media, $path ); |
|
303 | + $this->css[] = array($media, $path); |
|
304 | 304 | } else { |
305 | 305 | // Link is dynamic (.php etc). |
306 | - $new_tag = $this->optionally_defer_excluded( $tag, 'none' ); |
|
307 | - if ( '' !== $new_tag && $new_tag !== $tag ) { |
|
308 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
306 | + $new_tag = $this->optionally_defer_excluded($tag, 'none'); |
|
307 | + if ('' !== $new_tag && $new_tag !== $tag) { |
|
308 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
309 | 309 | } |
310 | 310 | $tag = ''; |
311 | 311 | } |
312 | 312 | } else { |
313 | 313 | // Inline css in style tags can be wrapped in comment tags, so restore comments. |
314 | - $tag = $this->restore_comments( $tag ); |
|
315 | - preg_match( '#<style.*>(.*)</style>#Usmi', $tag, $code ); |
|
314 | + $tag = $this->restore_comments($tag); |
|
315 | + preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code); |
|
316 | 316 | |
317 | 317 | // And re-hide them to be able to to the removal based on tag. |
318 | - $tag = $this->hide_comments( $tag ); |
|
318 | + $tag = $this->hide_comments($tag); |
|
319 | 319 | |
320 | - if ( $this->include_inline ) { |
|
321 | - $code = preg_replace( '#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1] ); |
|
322 | - $this->css[] = array( $media, 'INLINE;' . $code ); |
|
320 | + if ($this->include_inline) { |
|
321 | + $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]); |
|
322 | + $this->css[] = array($media, 'INLINE;'.$code); |
|
323 | 323 | } else { |
324 | 324 | $tag = ''; |
325 | 325 | } |
326 | 326 | } |
327 | 327 | |
328 | 328 | // Remove the original style tag. |
329 | - $this->content = str_replace( $tag, '', $this->content ); |
|
329 | + $this->content = str_replace($tag, '', $this->content); |
|
330 | 330 | } else { |
331 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
332 | - $exploded_url = explode( '?', $source[2], 2 ); |
|
331 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
332 | + $exploded_url = explode('?', $source[2], 2); |
|
333 | 333 | $url = $exploded_url[0]; |
334 | - $path = $this->getpath( $url ); |
|
334 | + $path = $this->getpath($url); |
|
335 | 335 | $new_tag = $tag; |
336 | 336 | |
337 | 337 | // Excluded CSS, minify that file: |
338 | 338 | // -> if aggregate is on and exclude minify is on |
339 | 339 | // -> if aggregate is off and the file is not in dontmove. |
340 | - if ( $path && $this->minify_excluded ) { |
|
341 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false ); |
|
342 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
343 | - $minified_url = $this->minify_single( $path ); |
|
344 | - if ( ! empty( $minified_url ) ) { |
|
340 | + if ($path && $this->minify_excluded) { |
|
341 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false); |
|
342 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
343 | + $minified_url = $this->minify_single($path); |
|
344 | + if (!empty($minified_url)) { |
|
345 | 345 | // Replace orig URL with cached minified URL. |
346 | - $new_tag = str_replace( $url, $minified_url, $tag ); |
|
347 | - } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { |
|
346 | + $new_tag = str_replace($url, $minified_url, $tag); |
|
347 | + } elseif (apply_filters('autoptimize_filter_ccsjs_remove_empty_minified_url', false)) { |
|
348 | 348 | // Remove the original style tag, because cache content is empty but only if |
349 | 349 | // filter is true-ed because $minified_url is also false if file is minified already. |
350 | 350 | $new_tag = ''; |
@@ -352,14 +352,14 @@ discard block |
||
352 | 352 | } |
353 | 353 | } |
354 | 354 | |
355 | - if ( '' !== $new_tag ) { |
|
355 | + if ('' !== $new_tag) { |
|
356 | 356 | // Optionally defer (preload) non-aggregated CSS. |
357 | - $new_tag = $this->optionally_defer_excluded( $new_tag, $url ); |
|
357 | + $new_tag = $this->optionally_defer_excluded($new_tag, $url); |
|
358 | 358 | } |
359 | 359 | |
360 | 360 | // And replace! |
361 | - if ( ( '' !== $new_tag && $new_tag !== $tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_css_remove_empty_files', false ) ) ) { |
|
362 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
361 | + if (('' !== $new_tag && $new_tag !== $tag) || ('' === $new_tag && apply_filters('autoptimize_filter_css_remove_empty_files', false))) { |
|
362 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
363 | 363 | } |
364 | 364 | } |
365 | 365 | } |
@@ -380,30 +380,30 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return string $new_tag |
382 | 382 | */ |
383 | - private function optionally_defer_excluded( $tag, $url = '' ) |
|
383 | + private function optionally_defer_excluded($tag, $url = '') |
|
384 | 384 | { |
385 | 385 | // Defer single CSS if "inline & defer" is ON and there is inline CSS. |
386 | - if ( ! empty( $tag ) && false === strpos( $tag, ' onload=' ) && $this->defer && ! empty( $this->defer_inline ) && apply_filters( 'autoptimize_filter_css_defer_excluded', true, $tag ) ) { |
|
386 | + if (!empty($tag) && false === strpos($tag, ' onload=') && $this->defer && !empty($this->defer_inline) && apply_filters('autoptimize_filter_css_defer_excluded', true, $tag)) { |
|
387 | 387 | // get media attribute and based on that create onload JS attribute value. |
388 | - if ( false === strpos( $tag, 'media=' ) ) { |
|
389 | - $tag = str_replace( '<link', "<link media='all'", $tag ); |
|
388 | + if (false === strpos($tag, 'media=')) { |
|
389 | + $tag = str_replace('<link', "<link media='all'", $tag); |
|
390 | 390 | } |
391 | 391 | |
392 | - preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $_medias ); |
|
392 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $_medias); |
|
393 | 393 | $_media = $_medias[1]; |
394 | - $_preload_onload = autoptimizeConfig::get_ao_css_preload_onload( $_media ); |
|
394 | + $_preload_onload = autoptimizeConfig::get_ao_css_preload_onload($_media); |
|
395 | 395 | |
396 | - if ( 'print' !== $_media ) { |
|
396 | + if ('print' !== $_media) { |
|
397 | 397 | // If not media=print, adapt original <link> element for CSS to be preloaded and add <noscript>-version for fallback. |
398 | - $new_tag = '<noscript>' . autoptimizeUtils::remove_id_from_node( $tag ) . '</noscript>' . str_replace( |
|
398 | + $new_tag = '<noscript>'.autoptimizeUtils::remove_id_from_node($tag).'</noscript>'.str_replace( |
|
399 | 399 | $_medias[0], |
400 | - "media='print' onload=\"" . $_preload_onload . '"', |
|
400 | + "media='print' onload=\"".$_preload_onload.'"', |
|
401 | 401 | $tag |
402 | 402 | ); |
403 | 403 | |
404 | 404 | // Optionally (but default false) preload the (excluded) CSS-file. |
405 | - if ( apply_filters( 'autoptimize_fitler_css_preload_and_print', false ) && 'none' !== $url ) { |
|
406 | - $new_tag = '<link rel="preload" as="stylesheet" href="' . $url . '"/>' . $new_tag; |
|
405 | + if (apply_filters('autoptimize_fitler_css_preload_and_print', false) && 'none' !== $url) { |
|
406 | + $new_tag = '<link rel="preload" as="stylesheet" href="'.$url.'"/>'.$new_tag; |
|
407 | 407 | } |
408 | 408 | } else { |
409 | 409 | $new_tag = $tag; |
@@ -423,22 +423,22 @@ discard block |
||
423 | 423 | * @param string $path image path. |
424 | 424 | * @return boolean |
425 | 425 | */ |
426 | - private function is_datauri_candidate( $path ) |
|
426 | + private function is_datauri_candidate($path) |
|
427 | 427 | { |
428 | 428 | // Call only once since it's called from a loop. |
429 | 429 | static $max_size = null; |
430 | - if ( null === $max_size ) { |
|
430 | + if (null === $max_size) { |
|
431 | 431 | $max_size = $this->get_datauri_maxsize(); |
432 | 432 | } |
433 | 433 | |
434 | - if ( $path && preg_match( '#\.(jpe?g|png|gif|webp|bmp)$#i', $path ) && |
|
435 | - file_exists( $path ) && is_readable( $path ) && filesize( $path ) <= $max_size ) { |
|
434 | + if ($path && preg_match('#\.(jpe?g|png|gif|webp|bmp)$#i', $path) && |
|
435 | + file_exists($path) && is_readable($path) && filesize($path) <= $max_size) { |
|
436 | 436 | |
437 | 437 | // Seems we have a candidate. |
438 | 438 | $is_candidate = true; |
439 | 439 | } else { |
440 | 440 | // Filter allows overriding default decision (which checks for local file existence). |
441 | - $is_candidate = apply_filters( 'autoptimize_filter_css_is_datauri_candidate', false, $path ); |
|
441 | + $is_candidate = apply_filters('autoptimize_filter_css_is_datauri_candidate', false, $path); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | return $is_candidate; |
@@ -463,29 +463,29 @@ discard block |
||
463 | 463 | * of thing you're probably better of building assets completely |
464 | 464 | * outside of WordPress anyway. |
465 | 465 | */ |
466 | - if ( null === $max_size ) { |
|
467 | - $max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', 4096 ); |
|
466 | + if (null === $max_size) { |
|
467 | + $max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', 4096); |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | return $max_size; |
471 | 471 | } |
472 | 472 | |
473 | - private function check_datauri_exclude_list( $url ) |
|
473 | + private function check_datauri_exclude_list($url) |
|
474 | 474 | { |
475 | 475 | static $exclude_list = null; |
476 | 476 | $no_datauris = array(); |
477 | 477 | |
478 | 478 | // Again, skip doing certain stuff repeatedly when loop-called. |
479 | - if ( null === $exclude_list ) { |
|
480 | - $exclude_list = apply_filters( 'autoptimize_filter_css_datauri_exclude', '' ); |
|
481 | - $no_datauris = array_filter( array_map( 'trim', explode( ',', $exclude_list ) ) ); |
|
479 | + if (null === $exclude_list) { |
|
480 | + $exclude_list = apply_filters('autoptimize_filter_css_datauri_exclude', ''); |
|
481 | + $no_datauris = array_filter(array_map('trim', explode(',', $exclude_list))); |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | $matched = false; |
485 | 485 | |
486 | - if ( ! empty( $exclude_list ) ) { |
|
487 | - foreach ( $no_datauris as $no_datauri ) { |
|
488 | - if ( false !== strpos( $url, $no_datauri ) ) { |
|
486 | + if (!empty($exclude_list)) { |
|
487 | + foreach ($no_datauris as $no_datauri) { |
|
488 | + if (false !== strpos($url, $no_datauri)) { |
|
489 | 489 | $matched = true; |
490 | 490 | break; |
491 | 491 | } |
@@ -495,7 +495,7 @@ discard block |
||
495 | 495 | return $matched; |
496 | 496 | } |
497 | 497 | |
498 | - private function build_or_get_datauri_image( $path ) |
|
498 | + private function build_or_get_datauri_image($path) |
|
499 | 499 | { |
500 | 500 | /** |
501 | 501 | * TODO/FIXME: document the required return array format, or better yet, |
@@ -504,27 +504,27 @@ discard block |
||
504 | 504 | */ |
505 | 505 | |
506 | 506 | // Allows short-circuiting datauri generation for an image. |
507 | - $result = apply_filters( 'autoptimize_filter_css_datauri_image', array(), $path ); |
|
508 | - if ( ! empty( $result ) ) { |
|
509 | - if ( is_array( $result ) && isset( $result['full'] ) && isset( $result['base64data'] ) ) { |
|
507 | + $result = apply_filters('autoptimize_filter_css_datauri_image', array(), $path); |
|
508 | + if (!empty($result)) { |
|
509 | + if (is_array($result) && isset($result['full']) && isset($result['base64data'])) { |
|
510 | 510 | return $result; |
511 | 511 | } |
512 | 512 | } |
513 | 513 | |
514 | - $hash = md5( $path ); |
|
515 | - $check = new autoptimizeCache( $hash, 'img' ); |
|
516 | - if ( $check->check() ) { |
|
514 | + $hash = md5($path); |
|
515 | + $check = new autoptimizeCache($hash, 'img'); |
|
516 | + if ($check->check()) { |
|
517 | 517 | // we have the base64 image in cache. |
518 | 518 | $head_and_data = $check->retrieve(); |
519 | - $_base64data = explode( ';base64,', $head_and_data ); |
|
519 | + $_base64data = explode(';base64,', $head_and_data); |
|
520 | 520 | $base64data = $_base64data[1]; |
521 | - unset( $_base64data ); |
|
521 | + unset($_base64data); |
|
522 | 522 | } else { |
523 | 523 | // It's an image and we don't have it in cache, get the type by extension. |
524 | - $exploded_path = explode( '.', $path ); |
|
525 | - $type = end( $exploded_path ); |
|
524 | + $exploded_path = explode('.', $path); |
|
525 | + $type = end($exploded_path); |
|
526 | 526 | |
527 | - switch ( $type ) { |
|
527 | + switch ($type) { |
|
528 | 528 | case 'jpg': |
529 | 529 | case 'jpeg': |
530 | 530 | $dataurihead = 'data:image/jpeg;base64,'; |
@@ -546,13 +546,13 @@ discard block |
||
546 | 546 | } |
547 | 547 | |
548 | 548 | // Encode the data. |
549 | - $base64data = base64_encode( file_get_contents( $path ) ); |
|
550 | - $head_and_data = $dataurihead . $base64data; |
|
549 | + $base64data = base64_encode(file_get_contents($path)); |
|
550 | + $head_and_data = $dataurihead.$base64data; |
|
551 | 551 | |
552 | 552 | // Save in cache. |
553 | - $check->cache( $head_and_data, 'text/plain' ); |
|
553 | + $check->cache($head_and_data, 'text/plain'); |
|
554 | 554 | } |
555 | - unset( $check ); |
|
555 | + unset($check); |
|
556 | 556 | |
557 | 557 | return array( |
558 | 558 | 'full' => $head_and_data, |
@@ -569,13 +569,13 @@ discard block |
||
569 | 569 | * |
570 | 570 | * @return string |
571 | 571 | */ |
572 | - protected static function replace_longest_matches_first( $string, $replacements = array() ) |
|
572 | + protected static function replace_longest_matches_first($string, $replacements = array()) |
|
573 | 573 | { |
574 | - if ( ! empty( $replacements ) ) { |
|
574 | + if (!empty($replacements)) { |
|
575 | 575 | // Sort the replacements array by key length in desc order (so that the longest strings are replaced first). |
576 | - $keys = array_map( 'strlen', array_keys( $replacements ) ); |
|
577 | - array_multisort( $keys, SORT_DESC, $replacements ); |
|
578 | - $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string ); |
|
576 | + $keys = array_map('strlen', array_keys($replacements)); |
|
577 | + array_multisort($keys, SORT_DESC, $replacements); |
|
578 | + $string = str_replace(array_keys($replacements), array_values($replacements), $string); |
|
579 | 579 | } |
580 | 580 | |
581 | 581 | return $string; |
@@ -591,15 +591,15 @@ discard block |
||
591 | 591 | * |
592 | 592 | * @return string |
593 | 593 | */ |
594 | - public function replace_urls( $code = '' ) |
|
594 | + public function replace_urls($code = '') |
|
595 | 595 | { |
596 | 596 | $replacements = array(); |
597 | 597 | |
598 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
599 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
600 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
598 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
599 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
600 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
601 | 601 | // Removes quotes and other cruft. |
602 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
602 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
603 | 603 | |
604 | 604 | /** |
605 | 605 | * TODO/FIXME: Add a way for other code / callable to be called here |
@@ -617,17 +617,17 @@ discard block |
||
617 | 617 | */ |
618 | 618 | |
619 | 619 | // Do CDN replacement if needed. |
620 | - if ( ! empty( $this->cdn_url ) ) { |
|
621 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
620 | + if (!empty($this->cdn_url)) { |
|
621 | + $replacement_url = $this->url_replace_cdn($url); |
|
622 | 622 | // Prepare replacements array. |
623 | - $replacements[ $url_src_matches[1][ $count ] ] = str_replace( |
|
624 | - $original_url, $replacement_url, $url_src_matches[1][ $count ] |
|
623 | + $replacements[$url_src_matches[1][$count]] = str_replace( |
|
624 | + $original_url, $replacement_url, $url_src_matches[1][$count] |
|
625 | 625 | ); |
626 | 626 | } |
627 | 627 | } |
628 | 628 | } |
629 | 629 | |
630 | - $code = self::replace_longest_matches_first( $code, $replacements ); |
|
630 | + $code = self::replace_longest_matches_first($code, $replacements); |
|
631 | 631 | |
632 | 632 | return $code; |
633 | 633 | } |
@@ -640,26 +640,26 @@ discard block |
||
640 | 640 | * @param string $code HTML being processed to hide fonts. |
641 | 641 | * @return string |
642 | 642 | */ |
643 | - public function hide_fontface_and_maybe_cdn( $code ) |
|
643 | + public function hide_fontface_and_maybe_cdn($code) |
|
644 | 644 | { |
645 | 645 | // Proceed only if @font-face declarations exist within $code. |
646 | - preg_match_all( self::FONT_FACE_REGEX, $code, $fontfaces ); |
|
647 | - if ( isset( $fontfaces[0] ) ) { |
|
646 | + preg_match_all(self::FONT_FACE_REGEX, $code, $fontfaces); |
|
647 | + if (isset($fontfaces[0])) { |
|
648 | 648 | // Check if we need to cdn fonts or not. |
649 | - $do_font_cdn = apply_filters( 'autoptimize_filter_css_fonts_cdn', false ); |
|
649 | + $do_font_cdn = apply_filters('autoptimize_filter_css_fonts_cdn', false); |
|
650 | 650 | |
651 | - foreach ( $fontfaces[0] as $full_match ) { |
|
651 | + foreach ($fontfaces[0] as $full_match) { |
|
652 | 652 | // Keep original match so we can search/replace it. |
653 | 653 | $match_search = $full_match; |
654 | 654 | |
655 | 655 | // Do font cdn if needed. |
656 | - if ( $do_font_cdn ) { |
|
657 | - $full_match = $this->replace_urls( $full_match ); |
|
656 | + if ($do_font_cdn) { |
|
657 | + $full_match = $this->replace_urls($full_match); |
|
658 | 658 | } |
659 | 659 | |
660 | 660 | // Replace declaration with its base64 encoded string. |
661 | - $replacement = self::build_marker( 'FONTFACE', $full_match ); |
|
662 | - $code = str_replace( $match_search, $replacement, $code ); |
|
661 | + $replacement = self::build_marker('FONTFACE', $full_match); |
|
662 | + $code = str_replace($match_search, $replacement, $code); |
|
663 | 663 | } |
664 | 664 | } |
665 | 665 | |
@@ -673,9 +673,9 @@ discard block |
||
673 | 673 | * @param string $code HTML being processed to unhide fonts. |
674 | 674 | * @return string |
675 | 675 | */ |
676 | - public function restore_fontface( $code ) |
|
676 | + public function restore_fontface($code) |
|
677 | 677 | { |
678 | - return $this->restore_marked_content( 'FONTFACE', $code ); |
|
678 | + return $this->restore_marked_content('FONTFACE', $code); |
|
679 | 679 | } |
680 | 680 | |
681 | 681 | /** |
@@ -684,10 +684,10 @@ discard block |
||
684 | 684 | * @param string $code HTML being processed rewrite assets. |
685 | 685 | * @return string |
686 | 686 | */ |
687 | - public function rewrite_assets( $code ) |
|
687 | + public function rewrite_assets($code) |
|
688 | 688 | { |
689 | 689 | // Handle @font-face rules by hiding and processing them separately. |
690 | - $code = $this->hide_fontface_and_maybe_cdn( $code ); |
|
690 | + $code = $this->hide_fontface_and_maybe_cdn($code); |
|
691 | 691 | |
692 | 692 | /** |
693 | 693 | * TODO/FIXME: |
@@ -704,35 +704,35 @@ discard block |
||
704 | 704 | $imgreplace = array(); |
705 | 705 | |
706 | 706 | // Matches and captures anything specified within the literal `url()` and excludes those containing data: URIs. |
707 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
708 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
709 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
707 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
708 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
709 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
710 | 710 | // Removes quotes and other cruft. |
711 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
711 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
712 | 712 | |
713 | 713 | // If datauri inlining is turned on, do it. |
714 | 714 | $inlined = false; |
715 | - if ( $this->datauris ) { |
|
715 | + if ($this->datauris) { |
|
716 | 716 | $iurl = $url; |
717 | - if ( false !== strpos( $iurl, '?' ) ) { |
|
718 | - $iurl = strtok( $iurl, '?' ); |
|
717 | + if (false !== strpos($iurl, '?')) { |
|
718 | + $iurl = strtok($iurl, '?'); |
|
719 | 719 | } |
720 | 720 | |
721 | - $ipath = $this->getpath( $iurl ); |
|
721 | + $ipath = $this->getpath($iurl); |
|
722 | 722 | |
723 | - $excluded = $this->check_datauri_exclude_list( $ipath ); |
|
724 | - if ( ! $excluded ) { |
|
725 | - $is_datauri_candidate = $this->is_datauri_candidate( $ipath ); |
|
726 | - if ( $is_datauri_candidate ) { |
|
727 | - $datauri = $this->build_or_get_datauri_image( $ipath ); |
|
723 | + $excluded = $this->check_datauri_exclude_list($ipath); |
|
724 | + if (!$excluded) { |
|
725 | + $is_datauri_candidate = $this->is_datauri_candidate($ipath); |
|
726 | + if ($is_datauri_candidate) { |
|
727 | + $datauri = $this->build_or_get_datauri_image($ipath); |
|
728 | 728 | $base64data = $datauri['base64data']; |
729 | 729 | // Add it to the list for replacement. |
730 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
730 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
731 | 731 | $original_url, |
732 | 732 | $datauri['full'], |
733 | - $url_src_matches[1][ $count ] |
|
733 | + $url_src_matches[1][$count] |
|
734 | 734 | ); |
735 | - $inlined = true; |
|
735 | + $inlined = true; |
|
736 | 736 | } |
737 | 737 | } |
738 | 738 | } |
@@ -743,20 +743,20 @@ discard block |
||
743 | 743 | * inlining isn't turned on, or if a resource is skipped from |
744 | 744 | * being inlined for whatever reason above. |
745 | 745 | */ |
746 | - if ( ! $inlined && ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) ) { |
|
746 | + if (!$inlined && (!empty($this->cdn_url) || has_filter('autoptimize_filter_base_replace_cdn'))) { |
|
747 | 747 | // Just do the "simple" CDN replacement. |
748 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
749 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
750 | - $original_url, $replacement_url, $url_src_matches[1][ $count ] |
|
748 | + $replacement_url = $this->url_replace_cdn($url); |
|
749 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
750 | + $original_url, $replacement_url, $url_src_matches[1][$count] |
|
751 | 751 | ); |
752 | 752 | } |
753 | 753 | } |
754 | 754 | } |
755 | 755 | |
756 | - $code = self::replace_longest_matches_first( $code, $imgreplace ); |
|
756 | + $code = self::replace_longest_matches_first($code, $imgreplace); |
|
757 | 757 | |
758 | 758 | // Replace back font-face markers with actual font-face declarations. |
759 | - $code = $this->restore_fontface( $code ); |
|
759 | + $code = $this->restore_fontface($code); |
|
760 | 760 | |
761 | 761 | return $code; |
762 | 762 | } |
@@ -766,29 +766,29 @@ discard block |
||
766 | 766 | */ |
767 | 767 | public function minify() |
768 | 768 | { |
769 | - foreach ( $this->css as $group ) { |
|
770 | - list( $media, $css ) = $group; |
|
771 | - if ( preg_match( '#^INLINE;#', $css ) ) { |
|
769 | + foreach ($this->css as $group) { |
|
770 | + list($media, $css) = $group; |
|
771 | + if (preg_match('#^INLINE;#', $css)) { |
|
772 | 772 | // <style>. |
773 | - $css = preg_replace( '#^INLINE;#', '', $css ); |
|
774 | - $css = self::fixurls( ABSPATH . 'index.php', $css ); // ABSPATH already contains a trailing slash. |
|
775 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, '' ); |
|
776 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
773 | + $css = preg_replace('#^INLINE;#', '', $css); |
|
774 | + $css = self::fixurls(ABSPATH.'index.php', $css); // ABSPATH already contains a trailing slash. |
|
775 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, ''); |
|
776 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
777 | 777 | $css = $tmpstyle; |
778 | 778 | $this->alreadyminified = true; |
779 | 779 | } |
780 | 780 | } else { |
781 | 781 | // <link> |
782 | - if ( false !== $css && file_exists( $css ) && is_readable( $css ) ) { |
|
782 | + if (false !== $css && file_exists($css) && is_readable($css)) { |
|
783 | 783 | $css_path = $css; |
784 | - $css = self::fixurls( $css_path, file_get_contents( $css_path ) ); |
|
785 | - $css = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $css ); |
|
786 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $css_path ); |
|
787 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
784 | + $css = self::fixurls($css_path, file_get_contents($css_path)); |
|
785 | + $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css); |
|
786 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $css_path); |
|
787 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
788 | 788 | $css = $tmpstyle; |
789 | 789 | $this->alreadyminified = true; |
790 | - } elseif ( $this->can_inject_late( $css_path, $css ) ) { |
|
791 | - $css = self::build_injectlater_marker( $css_path, md5( $css ) ); |
|
790 | + } elseif ($this->can_inject_late($css_path, $css)) { |
|
791 | + $css = self::build_injectlater_marker($css_path, md5($css)); |
|
792 | 792 | } |
793 | 793 | } else { |
794 | 794 | // Couldn't read CSS. Maybe getpath isn't working? |
@@ -796,12 +796,12 @@ discard block |
||
796 | 796 | } |
797 | 797 | } |
798 | 798 | |
799 | - foreach ( $media as $elem ) { |
|
800 | - if ( ! empty( $css ) ) { |
|
801 | - if ( ! isset( $this->csscode[ $elem ] ) ) { |
|
802 | - $this->csscode[ $elem ] = ''; |
|
799 | + foreach ($media as $elem) { |
|
800 | + if (!empty($css)) { |
|
801 | + if (!isset($this->csscode[$elem])) { |
|
802 | + $this->csscode[$elem] = ''; |
|
803 | 803 | } |
804 | - $this->csscode[ $elem ] .= "\n/*FILESTART*/" . $css; |
|
804 | + $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
|
805 | 805 | } |
806 | 806 | } |
807 | 807 | } |
@@ -809,133 +809,133 @@ discard block |
||
809 | 809 | // Check for duplicate code. |
810 | 810 | $md5list = array(); |
811 | 811 | $tmpcss = $this->csscode; |
812 | - foreach ( $tmpcss as $media => $code ) { |
|
813 | - $md5sum = md5( $code ); |
|
812 | + foreach ($tmpcss as $media => $code) { |
|
813 | + $md5sum = md5($code); |
|
814 | 814 | $medianame = $media; |
815 | - foreach ( $md5list as $med => $sum ) { |
|
815 | + foreach ($md5list as $med => $sum) { |
|
816 | 816 | // If same code. |
817 | - if ( $sum === $md5sum ) { |
|
817 | + if ($sum === $md5sum) { |
|
818 | 818 | // Add the merged code. |
819 | - $medianame = $med . ', ' . $media; |
|
820 | - $this->csscode[ $medianame ] = $code; |
|
821 | - $md5list[ $medianame ] = $md5list[ $med ]; |
|
822 | - unset( $this->csscode[ $med ], $this->csscode[ $media ], $md5list[ $med ] ); |
|
819 | + $medianame = $med.', '.$media; |
|
820 | + $this->csscode[$medianame] = $code; |
|
821 | + $md5list[$medianame] = $md5list[$med]; |
|
822 | + unset($this->csscode[$med], $this->csscode[$media], $md5list[$med]); |
|
823 | 823 | } |
824 | 824 | } |
825 | - $md5list[ $medianame ] = $md5sum; |
|
825 | + $md5list[$medianame] = $md5sum; |
|
826 | 826 | } |
827 | - unset( $tmpcss ); |
|
827 | + unset($tmpcss); |
|
828 | 828 | |
829 | 829 | // Manage @imports, while is for recursive import management. |
830 | - foreach ( $this->csscode as &$thiscss ) { |
|
830 | + foreach ($this->csscode as &$thiscss) { |
|
831 | 831 | // Flag to trigger import reconstitution and var to hold external imports. |
832 | 832 | $fiximports = false; |
833 | 833 | $external_imports = ''; |
834 | 834 | |
835 | 835 | // remove comments to avoid importing commented-out imports. |
836 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
837 | - while ( preg_match_all( '#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches ) ) { |
|
838 | - foreach ( $matches[0] as $import ) { |
|
839 | - if ( $this->isremovable( $import, $this->cssremovables ) ) { |
|
840 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
836 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
837 | + while (preg_match_all('#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches)) { |
|
838 | + foreach ($matches[0] as $import) { |
|
839 | + if ($this->isremovable($import, $this->cssremovables)) { |
|
840 | + $thiscss = str_replace($import, '', $thiscss); |
|
841 | 841 | $import_ok = true; |
842 | 842 | } else { |
843 | - $url = trim( preg_replace( '#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim( $import ) ), " \t\n\r\0\x0B\"'" ); |
|
844 | - $path = $this->getpath( $url ); |
|
843 | + $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'"); |
|
844 | + $path = $this->getpath($url); |
|
845 | 845 | $import_ok = false; |
846 | - if ( file_exists( $path ) && is_readable( $path ) ) { |
|
847 | - $code = addcslashes( self::fixurls( $path, file_get_contents( $path ) ), '\\' ); |
|
848 | - $code = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $code ); |
|
849 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, '' ); |
|
850 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
846 | + if (file_exists($path) && is_readable($path)) { |
|
847 | + $code = addcslashes(self::fixurls($path, file_get_contents($path)), '\\'); |
|
848 | + $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code); |
|
849 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, ''); |
|
850 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
851 | 851 | $code = $tmpstyle; |
852 | 852 | $this->alreadyminified = true; |
853 | - } elseif ( $this->can_inject_late( $path, $code ) ) { |
|
854 | - $code = self::build_injectlater_marker( $path, md5( $code ) ); |
|
853 | + } elseif ($this->can_inject_late($path, $code)) { |
|
854 | + $code = self::build_injectlater_marker($path, md5($code)); |
|
855 | 855 | } |
856 | 856 | |
857 | - if ( ! empty( $code ) ) { |
|
858 | - $tmp_thiscss = preg_replace( '#(/\*FILESTART\*/.*)' . preg_quote( $import, '#' ) . '#Us', '/*FILESTART2*/' . $code . '$1', $thiscss ); |
|
859 | - if ( ! empty( $tmp_thiscss ) ) { |
|
857 | + if (!empty($code)) { |
|
858 | + $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss); |
|
859 | + if (!empty($tmp_thiscss)) { |
|
860 | 860 | $thiscss = $tmp_thiscss; |
861 | 861 | $import_ok = true; |
862 | - unset( $tmp_thiscss ); |
|
862 | + unset($tmp_thiscss); |
|
863 | 863 | } |
864 | 864 | } |
865 | - unset( $code ); |
|
865 | + unset($code); |
|
866 | 866 | } |
867 | 867 | } |
868 | - if ( ! $import_ok ) { |
|
868 | + if (!$import_ok) { |
|
869 | 869 | // External imports and general fall-back. |
870 | 870 | $external_imports .= $import; |
871 | 871 | |
872 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
872 | + $thiscss = str_replace($import, '', $thiscss); |
|
873 | 873 | $fiximports = true; |
874 | 874 | } |
875 | 875 | } |
876 | - $thiscss = preg_replace( '#/\*FILESTART\*/#', '', $thiscss ); |
|
877 | - $thiscss = preg_replace( '#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss ); |
|
876 | + $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss); |
|
877 | + $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss); |
|
878 | 878 | |
879 | 879 | // and update $thiscss_nocomments before going into next iteration in while loop. |
880 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
880 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
881 | 881 | } |
882 | - unset( $thiscss_nocomments ); |
|
882 | + unset($thiscss_nocomments); |
|
883 | 883 | |
884 | 884 | // Add external imports to top of aggregated CSS. |
885 | - if ( $fiximports ) { |
|
886 | - $thiscss = $external_imports . $thiscss; |
|
885 | + if ($fiximports) { |
|
886 | + $thiscss = $external_imports.$thiscss; |
|
887 | 887 | } |
888 | 888 | } |
889 | - unset( $thiscss ); |
|
889 | + unset($thiscss); |
|
890 | 890 | |
891 | 891 | // $this->csscode has all the uncompressed code now. |
892 | - foreach ( $this->csscode as &$code ) { |
|
892 | + foreach ($this->csscode as &$code) { |
|
893 | 893 | // Check for already-minified code. |
894 | - $hash = md5( $code ); |
|
895 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
896 | - $ccheck = new autoptimizeCache( $hash, 'css' ); |
|
897 | - if ( $ccheck->check() ) { |
|
894 | + $hash = md5($code); |
|
895 | + do_action('autoptimize_action_css_hash', $hash); |
|
896 | + $ccheck = new autoptimizeCache($hash, 'css'); |
|
897 | + if ($ccheck->check()) { |
|
898 | 898 | $code = $ccheck->retrieve(); |
899 | - $this->hashmap[ md5( $code ) ] = $hash; |
|
899 | + $this->hashmap[md5($code)] = $hash; |
|
900 | 900 | continue; |
901 | 901 | } |
902 | - unset( $ccheck ); |
|
902 | + unset($ccheck); |
|
903 | 903 | |
904 | 904 | // Rewrite and/or inline referenced assets. |
905 | - $code = $this->rewrite_assets( $code ); |
|
905 | + $code = $this->rewrite_assets($code); |
|
906 | 906 | |
907 | 907 | // Minify. |
908 | - $code = $this->run_minifier_on( $code ); |
|
908 | + $code = $this->run_minifier_on($code); |
|
909 | 909 | |
910 | 910 | // Bring back INJECTLATER stuff. |
911 | - $code = $this->inject_minified( $code ); |
|
911 | + $code = $this->inject_minified($code); |
|
912 | 912 | |
913 | 913 | // Filter results. |
914 | - $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code ); |
|
915 | - if ( ! empty( $tmp_code ) ) { |
|
914 | + $tmp_code = apply_filters('autoptimize_css_after_minify', $code); |
|
915 | + if (!empty($tmp_code)) { |
|
916 | 916 | $code = $tmp_code; |
917 | - unset( $tmp_code ); |
|
917 | + unset($tmp_code); |
|
918 | 918 | } |
919 | 919 | |
920 | - $this->hashmap[ md5( $code ) ] = $hash; |
|
920 | + $this->hashmap[md5($code)] = $hash; |
|
921 | 921 | } |
922 | 922 | |
923 | - unset( $code ); |
|
923 | + unset($code); |
|
924 | 924 | return true; |
925 | 925 | } |
926 | 926 | |
927 | - public function run_minifier_on( $code ) |
|
927 | + public function run_minifier_on($code) |
|
928 | 928 | { |
929 | - if ( ! $this->alreadyminified ) { |
|
930 | - $do_minify = apply_filters( 'autoptimize_css_do_minify', true ); |
|
929 | + if (!$this->alreadyminified) { |
|
930 | + $do_minify = apply_filters('autoptimize_css_do_minify', true); |
|
931 | 931 | |
932 | - if ( $do_minify ) { |
|
932 | + if ($do_minify) { |
|
933 | 933 | $cssmin = new autoptimizeCSSmin(); |
934 | - $tmp_code = trim( $cssmin->run( $code ) ); |
|
934 | + $tmp_code = trim($cssmin->run($code)); |
|
935 | 935 | |
936 | - if ( ! empty( $tmp_code ) ) { |
|
936 | + if (!empty($tmp_code)) { |
|
937 | 937 | $code = $tmp_code; |
938 | - unset( $tmp_code ); |
|
938 | + unset($tmp_code); |
|
939 | 939 | } |
940 | 940 | } |
941 | 941 | } |
@@ -949,18 +949,18 @@ discard block |
||
949 | 949 | public function cache() |
950 | 950 | { |
951 | 951 | // CSS cache. |
952 | - foreach ( $this->csscode as $media => $code ) { |
|
953 | - if ( empty( $code ) ) { |
|
952 | + foreach ($this->csscode as $media => $code) { |
|
953 | + if (empty($code)) { |
|
954 | 954 | continue; |
955 | 955 | } |
956 | 956 | |
957 | - $md5 = $this->hashmap[ md5( $code ) ]; |
|
958 | - $cache = new autoptimizeCache( $md5, 'css' ); |
|
959 | - if ( ! $cache->check() ) { |
|
957 | + $md5 = $this->hashmap[md5($code)]; |
|
958 | + $cache = new autoptimizeCache($md5, 'css'); |
|
959 | + if (!$cache->check()) { |
|
960 | 960 | // Cache our code. |
961 | - $cache->cache( $code, 'text/css' ); |
|
961 | + $cache->cache($code, 'text/css'); |
|
962 | 962 | } |
963 | - $this->url[ $media ] = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
963 | + $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
964 | 964 | } |
965 | 965 | } |
966 | 966 | |
@@ -970,95 +970,95 @@ discard block |
||
970 | 970 | public function getcontent() |
971 | 971 | { |
972 | 972 | // Restore the full content (only applies when "autoptimize_filter_css_justhead" filter is true). |
973 | - if ( ! empty( $this->restofcontent ) ) { |
|
973 | + if (!empty($this->restofcontent)) { |
|
974 | 974 | $this->content .= $this->restofcontent; |
975 | 975 | $this->restofcontent = ''; |
976 | 976 | } |
977 | 977 | |
978 | 978 | // type is not added by default. |
979 | 979 | $type_css = ''; |
980 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
980 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
981 | 981 | $type_css = 'type="text/css" '; |
982 | 982 | } |
983 | 983 | |
984 | 984 | // Inject the new stylesheets. |
985 | - $replace_tag = array( '<title', 'before' ); |
|
986 | - $replace_tag = apply_filters( 'autoptimize_filter_css_replacetag', $replace_tag, $this->content ); |
|
985 | + $replace_tag = array('<title', 'before'); |
|
986 | + $replace_tag = apply_filters('autoptimize_filter_css_replacetag', $replace_tag, $this->content); |
|
987 | 987 | |
988 | - if ( $this->inline ) { |
|
989 | - foreach ( $this->csscode as $media => $code ) { |
|
990 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<style ' . $type_css . 'media="' . $media . '">' . $code . '</style>' ), $replace_tag ); |
|
988 | + if ($this->inline) { |
|
989 | + foreach ($this->csscode as $media => $code) { |
|
990 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', '<style '.$type_css.'media="'.$media.'">'.$code.'</style>'), $replace_tag); |
|
991 | 991 | } |
992 | 992 | } else { |
993 | - if ( $this->defer ) { |
|
993 | + if ($this->defer) { |
|
994 | 994 | $preload_css_block = ''; |
995 | 995 | $inlined_ccss_block = ''; |
996 | 996 | $noscript_css_block = '<noscript id="aonoscrcss">'; |
997 | 997 | |
998 | 998 | $defer_inline_code = $this->defer_inline; |
999 | - if ( ! empty( $defer_inline_code ) ) { |
|
1000 | - if ( apply_filters( 'autoptimize_filter_css_critcss_minify', true ) ) { |
|
1001 | - $icss_hash = md5( $defer_inline_code ); |
|
1002 | - $icss_cache = new autoptimizeCache( $icss_hash, 'css' ); |
|
1003 | - if ( $icss_cache->check() ) { |
|
999 | + if (!empty($defer_inline_code)) { |
|
1000 | + if (apply_filters('autoptimize_filter_css_critcss_minify', true)) { |
|
1001 | + $icss_hash = md5($defer_inline_code); |
|
1002 | + $icss_cache = new autoptimizeCache($icss_hash, 'css'); |
|
1003 | + if ($icss_cache->check()) { |
|
1004 | 1004 | // we have the optimized inline CSS in cache. |
1005 | 1005 | $defer_inline_code = $icss_cache->retrieve(); |
1006 | 1006 | } else { |
1007 | 1007 | $cssmin = new autoptimizeCSSmin(); |
1008 | - $tmp_code = trim( $cssmin->run( $defer_inline_code ) ); |
|
1008 | + $tmp_code = trim($cssmin->run($defer_inline_code)); |
|
1009 | 1009 | |
1010 | - if ( ! empty( $tmp_code ) ) { |
|
1010 | + if (!empty($tmp_code)) { |
|
1011 | 1011 | $defer_inline_code = $tmp_code; |
1012 | - $icss_cache->cache( $defer_inline_code, 'text/css' ); |
|
1013 | - unset( $tmp_code ); |
|
1012 | + $icss_cache->cache($defer_inline_code, 'text/css'); |
|
1013 | + unset($tmp_code); |
|
1014 | 1014 | } |
1015 | 1015 | } |
1016 | 1016 | } |
1017 | 1017 | // inlined critical css set here, but injected when full CSS is injected |
1018 | 1018 | // to avoid CSS containing SVG with <title tag receiving the full CSS link. |
1019 | - $inlined_ccss_block = '<style ' . $type_css . 'id="aoatfcss" media="all">' . $defer_inline_code . '</style>'; |
|
1019 | + $inlined_ccss_block = '<style '.$type_css.'id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
1020 | 1020 | } |
1021 | 1021 | } |
1022 | 1022 | |
1023 | - foreach ( $this->url as $media => $url ) { |
|
1024 | - $url = $this->url_replace_cdn( $url ); |
|
1023 | + foreach ($this->url as $media => $url) { |
|
1024 | + $url = $this->url_replace_cdn($url); |
|
1025 | 1025 | |
1026 | 1026 | // Add the stylesheet either deferred (import at bottom) or normal links in head. |
1027 | - if ( $this->defer && 'print' !== $media ) { |
|
1028 | - $preload_onload = autoptimizeConfig::get_ao_css_preload_onload( $media ); |
|
1027 | + if ($this->defer && 'print' !== $media) { |
|
1028 | + $preload_onload = autoptimizeConfig::get_ao_css_preload_onload($media); |
|
1029 | 1029 | |
1030 | - $preload_css_block .= '<link rel="stylesheet" media="print" href="' . $url . '" onload="' . $preload_onload . '" />'; |
|
1031 | - if ( apply_filters( 'autoptimize_fitler_css_preload_and_print', false ) ) { |
|
1032 | - $preload_css_block = '<link rel="preload" as="stylesheet" href="' . $url . '"/>' . $preload_css_block; |
|
1030 | + $preload_css_block .= '<link rel="stylesheet" media="print" href="'.$url.'" onload="'.$preload_onload.'" />'; |
|
1031 | + if (apply_filters('autoptimize_fitler_css_preload_and_print', false)) { |
|
1032 | + $preload_css_block = '<link rel="preload" as="stylesheet" href="'.$url.'"/>'.$preload_css_block; |
|
1033 | 1033 | } |
1034 | - $noscript_css_block .= '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />'; |
|
1034 | + $noscript_css_block .= '<link '.$type_css.'media="'.$media.'" href="'.$url.'" rel="stylesheet" />'; |
|
1035 | 1035 | } else { |
1036 | - if ( strlen( $this->csscode[ $media ] ) > $this->cssinlinesize ) { |
|
1037 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />' ), $replace_tag ); |
|
1038 | - } elseif ( strlen( $this->csscode[ $media ] ) > 0 ) { |
|
1039 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', '<style ' . $type_css . 'media="' . $media . '">' . $this->csscode[ $media ] . '</style>' ), $replace_tag ); |
|
1036 | + if (strlen($this->csscode[$media]) > $this->cssinlinesize) { |
|
1037 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', '<link '.$type_css.'media="'.$media.'" href="'.$url.'" rel="stylesheet" />'), $replace_tag); |
|
1038 | + } elseif (strlen($this->csscode[$media]) > 0) { |
|
1039 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', '<style '.$type_css.'media="'.$media.'">'.$this->csscode[$media].'</style>'), $replace_tag); |
|
1040 | 1040 | } |
1041 | 1041 | } |
1042 | 1042 | } |
1043 | 1043 | |
1044 | - if ( $this->defer ) { |
|
1044 | + if ($this->defer) { |
|
1045 | 1045 | $noscript_css_block .= '</noscript>'; |
1046 | 1046 | // Inject inline critical CSS, the preloaded full CSS and the noscript-CSS. |
1047 | - $this->inject_in_html( apply_filters( 'autoptimize_filter_css_bodyreplacementpayload', $inlined_ccss_block . $preload_css_block . $noscript_css_block ), $replace_tag ); |
|
1047 | + $this->inject_in_html(apply_filters('autoptimize_filter_css_bodyreplacementpayload', $inlined_ccss_block.$preload_css_block.$noscript_css_block), $replace_tag); |
|
1048 | 1048 | } |
1049 | 1049 | } |
1050 | 1050 | |
1051 | 1051 | // restore comments. |
1052 | - $this->content = $this->restore_comments( $this->content ); |
|
1052 | + $this->content = $this->restore_comments($this->content); |
|
1053 | 1053 | |
1054 | 1054 | // restore IE hacks. |
1055 | - $this->content = $this->restore_iehacks( $this->content ); |
|
1055 | + $this->content = $this->restore_iehacks($this->content); |
|
1056 | 1056 | |
1057 | 1057 | // restore (no)script. |
1058 | - $this->content = $this->restore_marked_content( 'SCRIPT', $this->content ); |
|
1058 | + $this->content = $this->restore_marked_content('SCRIPT', $this->content); |
|
1059 | 1059 | |
1060 | 1060 | // Restore noptimize. |
1061 | - $this->content = $this->restore_noptimize( $this->content ); |
|
1061 | + $this->content = $this->restore_noptimize($this->content); |
|
1062 | 1062 | |
1063 | 1063 | // Return the modified stylesheet. |
1064 | 1064 | return $this->content; |
@@ -1070,44 +1070,44 @@ discard block |
||
1070 | 1070 | * @param string $file filename of optimized CSS-file. |
1071 | 1071 | * @param string $code CSS-code in which to fix URL's. |
1072 | 1072 | */ |
1073 | - static function fixurls( $file, $code ) |
|
1073 | + static function fixurls($file, $code) |
|
1074 | 1074 | { |
1075 | 1075 | // Switch all imports to the url() syntax. |
1076 | - $code = preg_replace( '#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code ); |
|
1076 | + $code = preg_replace('#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code); |
|
1077 | 1077 | |
1078 | - if ( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) { |
|
1079 | - $file = str_replace( WP_ROOT_DIR, '/', $file ); |
|
1078 | + if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) { |
|
1079 | + $file = str_replace(WP_ROOT_DIR, '/', $file); |
|
1080 | 1080 | /** |
1081 | 1081 | * Rollback as per https://github.com/futtta/autoptimize/issues/94 |
1082 | 1082 | * $file = str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', $file ); |
1083 | 1083 | */ |
1084 | - $dir = dirname( $file ); // Like /themes/expound/css. |
|
1084 | + $dir = dirname($file); // Like /themes/expound/css. |
|
1085 | 1085 | |
1086 | 1086 | /** |
1087 | 1087 | * $dir should not contain backslashes, since it's used to replace |
1088 | 1088 | * urls, but it can contain them when running on Windows because |
1089 | 1089 | * fixurls() is sometimes called with `ABSPATH . 'index.php'` |
1090 | 1090 | */ |
1091 | - $dir = str_replace( '\\', '/', $dir ); |
|
1092 | - unset( $file ); // not used below at all. |
|
1091 | + $dir = str_replace('\\', '/', $dir); |
|
1092 | + unset($file); // not used below at all. |
|
1093 | 1093 | |
1094 | 1094 | $replace = array(); |
1095 | - foreach ( $matches[1] as $k => $url ) { |
|
1095 | + foreach ($matches[1] as $k => $url) { |
|
1096 | 1096 | // Remove quotes. |
1097 | - $url = trim( $url, " \t\n\r\0\x0B\"'" ); |
|
1098 | - $no_q_url = trim( $url, "\"'" ); |
|
1099 | - if ( $url !== $no_q_url ) { |
|
1097 | + $url = trim($url, " \t\n\r\0\x0B\"'"); |
|
1098 | + $no_q_url = trim($url, "\"'"); |
|
1099 | + if ($url !== $no_q_url) { |
|
1100 | 1100 | $removed_quotes = true; |
1101 | 1101 | } else { |
1102 | 1102 | $removed_quotes = false; |
1103 | 1103 | } |
1104 | 1104 | |
1105 | - if ( '' === $no_q_url ) { |
|
1105 | + if ('' === $no_q_url) { |
|
1106 | 1106 | continue; |
1107 | 1107 | } |
1108 | 1108 | |
1109 | 1109 | $url = $no_q_url; |
1110 | - if ( '/' === $url[0] || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) { |
|
1110 | + if ('/' === $url[0] || preg_match('#^(https?://|ftp://|data:)#i', $url)) { |
|
1111 | 1111 | // URL is protocol-relative, host-relative or something we don't touch. |
1112 | 1112 | continue; |
1113 | 1113 | } else { // Relative URL. |
@@ -1116,49 +1116,49 @@ discard block |
||
1116 | 1116 | * rollback as per https://github.com/futtta/autoptimize/issues/94 |
1117 | 1117 | * $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_CONTENT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
1118 | 1118 | */ |
1119 | - $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
|
1120 | - $newurl = apply_filters( 'autoptimize_filter_css_fixurl_newurl', $newurl ); |
|
1119 | + $newurl = preg_replace('/https?:/', '', str_replace(' ', '%20', AUTOPTIMIZE_WP_ROOT_URL.str_replace('//', '/', $dir.'/'.$url))); |
|
1120 | + $newurl = apply_filters('autoptimize_filter_css_fixurl_newurl', $newurl); |
|
1121 | 1121 | |
1122 | 1122 | /** |
1123 | 1123 | * Hash the url + whatever was behind potentially for replacement |
1124 | 1124 | * We must do this, or different css classes referencing the same bg image (but |
1125 | 1125 | * different parts of it, say, in sprites and such) loose their stuff... |
1126 | 1126 | */ |
1127 | - $hash = md5( $url . $matches[2][ $k ] ); |
|
1128 | - $code = str_replace( $matches[0][ $k ], $hash, $code ); |
|
1127 | + $hash = md5($url.$matches[2][$k]); |
|
1128 | + $code = str_replace($matches[0][$k], $hash, $code); |
|
1129 | 1129 | |
1130 | - if ( $removed_quotes ) { |
|
1131 | - $replace[ $hash ] = "url('" . $newurl . "')" . $matches[2][ $k ]; |
|
1130 | + if ($removed_quotes) { |
|
1131 | + $replace[$hash] = "url('".$newurl."')".$matches[2][$k]; |
|
1132 | 1132 | } else { |
1133 | - $replace[ $hash ] = 'url(' . $newurl . ')' . $matches[2][ $k ]; |
|
1133 | + $replace[$hash] = 'url('.$newurl.')'.$matches[2][$k]; |
|
1134 | 1134 | } |
1135 | 1135 | } |
1136 | 1136 | } |
1137 | 1137 | |
1138 | - $code = self::replace_longest_matches_first( $code, $replace ); |
|
1138 | + $code = self::replace_longest_matches_first($code, $replace); |
|
1139 | 1139 | } |
1140 | 1140 | |
1141 | 1141 | return $code; |
1142 | 1142 | } |
1143 | 1143 | |
1144 | - private function ismovable( $tag ) |
|
1144 | + private function ismovable($tag) |
|
1145 | 1145 | { |
1146 | - if ( ! $this->aggregate ) { |
|
1146 | + if (!$this->aggregate) { |
|
1147 | 1147 | return false; |
1148 | 1148 | } |
1149 | 1149 | |
1150 | - if ( ! empty( $this->allowlist ) ) { |
|
1151 | - foreach ( $this->allowlist as $match ) { |
|
1152 | - if ( false !== strpos( $tag, $match ) ) { |
|
1150 | + if (!empty($this->allowlist)) { |
|
1151 | + foreach ($this->allowlist as $match) { |
|
1152 | + if (false !== strpos($tag, $match)) { |
|
1153 | 1153 | return true; |
1154 | 1154 | } |
1155 | 1155 | } |
1156 | 1156 | // no match with allowlist. |
1157 | 1157 | return false; |
1158 | 1158 | } else { |
1159 | - if ( is_array( $this->dontmove ) && ! empty( $this->dontmove ) ) { |
|
1160 | - foreach ( $this->dontmove as $match ) { |
|
1161 | - if ( false !== strpos( $tag, $match ) ) { |
|
1159 | + if (is_array($this->dontmove) && !empty($this->dontmove)) { |
|
1160 | + foreach ($this->dontmove as $match) { |
|
1161 | + if (false !== strpos($tag, $match)) { |
|
1162 | 1162 | // Matched something. |
1163 | 1163 | return false; |
1164 | 1164 | } |
@@ -1170,22 +1170,22 @@ discard block |
||
1170 | 1170 | } |
1171 | 1171 | } |
1172 | 1172 | |
1173 | - private function can_inject_late( $css_path, $css ) |
|
1173 | + private function can_inject_late($css_path, $css) |
|
1174 | 1174 | { |
1175 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $css_path ); |
|
1176 | - if ( true !== $this->inject_min_late ) { |
|
1175 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $css_path); |
|
1176 | + if (true !== $this->inject_min_late) { |
|
1177 | 1177 | // late-inject turned off. |
1178 | 1178 | return false; |
1179 | - } elseif ( ( false === strpos( $css_path, 'min.css' ) ) && ( str_replace( $consider_minified_array, '', $css_path ) === $css_path ) ) { |
|
1179 | + } elseif ((false === strpos($css_path, 'min.css')) && (str_replace($consider_minified_array, '', $css_path) === $css_path)) { |
|
1180 | 1180 | // file not minified based on filename & filter. |
1181 | 1181 | return false; |
1182 | - } elseif ( false !== strpos( $css, '@import' ) ) { |
|
1182 | + } elseif (false !== strpos($css, '@import')) { |
|
1183 | 1183 | // can't late-inject files with imports as those need to be aggregated. |
1184 | 1184 | return false; |
1185 | - } elseif ( ( false !== strpos( $css, '@font-face' ) ) && ( apply_filters( 'autoptimize_filter_css_fonts_cdn', false ) === true ) && ( ! empty( $this->cdn_url ) ) ) { |
|
1185 | + } elseif ((false !== strpos($css, '@font-face')) && (apply_filters('autoptimize_filter_css_fonts_cdn', false) === true) && (!empty($this->cdn_url))) { |
|
1186 | 1186 | // don't late-inject CSS with font-src's if fonts are set to be CDN'ed. |
1187 | 1187 | return false; |
1188 | - } elseif ( ( ( true == $this->datauris ) || ( ! empty( $this->cdn_url ) ) ) && preg_match( '#background[^;}]*url\(#Ui', $css ) ) { |
|
1188 | + } elseif (((true == $this->datauris) || (!empty($this->cdn_url))) && preg_match('#background[^;}]*url\(#Ui', $css)) { |
|
1189 | 1189 | // don't late-inject CSS with images if CDN is set OR if image inlining is on. |
1190 | 1190 | return false; |
1191 | 1191 | } else { |
@@ -1203,44 +1203,44 @@ discard block |
||
1203 | 1203 | * |
1204 | 1204 | * @return bool|string Url pointing to the minified css file or false. |
1205 | 1205 | */ |
1206 | - public function minify_single( $filepath, $cache_miss = false ) |
|
1206 | + public function minify_single($filepath, $cache_miss = false) |
|
1207 | 1207 | { |
1208 | - $contents = $this->prepare_minify_single( $filepath ); |
|
1208 | + $contents = $this->prepare_minify_single($filepath); |
|
1209 | 1209 | |
1210 | - if ( empty( $contents ) ) { |
|
1210 | + if (empty($contents)) { |
|
1211 | 1211 | return false; |
1212 | 1212 | } |
1213 | 1213 | |
1214 | 1214 | // Check cache. |
1215 | - $hash = 'single_' . md5( $contents ); |
|
1216 | - $cache = new autoptimizeCache( $hash, 'css' ); |
|
1217 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
1215 | + $hash = 'single_'.md5($contents); |
|
1216 | + $cache = new autoptimizeCache($hash, 'css'); |
|
1217 | + do_action('autoptimize_action_css_hash', $hash); |
|
1218 | 1218 | |
1219 | 1219 | // If not in cache already, minify... |
1220 | - if ( ! $cache->check() || $cache_miss ) { |
|
1220 | + if (!$cache->check() || $cache_miss) { |
|
1221 | 1221 | // Fixurls... |
1222 | - $contents = self::fixurls( $filepath, $contents ); |
|
1222 | + $contents = self::fixurls($filepath, $contents); |
|
1223 | 1223 | // CDN-replace any referenced assets if needed... |
1224 | - $contents = $this->hide_fontface_and_maybe_cdn( $contents ); |
|
1225 | - $contents = $this->replace_urls( $contents ); |
|
1226 | - $contents = $this->restore_fontface( $contents ); |
|
1224 | + $contents = $this->hide_fontface_and_maybe_cdn($contents); |
|
1225 | + $contents = $this->replace_urls($contents); |
|
1226 | + $contents = $this->restore_fontface($contents); |
|
1227 | 1227 | // Now minify... |
1228 | 1228 | $cssmin = new autoptimizeCSSmin(); |
1229 | - $contents = trim( $cssmin->run( $contents ) ); |
|
1229 | + $contents = trim($cssmin->run($contents)); |
|
1230 | 1230 | |
1231 | 1231 | // Check if minified cache content is empty. |
1232 | - if ( empty( $contents ) ) { |
|
1232 | + if (empty($contents)) { |
|
1233 | 1233 | return false; |
1234 | 1234 | } |
1235 | 1235 | |
1236 | 1236 | // Filter contents of excluded minified CSS. |
1237 | - $contents = apply_filters( 'autoptimize_filter_css_single_after_minify', $contents ); |
|
1237 | + $contents = apply_filters('autoptimize_filter_css_single_after_minify', $contents); |
|
1238 | 1238 | |
1239 | 1239 | // Store in cache. |
1240 | - $cache->cache( $contents, 'text/css' ); |
|
1240 | + $cache->cache($contents, 'text/css'); |
|
1241 | 1241 | } |
1242 | 1242 | |
1243 | - $url = $this->build_minify_single_url( $cache ); |
|
1243 | + $url = $this->build_minify_single_url($cache); |
|
1244 | 1244 | |
1245 | 1245 | return $url; |
1246 | 1246 | } |
@@ -1260,19 +1260,19 @@ discard block |
||
1260 | 1260 | return $this->options; |
1261 | 1261 | } |
1262 | 1262 | |
1263 | - public function replaceOptions( $options ) |
|
1263 | + public function replaceOptions($options) |
|
1264 | 1264 | { |
1265 | 1265 | $this->options = $options; |
1266 | 1266 | } |
1267 | 1267 | |
1268 | - public function setOption( $name, $value ) |
|
1268 | + public function setOption($name, $value) |
|
1269 | 1269 | { |
1270 | - $this->options[ $name ] = $value; |
|
1270 | + $this->options[$name] = $value; |
|
1271 | 1271 | $this->$name = $value; |
1272 | 1272 | } |
1273 | 1273 | |
1274 | - public function getOption( $name ) |
|
1274 | + public function getOption($name) |
|
1275 | 1275 | { |
1276 | - return $this->options[ $name ]; |
|
1276 | + return $this->options[$name]; |
|
1277 | 1277 | } |
1278 | 1278 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * gets called by AO core, checks the rules and if a matching rule is found returns the associated CCSS. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
@@ -28,28 +28,28 @@ discard block |
||
28 | 28 | global $ao_ccss_unloadccss; |
29 | 29 | |
30 | 30 | // add all filters to do CCSS if key present. |
31 | - if ( $ao_css_defer && isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) ) { |
|
31 | + if ($ao_css_defer && isset($ao_ccss_key) && !empty($ao_ccss_key)) { |
|
32 | 32 | // Set AO behavior: disable minification to avoid double minifying and caching. |
33 | - add_filter( 'autoptimize_filter_css_critcss_minify', '__return_false' ); |
|
34 | - add_filter( 'autoptimize_filter_css_defer_inline', array( $this, 'ao_ccss_frontend' ), 10, 1 ); |
|
33 | + add_filter('autoptimize_filter_css_critcss_minify', '__return_false'); |
|
34 | + add_filter('autoptimize_filter_css_defer_inline', array($this, 'ao_ccss_frontend'), 10, 1); |
|
35 | 35 | |
36 | 36 | // Add the action to enqueue jobs for CriticalCSS cron. |
37 | - add_action( 'autoptimize_action_css_hash', array( 'autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue' ), 10, 1 ); |
|
37 | + add_action('autoptimize_action_css_hash', array('autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue'), 10, 1); |
|
38 | 38 | |
39 | 39 | // conditionally add the filter to defer jquery and others. |
40 | - if ( $ao_ccss_deferjquery ) { |
|
41 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_defer_jquery' ), 11, 1 ); |
|
40 | + if ($ao_ccss_deferjquery) { |
|
41 | + add_filter('autoptimize_html_after_minify', array($this, 'ao_ccss_defer_jquery'), 11, 1); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | // conditionally add filter to unload the CCSS. |
45 | - if ( $ao_ccss_unloadccss ) { |
|
46 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_unloadccss' ), 12, 1 ); |
|
45 | + if ($ao_ccss_unloadccss) { |
|
46 | + add_filter('autoptimize_html_after_minify', array($this, 'ao_ccss_unloadccss'), 12, 1); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | // Order paths by length, as longest ones have greater priority in the rules. |
50 | - if ( ! empty( $ao_ccss_rules['paths'] ) ) { |
|
51 | - $keys = array_map( 'strlen', array_keys( $ao_ccss_rules['paths'] ) ); |
|
52 | - array_multisort( $keys, SORT_DESC, $ao_ccss_rules['paths'] ); |
|
50 | + if (!empty($ao_ccss_rules['paths'])) { |
|
51 | + $keys = array_map('strlen', array_keys($ao_ccss_rules['paths'])); |
|
52 | + array_multisort($keys, SORT_DESC, $ao_ccss_rules['paths']); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | // Add an array with default WordPress's conditional tags |
@@ -58,14 +58,14 @@ discard block |
||
58 | 58 | $ao_ccss_types = $this->get_ao_ccss_core_types(); |
59 | 59 | |
60 | 60 | // Extend conditional tags on plugin initalization. |
61 | - add_action( apply_filters( 'autoptimize_filter_ccss_extend_types_hook', 'init' ), array( $this, 'ao_ccss_extend_types' ) ); |
|
61 | + add_action(apply_filters('autoptimize_filter_ccss_extend_types_hook', 'init'), array($this, 'ao_ccss_extend_types')); |
|
62 | 62 | |
63 | 63 | // When autoptimize cache is cleared, also clear transient cache for page templates. |
64 | - add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCriticalCSSCore', 'ao_ccss_clear_page_tpl_cache' ), 10, 0 ); |
|
64 | + add_action('autoptimize_action_cachepurged', array('autoptimizeCriticalCSSCore', 'ao_ccss_clear_page_tpl_cache'), 10, 0); |
|
65 | 65 | } |
66 | 66 | } |
67 | 67 | |
68 | - public function ao_ccss_frontend( $inlined ) { |
|
68 | + public function ao_ccss_frontend($inlined) { |
|
69 | 69 | // Apply CriticalCSS to frontend pages |
70 | 70 | // Attach types and settings arrays. |
71 | 71 | global $ao_ccss_types; |
@@ -77,20 +77,20 @@ discard block |
||
77 | 77 | $no_ccss = ''; |
78 | 78 | |
79 | 79 | // Only if keystatus is OK and option to add CCSS for logged on users is on or user is not logged in. |
80 | - if ( ( $ao_ccss_keyst && 2 == $ao_ccss_keyst ) && ( $ao_ccss_loggedin || ! is_user_logged_in() ) ) { |
|
80 | + if (($ao_ccss_keyst && 2 == $ao_ccss_keyst) && ($ao_ccss_loggedin || !is_user_logged_in())) { |
|
81 | 81 | // Check for a valid CriticalCSS based on path to return its contents. |
82 | - $req_path = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
83 | - if ( ! empty( $ao_ccss_rules['paths'] ) ) { |
|
84 | - foreach ( $ao_ccss_rules['paths'] as $path => $rule ) { |
|
82 | + $req_path = strtok($_SERVER['REQUEST_URI'], '?'); |
|
83 | + if (!empty($ao_ccss_rules['paths'])) { |
|
84 | + foreach ($ao_ccss_rules['paths'] as $path => $rule) { |
|
85 | 85 | // explicit match OR partial match if MANUAL rule. |
86 | - if ( $req_path == $path || urldecode( $req_path ) == $path || ( false == $rule['hash'] && false != $rule['file'] && strpos( $req_path, str_replace( site_url(), '', $path ) ) !== false ) ) { |
|
87 | - if ( file_exists( AO_CCSS_DIR . $rule['file'] ) ) { |
|
88 | - $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] ); |
|
89 | - if ( 'none' != $_ccss_contents ) { |
|
90 | - if ( $ao_ccss_debug ) { |
|
91 | - $_ccss_contents = '/* PATH: ' . $path . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
86 | + if ($req_path == $path || urldecode($req_path) == $path || (false == $rule['hash'] && false != $rule['file'] && strpos($req_path, str_replace(site_url(), '', $path)) !== false)) { |
|
87 | + if (file_exists(AO_CCSS_DIR.$rule['file'])) { |
|
88 | + $_ccss_contents = file_get_contents(AO_CCSS_DIR.$rule['file']); |
|
89 | + if ('none' != $_ccss_contents) { |
|
90 | + if ($ao_ccss_debug) { |
|
91 | + $_ccss_contents = '/* PATH: '.$path.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
92 | 92 | } |
93 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
93 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
94 | 94 | } else { |
95 | 95 | $no_ccss = 'none'; |
96 | 96 | } |
@@ -100,55 +100,55 @@ discard block |
||
100 | 100 | } |
101 | 101 | |
102 | 102 | // Check for a valid CriticalCSS based on conditional tags to return its contents. |
103 | - if ( ! empty( $ao_ccss_rules['types'] ) && 'none' !== $no_ccss ) { |
|
103 | + if (!empty($ao_ccss_rules['types']) && 'none' !== $no_ccss) { |
|
104 | 104 | // order types-rules by the order of the original $ao_ccss_types array so as not to depend on the order in which rules were added. |
105 | - $ao_ccss_rules['types'] = array_replace( array_intersect_key( array_flip( $ao_ccss_types ), $ao_ccss_rules['types'] ), $ao_ccss_rules['types'] ); |
|
105 | + $ao_ccss_rules['types'] = array_replace(array_intersect_key(array_flip($ao_ccss_types), $ao_ccss_rules['types']), $ao_ccss_rules['types']); |
|
106 | 106 | $is_front_page = is_front_page(); |
107 | 107 | |
108 | - foreach ( $ao_ccss_rules['types'] as $type => $rule ) { |
|
109 | - if ( in_array( $type, $ao_ccss_types ) && file_exists( AO_CCSS_DIR . $rule['file'] ) ) { |
|
110 | - $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] ); |
|
111 | - if ( $is_front_page && 'is_front_page' == $type ) { |
|
112 | - if ( 'none' != $_ccss_contents ) { |
|
113 | - if ( $ao_ccss_debug ) { |
|
114 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
108 | + foreach ($ao_ccss_rules['types'] as $type => $rule) { |
|
109 | + if (in_array($type, $ao_ccss_types) && file_exists(AO_CCSS_DIR.$rule['file'])) { |
|
110 | + $_ccss_contents = file_get_contents(AO_CCSS_DIR.$rule['file']); |
|
111 | + if ($is_front_page && 'is_front_page' == $type) { |
|
112 | + if ('none' != $_ccss_contents) { |
|
113 | + if ($ao_ccss_debug) { |
|
114 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
115 | 115 | } |
116 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
116 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
117 | 117 | } else { |
118 | 118 | $no_ccss = 'none'; |
119 | 119 | } |
120 | - } elseif ( strpos( $type, 'custom_post_' ) === 0 && ! $is_front_page ) { |
|
121 | - if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) { |
|
122 | - if ( 'none' != $_ccss_contents ) { |
|
123 | - if ( $ao_ccss_debug ) { |
|
124 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
120 | + } elseif (strpos($type, 'custom_post_') === 0 && !$is_front_page) { |
|
121 | + if (get_post_type(get_the_ID()) === substr($type, 12)) { |
|
122 | + if ('none' != $_ccss_contents) { |
|
123 | + if ($ao_ccss_debug) { |
|
124 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
125 | 125 | } |
126 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
126 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
127 | 127 | } else { |
128 | 128 | $no_ccss = 'none'; |
129 | 129 | } |
130 | 130 | } |
131 | - } elseif ( 0 === strpos( $type, 'template_' ) && ! $is_front_page ) { |
|
132 | - if ( is_page_template( substr( $type, 9 ) ) ) { |
|
133 | - if ( 'none' != $_ccss_contents ) { |
|
134 | - if ( $ao_ccss_debug ) { |
|
135 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
131 | + } elseif (0 === strpos($type, 'template_') && !$is_front_page) { |
|
132 | + if (is_page_template(substr($type, 9))) { |
|
133 | + if ('none' != $_ccss_contents) { |
|
134 | + if ($ao_ccss_debug) { |
|
135 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
136 | 136 | } |
137 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
137 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
138 | 138 | } else { |
139 | 139 | $no_ccss = 'none'; |
140 | 140 | } |
141 | 141 | } |
142 | - } elseif ( ! $is_front_page ) { |
|
142 | + } elseif (!$is_front_page) { |
|
143 | 143 | // all "normal" conditional tags, core + woo + buddypress + edd + bbpress |
144 | 144 | // but we have to remove the prefix for the non-core ones for them to function. |
145 | - $type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type ); |
|
146 | - if ( function_exists( $type ) && call_user_func( $type ) ) { |
|
147 | - if ( 'none' != $_ccss_contents ) { |
|
148 | - if ( $ao_ccss_debug ) { |
|
149 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
145 | + $type = str_replace(array('woo_', 'bp_', 'bbp_', 'edd_'), '', $type); |
|
146 | + if (function_exists($type) && call_user_func($type)) { |
|
147 | + if ('none' != $_ccss_contents) { |
|
148 | + if ($ao_ccss_debug) { |
|
149 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
150 | 150 | } |
151 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
151 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
152 | 152 | } else { |
153 | 153 | $no_ccss = 'none'; |
154 | 154 | } |
@@ -161,45 +161,45 @@ discard block |
||
161 | 161 | |
162 | 162 | // Finally, inline the default CriticalCSS if any or else the entire CSS for the page |
163 | 163 | // This also applies to logged in users if the option to add CCSS for logged in users has been disabled. |
164 | - if ( ! empty( $inlined ) && 'none' !== $no_ccss ) { |
|
165 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $inlined . $ao_ccss_additional ); |
|
164 | + if (!empty($inlined) && 'none' !== $no_ccss) { |
|
165 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $inlined.$ao_ccss_additional); |
|
166 | 166 | } else { |
167 | - add_filter( 'autoptimize_filter_css_inline', '__return_true' ); |
|
167 | + add_filter('autoptimize_filter_css_inline', '__return_true'); |
|
168 | 168 | return; |
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
172 | - public function ao_ccss_defer_jquery( $in ) { |
|
172 | + public function ao_ccss_defer_jquery($in) { |
|
173 | 173 | global $ao_ccss_loggedin; |
174 | 174 | // defer all linked and inline JS. |
175 | - if ( ( ! is_user_logged_in() || $ao_ccss_loggedin ) && preg_match_all( '#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER ) ) { |
|
176 | - foreach ( $matches as $match ) { |
|
177 | - if ( str_replace( apply_filters( 'autoptimize_filter_ccss_core_defer_exclude', array( 'data-noptimize="1"', 'data-cfasync="false"', 'data-pagespeed-no-defer' ) ), '', $match[0] ) !== $match[0] ) { |
|
175 | + if ((!is_user_logged_in() || $ao_ccss_loggedin) && preg_match_all('#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER)) { |
|
176 | + foreach ($matches as $match) { |
|
177 | + if (str_replace(apply_filters('autoptimize_filter_ccss_core_defer_exclude', array('data-noptimize="1"', 'data-cfasync="false"', 'data-pagespeed-no-defer')), '', $match[0]) !== $match[0]) { |
|
178 | 178 | // do not touch JS with noptimize/ cfasync/ pagespeed-no-defer flags. |
179 | 179 | continue; |
180 | - } elseif ( '' !== $match[1] && ( ! preg_match( '/<script.* type\s?=.*>/', $match[0] ) || preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $match[0] ) ) ) { |
|
180 | + } elseif ('' !== $match[1] && (!preg_match('/<script.* type\s?=.*>/', $match[0]) || preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $match[0]))) { |
|
181 | 181 | // base64-encode and defer all inline JS. |
182 | - $base64_js = '<script defer src="data:text/javascript;base64,' . base64_encode( $match[1] ) . '"></script>'; |
|
183 | - $in = str_replace( $match[0], $base64_js, $in ); |
|
184 | - } elseif ( str_replace( array( ' defer', ' async' ), '', $match[0] ) === $match[0] ) { |
|
182 | + $base64_js = '<script defer src="data:text/javascript;base64,'.base64_encode($match[1]).'"></script>'; |
|
183 | + $in = str_replace($match[0], $base64_js, $in); |
|
184 | + } elseif (str_replace(array(' defer', ' async'), '', $match[0]) === $match[0]) { |
|
185 | 185 | // and defer linked JS unless already deferred or asynced. |
186 | - $new_match = str_replace( '<script ', '<script defer ', $match[0] ); |
|
187 | - $in = str_replace( $match[0], $new_match, $in ); |
|
186 | + $new_match = str_replace('<script ', '<script defer ', $match[0]); |
|
187 | + $in = str_replace($match[0], $new_match, $in); |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 | } |
191 | 191 | return $in; |
192 | 192 | } |
193 | 193 | |
194 | - public function ao_ccss_unloadccss( $html_in ) { |
|
194 | + public function ao_ccss_unloadccss($html_in) { |
|
195 | 195 | // set media attrib of inline CCSS to none at onLoad to avoid it impacting full CSS (rarely needed). |
196 | - $_unloadccss_js = apply_filters( 'autoptimize_filter_ccss_core_unloadccss_js', '<script>window.addEventListener("load", function(event) {document.getElementById("aoatfcss").media="none";})</script>' ); |
|
196 | + $_unloadccss_js = apply_filters('autoptimize_filter_ccss_core_unloadccss_js', '<script>window.addEventListener("load", function(event) {document.getElementById("aoatfcss").media="none";})</script>'); |
|
197 | 197 | |
198 | - if ( false !== strpos( $html_in, $_unloadccss_js . '</body>' ) ) { |
|
198 | + if (false !== strpos($html_in, $_unloadccss_js.'</body>')) { |
|
199 | 199 | return $html_in; |
200 | 200 | } |
201 | 201 | |
202 | - return str_replace( '</body>', $_unloadccss_js . '</body>', $html_in ); |
|
202 | + return str_replace('</body>', $_unloadccss_js.'</body>', $html_in); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | public function ao_ccss_extend_types() { |
@@ -208,9 +208,9 @@ discard block |
||
208 | 208 | global $ao_ccss_types; |
209 | 209 | |
210 | 210 | // in some cases $ao_ccss_types is empty and/or not an array, this should work around that problem. |
211 | - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) { |
|
211 | + if (empty($ao_ccss_types) || !is_array($ao_ccss_types)) { |
|
212 | 212 | $ao_ccss_types = get_ao_ccss_core_types(); |
213 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Empty types array in extend, refetching array with core conditionals.', 3 ); |
|
213 | + autoptimizeCriticalCSSCore::ao_ccss_log('Empty types array in extend, refetching array with core conditionals.', 3); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | // Custom Post Types. |
@@ -222,23 +222,23 @@ discard block |
||
222 | 222 | 'names', |
223 | 223 | 'and' |
224 | 224 | ); |
225 | - foreach ( $cpts as $cpt ) { |
|
226 | - array_unshift( $ao_ccss_types, 'custom_post_' . $cpt ); |
|
225 | + foreach ($cpts as $cpt) { |
|
226 | + array_unshift($ao_ccss_types, 'custom_post_'.$cpt); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | // Templates. |
230 | 230 | // Transient cache to avoid frequent disk reads. |
231 | - $templates = get_transient( 'autoptimize_ccss_page_templates' ); |
|
232 | - if ( ! $templates ) { |
|
231 | + $templates = get_transient('autoptimize_ccss_page_templates'); |
|
232 | + if (!$templates) { |
|
233 | 233 | $templates = wp_get_theme()->get_page_templates(); |
234 | - set_transient( 'autoptimize_ccss_page_templates', $templates, HOUR_IN_SECONDS ); |
|
234 | + set_transient('autoptimize_ccss_page_templates', $templates, HOUR_IN_SECONDS); |
|
235 | 235 | } |
236 | - foreach ( $templates as $tplfile => $tplname ) { |
|
237 | - array_unshift( $ao_ccss_types, 'template_' . $tplfile ); |
|
236 | + foreach ($templates as $tplfile => $tplname) { |
|
237 | + array_unshift($ao_ccss_types, 'template_'.$tplfile); |
|
238 | 238 | } |
239 | 239 | |
240 | 240 | // bbPress tags. |
241 | - if ( function_exists( 'is_bbpress' ) ) { |
|
241 | + if (function_exists('is_bbpress')) { |
|
242 | 242 | $ao_ccss_types = array_merge( |
243 | 243 | array( |
244 | 244 | 'bbp_is_bbpress', |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | } |
271 | 271 | |
272 | 272 | // BuddyPress tags. |
273 | - if ( function_exists( 'is_buddypress' ) ) { |
|
273 | + if (function_exists('is_buddypress')) { |
|
274 | 274 | $ao_ccss_types = array_merge( |
275 | 275 | array( |
276 | 276 | 'bp_is_activation_page', |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | } |
313 | 313 | |
314 | 314 | // Easy Digital Downloads (EDD) tags. |
315 | - if ( function_exists( 'edd_is_checkout' ) ) { |
|
315 | + if (function_exists('edd_is_checkout')) { |
|
316 | 316 | $ao_ccss_types = array_merge( |
317 | 317 | array( |
318 | 318 | 'edd_is_checkout', |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | } |
325 | 325 | |
326 | 326 | // WooCommerce tags. |
327 | - if ( class_exists( 'WooCommerce' ) ) { |
|
327 | + if (class_exists('WooCommerce')) { |
|
328 | 328 | $ao_ccss_types = array_merge( |
329 | 329 | array( |
330 | 330 | 'woo_is_account_page', |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | |
344 | 344 | public function get_ao_ccss_core_types() { |
345 | 345 | global $ao_ccss_types; |
346 | - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) { |
|
346 | + if (empty($ao_ccss_types) || !is_array($ao_ccss_types)) { |
|
347 | 347 | return array( |
348 | 348 | 'is_404', |
349 | 349 | 'is_archive', |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | } |
365 | 365 | } |
366 | 366 | |
367 | - public static function ao_ccss_key_status( $render ) { |
|
367 | + public static function ao_ccss_key_status($render) { |
|
368 | 368 | // Provide key status |
369 | 369 | // Get key and key status. |
370 | 370 | global $ao_ccss_key; |
@@ -377,55 +377,55 @@ discard block |
||
377 | 377 | $key_return = array(); |
378 | 378 | $status = false; |
379 | 379 | |
380 | - if ( $key && 2 == $key_status ) { |
|
380 | + if ($key && 2 == $key_status) { |
|
381 | 381 | // Key exists and its status is valid. |
382 | 382 | // Set valid key status. |
383 | 383 | $status = 'valid'; |
384 | - $status_msg = __( 'Valid' ); |
|
384 | + $status_msg = __('Valid'); |
|
385 | 385 | $color = '#46b450'; // Green. |
386 | 386 | $message = null; |
387 | - } elseif ( $key && 1 == $key_status ) { |
|
387 | + } elseif ($key && 1 == $key_status) { |
|
388 | 388 | // Key exists but its validation has failed. |
389 | 389 | // Set invalid key status. |
390 | 390 | $status = 'invalid'; |
391 | - $status_msg = __( 'Invalid' ); |
|
391 | + $status_msg = __('Invalid'); |
|
392 | 392 | $color = '#dc3232'; // Red. |
393 | - $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' ); |
|
394 | - } elseif ( $key && ! $key_status ) { |
|
393 | + $message = __('Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize'); |
|
394 | + } elseif ($key && !$key_status) { |
|
395 | 395 | // Key exists but it has no valid status yet |
396 | 396 | // Perform key validation. |
397 | - $key_check = $self->ao_ccss_key_validation( $key ); |
|
397 | + $key_check = $self->ao_ccss_key_validation($key); |
|
398 | 398 | |
399 | 399 | // Key is valid, set valid status. |
400 | - if ( $key_check ) { |
|
400 | + if ($key_check) { |
|
401 | 401 | $status = 'valid'; |
402 | - $status_msg = __( 'Valid' ); |
|
402 | + $status_msg = __('Valid'); |
|
403 | 403 | $color = '#46b450'; // Green. |
404 | 404 | $message = null; |
405 | 405 | } else { |
406 | 406 | // Key is invalid, set invalid status. |
407 | 407 | $status = 'invalid'; |
408 | - $status_msg = __( 'Invalid' ); |
|
408 | + $status_msg = __('Invalid'); |
|
409 | 409 | $color = '#dc3232'; // Red. |
410 | - if ( get_option( 'autoptimize_ccss_keyst' ) == 1 ) { |
|
411 | - $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' ); |
|
410 | + if (get_option('autoptimize_ccss_keyst') == 1) { |
|
411 | + $message = __('Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize'); |
|
412 | 412 | } else { |
413 | - $message = __( 'Something went wrong when checking your API key, make sure you server can communicate with https://criticalcss.com and/ or try again later.', 'autoptimize' ); |
|
413 | + $message = __('Something went wrong when checking your API key, make sure you server can communicate with https://criticalcss.com and/ or try again later.', 'autoptimize'); |
|
414 | 414 | } |
415 | 415 | } |
416 | 416 | } else { |
417 | 417 | // No key nor status |
418 | 418 | // Set no key status. |
419 | 419 | $status = 'nokey'; |
420 | - $status_msg = __( 'None' ); |
|
420 | + $status_msg = __('None'); |
|
421 | 421 | $color = '#ffb900'; // Yellow. |
422 | - $message = __( 'Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key to start.', 'autoptimize' ); |
|
422 | + $message = __('Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key to start.', 'autoptimize'); |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | // Fill returned values. |
426 | 426 | $key_return['status'] = $status; |
427 | 427 | // Provide rendering information if required. |
428 | - if ( $render ) { |
|
428 | + if ($render) { |
|
429 | 429 | $key_return['stmsg'] = $status_msg; |
430 | 430 | $key_return['color'] = $color; |
431 | 431 | $key_return['msg'] = $message; |
@@ -435,7 +435,7 @@ discard block |
||
435 | 435 | return $key_return; |
436 | 436 | } |
437 | 437 | |
438 | - public function ao_ccss_key_validation( $key ) { |
|
438 | + public function ao_ccss_key_validation($key) { |
|
439 | 439 | global $ao_ccss_noptimize; |
440 | 440 | |
441 | 441 | // POST a dummy job to criticalcss.com to check for key validation |
@@ -443,26 +443,26 @@ discard block |
||
443 | 443 | $src_url = get_home_url(); |
444 | 444 | |
445 | 445 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
446 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
446 | + if (!empty($ao_ccss_noptimize)) { |
|
447 | 447 | $src_url .= '?ao_noptirocket=1'; |
448 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
448 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
449 | 449 | $src_url .= '?ao_nolazy=1'; |
450 | 450 | } |
451 | 451 | |
452 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
452 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
453 | 453 | |
454 | 454 | // Prepare the request. |
455 | - $url = esc_url_raw( AO_CCSS_API . 'generate' ); |
|
455 | + $url = esc_url_raw(AO_CCSS_API.'generate'); |
|
456 | 456 | $args = array( |
457 | 457 | 'headers' => array( |
458 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
458 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
459 | 459 | 'Content-type' => 'application/json; charset=utf-8', |
460 | - 'Authorization' => 'JWT ' . $key, |
|
460 | + 'Authorization' => 'JWT '.$key, |
|
461 | 461 | 'Connection' => 'close', |
462 | 462 | ), |
463 | 463 | // Body must be JSON. |
464 | 464 | 'body' => json_encode( |
465 | - apply_filters( 'autoptimize_ccss_cron_api_generate_body', |
|
465 | + apply_filters('autoptimize_ccss_cron_api_generate_body', |
|
466 | 466 | array( |
467 | 467 | 'url' => $src_url, |
468 | 468 | 'aff' => 1, |
@@ -473,23 +473,23 @@ discard block |
||
473 | 473 | ); |
474 | 474 | |
475 | 475 | // Dispatch the request and store its response code. |
476 | - $req = wp_safe_remote_post( $url, $args ); |
|
477 | - $code = wp_remote_retrieve_response_code( $req ); |
|
478 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
476 | + $req = wp_safe_remote_post($url, $args); |
|
477 | + $code = wp_remote_retrieve_response_code($req); |
|
478 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
479 | 479 | |
480 | - if ( 200 == $code ) { |
|
480 | + if (200 == $code) { |
|
481 | 481 | // Response is OK. |
482 | 482 | // Set key status as valid and log key check. |
483 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
484 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
483 | + update_option('autoptimize_ccss_keyst', 2); |
|
484 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
485 | 485 | |
486 | 486 | // extract job-id from $body and put it in the queue as a P job |
487 | 487 | // but only if no jobs and no rules! |
488 | 488 | global $ao_ccss_queue; |
489 | 489 | global $ao_ccss_rules; |
490 | 490 | |
491 | - if ( 0 == count( $ao_ccss_queue ) && 0 == count( $ao_ccss_rules['types'] ) && 0 == count( $ao_ccss_rules['paths'] ) ) { |
|
492 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] ) { |
|
491 | + if (0 == count($ao_ccss_queue) && 0 == count($ao_ccss_rules['types']) && 0 == count($ao_ccss_rules['paths'])) { |
|
492 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status']) { |
|
493 | 493 | $jprops['ljid'] = 'firstrun'; |
494 | 494 | $jprops['rtarget'] = 'types|is_front_page'; |
495 | 495 | $jprops['ptype'] = 'is_front_page'; |
@@ -500,30 +500,30 @@ discard block |
||
500 | 500 | $jprops['jqstat'] = $body['job']['status']; |
501 | 501 | $jprops['jrstat'] = null; |
502 | 502 | $jprops['jvstat'] = null; |
503 | - $jprops['jctime'] = microtime( true ); |
|
503 | + $jprops['jctime'] = microtime(true); |
|
504 | 504 | $jprops['jftime'] = null; |
505 | 505 | $ao_ccss_queue['/'] = $jprops; |
506 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
507 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
508 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Created P job for is_front_page based on API key check response.', 3 ); |
|
506 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
507 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
508 | + autoptimizeCriticalCSSCore::ao_ccss_log('Created P job for is_front_page based on API key check response.', 3); |
|
509 | 509 | } |
510 | 510 | } |
511 | 511 | return true; |
512 | - } elseif ( 401 == $code ) { |
|
512 | + } elseif (401 == $code) { |
|
513 | 513 | // Response is unauthorized |
514 | 514 | // Set key status as invalid and log key check. |
515 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
516 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
515 | + update_option('autoptimize_ccss_keyst', 1); |
|
516 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
517 | 517 | return false; |
518 | 518 | } else { |
519 | 519 | // Response unkown |
520 | 520 | // Log key check attempt. |
521 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2 ); |
|
522 | - if ( ! empty( $body ) ) { |
|
523 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
521 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2); |
|
522 | + if (!empty($body)) { |
|
523 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
524 | 524 | } |
525 | - if ( is_wp_error( $req ) ) { |
|
526 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
525 | + if (is_wp_error($req)) { |
|
526 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
527 | 527 | } |
528 | 528 | return false; |
529 | 529 | } |
@@ -538,14 +538,14 @@ discard block |
||
538 | 538 | $viewport = array(); |
539 | 539 | |
540 | 540 | // Viewport Width. |
541 | - if ( ! empty( $ao_ccss_viewport['w'] ) ) { |
|
541 | + if (!empty($ao_ccss_viewport['w'])) { |
|
542 | 542 | $viewport['w'] = $ao_ccss_viewport['w']; |
543 | 543 | } else { |
544 | 544 | $viewport['w'] = ''; |
545 | 545 | } |
546 | 546 | |
547 | 547 | // Viewport Height. |
548 | - if ( ! empty( $ao_ccss_viewport['h'] ) ) { |
|
548 | + if (!empty($ao_ccss_viewport['h'])) { |
|
549 | 549 | $viewport['h'] = $ao_ccss_viewport['h']; |
550 | 550 | } else { |
551 | 551 | $viewport['h'] = ''; |
@@ -554,23 +554,23 @@ discard block |
||
554 | 554 | return $viewport; |
555 | 555 | } |
556 | 556 | |
557 | - public static function ao_ccss_check_contents( $ccss ) { |
|
557 | + public static function ao_ccss_check_contents($ccss) { |
|
558 | 558 | // Perform basic exploit avoidance and CSS validation. |
559 | - if ( ! empty( $ccss ) ) { |
|
559 | + if (!empty($ccss)) { |
|
560 | 560 | // Try to avoid code injection. |
561 | - $blocklist = array( '#!/', 'function(', '<script', '<?php' ); |
|
562 | - foreach ( $blocklist as $blocklisted ) { |
|
563 | - if ( strpos( $ccss, $blocklisted ) !== false ) { |
|
564 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received contained blocklisted content.', 2 ); |
|
561 | + $blocklist = array('#!/', 'function(', '<script', '<?php'); |
|
562 | + foreach ($blocklist as $blocklisted) { |
|
563 | + if (strpos($ccss, $blocklisted) !== false) { |
|
564 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received contained blocklisted content.', 2); |
|
565 | 565 | return false; |
566 | 566 | } |
567 | 567 | } |
568 | 568 | |
569 | 569 | // Check for most basics CSS structures. |
570 | - $needlist = array( '{', '}', ':' ); |
|
571 | - foreach ( $needlist as $needed ) { |
|
572 | - if ( false === strpos( $ccss, $needed ) && 'none' !== $ccss ) { |
|
573 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not seem to contain real CSS.', 2 ); |
|
570 | + $needlist = array('{', '}', ':'); |
|
571 | + foreach ($needlist as $needed) { |
|
572 | + if (false === strpos($ccss, $needed) && 'none' !== $ccss) { |
|
573 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not seem to contain real CSS.', 2); |
|
574 | 574 | return false; |
575 | 575 | } |
576 | 576 | } |
@@ -580,7 +580,7 @@ discard block |
||
580 | 580 | return true; |
581 | 581 | } |
582 | 582 | |
583 | - public static function ao_ccss_log( $msg, $lvl ) { |
|
583 | + public static function ao_ccss_log($msg, $lvl) { |
|
584 | 584 | // Commom logging facility |
585 | 585 | // Attach debug option. |
586 | 586 | global $ao_ccss_debug; |
@@ -591,7 +591,7 @@ discard block |
||
591 | 591 | // 3: DD (for debug) |
592 | 592 | // Default: UU (for unkown). |
593 | 593 | $level = false; |
594 | - switch ( $lvl ) { |
|
594 | + switch ($lvl) { |
|
595 | 595 | case 1: |
596 | 596 | $level = 'II'; |
597 | 597 | break; |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | break; |
601 | 601 | case 3: |
602 | 602 | // Output debug messages only if debug mode is enabled. |
603 | - if ( $ao_ccss_debug ) { |
|
603 | + if ($ao_ccss_debug) { |
|
604 | 604 | $level = 'DD'; |
605 | 605 | } |
606 | 606 | break; |
@@ -609,19 +609,19 @@ discard block |
||
609 | 609 | } |
610 | 610 | |
611 | 611 | // Prepare and write a log message if there's a valid level. |
612 | - if ( $level ) { |
|
612 | + if ($level) { |
|
613 | 613 | |
614 | 614 | // Prepare message. |
615 | - $message = date( 'c' ) . ' - [' . $level . '] ' . htmlentities( $msg ) . '<br>'; |
|
615 | + $message = date('c').' - ['.$level.'] '.htmlentities($msg).'<br>'; |
|
616 | 616 | |
617 | 617 | // Write message to log file. |
618 | - error_log( $message, 3, AO_CCSS_LOG ); |
|
618 | + error_log($message, 3, AO_CCSS_LOG); |
|
619 | 619 | } |
620 | 620 | } |
621 | 621 | |
622 | 622 | public static function ao_ccss_clear_page_tpl_cache() { |
623 | 623 | // Clears transient cache for page templates. |
624 | - delete_transient( 'autoptimize_ccss_page_templates' ); |
|
624 | + delete_transient('autoptimize_ccss_page_templates'); |
|
625 | 625 | } |
626 | 626 | |
627 | 627 | } |
@@ -20,24 +20,24 @@ |
||
20 | 20 | * to files that don't exist in AO's cache. |
21 | 21 | */ |
22 | 22 | |
23 | -$original_request = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
23 | +$original_request = strtok($_SERVER['REQUEST_URI'], '?'); |
|
24 | 24 | |
25 | -if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) { |
|
26 | - $original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request ); |
|
25 | +if (strpos($original_request, 'uucss/uucss-') !== false) { |
|
26 | + $original_request = preg_replace('/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request); |
|
27 | 27 | } |
28 | 28 | |
29 | -$fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); |
|
29 | +$fallback_target = preg_replace('/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request); |
|
30 | 30 | $ao_cache_dir = '<!--ao-cache-dir-->'; |
31 | -$js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); |
|
32 | -$fallback_path = $ao_cache_dir . $js_or_css . '/autoptimize_fallback.' . $js_or_css; |
|
31 | +$js_or_css = pathinfo($original_request, PATHINFO_EXTENSION); |
|
32 | +$fallback_path = $ao_cache_dir.$js_or_css.'/autoptimize_fallback.'.$js_or_css; |
|
33 | 33 | |
34 | -if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) { |
|
34 | +if ($original_request !== $fallback_target && file_exists($fallback_path)) { |
|
35 | 35 | // error_log( 'Autoptimize file ' . $original_request . ' not found, using fallback instead.' ); |
36 | - header( 'HTTP/1.1 301 Moved Permanently' ); |
|
37 | - header( 'Location: ' . $fallback_target ); |
|
36 | + header('HTTP/1.1 301 Moved Permanently'); |
|
37 | + header('Location: '.$fallback_target); |
|
38 | 38 | } else { |
39 | 39 | // error_log( 'Autoptimize file ' . $original_request . ' not found, sending 410 gone response.' ); |
40 | - header( 'HTTP/1.1 410 Gone' ); |
|
40 | + header('HTTP/1.1 410 Gone'); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | exit(); |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for JS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -206,133 +206,133 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @param array $options all options. |
208 | 208 | */ |
209 | - public function read( $options ) |
|
209 | + public function read($options) |
|
210 | 210 | { |
211 | - $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
|
212 | - if ( $noptimize_js ) { |
|
211 | + $noptimize_js = apply_filters('autoptimize_filter_js_noptimize', false, $this->content); |
|
212 | + if ($noptimize_js) { |
|
213 | 213 | return false; |
214 | 214 | } |
215 | 215 | |
216 | 216 | // only optimize known good JS? |
217 | - $allowlist_js = apply_filters( 'autoptimize_filter_js_allowlist', '', $this->content ); |
|
218 | - $allowlist_js = apply_filters( 'autoptimize_filter_js_whitelist', $allowlist_js, $this->content ); // fixme: to be removed in next version. |
|
219 | - if ( ! empty( $allowlist_js ) ) { |
|
220 | - $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_js ) ) ); |
|
217 | + $allowlist_js = apply_filters('autoptimize_filter_js_allowlist', '', $this->content); |
|
218 | + $allowlist_js = apply_filters('autoptimize_filter_js_whitelist', $allowlist_js, $this->content); // fixme: to be removed in next version. |
|
219 | + if (!empty($allowlist_js)) { |
|
220 | + $this->allowlist = array_filter(array_map('trim', explode(',', $allowlist_js))); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | // is there JS we should simply remove? |
224 | - $removable_js = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); |
|
225 | - if ( ! empty( $removable_js ) ) { |
|
226 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removable_js ) ) ); |
|
224 | + $removable_js = apply_filters('autoptimize_filter_js_removables', '', $this->content); |
|
225 | + if (!empty($removable_js)) { |
|
226 | + $this->jsremovables = array_filter(array_map('trim', explode(',', $removable_js))); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | // only header? |
230 | - if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { |
|
231 | - $content = explode( '</head>', $this->content, 2 ); |
|
232 | - $this->content = $content[0] . '</head>'; |
|
230 | + if (apply_filters('autoptimize_filter_js_justhead', $options['justhead'])) { |
|
231 | + $content = explode('</head>', $this->content, 2); |
|
232 | + $this->content = $content[0].'</head>'; |
|
233 | 233 | $this->restofcontent = $content[1]; |
234 | 234 | } |
235 | 235 | |
236 | 236 | // Determine whether we're doing JS-files aggregation or not. |
237 | - if ( ! $options['aggregate'] ) { |
|
237 | + if (!$options['aggregate']) { |
|
238 | 238 | $this->aggregate = false; |
239 | 239 | } |
240 | 240 | // Returning true for "dontaggregate" turns off aggregation. |
241 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
|
241 | + if ($this->aggregate && apply_filters('autoptimize_filter_js_dontaggregate', false)) { |
|
242 | 242 | $this->aggregate = false; |
243 | 243 | } |
244 | 244 | |
245 | 245 | // Defer when not aggregating. |
246 | - if ( false === $this->aggregate && apply_filters( 'autoptimize_js_filter_defer_not_aggregate', $options['defer_not_aggregate'] ) ) { |
|
246 | + if (false === $this->aggregate && apply_filters('autoptimize_js_filter_defer_not_aggregate', $options['defer_not_aggregate'])) { |
|
247 | 247 | $this->defer_not_aggregate = true; |
248 | 248 | } |
249 | 249 | |
250 | 250 | // include inline? |
251 | - if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
|
251 | + if (apply_filters('autoptimize_js_include_inline', $options['include_inline'])) { |
|
252 | 252 | $this->include_inline = true; |
253 | 253 | } |
254 | 254 | |
255 | 255 | // filter to "late inject minified JS", default to true for now (it is faster). |
256 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); |
|
256 | + $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late', true); |
|
257 | 257 | |
258 | 258 | // filters to override hardcoded do(nt)move(last) array contents (array in, array out!). |
259 | - $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); |
|
260 | - $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
|
261 | - $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
|
259 | + $this->dontmove = apply_filters('autoptimize_filter_js_dontmove', $this->dontmove); |
|
260 | + $this->domovelast = apply_filters('autoptimize_filter_js_movelast', $this->domovelast); |
|
261 | + $this->domove = apply_filters('autoptimize_filter_js_domove', $this->domove); |
|
262 | 262 | |
263 | 263 | // Determine whether excluded files should be minified if not yet so. |
264 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
264 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
265 | 265 | $this->minify_excluded = false; |
266 | 266 | } |
267 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_js_minify_excluded', $this->minify_excluded, '' ); |
|
267 | + $this->minify_excluded = apply_filters('autoptimize_filter_js_minify_excluded', $this->minify_excluded, ''); |
|
268 | 268 | |
269 | 269 | // get extra exclusions settings or filter. |
270 | 270 | $exclude_js = $options['js_exclude']; |
271 | - $exclude_js = apply_filters( 'autoptimize_filter_js_exclude', $exclude_js, $this->content ); |
|
272 | - |
|
273 | - if ( '' !== $exclude_js ) { |
|
274 | - if ( is_array( $exclude_js ) ) { |
|
275 | - $remove_keys = array_keys( $exclude_js, 'remove' ); |
|
276 | - if ( false !== $remove_keys ) { |
|
277 | - foreach ( $remove_keys as $remove_key ) { |
|
278 | - unset( $exclude_js[ $remove_key ] ); |
|
271 | + $exclude_js = apply_filters('autoptimize_filter_js_exclude', $exclude_js, $this->content); |
|
272 | + |
|
273 | + if ('' !== $exclude_js) { |
|
274 | + if (is_array($exclude_js)) { |
|
275 | + $remove_keys = array_keys($exclude_js, 'remove'); |
|
276 | + if (false !== $remove_keys) { |
|
277 | + foreach ($remove_keys as $remove_key) { |
|
278 | + unset($exclude_js[$remove_key]); |
|
279 | 279 | $this->jsremovables[] = $remove_key; |
280 | 280 | } |
281 | 281 | } |
282 | - $excl_js_arr = array_keys( $exclude_js ); |
|
282 | + $excl_js_arr = array_keys($exclude_js); |
|
283 | 283 | } else { |
284 | - $excl_js_arr = array_filter( array_map( 'trim', explode( ',', $exclude_js ) ) ); |
|
284 | + $excl_js_arr = array_filter(array_map('trim', explode(',', $exclude_js))); |
|
285 | 285 | } |
286 | - $this->dontmove = array_merge( $excl_js_arr, $this->dontmove ); |
|
286 | + $this->dontmove = array_merge($excl_js_arr, $this->dontmove); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | // Should we add try-catch? |
290 | - if ( $options['trycatch'] ) { |
|
290 | + if ($options['trycatch']) { |
|
291 | 291 | $this->trycatch = true; |
292 | 292 | } |
293 | 293 | |
294 | 294 | // force js in head? |
295 | - if ( $options['forcehead'] ) { |
|
295 | + if ($options['forcehead']) { |
|
296 | 296 | $this->forcehead = true; |
297 | 297 | } else { |
298 | 298 | $this->forcehead = false; |
299 | 299 | } |
300 | 300 | |
301 | - $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
|
301 | + $this->forcehead = apply_filters('autoptimize_filter_js_forcehead', $this->forcehead); |
|
302 | 302 | |
303 | 303 | // get cdn url. |
304 | 304 | $this->cdn_url = $options['cdn_url']; |
305 | 305 | |
306 | 306 | // noptimize me. |
307 | - $this->content = $this->hide_noptimize( $this->content ); |
|
307 | + $this->content = $this->hide_noptimize($this->content); |
|
308 | 308 | |
309 | 309 | // Save IE hacks. |
310 | - $this->content = $this->hide_iehacks( $this->content ); |
|
310 | + $this->content = $this->hide_iehacks($this->content); |
|
311 | 311 | |
312 | 312 | // comments. |
313 | - $this->content = $this->hide_comments( $this->content ); |
|
313 | + $this->content = $this->hide_comments($this->content); |
|
314 | 314 | |
315 | 315 | // Get script files. |
316 | - if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { |
|
317 | - foreach ( $matches[0] as $tag ) { |
|
316 | + if (preg_match_all('#<script.*</script>#Usmi', $this->content, $matches)) { |
|
317 | + foreach ($matches[0] as $tag) { |
|
318 | 318 | // only consider script aggregation for types allowlisted in should_aggregate-function. |
319 | - $should_aggregate = $this->should_aggregate( $tag ); |
|
320 | - if ( ! $should_aggregate ) { |
|
319 | + $should_aggregate = $this->should_aggregate($tag); |
|
320 | + if (!$should_aggregate) { |
|
321 | 321 | $tag = ''; |
322 | 322 | continue; |
323 | 323 | } |
324 | 324 | |
325 | - if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
|
325 | + if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source)) { |
|
326 | 326 | // non-inline script. |
327 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
328 | - $this->content = str_replace( $tag, '', $this->content ); |
|
327 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
328 | + $this->content = str_replace($tag, '', $this->content); |
|
329 | 329 | continue; |
330 | 330 | } |
331 | 331 | |
332 | 332 | $orig_tag = null; |
333 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
334 | - $path = $this->getpath( $url ); |
|
335 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable( $tag ) ) { |
|
333 | + $url = current(explode('?', $source[2], 2)); |
|
334 | + $path = $this->getpath($url); |
|
335 | + if (false !== $path && preg_match('#\.js$#', $path) && $this->ismergeable($tag)) { |
|
336 | 336 | // ok to optimize, add to array. |
337 | 337 | $this->scripts[] = $path; |
338 | 338 | } else { |
@@ -340,35 +340,35 @@ discard block |
||
340 | 340 | $new_tag = $tag; |
341 | 341 | |
342 | 342 | // non-mergeable script (excluded or dynamic or external). |
343 | - if ( is_array( $exclude_js ) ) { |
|
343 | + if (is_array($exclude_js)) { |
|
344 | 344 | // should we add flags? |
345 | - foreach ( $exclude_js as $excl_tag => $excl_flags ) { |
|
346 | - if ( false !== strpos( $orig_tag, $excl_tag ) && in_array( $excl_flags, array( 'async', 'defer' ) ) ) { |
|
347 | - $new_tag = str_replace( '<script ', '<script ' . $excl_flags . ' ', $new_tag ); |
|
345 | + foreach ($exclude_js as $excl_tag => $excl_flags) { |
|
346 | + if (false !== strpos($orig_tag, $excl_tag) && in_array($excl_flags, array('async', 'defer'))) { |
|
347 | + $new_tag = str_replace('<script ', '<script '.$excl_flags.' ', $new_tag); |
|
348 | 348 | } |
349 | 349 | } |
350 | 350 | } |
351 | 351 | |
352 | 352 | // not aggregating but deferring? |
353 | - if ( $this->defer_not_aggregate && false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path && strpos( $new_tag, ' defer' ) === false ) { |
|
354 | - $new_tag = str_replace( '<script ', '<script defer ', $new_tag ); |
|
353 | + if ($this->defer_not_aggregate && false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path && strpos($new_tag, ' defer') === false) { |
|
354 | + $new_tag = str_replace('<script ', '<script defer ', $new_tag); |
|
355 | 355 | // and remove async as async+defer=async while we explicitly want defer. |
356 | - if ( strpos( $new_tag, ' async' ) !== false && apply_filters( 'autoptimize_filter_js_defer_remove_async', true ) ) { |
|
357 | - $new_tag = str_replace( array( ' async', ' async="async"', " async='async'" ), '', $new_tag ); |
|
356 | + if (strpos($new_tag, ' async') !== false && apply_filters('autoptimize_filter_js_defer_remove_async', true)) { |
|
357 | + $new_tag = str_replace(array(' async', ' async="async"', " async='async'"), '', $new_tag); |
|
358 | 358 | } |
359 | 359 | } |
360 | 360 | |
361 | 361 | // Should we minify the non-aggregated script? |
362 | 362 | // -> if aggregate is on and exclude minify is on |
363 | 363 | // -> if aggregate is off and the file is not in dontmove. |
364 | - if ( $path && $this->minify_excluded ) { |
|
365 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
366 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
367 | - $minified_url = $this->minify_single( $path ); |
|
368 | - if ( ! empty( $minified_url ) ) { |
|
364 | + if ($path && $this->minify_excluded) { |
|
365 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
366 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
367 | + $minified_url = $this->minify_single($path); |
|
368 | + if (!empty($minified_url)) { |
|
369 | 369 | // Replace original URL with minified URL from cache. |
370 | - $new_tag = str_replace( $url, $minified_url, $new_tag ); |
|
371 | - } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { |
|
370 | + $new_tag = str_replace($url, $minified_url, $new_tag); |
|
371 | + } elseif (apply_filters('autoptimize_filter_ccsjs_remove_empty_minified_url', false)) { |
|
372 | 372 | // Remove the original script tag, because cache content is empty but only if filter |
373 | 373 | // is trued because $minified_url is also false if original JS is minified already. |
374 | 374 | $new_tag = ''; |
@@ -376,17 +376,17 @@ discard block |
||
376 | 376 | } |
377 | 377 | } |
378 | 378 | |
379 | - if ( $this->ismovable( $new_tag ) ) { |
|
379 | + if ($this->ismovable($new_tag)) { |
|
380 | 380 | // can be moved, flags and all. |
381 | - if ( $this->movetolast( $new_tag ) ) { |
|
381 | + if ($this->movetolast($new_tag)) { |
|
382 | 382 | $this->move['last'][] = $new_tag; |
383 | 383 | } else { |
384 | 384 | $this->move['first'][] = $new_tag; |
385 | 385 | } |
386 | 386 | } else { |
387 | 387 | // cannot be moved, so if flag was added re-inject altered tag immediately. |
388 | - if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { |
|
389 | - $this->content = str_replace( $orig_tag, $new_tag, $this->content ); |
|
388 | + if (('' !== $new_tag && $orig_tag !== $new_tag) || ('' === $new_tag && apply_filters('autoptimize_filter_js_remove_empty_files', false))) { |
|
389 | + $this->content = str_replace($orig_tag, $new_tag, $this->content); |
|
390 | 390 | $orig_tag = ''; |
391 | 391 | } |
392 | 392 | // and forget about the $tag (not to be touched any more). |
@@ -395,23 +395,23 @@ discard block |
||
395 | 395 | } |
396 | 396 | } else { |
397 | 397 | // Inline script. |
398 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
399 | - $this->content = str_replace( $tag, '', $this->content ); |
|
398 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
399 | + $this->content = str_replace($tag, '', $this->content); |
|
400 | 400 | continue; |
401 | 401 | } |
402 | 402 | |
403 | 403 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. |
404 | - $tag = $this->restore_comments( $tag ); |
|
405 | - if ( $this->ismergeable( $tag ) && $this->include_inline ) { |
|
406 | - preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); |
|
407 | - $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); |
|
408 | - $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); |
|
409 | - $this->scripts[] = 'INLINE;' . $code; |
|
404 | + $tag = $this->restore_comments($tag); |
|
405 | + if ($this->ismergeable($tag) && $this->include_inline) { |
|
406 | + preg_match('#<script.*>(.*)</script>#Usmi', $tag, $code); |
|
407 | + $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1]); |
|
408 | + $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code); |
|
409 | + $this->scripts[] = 'INLINE;'.$code; |
|
410 | 410 | } else { |
411 | 411 | // Can we move this? |
412 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); |
|
413 | - if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { |
|
414 | - if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { |
|
412 | + $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $tag); |
|
413 | + if ($this->ismovable($tag) || '' !== $autoptimize_js_moveable) { |
|
414 | + if ($this->movetolast($tag) || 'last' === $autoptimize_js_moveable) { |
|
415 | 415 | $this->move['last'][] = $tag; |
416 | 416 | } else { |
417 | 417 | $this->move['first'][] = $tag; |
@@ -422,11 +422,11 @@ discard block |
||
422 | 422 | } |
423 | 423 | } |
424 | 424 | // Re-hide comments to be able to do the removal based on tag from $this->content. |
425 | - $tag = $this->hide_comments( $tag ); |
|
425 | + $tag = $this->hide_comments($tag); |
|
426 | 426 | } |
427 | 427 | |
428 | 428 | // Remove the original script tag. |
429 | - $this->content = str_replace( $tag, '', $this->content ); |
|
429 | + $this->content = str_replace($tag, '', $this->content); |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | return true; |
@@ -451,30 +451,30 @@ discard block |
||
451 | 451 | * @param string $tag Script node & child(ren). |
452 | 452 | * @return bool |
453 | 453 | */ |
454 | - public function should_aggregate( $tag ) |
|
454 | + public function should_aggregate($tag) |
|
455 | 455 | { |
456 | - if ( empty( $tag ) ) { |
|
456 | + if (empty($tag)) { |
|
457 | 457 | return false; |
458 | 458 | } |
459 | 459 | |
460 | 460 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
461 | 461 | // inline code that might just contain the 'type=' string... |
462 | 462 | $tag_parts = array(); |
463 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts ); |
|
463 | + preg_match('#<(script[^>]*)>#i', $tag, $tag_parts); |
|
464 | 464 | $tag_without_contents = null; |
465 | - if ( ! empty( $tag_parts[1] ) ) { |
|
465 | + if (!empty($tag_parts[1])) { |
|
466 | 466 | $tag_without_contents = $tag_parts[1]; |
467 | 467 | } |
468 | 468 | |
469 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); |
|
469 | + $has_type = (strpos($tag_without_contents, 'type') !== false); |
|
470 | 470 | |
471 | 471 | $type_valid = false; |
472 | - if ( $has_type ) { |
|
473 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); |
|
472 | + if ($has_type) { |
|
473 | + $type_valid = (bool) preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents); |
|
474 | 474 | } |
475 | 475 | |
476 | 476 | $should_aggregate = false; |
477 | - if ( ! $has_type || $type_valid ) { |
|
477 | + if (!$has_type || $type_valid) { |
|
478 | 478 | $should_aggregate = true; |
479 | 479 | } |
480 | 480 | |
@@ -486,75 +486,75 @@ discard block |
||
486 | 486 | */ |
487 | 487 | public function minify() |
488 | 488 | { |
489 | - foreach ( $this->scripts as $script ) { |
|
490 | - if ( empty( $script ) ) { |
|
489 | + foreach ($this->scripts as $script) { |
|
490 | + if (empty($script)) { |
|
491 | 491 | continue; |
492 | 492 | } |
493 | 493 | |
494 | 494 | // TODO/FIXME: some duplicate code here, can be reduced/simplified. |
495 | - if ( preg_match( '#^INLINE;#', $script ) ) { |
|
495 | + if (preg_match('#^INLINE;#', $script)) { |
|
496 | 496 | // Inline script. |
497 | - $script = preg_replace( '#^INLINE;#', '', $script ); |
|
498 | - $script = rtrim( $script, ";\n\t\r" ) . ';'; |
|
497 | + $script = preg_replace('#^INLINE;#', '', $script); |
|
498 | + $script = rtrim($script, ";\n\t\r").';'; |
|
499 | 499 | // Add try-catch? |
500 | - if ( $this->trycatch ) { |
|
501 | - $script = 'try{' . $script . '}catch(e){}'; |
|
500 | + if ($this->trycatch) { |
|
501 | + $script = 'try{'.$script.'}catch(e){}'; |
|
502 | 502 | } |
503 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
|
504 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
|
503 | + $tmpscript = apply_filters('autoptimize_js_individual_script', $script, ''); |
|
504 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) { |
|
505 | 505 | $script = $tmpscript; |
506 | 506 | $this->alreadyminified = true; |
507 | 507 | } |
508 | - $this->jscode .= "\n" . $script; |
|
508 | + $this->jscode .= "\n".$script; |
|
509 | 509 | } else { |
510 | 510 | // External script. |
511 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
|
512 | - $scriptsrc = file_get_contents( $script ); |
|
513 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
|
514 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
|
511 | + if (false !== $script && file_exists($script) && is_readable($script)) { |
|
512 | + $scriptsrc = file_get_contents($script); |
|
513 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc); |
|
514 | + $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';'; |
|
515 | 515 | // Add try-catch? |
516 | - if ( $this->trycatch ) { |
|
517 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
|
516 | + if ($this->trycatch) { |
|
517 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
|
518 | 518 | } |
519 | - $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
|
520 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
|
519 | + $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script); |
|
520 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) { |
|
521 | 521 | $scriptsrc = $tmpscriptsrc; |
522 | 522 | $this->alreadyminified = true; |
523 | - } elseif ( $this->can_inject_late( $script ) ) { |
|
524 | - $scriptsrc = self::build_injectlater_marker( $script, md5( $scriptsrc ) ); |
|
523 | + } elseif ($this->can_inject_late($script)) { |
|
524 | + $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); |
|
525 | 525 | } |
526 | - $this->jscode .= "\n" . $scriptsrc; |
|
526 | + $this->jscode .= "\n".$scriptsrc; |
|
527 | 527 | } |
528 | 528 | } |
529 | 529 | } |
530 | 530 | |
531 | 531 | // Check for already-minified code. |
532 | - $this->md5hash = md5( $this->jscode ); |
|
533 | - $ccheck = new autoptimizeCache( $this->md5hash, 'js' ); |
|
534 | - if ( $ccheck->check() ) { |
|
532 | + $this->md5hash = md5($this->jscode); |
|
533 | + $ccheck = new autoptimizeCache($this->md5hash, 'js'); |
|
534 | + if ($ccheck->check()) { |
|
535 | 535 | $this->jscode = $ccheck->retrieve(); |
536 | 536 | return true; |
537 | 537 | } |
538 | - unset( $ccheck ); |
|
538 | + unset($ccheck); |
|
539 | 539 | |
540 | 540 | // $this->jscode has all the uncompressed code now. |
541 | - if ( true !== $this->alreadyminified ) { |
|
542 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { |
|
543 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); |
|
544 | - if ( ! empty( $tmp_jscode ) ) { |
|
541 | + if (true !== $this->alreadyminified) { |
|
542 | + if (apply_filters('autoptimize_js_do_minify', true)) { |
|
543 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); |
|
544 | + if (!empty($tmp_jscode)) { |
|
545 | 545 | $this->jscode = $tmp_jscode; |
546 | - unset( $tmp_jscode ); |
|
546 | + unset($tmp_jscode); |
|
547 | 547 | } |
548 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
549 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
548 | + $this->jscode = $this->inject_minified($this->jscode); |
|
549 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
550 | 550 | return true; |
551 | 551 | } else { |
552 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
552 | + $this->jscode = $this->inject_minified($this->jscode); |
|
553 | 553 | return false; |
554 | 554 | } |
555 | 555 | } |
556 | 556 | |
557 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
557 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
558 | 558 | return true; |
559 | 559 | } |
560 | 560 | |
@@ -563,13 +563,13 @@ discard block |
||
563 | 563 | */ |
564 | 564 | public function cache() |
565 | 565 | { |
566 | - $cache = new autoptimizeCache( $this->md5hash, 'js' ); |
|
567 | - if ( ! $cache->check() ) { |
|
566 | + $cache = new autoptimizeCache($this->md5hash, 'js'); |
|
567 | + if (!$cache->check()) { |
|
568 | 568 | // Cache our code. |
569 | - $cache->cache( $this->jscode, 'text/javascript' ); |
|
569 | + $cache->cache($this->jscode, 'text/javascript'); |
|
570 | 570 | } |
571 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
572 | - $this->url = $this->url_replace_cdn( $this->url ); |
|
571 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
572 | + $this->url = $this->url_replace_cdn($this->url); |
|
573 | 573 | } |
574 | 574 | |
575 | 575 | /** |
@@ -578,47 +578,47 @@ discard block |
||
578 | 578 | public function getcontent() |
579 | 579 | { |
580 | 580 | // Restore the full content. |
581 | - if ( ! empty( $this->restofcontent ) ) { |
|
581 | + if (!empty($this->restofcontent)) { |
|
582 | 582 | $this->content .= $this->restofcontent; |
583 | 583 | $this->restofcontent = ''; |
584 | 584 | } |
585 | 585 | |
586 | 586 | // Add the scripts taking forcehead/ deferred (default) into account. |
587 | - if ( $this->forcehead ) { |
|
588 | - $replace_tag = array( '</head>', 'before' ); |
|
587 | + if ($this->forcehead) { |
|
588 | + $replace_tag = array('</head>', 'before'); |
|
589 | 589 | $defer = ''; |
590 | 590 | } else { |
591 | - $replace_tag = array( '</body>', 'before' ); |
|
591 | + $replace_tag = array('</body>', 'before'); |
|
592 | 592 | $defer = 'defer '; |
593 | 593 | } |
594 | 594 | |
595 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
|
595 | + $defer = apply_filters('autoptimize_filter_js_defer', $defer); |
|
596 | 596 | $type_js = ''; |
597 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
597 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
598 | 598 | $type_js = 'type="text/javascript" '; |
599 | 599 | } |
600 | 600 | |
601 | - $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>'; |
|
602 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
|
601 | + $bodyreplacementpayload = '<script '.$type_js.$defer.'src="'.$this->url.'"></script>'; |
|
602 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload); |
|
603 | 603 | |
604 | - $bodyreplacement = implode( '', $this->move['first'] ); |
|
604 | + $bodyreplacement = implode('', $this->move['first']); |
|
605 | 605 | $bodyreplacement .= $bodyreplacementpayload; |
606 | - $bodyreplacement .= implode( '', $this->move['last'] ); |
|
606 | + $bodyreplacement .= implode('', $this->move['last']); |
|
607 | 607 | |
608 | - $replace_tag = apply_filters( 'autoptimize_filter_js_replacetag', $replace_tag ); |
|
608 | + $replace_tag = apply_filters('autoptimize_filter_js_replacetag', $replace_tag); |
|
609 | 609 | |
610 | - if ( strlen( $this->jscode ) > 0 ) { |
|
611 | - $this->inject_in_html( $bodyreplacement, $replace_tag ); |
|
610 | + if (strlen($this->jscode) > 0) { |
|
611 | + $this->inject_in_html($bodyreplacement, $replace_tag); |
|
612 | 612 | } |
613 | 613 | |
614 | 614 | // Restore comments. |
615 | - $this->content = $this->restore_comments( $this->content ); |
|
615 | + $this->content = $this->restore_comments($this->content); |
|
616 | 616 | |
617 | 617 | // Restore IE hacks. |
618 | - $this->content = $this->restore_iehacks( $this->content ); |
|
618 | + $this->content = $this->restore_iehacks($this->content); |
|
619 | 619 | |
620 | 620 | // Restore noptimize. |
621 | - $this->content = $this->restore_noptimize( $this->content ); |
|
621 | + $this->content = $this->restore_noptimize($this->content); |
|
622 | 622 | |
623 | 623 | // Return the modified HTML. |
624 | 624 | return $this->content; |
@@ -629,34 +629,34 @@ discard block |
||
629 | 629 | * |
630 | 630 | * @param string $tag JS tag. |
631 | 631 | */ |
632 | - private function ismergeable( $tag ) |
|
632 | + private function ismergeable($tag) |
|
633 | 633 | { |
634 | - if ( empty( $tag ) || ! $this->aggregate ) { |
|
634 | + if (empty($tag) || !$this->aggregate) { |
|
635 | 635 | return false; |
636 | 636 | } |
637 | 637 | |
638 | - if ( ! empty( $this->allowlist ) ) { |
|
639 | - foreach ( $this->allowlist as $match ) { |
|
640 | - if ( false !== strpos( $tag, $match ) ) { |
|
638 | + if (!empty($this->allowlist)) { |
|
639 | + foreach ($this->allowlist as $match) { |
|
640 | + if (false !== strpos($tag, $match)) { |
|
641 | 641 | return true; |
642 | 642 | } |
643 | 643 | } |
644 | 644 | // No match with allowlist. |
645 | 645 | return false; |
646 | 646 | } else { |
647 | - foreach ( $this->domove as $match ) { |
|
648 | - if ( false !== strpos( $tag, $match ) ) { |
|
647 | + foreach ($this->domove as $match) { |
|
648 | + if (false !== strpos($tag, $match)) { |
|
649 | 649 | // Matched something. |
650 | 650 | return false; |
651 | 651 | } |
652 | 652 | } |
653 | 653 | |
654 | - if ( $this->movetolast( $tag ) ) { |
|
654 | + if ($this->movetolast($tag)) { |
|
655 | 655 | return false; |
656 | 656 | } |
657 | 657 | |
658 | - foreach ( $this->dontmove as $match ) { |
|
659 | - if ( false !== strpos( $tag, $match ) ) { |
|
658 | + foreach ($this->dontmove as $match) { |
|
659 | + if (false !== strpos($tag, $match)) { |
|
660 | 660 | // Matched something. |
661 | 661 | return false; |
662 | 662 | } |
@@ -672,25 +672,25 @@ discard block |
||
672 | 672 | * |
673 | 673 | * @param string $tag tag to check for blocklist (exclusions). |
674 | 674 | */ |
675 | - private function ismovable( $tag ) |
|
675 | + private function ismovable($tag) |
|
676 | 676 | { |
677 | - if ( empty( $tag ) || true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { |
|
677 | + if (empty($tag) || true !== $this->include_inline || apply_filters('autoptimize_filter_js_unmovable', true)) { |
|
678 | 678 | return false; |
679 | 679 | } |
680 | 680 | |
681 | - foreach ( $this->domove as $match ) { |
|
682 | - if ( false !== strpos( $tag, $match ) ) { |
|
681 | + foreach ($this->domove as $match) { |
|
682 | + if (false !== strpos($tag, $match)) { |
|
683 | 683 | // Matched something. |
684 | 684 | return true; |
685 | 685 | } |
686 | 686 | } |
687 | 687 | |
688 | - if ( $this->movetolast( $tag ) ) { |
|
688 | + if ($this->movetolast($tag)) { |
|
689 | 689 | return true; |
690 | 690 | } |
691 | 691 | |
692 | - foreach ( $this->dontmove as $match ) { |
|
693 | - if ( false !== strpos( $tag, $match ) ) { |
|
692 | + foreach ($this->dontmove as $match) { |
|
693 | + if (false !== strpos($tag, $match)) { |
|
694 | 694 | // Matched something. |
695 | 695 | return false; |
696 | 696 | } |
@@ -700,14 +700,14 @@ discard block |
||
700 | 700 | return true; |
701 | 701 | } |
702 | 702 | |
703 | - private function movetolast( $tag ) |
|
703 | + private function movetolast($tag) |
|
704 | 704 | { |
705 | - if ( empty( $tag ) ) { |
|
705 | + if (empty($tag)) { |
|
706 | 706 | return false; |
707 | 707 | } |
708 | 708 | |
709 | - foreach ( $this->domovelast as $match ) { |
|
710 | - if ( false !== strpos( $tag, $match ) ) { |
|
709 | + foreach ($this->domovelast as $match) { |
|
710 | + if (false !== strpos($tag, $match)) { |
|
711 | 711 | // Matched, return true. |
712 | 712 | return true; |
713 | 713 | } |
@@ -727,12 +727,12 @@ discard block |
||
727 | 727 | * @param string $js_path Path to JS file. |
728 | 728 | * @return bool |
729 | 729 | */ |
730 | - private function can_inject_late( $js_path ) { |
|
731 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
732 | - if ( true !== $this->inject_min_late ) { |
|
730 | + private function can_inject_late($js_path) { |
|
731 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
732 | + if (true !== $this->inject_min_late) { |
|
733 | 733 | // late-inject turned off. |
734 | 734 | return false; |
735 | - } elseif ( ( false === strpos( $js_path, 'min.js' ) ) && ( false === strpos( $js_path, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $js_path ) === $js_path ) ) { |
|
735 | + } elseif ((false === strpos($js_path, 'min.js')) && (false === strpos($js_path, 'wp-includes/js/jquery/jquery.js')) && (str_replace($consider_minified_array, '', $js_path) === $js_path)) { |
|
736 | 736 | // file not minified based on filename & filter. |
737 | 737 | return false; |
738 | 738 | } else { |
@@ -759,35 +759,35 @@ discard block |
||
759 | 759 | * |
760 | 760 | * @return bool|string Url pointing to the minified js file or false. |
761 | 761 | */ |
762 | - public function minify_single( $filepath, $cache_miss = false ) |
|
762 | + public function minify_single($filepath, $cache_miss = false) |
|
763 | 763 | { |
764 | - $contents = $this->prepare_minify_single( $filepath ); |
|
764 | + $contents = $this->prepare_minify_single($filepath); |
|
765 | 765 | |
766 | - if ( empty( $contents ) ) { |
|
766 | + if (empty($contents)) { |
|
767 | 767 | return false; |
768 | 768 | } |
769 | 769 | |
770 | 770 | // Check cache. |
771 | - $hash = 'single_' . md5( $contents ); |
|
772 | - $cache = new autoptimizeCache( $hash, 'js' ); |
|
771 | + $hash = 'single_'.md5($contents); |
|
772 | + $cache = new autoptimizeCache($hash, 'js'); |
|
773 | 773 | |
774 | 774 | // If not in cache already, minify... |
775 | - if ( ! $cache->check() || $cache_miss ) { |
|
776 | - $contents = trim( JSMin::minify( $contents ) ); |
|
775 | + if (!$cache->check() || $cache_miss) { |
|
776 | + $contents = trim(JSMin::minify($contents)); |
|
777 | 777 | |
778 | 778 | // Check if minified cache content is empty. |
779 | - if ( empty( $contents ) ) { |
|
779 | + if (empty($contents)) { |
|
780 | 780 | return false; |
781 | 781 | } |
782 | 782 | |
783 | 783 | // Filter contents of excluded minified CSS. |
784 | - $contents = apply_filters( 'autoptimize_filter_js_single_after_minify', $contents ); |
|
784 | + $contents = apply_filters('autoptimize_filter_js_single_after_minify', $contents); |
|
785 | 785 | |
786 | 786 | // Store in cache. |
787 | - $cache->cache( $contents, 'text/javascript' ); |
|
787 | + $cache->cache($contents, 'text/javascript'); |
|
788 | 788 | } |
789 | 789 | |
790 | - $url = $this->build_minify_single_url( $cache ); |
|
790 | + $url = $this->build_minify_single_url($cache); |
|
791 | 791 | |
792 | 792 | return $url; |
793 | 793 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles autoptimizeExtra frontend features + admin options page |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -28,9 +28,9 @@ discard block |
||
28 | 28 | * |
29 | 29 | * @param array $options Optional. Allows overriding options without having to specify them via admin options page. |
30 | 30 | */ |
31 | - public function __construct( $options = array() ) |
|
31 | + public function __construct($options = array()) |
|
32 | 32 | { |
33 | - if ( empty( $options ) ) { |
|
33 | + if (empty($options)) { |
|
34 | 34 | $options = self::fetch_options(); |
35 | 35 | } |
36 | 36 | |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public static function instance() |
49 | 49 | { |
50 | - if ( null === self::$instance ) { |
|
50 | + if (null === self::$instance) { |
|
51 | 51 | self::$instance = new self(); |
52 | 52 | } |
53 | 53 | |
@@ -56,19 +56,19 @@ discard block |
||
56 | 56 | |
57 | 57 | public function run() |
58 | 58 | { |
59 | - if ( is_admin() ) { |
|
60 | - if ( is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network() ) { |
|
61 | - add_action( 'network_admin_menu', array( $this, 'admin_menu' ) ); |
|
59 | + if (is_admin()) { |
|
60 | + if (is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network()) { |
|
61 | + add_action('network_admin_menu', array($this, 'admin_menu')); |
|
62 | 62 | } else { |
63 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
63 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
64 | 64 | } |
65 | - add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) ); |
|
65 | + add_filter('autoptimize_filter_settingsscreen_tabs', array($this, 'add_extra_tab')); |
|
66 | 66 | } else { |
67 | - add_action( 'wp', array( $this, 'run_on_frontend' ) ); |
|
67 | + add_action('wp', array($this, 'run_on_frontend')); |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
71 | - public function set_options( array $options ) |
|
71 | + public function set_options(array $options) |
|
72 | 72 | { |
73 | 73 | $this->options = $options; |
74 | 74 | |
@@ -77,8 +77,8 @@ discard block |
||
77 | 77 | |
78 | 78 | public static function fetch_options() |
79 | 79 | { |
80 | - $value = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings' ); |
|
81 | - if ( empty( $value ) ) { |
|
80 | + $value = autoptimizeOptionWrapper::get_option('autoptimize_extra_settings'); |
|
81 | + if (empty($value)) { |
|
82 | 82 | // Fallback to returning defaults when no stored option exists yet. |
83 | 83 | $value = autoptimizeConfig::get_ao_extra_default_options(); |
84 | 84 | } |
@@ -89,55 +89,55 @@ discard block |
||
89 | 89 | public function disable_emojis() |
90 | 90 | { |
91 | 91 | // Removing all actions related to emojis! |
92 | - remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
|
93 | - remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
|
94 | - remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
|
95 | - remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
|
96 | - remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
|
97 | - remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
|
98 | - remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
|
92 | + remove_action('admin_print_styles', 'print_emoji_styles'); |
|
93 | + remove_action('wp_head', 'print_emoji_detection_script', 7); |
|
94 | + remove_action('admin_print_scripts', 'print_emoji_detection_script'); |
|
95 | + remove_action('wp_print_styles', 'print_emoji_styles'); |
|
96 | + remove_filter('wp_mail', 'wp_staticize_emoji_for_email'); |
|
97 | + remove_filter('the_content_feed', 'wp_staticize_emoji'); |
|
98 | + remove_filter('comment_text_rss', 'wp_staticize_emoji'); |
|
99 | 99 | |
100 | 100 | // Removes TinyMCE emojis. |
101 | - add_filter( 'tiny_mce_plugins', array( $this, 'filter_disable_emojis_tinymce' ) ); |
|
101 | + add_filter('tiny_mce_plugins', array($this, 'filter_disable_emojis_tinymce')); |
|
102 | 102 | |
103 | 103 | // Removes emoji dns-preftech. |
104 | - add_filter( 'wp_resource_hints', array( $this, 'filter_remove_emoji_dns_prefetch' ), 10, 2 ); |
|
104 | + add_filter('wp_resource_hints', array($this, 'filter_remove_emoji_dns_prefetch'), 10, 2); |
|
105 | 105 | } |
106 | 106 | |
107 | - public function filter_disable_emojis_tinymce( $plugins ) |
|
107 | + public function filter_disable_emojis_tinymce($plugins) |
|
108 | 108 | { |
109 | - if ( is_array( $plugins ) ) { |
|
110 | - return array_diff( $plugins, array( 'wpemoji' ) ); |
|
109 | + if (is_array($plugins)) { |
|
110 | + return array_diff($plugins, array('wpemoji')); |
|
111 | 111 | } else { |
112 | 112 | return array(); |
113 | 113 | } |
114 | 114 | } |
115 | 115 | |
116 | - public function filter_remove_qs( $src ) |
|
116 | + public function filter_remove_qs($src) |
|
117 | 117 | { |
118 | - if ( strpos( $src, '?ver=' ) ) { |
|
119 | - $src = remove_query_arg( 'ver', $src ); |
|
118 | + if (strpos($src, '?ver=')) { |
|
119 | + $src = remove_query_arg('ver', $src); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | return $src; |
123 | 123 | } |
124 | 124 | |
125 | - public function extra_async_js( $in ) |
|
125 | + public function extra_async_js($in) |
|
126 | 126 | { |
127 | 127 | $exclusions = array(); |
128 | - if ( ! empty( $in ) ) { |
|
129 | - $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' ); |
|
128 | + if (!empty($in)) { |
|
129 | + $exclusions = array_fill_keys(array_filter(array_map('trim', explode(',', $in))), ''); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | $settings = $this->options['autoptimize_extra_text_field_3']; |
133 | - $async = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' ); |
|
134 | - $attr = apply_filters( 'autoptimize_filter_extra_async', 'async' ); |
|
135 | - foreach ( $async as $k => $v ) { |
|
136 | - $async[ $k ] = $attr; |
|
133 | + $async = array_fill_keys(array_filter(array_map('trim', explode(',', $settings))), ''); |
|
134 | + $attr = apply_filters('autoptimize_filter_extra_async', 'async'); |
|
135 | + foreach ($async as $k => $v) { |
|
136 | + $async[$k] = $attr; |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | // Merge exclusions & asyncs in one array and return to AO API. |
140 | - $merged = array_merge( $exclusions, $async ); |
|
140 | + $merged = array_merge($exclusions, $async); |
|
141 | 141 | |
142 | 142 | return $merged; |
143 | 143 | } |
@@ -148,68 +148,68 @@ discard block |
||
148 | 148 | // for optimizations are met, this to ensure e.g. removing querystrings |
149 | 149 | // is not done when optimizing for logged in users is off, breaking |
150 | 150 | // some pagebuilders (Divi & Elementor). |
151 | - if ( false === autoptimizeMain::should_buffer() ) { |
|
151 | + if (false === autoptimizeMain::should_buffer()) { |
|
152 | 152 | return; |
153 | 153 | } |
154 | 154 | |
155 | 155 | $options = $this->options; |
156 | 156 | |
157 | 157 | // Disable emojis if specified. |
158 | - if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) { |
|
158 | + if (!empty($options['autoptimize_extra_checkbox_field_1'])) { |
|
159 | 159 | $this->disable_emojis(); |
160 | 160 | } |
161 | 161 | |
162 | 162 | // Remove version query parameters. |
163 | - if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) ) { |
|
164 | - add_filter( 'script_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 ); |
|
165 | - add_filter( 'style_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 ); |
|
163 | + if (!empty($options['autoptimize_extra_checkbox_field_0'])) { |
|
164 | + add_filter('script_loader_src', array($this, 'filter_remove_qs'), 15, 1); |
|
165 | + add_filter('style_loader_src', array($this, 'filter_remove_qs'), 15, 1); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | // Avoiding conflicts of interest when async-javascript plugin is active! |
169 | - $async_js_plugin_active = autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ); |
|
170 | - if ( ! empty( $options['autoptimize_extra_text_field_3'] ) && ! $async_js_plugin_active ) { |
|
171 | - add_filter( 'autoptimize_filter_js_exclude', array( $this, 'extra_async_js' ), 10, 1 ); |
|
169 | + $async_js_plugin_active = autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php'); |
|
170 | + if (!empty($options['autoptimize_extra_text_field_3']) && !$async_js_plugin_active) { |
|
171 | + add_filter('autoptimize_filter_js_exclude', array($this, 'extra_async_js'), 10, 1); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | // Optimize google fonts! |
175 | - if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) { |
|
176 | - add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 ); |
|
177 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 ); |
|
178 | - add_filter( 'autoptimize_extra_filter_tobepreconn', array( $this, 'filter_preconnect_google_fonts' ), 10, 1 ); |
|
175 | + if (!empty($options['autoptimize_extra_radio_field_4']) && ('1' !== $options['autoptimize_extra_radio_field_4'])) { |
|
176 | + add_filter('wp_resource_hints', array($this, 'filter_remove_gfonts_dnsprefetch'), 10, 2); |
|
177 | + add_filter('autoptimize_html_after_minify', array($this, 'filter_optimize_google_fonts'), 10, 1); |
|
178 | + add_filter('autoptimize_extra_filter_tobepreconn', array($this, 'filter_preconnect_google_fonts'), 10, 1); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | // Preconnect! |
182 | - if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) { |
|
183 | - add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 ); |
|
182 | + if (!empty($options['autoptimize_extra_text_field_2']) || has_filter('autoptimize_extra_filter_tobepreconn')) { |
|
183 | + add_filter('wp_resource_hints', array($this, 'filter_preconnect'), 10, 2); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // Preload! |
187 | - if ( ! empty( $options['autoptimize_extra_text_field_7'] ) || has_filter( 'autoptimize_filter_extra_tobepreloaded' ) ) { |
|
188 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 ); |
|
187 | + if (!empty($options['autoptimize_extra_text_field_7']) || has_filter('autoptimize_filter_extra_tobepreloaded')) { |
|
188 | + add_filter('autoptimize_html_after_minify', array($this, 'filter_preload'), 10, 2); |
|
189 | 189 | } |
190 | 190 | } |
191 | 191 | |
192 | - public function filter_remove_emoji_dns_prefetch( $urls, $relation_type ) |
|
192 | + public function filter_remove_emoji_dns_prefetch($urls, $relation_type) |
|
193 | 193 | { |
194 | - $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/' ); |
|
194 | + $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/'); |
|
195 | 195 | |
196 | - return $this->filter_remove_dns_prefetch( $urls, $relation_type, $emoji_svg_url ); |
|
196 | + return $this->filter_remove_dns_prefetch($urls, $relation_type, $emoji_svg_url); |
|
197 | 197 | } |
198 | 198 | |
199 | - public function filter_remove_gfonts_dnsprefetch( $urls, $relation_type ) |
|
199 | + public function filter_remove_gfonts_dnsprefetch($urls, $relation_type) |
|
200 | 200 | { |
201 | - return $this->filter_remove_dns_prefetch( $urls, $relation_type, 'fonts.googleapis.com' ); |
|
201 | + return $this->filter_remove_dns_prefetch($urls, $relation_type, 'fonts.googleapis.com'); |
|
202 | 202 | } |
203 | 203 | |
204 | - public function filter_remove_dns_prefetch( $urls, $relation_type, $url_to_remove ) |
|
204 | + public function filter_remove_dns_prefetch($urls, $relation_type, $url_to_remove) |
|
205 | 205 | { |
206 | 206 | $url_to_remove = (string) $url_to_remove; |
207 | 207 | |
208 | - if ( ! empty( $url_to_remove ) && 'dns-prefetch' === $relation_type ) { |
|
208 | + if (!empty($url_to_remove) && 'dns-prefetch' === $relation_type) { |
|
209 | 209 | $cnt = 0; |
210 | - foreach ( $urls as $url ) { |
|
211 | - if ( false !== strpos( $url, $url_to_remove ) ) { |
|
212 | - unset( $urls[ $cnt ] ); |
|
210 | + foreach ($urls as $url) { |
|
211 | + if (false !== strpos($url, $url_to_remove)) { |
|
212 | + unset($urls[$cnt]); |
|
213 | 213 | } |
214 | 214 | $cnt++; |
215 | 215 | } |
@@ -218,153 +218,153 @@ discard block |
||
218 | 218 | return $urls; |
219 | 219 | } |
220 | 220 | |
221 | - public function filter_optimize_google_fonts( $in ) |
|
221 | + public function filter_optimize_google_fonts($in) |
|
222 | 222 | { |
223 | 223 | // Extract fonts, partly based on wp rocket's extraction code. |
224 | - $markup = preg_replace( '/<!--(.*)-->/Uis', '', $in ); |
|
225 | - preg_match_all( '#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches ); |
|
224 | + $markup = preg_replace('/<!--(.*)-->/Uis', '', $in); |
|
225 | + preg_match_all('#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches); |
|
226 | 226 | |
227 | 227 | $fonts_collection = array(); |
228 | - if ( ! $matches[2] ) { |
|
228 | + if (!$matches[2]) { |
|
229 | 229 | return $in; |
230 | 230 | } |
231 | 231 | |
232 | 232 | // Store them in $fonts array. |
233 | 233 | $i = 0; |
234 | - foreach ( $matches[2] as $font ) { |
|
235 | - if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) { |
|
234 | + foreach ($matches[2] as $font) { |
|
235 | + if (!preg_match('/rel=["\']dns-prefetch["\']/', $matches[0][$i])) { |
|
236 | 236 | // Get fonts name. |
237 | - $font = str_replace( array( '%7C', '%7c' ), '|', $font ); |
|
238 | - if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) { |
|
237 | + $font = str_replace(array('%7C', '%7c'), '|', $font); |
|
238 | + if (strpos($font, 'fonts.googleapis.com/css2') !== false) { |
|
239 | 239 | // (Somewhat) change Google Fonts APIv2 syntax back to v1. |
240 | 240 | // todo: support for 100..900 |
241 | - $font = rawurldecode( $font ); |
|
242 | - $font = str_replace( array( 'css2?', 'ital,wght@', 'wght@', 'ital@', '0,', '1,', ':1', ';', '&family=' ), array( 'css?', '', '', '', '', 'italic', ':italic', ',', '%7C' ), $font ); |
|
241 | + $font = rawurldecode($font); |
|
242 | + $font = str_replace(array('css2?', 'ital,wght@', 'wght@', 'ital@', '0,', '1,', ':1', ';', '&family='), array('css?', '', '', '', '', 'italic', ':italic', ',', '%7C'), $font); |
|
243 | 243 | } |
244 | - $font = explode( 'family=', $font ); |
|
245 | - $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array(); |
|
244 | + $font = explode('family=', $font); |
|
245 | + $font = (isset($font[1])) ? explode('&', $font[1]) : array(); |
|
246 | 246 | // Add font to $fonts[$i] but make sure not to pollute with an empty family! |
247 | - $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) ); |
|
248 | - if ( ! empty( $_thisfont ) ) { |
|
249 | - $fonts_collection[ $i ]['fonts'] = $_thisfont; |
|
247 | + $_thisfont = array_values(array_filter(explode('|', reset($font)))); |
|
248 | + if (!empty($_thisfont)) { |
|
249 | + $fonts_collection[$i]['fonts'] = $_thisfont; |
|
250 | 250 | // And add subset if any! |
251 | - $subset = ( is_array( $font ) ) ? end( $font ) : ''; |
|
252 | - if ( false !== strpos( $subset, 'subset=' ) ) { |
|
253 | - $subset = str_replace( array( '%2C', '%2c' ), ',', $subset ); |
|
254 | - $subset = explode( 'subset=', $subset ); |
|
255 | - $fonts_collection[ $i ]['subsets'] = explode( ',', $subset[1] ); |
|
251 | + $subset = (is_array($font)) ? end($font) : ''; |
|
252 | + if (false !== strpos($subset, 'subset=')) { |
|
253 | + $subset = str_replace(array('%2C', '%2c'), ',', $subset); |
|
254 | + $subset = explode('subset=', $subset); |
|
255 | + $fonts_collection[$i]['subsets'] = explode(',', $subset[1]); |
|
256 | 256 | } |
257 | 257 | } |
258 | 258 | // And remove Google Fonts. |
259 | - $in = str_replace( $matches[0][ $i ], '', $in ); |
|
259 | + $in = str_replace($matches[0][$i], '', $in); |
|
260 | 260 | } |
261 | 261 | $i++; |
262 | 262 | } |
263 | 263 | |
264 | 264 | $options = $this->options; |
265 | 265 | $fonts_markup = ''; |
266 | - if ( '2' === $options['autoptimize_extra_radio_field_4'] ) { |
|
266 | + if ('2' === $options['autoptimize_extra_radio_field_4']) { |
|
267 | 267 | // Remove Google Fonts. |
268 | - unset( $fonts_collection ); |
|
268 | + unset($fonts_collection); |
|
269 | 269 | return $in; |
270 | - } elseif ( '3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4'] ) { |
|
270 | + } elseif ('3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4']) { |
|
271 | 271 | // Aggregate & link! |
272 | 272 | $fonts_string = ''; |
273 | 273 | $subset_string = ''; |
274 | - foreach ( $fonts_collection as $font ) { |
|
275 | - $fonts_string .= '|' . trim( implode( '|', $font['fonts'] ), '|' ); |
|
276 | - if ( ! empty( $font['subsets'] ) ) { |
|
277 | - $subset_string .= ',' . trim( implode( ',', $font['subsets'] ), ',' ); |
|
274 | + foreach ($fonts_collection as $font) { |
|
275 | + $fonts_string .= '|'.trim(implode('|', $font['fonts']), '|'); |
|
276 | + if (!empty($font['subsets'])) { |
|
277 | + $subset_string .= ','.trim(implode(',', $font['subsets']), ','); |
|
278 | 278 | } |
279 | 279 | } |
280 | 280 | |
281 | - if ( ! empty( $subset_string ) ) { |
|
282 | - $subset_string = str_replace( ',', '%2C', ltrim( $subset_string, ',' ) ); |
|
283 | - $fonts_string = $fonts_string . '&subset=' . $subset_string; |
|
281 | + if (!empty($subset_string)) { |
|
282 | + $subset_string = str_replace(',', '%2C', ltrim($subset_string, ',')); |
|
283 | + $fonts_string = $fonts_string.'&subset='.$subset_string; |
|
284 | 284 | } |
285 | 285 | |
286 | - $fonts_string = apply_filters( 'autoptimize_filter_extra_gfont_fontstring', str_replace( '|', '%7C', ltrim( $fonts_string, '|' ) ) ); |
|
286 | + $fonts_string = apply_filters('autoptimize_filter_extra_gfont_fontstring', str_replace('|', '%7C', ltrim($fonts_string, '|'))); |
|
287 | 287 | // only add display parameter if there is none in $fonts_string (by virtue of the filter). |
288 | - if ( strpos( $fonts_string, 'display=' ) === false ) { |
|
289 | - $fonts_string .= apply_filters( 'autoptimize_filter_extra_gfont_display', '&display=swap' ); |
|
288 | + if (strpos($fonts_string, 'display=') === false) { |
|
289 | + $fonts_string .= apply_filters('autoptimize_filter_extra_gfont_display', '&display=swap'); |
|
290 | 290 | } |
291 | 291 | |
292 | - if ( ! empty( $fonts_string ) ) { |
|
293 | - if ( '5' === $options['autoptimize_extra_radio_field_4'] ) { |
|
294 | - $rel_string = 'rel="stylesheet" media="print" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"'; |
|
292 | + if (!empty($fonts_string)) { |
|
293 | + if ('5' === $options['autoptimize_extra_radio_field_4']) { |
|
294 | + $rel_string = 'rel="stylesheet" media="print" onload="'.autoptimizeConfig::get_ao_css_preload_onload().'"'; |
|
295 | 295 | } else { |
296 | 296 | $rel_string = 'rel="stylesheet"'; |
297 | 297 | } |
298 | - $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />'; |
|
298 | + $fonts_markup = '<link '.$rel_string.' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family='.$fonts_string.'" />'; |
|
299 | 299 | } |
300 | - } elseif ( '4' === $options['autoptimize_extra_radio_field_4'] ) { |
|
300 | + } elseif ('4' === $options['autoptimize_extra_radio_field_4']) { |
|
301 | 301 | // Aggregate & load async (webfont.js impl.)! |
302 | 302 | $fonts_array = array(); |
303 | - foreach ( $fonts_collection as $_fonts ) { |
|
304 | - if ( ! empty( $_fonts['subsets'] ) ) { |
|
305 | - $_subset = implode( ',', $_fonts['subsets'] ); |
|
306 | - foreach ( $_fonts['fonts'] as $key => $_one_font ) { |
|
307 | - $_one_font = $_one_font . ':' . $_subset; |
|
308 | - $_fonts['fonts'][ $key ] = $_one_font; |
|
303 | + foreach ($fonts_collection as $_fonts) { |
|
304 | + if (!empty($_fonts['subsets'])) { |
|
305 | + $_subset = implode(',', $_fonts['subsets']); |
|
306 | + foreach ($_fonts['fonts'] as $key => $_one_font) { |
|
307 | + $_one_font = $_one_font.':'.$_subset; |
|
308 | + $_fonts['fonts'][$key] = $_one_font; |
|
309 | 309 | } |
310 | 310 | } |
311 | - $fonts_array = array_merge( $fonts_array, $_fonts['fonts'] ); |
|
311 | + $fonts_array = array_merge($fonts_array, $_fonts['fonts']); |
|
312 | 312 | } |
313 | 313 | |
314 | - $fonts_array = array_map( 'urldecode', $fonts_array ); |
|
314 | + $fonts_array = array_map('urldecode', $fonts_array); |
|
315 | 315 | $fonts_array = array_map( |
316 | - function( $_f ) { |
|
317 | - return trim( $_f, ',' ); |
|
316 | + function($_f) { |
|
317 | + return trim($_f, ','); |
|
318 | 318 | }, |
319 | 319 | $fonts_array |
320 | 320 | ); |
321 | 321 | |
322 | 322 | // type attrib on <script not added by default. |
323 | 323 | $type_js = ''; |
324 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
324 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
325 | 325 | $type_js = 'type="text/javascript" '; |
326 | 326 | } |
327 | 327 | |
328 | - $fonts_markup = '<script ' . $type_js . 'data-cfasync="false" id="ao_optimized_gfonts_config">WebFontConfig={google:{families:' . wp_json_encode( $fonts_array ) . ' },classes:false, events:false, timeout:1500};</script>'; |
|
329 | - $fonts_library_markup = '<script ' . $type_js . 'data-cfasync="false" id="ao_optimized_gfonts_webfontloader">(function() {var wf = document.createElement(\'script\');wf.src=\'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js\';wf.type=\'text/javascript\';wf.async=\'true\';var s=document.getElementsByTagName(\'script\')[0];s.parentNode.insertBefore(wf, s);})();</script>'; |
|
330 | - $in = substr_replace( $in, $fonts_library_markup . '</head>', strpos( $in, '</head>' ), strlen( '</head>' ) ); |
|
328 | + $fonts_markup = '<script '.$type_js.'data-cfasync="false" id="ao_optimized_gfonts_config">WebFontConfig={google:{families:'.wp_json_encode($fonts_array).' },classes:false, events:false, timeout:1500};</script>'; |
|
329 | + $fonts_library_markup = '<script '.$type_js.'data-cfasync="false" id="ao_optimized_gfonts_webfontloader">(function() {var wf = document.createElement(\'script\');wf.src=\'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js\';wf.type=\'text/javascript\';wf.async=\'true\';var s=document.getElementsByTagName(\'script\')[0];s.parentNode.insertBefore(wf, s);})();</script>'; |
|
330 | + $in = substr_replace($in, $fonts_library_markup.'</head>', strpos($in, '</head>'), strlen('</head>')); |
|
331 | 331 | } |
332 | 332 | |
333 | 333 | // Replace back in markup. |
334 | - $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' ); |
|
335 | - $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) ); |
|
336 | - unset( $fonts_collection ); |
|
334 | + $inject_point = apply_filters('autoptimize_filter_extra_gfont_injectpoint', '<link'); |
|
335 | + $out = substr_replace($in, $fonts_markup.$inject_point, strpos($in, $inject_point), strlen($inject_point)); |
|
336 | + unset($fonts_collection); |
|
337 | 337 | |
338 | 338 | return $out; |
339 | 339 | } |
340 | 340 | |
341 | - public function filter_preconnect( $hints, $relation_type ) |
|
341 | + public function filter_preconnect($hints, $relation_type) |
|
342 | 342 | { |
343 | 343 | $options = $this->options; |
344 | 344 | $preconns = array(); |
345 | 345 | |
346 | 346 | // Get settings and store in array. |
347 | - if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { |
|
348 | - $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) ); |
|
347 | + if (array_key_exists('autoptimize_extra_text_field_2', $options)) { |
|
348 | + $preconns = array_filter(array_map('trim', explode(',', $options['autoptimize_extra_text_field_2']))); |
|
349 | 349 | } |
350 | - $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns ); |
|
350 | + $preconns = apply_filters('autoptimize_extra_filter_tobepreconn', $preconns); |
|
351 | 351 | |
352 | 352 | // Walk array, extract domain and add to new array with crossorigin attribute. |
353 | - foreach ( $preconns as $preconn ) { |
|
353 | + foreach ($preconns as $preconn) { |
|
354 | 354 | $domain = ''; |
355 | - $parsed = parse_url( $preconn ); |
|
356 | - if ( is_array( $parsed ) && ! empty( $parsed['host'] ) && empty( $parsed['scheme'] ) ) { |
|
357 | - $domain = '//' . $parsed['host']; |
|
358 | - } elseif ( is_array( $parsed ) && ! empty( $parsed['host'] ) ) { |
|
359 | - $domain = $parsed['scheme'] . '://' . $parsed['host']; |
|
355 | + $parsed = parse_url($preconn); |
|
356 | + if (is_array($parsed) && !empty($parsed['host']) && empty($parsed['scheme'])) { |
|
357 | + $domain = '//'.$parsed['host']; |
|
358 | + } elseif (is_array($parsed) && !empty($parsed['host'])) { |
|
359 | + $domain = $parsed['scheme'].'://'.$parsed['host']; |
|
360 | 360 | } |
361 | 361 | |
362 | - if ( ! empty( $domain ) ) { |
|
363 | - $hint = array( 'href' => $domain ); |
|
362 | + if (!empty($domain)) { |
|
363 | + $hint = array('href' => $domain); |
|
364 | 364 | // Fonts don't get preconnected unless crossorigin flag is set, non-fonts don't get preconnected if origin flag is set |
365 | 365 | // so hardcode fonts.gstatic.com to come with crossorigin and have filter to add other domains if needed. |
366 | - $crossorigins = apply_filters( 'autoptimize_extra_filter_preconn_crossorigin', array( 'https://fonts.gstatic.com' ) ); |
|
367 | - if ( in_array( $domain, $crossorigins ) ) { |
|
366 | + $crossorigins = apply_filters('autoptimize_extra_filter_preconn_crossorigin', array('https://fonts.gstatic.com')); |
|
367 | + if (in_array($domain, $crossorigins)) { |
|
368 | 368 | $hint['crossorigin'] = 'anonymous'; |
369 | 369 | } |
370 | 370 | $new_hints[] = $hint; |
@@ -372,21 +372,21 @@ discard block |
||
372 | 372 | } |
373 | 373 | |
374 | 374 | // Merge in WP's preconnect hints. |
375 | - if ( 'preconnect' === $relation_type && ! empty( $new_hints ) ) { |
|
376 | - $hints = array_merge( $hints, $new_hints ); |
|
375 | + if ('preconnect' === $relation_type && !empty($new_hints)) { |
|
376 | + $hints = array_merge($hints, $new_hints); |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | return $hints; |
380 | 380 | } |
381 | 381 | |
382 | - public function filter_preconnect_google_fonts( $in ) |
|
382 | + public function filter_preconnect_google_fonts($in) |
|
383 | 383 | { |
384 | - if ( '2' !== $this->options['autoptimize_extra_radio_field_4'] ) { |
|
384 | + if ('2' !== $this->options['autoptimize_extra_radio_field_4']) { |
|
385 | 385 | // Preconnect to fonts.gstatic.com unless we remove gfonts. |
386 | 386 | $in[] = 'https://fonts.gstatic.com'; |
387 | 387 | } |
388 | 388 | |
389 | - if ( '4' === $this->options['autoptimize_extra_radio_field_4'] ) { |
|
389 | + if ('4' === $this->options['autoptimize_extra_radio_field_4']) { |
|
390 | 390 | // Preconnect even more hosts for webfont.js! |
391 | 391 | $in[] = 'https://ajax.googleapis.com'; |
392 | 392 | $in[] = 'https://fonts.googleapis.com'; |
@@ -395,76 +395,76 @@ discard block |
||
395 | 395 | return $in; |
396 | 396 | } |
397 | 397 | |
398 | - public function filter_preload( $in ) { |
|
398 | + public function filter_preload($in) { |
|
399 | 399 | // make array from comma separated list. |
400 | 400 | $options = $this->options; |
401 | 401 | $preloads = array(); |
402 | - if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { |
|
403 | - $preloads = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_7'] ) ) ); |
|
402 | + if (array_key_exists('autoptimize_extra_text_field_7', $options)) { |
|
403 | + $preloads = array_filter(array_map('trim', explode(',', $options['autoptimize_extra_text_field_7']))); |
|
404 | 404 | } |
405 | - $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads ); |
|
405 | + $preloads = apply_filters('autoptimize_filter_extra_tobepreloaded', $preloads); |
|
406 | 406 | |
407 | 407 | // immediately return if nothing to be preloaded. |
408 | - if ( empty( $preloads ) ) { |
|
408 | + if (empty($preloads)) { |
|
409 | 409 | return $in; |
410 | 410 | } |
411 | 411 | |
412 | 412 | // iterate through array and add preload link to tmp string. |
413 | 413 | $preload_output = ''; |
414 | - foreach ( $preloads as $preload ) { |
|
414 | + foreach ($preloads as $preload) { |
|
415 | 415 | $crossorigin = ''; |
416 | 416 | $preload_as = ''; |
417 | 417 | $mime_type = ''; |
418 | - $_preload = strtok( $preload, '?' ); |
|
418 | + $_preload = strtok($preload, '?'); |
|
419 | 419 | |
420 | - if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) { |
|
420 | + if (autoptimizeUtils::str_ends_in($_preload, '.css')) { |
|
421 | 421 | $preload_as = 'style'; |
422 | - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) { |
|
422 | + } elseif (autoptimizeUtils::str_ends_in($_preload, '.js')) { |
|
423 | 423 | $preload_as = 'script'; |
424 | - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.woff' ) || autoptimizeUtils::str_ends_in( $_preload, '.woff2' ) || autoptimizeUtils::str_ends_in( $_preload, '.ttf' ) || autoptimizeUtils::str_ends_in( $_preload, '.eot' ) || autoptimizeUtils::str_ends_in( $_preload, '.otf' ) ) { |
|
424 | + } elseif (autoptimizeUtils::str_ends_in($_preload, '.woff') || autoptimizeUtils::str_ends_in($_preload, '.woff2') || autoptimizeUtils::str_ends_in($_preload, '.ttf') || autoptimizeUtils::str_ends_in($_preload, '.eot') || autoptimizeUtils::str_ends_in($_preload, '.otf')) { |
|
425 | 425 | $preload_as = 'font'; |
426 | 426 | $crossorigin = ' crossorigin'; |
427 | - $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"'; |
|
428 | - if ( ' type="font/eot"' === $mime_type ) { |
|
427 | + $mime_type = ' type="font/'.pathinfo($_preload, PATHINFO_EXTENSION).'"'; |
|
428 | + if (' type="font/eot"' === $mime_type) { |
|
429 | 429 | $mime_type = 'application/vnd.ms-fontobject'; |
430 | 430 | } |
431 | - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.jpeg' ) || autoptimizeUtils::str_ends_in( $_preload, '.jpg' ) || autoptimizeUtils::str_ends_in( $_preload, '.webp' ) || autoptimizeUtils::str_ends_in( $_preload, '.png' ) || autoptimizeUtils::str_ends_in( $_preload, '.gif' ) ) { |
|
431 | + } elseif (autoptimizeUtils::str_ends_in($_preload, '.jpeg') || autoptimizeUtils::str_ends_in($_preload, '.jpg') || autoptimizeUtils::str_ends_in($_preload, '.webp') || autoptimizeUtils::str_ends_in($_preload, '.png') || autoptimizeUtils::str_ends_in($_preload, '.gif')) { |
|
432 | 432 | $preload_as = 'image'; |
433 | 433 | } else { |
434 | 434 | $preload_as = 'other'; |
435 | 435 | } |
436 | 436 | |
437 | - $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>'; |
|
437 | + $preload_output .= '<link rel="preload" href="'.$preload.'" as="'.$preload_as.'"'.$mime_type.$crossorigin.'>'; |
|
438 | 438 | } |
439 | - $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output ); |
|
439 | + $preload_output = apply_filters('autoptimize_filter_extra_preload_output', $preload_output); |
|
440 | 440 | |
441 | 441 | // add string to head (before first link node by default). |
442 | - $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' ); |
|
443 | - $position = autoptimizeUtils::strpos( $in, $preload_inject ); |
|
442 | + $preload_inject = apply_filters('autoptimize_filter_extra_preload_inject', '<link'); |
|
443 | + $position = autoptimizeUtils::strpos($in, $preload_inject); |
|
444 | 444 | |
445 | - return autoptimizeUtils::substr_replace( $in, $preload_output . $preload_inject, $position, strlen( $preload_inject ) ); |
|
445 | + return autoptimizeUtils::substr_replace($in, $preload_output.$preload_inject, $position, strlen($preload_inject)); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | public function admin_menu() |
449 | 449 | { |
450 | 450 | // no acces if multisite and not network admin and no site config allowed. |
451 | - if ( autoptimizeConfig::should_show_menu_tabs() ) { |
|
451 | + if (autoptimizeConfig::should_show_menu_tabs()) { |
|
452 | 452 | add_submenu_page( |
453 | 453 | null, |
454 | 454 | 'autoptimize_extra', |
455 | 455 | 'autoptimize_extra', |
456 | 456 | 'manage_options', |
457 | 457 | 'autoptimize_extra', |
458 | - array( $this, 'options_page' ) |
|
458 | + array($this, 'options_page') |
|
459 | 459 | ); |
460 | 460 | } |
461 | - register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' ); |
|
461 | + register_setting('autoptimize_extra_settings', 'autoptimize_extra_settings'); |
|
462 | 462 | } |
463 | 463 | |
464 | - public function add_extra_tab( $in ) |
|
464 | + public function add_extra_tab($in) |
|
465 | 465 | { |
466 | - if ( autoptimizeConfig::should_show_menu_tabs() ) { |
|
467 | - $in = array_merge( $in, array( 'autoptimize_extra' => __( 'Extra', 'autoptimize' ) ) ); |
|
466 | + if (autoptimizeConfig::should_show_menu_tabs()) { |
|
467 | + $in = array_merge($in, array('autoptimize_extra' => __('Extra', 'autoptimize'))); |
|
468 | 468 | } |
469 | 469 | |
470 | 470 | return $in; |
@@ -484,97 +484,97 @@ discard block |
||
484 | 484 | #ao_settings_form .form-table th {font-weight: normal;} |
485 | 485 | #autoptimize_extra_descr{font-size: 120%;} |
486 | 486 | </style> |
487 | - <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script> |
|
487 | + <script>document.title = "Autoptimize: <?php _e('Extra', 'autoptimize'); ?> " + document.title;</script> |
|
488 | 488 | <div class="wrap"> |
489 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
|
489 | + <h1><?php _e('Autoptimize Settings', 'autoptimize'); ?></h1> |
|
490 | 490 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
491 | - <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> |
|
491 | + <?php if ('on' !== autoptimizeOptionWrapper::get_option('autoptimize_js') && 'on' !== autoptimizeOptionWrapper::get_option('autoptimize_css') && 'on' !== autoptimizeOptionWrapper::get_option('autoptimize_html') && !autoptimizeImages::imgopt_active()) { ?> |
|
492 | 492 | <div class="notice-warning notice"><p> |
493 | - <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?> |
|
493 | + <?php _e('Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize'); ?> |
|
494 | 494 | </p></div> |
495 | 495 | <?php } ?> |
496 | 496 | |
497 | - <form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'> |
|
498 | - <?php settings_fields( 'autoptimize_extra_settings' ); ?> |
|
499 | - <h2><?php _e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2> |
|
500 | - <span id='autoptimize_extra_descr'><?php _e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span> |
|
497 | + <form id='ao_settings_form' action='<?php echo admin_url('options.php'); ?>' method='post'> |
|
498 | + <?php settings_fields('autoptimize_extra_settings'); ?> |
|
499 | + <h2><?php _e('Extra Auto-Optimizations', 'autoptimize'); ?></h2> |
|
500 | + <span id='autoptimize_extra_descr'><?php _e('The following settings can improve your site\'s performance even more.', 'autoptimize'); ?></span> |
|
501 | 501 | <table class="form-table"> |
502 | 502 | <tr> |
503 | - <th scope="row"><?php _e( 'Google Fonts', 'autoptimize' ); ?></th> |
|
503 | + <th scope="row"><?php _e('Google Fonts', 'autoptimize'); ?></th> |
|
504 | 504 | <td> |
505 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="1" <?php if ( ! in_array( $gfonts, array( 2, 3, 4, 5 ) ) ) { echo 'checked'; } ?> ><?php _e( 'Leave as is', 'autoptimize' ); ?><br/> |
|
506 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked( 2, $gfonts, true ); ?> ><?php _e( 'Remove Google Fonts', 'autoptimize' ); ?><br/> |
|
505 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="1" <?php if (!in_array($gfonts, array(2, 3, 4, 5))) { echo 'checked'; } ?> ><?php _e('Leave as is', 'autoptimize'); ?><br/> |
|
506 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked(2, $gfonts, true); ?> ><?php _e('Remove Google Fonts', 'autoptimize'); ?><br/> |
|
507 | 507 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
508 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked( 3, $gfonts, true ); ?> ><?php echo __( 'Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize' ) . ', ' . sprintf( __( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/> |
|
508 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked(3, $gfonts, true); ?> ><?php echo __('Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize').', '.sprintf(__('includes %1$sdisplay:swap%2$s.', 'autoptimize'), '<code>', '</code>'); ?><br/> |
|
509 | 509 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
510 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php echo __( 'Combine and link deferred in head (fonts load late, but are not render-blocking)', 'autoptimize' ) . ', ' . sprintf( __( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/> |
|
511 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked( 4, $gfonts, true ); ?> ><?php echo __( 'Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize' ) . ' ' . __( '(deprecated)', 'autoptimize' ); ?><br/> |
|
510 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked(5, $gfonts, true); ?> ><?php echo __('Combine and link deferred in head (fonts load late, but are not render-blocking)', 'autoptimize').', '.sprintf(__('includes %1$sdisplay:swap%2$s.', 'autoptimize'), '<code>', '</code>'); ?><br/> |
|
511 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked(4, $gfonts, true); ?> ><?php echo __('Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize').' '.__('(deprecated)', 'autoptimize'); ?><br/> |
|
512 | 512 | </td> |
513 | 513 | </tr> |
514 | 514 | <tr> |
515 | - <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th> |
|
515 | + <th scope="row"><?php _e('Remove emojis', 'autoptimize'); ?></th> |
|
516 | 516 | <td> |
517 | - <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_1]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) && '1' === $options['autoptimize_extra_checkbox_field_1'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize' ); ?></label> |
|
517 | + <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_1]' <?php if (!empty($options['autoptimize_extra_checkbox_field_1']) && '1' === $options['autoptimize_extra_checkbox_field_1']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize'); ?></label> |
|
518 | 518 | </td> |
519 | 519 | </tr> |
520 | 520 | <tr> |
521 | - <th scope="row"><?php _e( 'Remove query strings from static resources', 'autoptimize' ); ?></th> |
|
521 | + <th scope="row"><?php _e('Remove query strings from static resources', 'autoptimize'); ?></th> |
|
522 | 522 | <td> |
523 | - <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_0]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) && '1' === $options['autoptimize_extra_checkbox_field_0'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Removing query strings (or more specifically the <code>ver</code> parameter) will not improve load time, but might improve performance scores.', 'autoptimize' ); ?></label> |
|
523 | + <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_0]' <?php if (!empty($options['autoptimize_extra_checkbox_field_0']) && '1' === $options['autoptimize_extra_checkbox_field_0']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Removing query strings (or more specifically the <code>ver</code> parameter) will not improve load time, but might improve performance scores.', 'autoptimize'); ?></label> |
|
524 | 524 | </td> |
525 | 525 | </tr> |
526 | 526 | <tr> |
527 | - <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
527 | + <th scope="row"><?php _e('Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
528 | 528 | <td> |
529 | - <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_2]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_2'] ); } ?>'><br /><?php _e( 'Add 3rd party domains you want the browser to <a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">preconnect</a> to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize' ); ?></label> |
|
529 | + <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_2]' value='<?php if (array_key_exists('autoptimize_extra_text_field_2', $options)) { echo esc_attr($options['autoptimize_extra_text_field_2']); } ?>'><br /><?php _e('Add 3rd party domains you want the browser to <a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">preconnect</a> to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize'); ?></label> |
|
530 | 530 | </td> |
531 | 531 | </tr> |
532 | 532 | <tr> |
533 | - <th scope="row"><?php _e( 'Preload specific requests <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
533 | + <th scope="row"><?php _e('Preload specific requests <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
534 | 534 | <td> |
535 | - <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_7]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_7'] ); } ?>'><br /><?php _e( 'Comma-separated list with full URL\'s of to to-be-preloaded resources. To be used sparingly!', 'autoptimize' ); ?></label> |
|
535 | + <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_7]' value='<?php if (array_key_exists('autoptimize_extra_text_field_7', $options)) { echo esc_attr($options['autoptimize_extra_text_field_7']); } ?>'><br /><?php _e('Comma-separated list with full URL\'s of to to-be-preloaded resources. To be used sparingly!', 'autoptimize'); ?></label> |
|
536 | 536 | </td> |
537 | 537 | </tr> |
538 | 538 | <tr> |
539 | - <th scope="row"><?php _e( 'Async Javascript-files <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
539 | + <th scope="row"><?php _e('Async Javascript-files <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
540 | 540 | <td> |
541 | 541 | <?php |
542 | - if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
|
542 | + if (autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php')) { |
|
543 | 543 | // translators: link points Async Javascript settings page. |
544 | - printf( __( 'You have "Async JavaScript" installed, %1$sconfiguration of async javascript is best done there%2$s.', 'autoptimize' ), '<a href="' . 'options-general.php?page=async-javascript' . '">', '</a>' ); |
|
544 | + printf(__('You have "Async JavaScript" installed, %1$sconfiguration of async javascript is best done there%2$s.', 'autoptimize'), '<a href="'.'options-general.php?page=async-javascript'.'">', '</a>'); |
|
545 | 545 | } else { |
546 | 546 | ?> |
547 | - <input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_3]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_3', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_3'] ); } ?>'> |
|
547 | + <input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_3]' value='<?php if (array_key_exists('autoptimize_extra_text_field_3', $options)) { echo esc_attr($options['autoptimize_extra_text_field_3']); } ?>'> |
|
548 | 548 | <br /> |
549 | 549 | <?php |
550 | - _e( 'Comma-separated list of local or 3rd party JS-files that should loaded with the <code>async</code> flag. JS-files from your own site will be automatically excluded if added here. ', 'autoptimize' ); |
|
550 | + _e('Comma-separated list of local or 3rd party JS-files that should loaded with the <code>async</code> flag. JS-files from your own site will be automatically excluded if added here. ', 'autoptimize'); |
|
551 | 551 | // translators: %s will be replaced by a link to the "async javascript" plugin. |
552 | - echo sprintf( __( 'Configuration of async javascript is easier and more flexible using the %s plugin.', 'autoptimize' ), '"<a href="https://wordpress.org/plugins/async-javascript" target="_blank">Async Javascript</a>"' ); |
|
553 | - $asj_install_url = network_admin_url() . 'plugin-install.php?s=async+javascript&tab=search&type=term'; |
|
554 | - echo sprintf( ' <a href="' . $asj_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) ); |
|
552 | + echo sprintf(__('Configuration of async javascript is easier and more flexible using the %s plugin.', 'autoptimize'), '"<a href="https://wordpress.org/plugins/async-javascript" target="_blank">Async Javascript</a>"'); |
|
553 | + $asj_install_url = network_admin_url().'plugin-install.php?s=async+javascript&tab=search&type=term'; |
|
554 | + echo sprintf(' <a href="'.$asj_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize')); |
|
555 | 555 | } |
556 | 556 | ?> |
557 | 557 | </td> |
558 | 558 | </tr> |
559 | 559 | <tr> |
560 | - <th scope="row"><?php _e( 'Optimize YouTube videos', 'autoptimize' ); ?></th> |
|
560 | + <th scope="row"><?php _e('Optimize YouTube videos', 'autoptimize'); ?></th> |
|
561 | 561 | <td> |
562 | 562 | <?php |
563 | - if ( autoptimizeUtils::is_plugin_active( 'wp-youtube-lyte/wp-youtube-lyte.php' ) ) { |
|
564 | - _e( 'Great, you have WP YouTube Lyte installed.', 'autoptimize' ); |
|
563 | + if (autoptimizeUtils::is_plugin_active('wp-youtube-lyte/wp-youtube-lyte.php')) { |
|
564 | + _e('Great, you have WP YouTube Lyte installed.', 'autoptimize'); |
|
565 | 565 | $lyte_config_url = 'options-general.php?page=lyte_settings_page'; |
566 | - echo sprintf( ' <a href="' . $lyte_config_url . '">%s</a>', __( 'Click here to configure it.', 'autoptimize' ) ); |
|
566 | + echo sprintf(' <a href="'.$lyte_config_url.'">%s</a>', __('Click here to configure it.', 'autoptimize')); |
|
567 | 567 | } else { |
568 | 568 | // translators: %s will be replaced by a link to "wp youtube lyte" plugin. |
569 | - echo sprintf( __( '%s allows you to “lazy load” your videos, by inserting responsive “Lite YouTube Embeds". ', 'autoptimize' ), '<a href="https://wordpress.org/plugins/wp-youtube-lyte" target="_blank">WP YouTube Lyte</a>' ); |
|
570 | - $lyte_install_url = network_admin_url() . 'plugin-install.php?s=lyte&tab=search&type=term'; |
|
571 | - echo sprintf( ' <a href="' . $lyte_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) ); |
|
569 | + echo sprintf(__('%s allows you to “lazy load” your videos, by inserting responsive “Lite YouTube Embeds". ', 'autoptimize'), '<a href="https://wordpress.org/plugins/wp-youtube-lyte" target="_blank">WP YouTube Lyte</a>'); |
|
570 | + $lyte_install_url = network_admin_url().'plugin-install.php?s=lyte&tab=search&type=term'; |
|
571 | + echo sprintf(' <a href="'.$lyte_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize')); |
|
572 | 572 | } |
573 | 573 | ?> |
574 | 574 | </td> |
575 | 575 | </tr> |
576 | 576 | </table> |
577 | - <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /></p> |
|
577 | + <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'autoptimize'); ?>" /></p> |
|
578 | 578 | </form> |
579 | 579 | <?php |
580 | 580 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles version updates and should only be instantiated in autoptimize.php if/when needed. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -16,9 +16,9 @@ discard block |
||
16 | 16 | */ |
17 | 17 | protected $current_major_version = null; |
18 | 18 | |
19 | - public function __construct( $current_version ) |
|
19 | + public function __construct($current_version) |
|
20 | 20 | { |
21 | - $this->current_major_version = substr( $current_version, 0, 3 ); |
|
21 | + $this->current_major_version = substr($current_version, 0, 3); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | { |
30 | 30 | $major_update = false; |
31 | 31 | |
32 | - switch ( $this->current_major_version ) { |
|
32 | + switch ($this->current_major_version) { |
|
33 | 33 | case '1.6': |
34 | 34 | $this->upgrade_from_1_6(); |
35 | 35 | $major_update = true; |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | $major_update = true; |
48 | 48 | // No break, intentionally, so all upgrades are ran during a single request... |
49 | 49 | case '2.4': |
50 | - if ( autoptimizeOptionWrapper::get_option( 'autoptimize_version', 'none' ) == '2.4.2' ) { |
|
50 | + if (autoptimizeOptionWrapper::get_option('autoptimize_version', 'none') == '2.4.2') { |
|
51 | 51 | $this->upgrade_from_2_4_2(); |
52 | 52 | } |
53 | 53 | $this->upgrade_from_2_4(); |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | // No break, intentionally, so all upgrades are ran during a single request... |
60 | 60 | } |
61 | 61 | |
62 | - if ( true === $major_update ) { |
|
62 | + if (true === $major_update) { |
|
63 | 63 | $this->on_major_version_update(); |
64 | 64 | } |
65 | 65 | } |
@@ -71,19 +71,19 @@ discard block |
||
71 | 71 | * |
72 | 72 | * @param string $target Target version to check against (ie., the currently running one). |
73 | 73 | */ |
74 | - public static function check_installed_and_update( $target ) |
|
74 | + public static function check_installed_and_update($target) |
|
75 | 75 | { |
76 | - $db_version = autoptimizeOptionWrapper::get_option( 'autoptimize_version', 'none' ); |
|
77 | - if ( $db_version !== $target ) { |
|
78 | - if ( 'none' === $db_version ) { |
|
79 | - add_action( 'admin_notices', 'autoptimizeMain::notice_installed' ); |
|
76 | + $db_version = autoptimizeOptionWrapper::get_option('autoptimize_version', 'none'); |
|
77 | + if ($db_version !== $target) { |
|
78 | + if ('none' === $db_version) { |
|
79 | + add_action('admin_notices', 'autoptimizeMain::notice_installed'); |
|
80 | 80 | } else { |
81 | - $updater = new self( $db_version ); |
|
81 | + $updater = new self($db_version); |
|
82 | 82 | $updater->run_needed_major_upgrades(); |
83 | 83 | } |
84 | 84 | |
85 | 85 | // Versions differed, upgrades happened if needed, store the new version. |
86 | - autoptimizeOptionWrapper::update_option( 'autoptimize_version', $target ); |
|
86 | + autoptimizeOptionWrapper::update_option('autoptimize_version', $target); |
|
87 | 87 | } |
88 | 88 | } |
89 | 89 | |
@@ -94,10 +94,10 @@ discard block |
||
94 | 94 | protected function on_major_version_update() |
95 | 95 | { |
96 | 96 | // The transients guard here prevents stale object caches from busting the cache on every request. |
97 | - if ( false == get_transient( 'autoptimize_stale_option_buster' ) ) { |
|
98 | - set_transient( 'autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS ); |
|
97 | + if (false == get_transient('autoptimize_stale_option_buster')) { |
|
98 | + set_transient('autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS); |
|
99 | 99 | autoptimizeCache::clearall(); |
100 | - add_action( 'admin_notices', 'autoptimizeMain::notice_updated' ); |
|
100 | + add_action('admin_notices', 'autoptimizeMain::notice_updated'); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | private function upgrade_from_1_6() |
108 | 108 | { |
109 | 109 | // If user was on version 1.6.x, force advanced options to be shown by default. |
110 | - autoptimizeOptionWrapper::update_option( 'autoptimize_show_adv', '1' ); |
|
110 | + autoptimizeOptionWrapper::update_option('autoptimize_show_adv', '1'); |
|
111 | 111 | |
112 | 112 | // And remove old options. |
113 | 113 | $to_delete_options = array( |
@@ -119,8 +119,8 @@ discard block |
||
119 | 119 | 'autoptimize_cdn_img_url', |
120 | 120 | 'autoptimize_css_yui', |
121 | 121 | ); |
122 | - foreach ( $to_delete_options as $del_opt ) { |
|
123 | - delete_option( $del_opt ); |
|
122 | + foreach ($to_delete_options as $del_opt) { |
|
123 | + delete_option($del_opt); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
@@ -131,29 +131,29 @@ discard block |
||
131 | 131 | */ |
132 | 132 | private function upgrade_from_1_7() |
133 | 133 | { |
134 | - if ( ! is_multisite() ) { |
|
135 | - $css_exclude = autoptimizeOptionWrapper::get_option( 'autoptimize_css_exclude' ); |
|
136 | - if ( empty( $css_exclude ) ) { |
|
134 | + if (!is_multisite()) { |
|
135 | + $css_exclude = autoptimizeOptionWrapper::get_option('autoptimize_css_exclude'); |
|
136 | + if (empty($css_exclude)) { |
|
137 | 137 | $css_exclude = 'admin-bar.min.css, dashicons.min.css'; |
138 | - } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) { |
|
138 | + } elseif (false === strpos($css_exclude, 'dashicons.min.css')) { |
|
139 | 139 | $css_exclude .= ', dashicons.min.css'; |
140 | 140 | } |
141 | - autoptimizeOptionWrapper::update_option( 'autoptimize_css_exclude', $css_exclude ); |
|
141 | + autoptimizeOptionWrapper::update_option('autoptimize_css_exclude', $css_exclude); |
|
142 | 142 | } else { |
143 | 143 | global $wpdb; |
144 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
144 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
145 | 145 | $original_blog_id = get_current_blog_id(); |
146 | - foreach ( $blog_ids as $blog_id ) { |
|
147 | - switch_to_blog( $blog_id ); |
|
148 | - $css_exclude = autoptimizeOptionWrapper::get_option( 'autoptimize_css_exclude' ); |
|
149 | - if ( empty( $css_exclude ) ) { |
|
146 | + foreach ($blog_ids as $blog_id) { |
|
147 | + switch_to_blog($blog_id); |
|
148 | + $css_exclude = autoptimizeOptionWrapper::get_option('autoptimize_css_exclude'); |
|
149 | + if (empty($css_exclude)) { |
|
150 | 150 | $css_exclude = 'admin-bar.min.css, dashicons.min.css'; |
151 | - } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) { |
|
151 | + } elseif (false === strpos($css_exclude, 'dashicons.min.css')) { |
|
152 | 152 | $css_exclude .= ', dashicons.min.css'; |
153 | 153 | } |
154 | - autoptimizeOptionWrapper::update_option( 'autoptimize_css_exclude', $css_exclude ); |
|
154 | + autoptimizeOptionWrapper::update_option('autoptimize_css_exclude', $css_exclude); |
|
155 | 155 | } |
156 | - switch_to_blog( $original_blog_id ); |
|
156 | + switch_to_blog($original_blog_id); |
|
157 | 157 | } |
158 | 158 | } |
159 | 159 | |
@@ -165,19 +165,19 @@ discard block |
||
165 | 165 | */ |
166 | 166 | private function upgrade_from_1_9() |
167 | 167 | { |
168 | - if ( ! is_multisite() ) { |
|
169 | - autoptimizeOptionWrapper::update_option( 'autoptimize_css_include_inline', 'on' ); |
|
170 | - autoptimizeOptionWrapper::update_option( 'autoptimize_js_include_inline', 'on' ); |
|
168 | + if (!is_multisite()) { |
|
169 | + autoptimizeOptionWrapper::update_option('autoptimize_css_include_inline', 'on'); |
|
170 | + autoptimizeOptionWrapper::update_option('autoptimize_js_include_inline', 'on'); |
|
171 | 171 | } else { |
172 | 172 | global $wpdb; |
173 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
173 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
174 | 174 | $original_blog_id = get_current_blog_id(); |
175 | - foreach ( $blog_ids as $blog_id ) { |
|
176 | - switch_to_blog( $blog_id ); |
|
177 | - autoptimizeOptionWrapper::update_option( 'autoptimize_css_include_inline', 'on' ); |
|
178 | - autoptimizeOptionWrapper::update_option( 'autoptimize_js_include_inline', 'on' ); |
|
175 | + foreach ($blog_ids as $blog_id) { |
|
176 | + switch_to_blog($blog_id); |
|
177 | + autoptimizeOptionWrapper::update_option('autoptimize_css_include_inline', 'on'); |
|
178 | + autoptimizeOptionWrapper::update_option('autoptimize_js_include_inline', 'on'); |
|
179 | 179 | } |
180 | - switch_to_blog( $original_blog_id ); |
|
180 | + switch_to_blog($original_blog_id); |
|
181 | 181 | } |
182 | 182 | } |
183 | 183 | |
@@ -188,17 +188,17 @@ discard block |
||
188 | 188 | */ |
189 | 189 | private function upgrade_from_2_2() |
190 | 190 | { |
191 | - if ( ! is_multisite() ) { |
|
191 | + if (!is_multisite()) { |
|
192 | 192 | $this->do_2_2_settings_update(); |
193 | 193 | } else { |
194 | 194 | global $wpdb; |
195 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
195 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
196 | 196 | $original_blog_id = get_current_blog_id(); |
197 | - foreach ( $blog_ids as $blog_id ) { |
|
198 | - switch_to_blog( $blog_id ); |
|
197 | + foreach ($blog_ids as $blog_id) { |
|
198 | + switch_to_blog($blog_id); |
|
199 | 199 | $this->do_2_2_settings_update(); |
200 | 200 | } |
201 | - switch_to_blog( $original_blog_id ); |
|
201 | + switch_to_blog($original_blog_id); |
|
202 | 202 | } |
203 | 203 | } |
204 | 204 | |
@@ -207,12 +207,12 @@ discard block |
||
207 | 207 | */ |
208 | 208 | private function do_2_2_settings_update() |
209 | 209 | { |
210 | - $nogooglefont = autoptimizeOptionWrapper::get_option( 'autoptimize_css_nogooglefont', '' ); |
|
211 | - $ao_extrasetting = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings', '' ); |
|
212 | - if ( ( $nogooglefont ) && ( empty( $ao_extrasetting ) ) ) { |
|
213 | - autoptimizeOptionWrapper::update_option( 'autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options() ); |
|
210 | + $nogooglefont = autoptimizeOptionWrapper::get_option('autoptimize_css_nogooglefont', ''); |
|
211 | + $ao_extrasetting = autoptimizeOptionWrapper::get_option('autoptimize_extra_settings', ''); |
|
212 | + if (($nogooglefont) && (empty($ao_extrasetting))) { |
|
213 | + autoptimizeOptionWrapper::update_option('autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options()); |
|
214 | 214 | } |
215 | - delete_option( 'autoptimize_css_nogooglefont' ); |
|
215 | + delete_option('autoptimize_css_nogooglefont'); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -224,33 +224,33 @@ discard block |
||
224 | 224 | $jobs = _get_cron_array(); |
225 | 225 | |
226 | 226 | // Remove all ao_cachechecker cron jobs (for now). |
227 | - foreach ( $jobs as $when => $job ) { |
|
228 | - $name = key( $job ); |
|
227 | + foreach ($jobs as $when => $job) { |
|
228 | + $name = key($job); |
|
229 | 229 | |
230 | - if ( false !== strpos( $name, 'ao_cachechecker' ) ) { |
|
231 | - unset( $jobs[ $when ] ); |
|
230 | + if (false !== strpos($name, 'ao_cachechecker')) { |
|
231 | + unset($jobs[$when]); |
|
232 | 232 | } |
233 | 233 | } |
234 | 234 | |
235 | 235 | // Save the data. |
236 | - _set_cron_array( $jobs ); |
|
236 | + _set_cron_array($jobs); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
240 | 240 | * Migrate imgopt options from autoptimize_extra_settings to autoptimize_imgopt_settings |
241 | 241 | */ |
242 | 242 | private function upgrade_from_2_4() { |
243 | - $extra_settings = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings', '' ); |
|
244 | - $imgopt_settings = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_settings', '' ); |
|
245 | - if ( empty( $imgopt_settings ) && ! empty( $extra_settings ) ) { |
|
243 | + $extra_settings = autoptimizeOptionWrapper::get_option('autoptimize_extra_settings', ''); |
|
244 | + $imgopt_settings = autoptimizeOptionWrapper::get_option('autoptimize_imgopt_settings', ''); |
|
245 | + if (empty($imgopt_settings) && !empty($extra_settings)) { |
|
246 | 246 | $imgopt_settings = autoptimizeConfig::get_ao_imgopt_default_options(); |
247 | - if ( array_key_exists( 'autoptimize_extra_checkbox_field_5', $extra_settings ) ) { |
|
247 | + if (array_key_exists('autoptimize_extra_checkbox_field_5', $extra_settings)) { |
|
248 | 248 | $imgopt_settings['autoptimize_imgopt_checkbox_field_1'] = $extra_settings['autoptimize_extra_checkbox_field_5']; |
249 | 249 | } |
250 | - if ( array_key_exists( 'autoptimize_extra_select_field_6', $extra_settings ) ) { |
|
250 | + if (array_key_exists('autoptimize_extra_select_field_6', $extra_settings)) { |
|
251 | 251 | $imgopt_settings['autoptimize_imgopt_select_field_2'] = $extra_settings['autoptimize_extra_select_field_6']; |
252 | 252 | } |
253 | - autoptimizeOptionWrapper::update_option( 'autoptimize_imgopt_settings', $imgopt_settings ); |
|
253 | + autoptimizeOptionWrapper::update_option('autoptimize_imgopt_settings', $imgopt_settings); |
|
254 | 254 | } |
255 | 255 | } |
256 | 256 | |
@@ -258,11 +258,11 @@ discard block |
||
258 | 258 | * remove CCSS request limit option + update jquery exclusion to include WordPress 5.6 jquery.min.js. |
259 | 259 | */ |
260 | 260 | private function upgrade_from_2_7() { |
261 | - delete_option( 'autoptimize_ccss_rlimit' ); |
|
262 | - $js_exclusions = get_option( 'autoptimize_js_exclude', '' ); |
|
263 | - if ( strpos( $js_exclusions, 'js/jquery/jquery.js' ) !== false && strpos( $js_exclusions, 'js/jquery/jquery.min.js' ) === false ) { |
|
261 | + delete_option('autoptimize_ccss_rlimit'); |
|
262 | + $js_exclusions = get_option('autoptimize_js_exclude', ''); |
|
263 | + if (strpos($js_exclusions, 'js/jquery/jquery.js') !== false && strpos($js_exclusions, 'js/jquery/jquery.min.js') === false) { |
|
264 | 264 | $js_exclusions .= ', js/jquery/jquery.min.js'; |
265 | - update_option( 'autoptimize_js_exclude', $js_exclusions ); |
|
265 | + update_option('autoptimize_js_exclude', $js_exclusions); |
|
266 | 266 | } |
267 | 267 | } |
268 | 268 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Critical CSS base file (initializes all ccss files). |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -19,35 +19,35 @@ discard block |
||
19 | 19 | public function __construct() |
20 | 20 | { |
21 | 21 | // define constant, but only once. |
22 | - if ( ! defined( 'AO_CCSS_DIR' ) ) { |
|
22 | + if (!defined('AO_CCSS_DIR')) { |
|
23 | 23 | // Define a constant with the directory to store critical CSS in. |
24 | - if ( is_multisite() ) { |
|
24 | + if (is_multisite()) { |
|
25 | 25 | $blog_id = get_current_blog_id(); |
26 | - define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' ); |
|
26 | + define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'.$blog_id.'/'); |
|
27 | 27 | } else { |
28 | - define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' ); |
|
28 | + define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'); |
|
29 | 29 | } |
30 | 30 | } |
31 | - if ( ! defined( 'AO_CCSS_VER' ) ) { |
|
31 | + if (!defined('AO_CCSS_VER')) { |
|
32 | 32 | // Define plugin version. |
33 | - define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
33 | + define('AO_CCSS_VER', 'AO_'.AUTOPTIMIZE_PLUGIN_VERSION); |
|
34 | 34 | |
35 | 35 | // Define constants for criticalcss.com base path and API endpoints. |
36 | 36 | // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option. |
37 | - define( 'AO_CCSS_URL', 'https://criticalcss.com' ); |
|
38 | - define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' ); |
|
39 | - define( 'AO_CCSS_SLEEP', 10 ); |
|
37 | + define('AO_CCSS_URL', 'https://criticalcss.com'); |
|
38 | + define('AO_CCSS_API', AO_CCSS_URL.'/api/premium/'); |
|
39 | + define('AO_CCSS_SLEEP', 10); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | // Define support files locations, in case they are not already defined. |
43 | - if ( ! defined( 'AO_CCSS_LOCK' ) ) { |
|
44 | - define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' ); |
|
43 | + if (!defined('AO_CCSS_LOCK')) { |
|
44 | + define('AO_CCSS_LOCK', AO_CCSS_DIR.'queue.lock'); |
|
45 | 45 | } |
46 | - if ( ! defined( 'AO_CCSS_LOG' ) ) { |
|
47 | - define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' ); |
|
46 | + if (!defined('AO_CCSS_LOG')) { |
|
47 | + define('AO_CCSS_LOG', AO_CCSS_DIR.'queuelog.html'); |
|
48 | 48 | } |
49 | - if ( ! defined( 'AO_CCSS_DEBUG' ) ) { |
|
50 | - define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' ); |
|
49 | + if (!defined('AO_CCSS_DEBUG')) { |
|
50 | + define('AO_CCSS_DEBUG', AO_CCSS_DIR.'queue.json'); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | $this->filepath = __FILE__; |
@@ -60,19 +60,19 @@ discard block |
||
60 | 60 | { |
61 | 61 | // get all options. |
62 | 62 | $all_options = $this->fetch_options(); |
63 | - foreach ( $all_options as $option => $value ) { |
|
63 | + foreach ($all_options as $option => $value) { |
|
64 | 64 | ${$option} = $value; |
65 | 65 | } |
66 | 66 | |
67 | 67 | // make sure the 10 minutes cron schedule is added. |
68 | - add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) ); |
|
68 | + add_filter('cron_schedules', array($this, 'ao_ccss_interval')); |
|
69 | 69 | |
70 | 70 | // check if we need to upgrade. |
71 | 71 | $this->check_upgrade(); |
72 | 72 | |
73 | 73 | // make sure ao_ccss_queue is scheduled OK if an API key is set. |
74 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
75 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
74 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) { |
|
75 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
76 | 76 | } |
77 | 77 | } |
78 | 78 | |
@@ -80,12 +80,12 @@ discard block |
||
80 | 80 | // Required libs, core is always needed. |
81 | 81 | $criticalcss_core = new autoptimizeCriticalCSSCore(); |
82 | 82 | |
83 | - if ( defined( 'WP_CLI' ) || defined( 'DOING_CRON' ) || is_admin() ) { |
|
83 | + if (defined('WP_CLI') || defined('DOING_CRON') || is_admin()) { |
|
84 | 84 | // TODO: also include if overridden somehow to force queue processing to be executed? |
85 | 85 | $criticalcss_cron = new autoptimizeCriticalCSSCron(); |
86 | 86 | } |
87 | 87 | |
88 | - if ( is_admin() ) { |
|
88 | + if (is_admin()) { |
|
89 | 89 | $criticalcss_settings = new autoptimizeCriticalCSSSettings(); |
90 | 90 | } else { |
91 | 91 | // enqueuing only done when not wp-admin. |
@@ -96,51 +96,51 @@ discard block |
||
96 | 96 | public static function fetch_options() { |
97 | 97 | static $autoptimize_ccss_options = null; |
98 | 98 | |
99 | - if ( null === $autoptimize_ccss_options ) { |
|
99 | + if (null === $autoptimize_ccss_options) { |
|
100 | 100 | // not cached yet, fetching from WordPress options. |
101 | - $autoptimize_ccss_options['ao_css_defer'] = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' ); |
|
102 | - $autoptimize_ccss_options['ao_css_defer_inline'] = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer_inline' ); |
|
103 | - $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option( 'autoptimize_ccss_rules', false ); |
|
104 | - $autoptimize_ccss_options['ao_ccss_additional'] = get_option( 'autoptimize_ccss_additional' ); |
|
105 | - $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option( 'autoptimize_ccss_queue', false ); |
|
106 | - $autoptimize_ccss_options['ao_ccss_viewport'] = get_option( 'autoptimize_ccss_viewport', false ); |
|
107 | - $autoptimize_ccss_options['ao_ccss_finclude'] = get_option( 'autoptimize_ccss_finclude', false ); |
|
108 | - $autoptimize_ccss_options['ao_ccss_rtimelimit'] = get_option( 'autoptimize_ccss_rtimelimit', '30' ); |
|
109 | - $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option( 'autoptimize_ccss_noptimize', false ); |
|
110 | - $autoptimize_ccss_options['ao_ccss_debug'] = get_option( 'autoptimize_ccss_debug', false ); |
|
111 | - $autoptimize_ccss_options['ao_ccss_key'] = get_option( 'autoptimize_ccss_key' ); |
|
112 | - $autoptimize_ccss_options['ao_ccss_keyst'] = get_option( 'autoptimize_ccss_keyst' ); |
|
113 | - $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option( 'autoptimize_ccss_loggedin', '1' ); |
|
114 | - $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option( 'autoptimize_ccss_forcepath', '1' ); |
|
115 | - $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' ); |
|
116 | - $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option( 'autoptimize_ccss_deferjquery', false ); |
|
117 | - $autoptimize_ccss_options['ao_ccss_domain'] = get_option( 'autoptimize_ccss_domain' ); |
|
118 | - $autoptimize_ccss_options['ao_ccss_unloadccss'] = get_option( 'autoptimize_ccss_unloadccss', false ); |
|
119 | - |
|
120 | - if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) { |
|
121 | - $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ); |
|
122 | - } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) { |
|
101 | + $autoptimize_ccss_options['ao_css_defer'] = autoptimizeOptionWrapper::get_option('autoptimize_css_defer'); |
|
102 | + $autoptimize_ccss_options['ao_css_defer_inline'] = autoptimizeOptionWrapper::get_option('autoptimize_css_defer_inline'); |
|
103 | + $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option('autoptimize_ccss_rules', false); |
|
104 | + $autoptimize_ccss_options['ao_ccss_additional'] = get_option('autoptimize_ccss_additional'); |
|
105 | + $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option('autoptimize_ccss_queue', false); |
|
106 | + $autoptimize_ccss_options['ao_ccss_viewport'] = get_option('autoptimize_ccss_viewport', false); |
|
107 | + $autoptimize_ccss_options['ao_ccss_finclude'] = get_option('autoptimize_ccss_finclude', false); |
|
108 | + $autoptimize_ccss_options['ao_ccss_rtimelimit'] = get_option('autoptimize_ccss_rtimelimit', '30'); |
|
109 | + $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option('autoptimize_ccss_noptimize', false); |
|
110 | + $autoptimize_ccss_options['ao_ccss_debug'] = get_option('autoptimize_ccss_debug', false); |
|
111 | + $autoptimize_ccss_options['ao_ccss_key'] = get_option('autoptimize_ccss_key'); |
|
112 | + $autoptimize_ccss_options['ao_ccss_keyst'] = get_option('autoptimize_ccss_keyst'); |
|
113 | + $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option('autoptimize_ccss_loggedin', '1'); |
|
114 | + $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option('autoptimize_ccss_forcepath', '1'); |
|
115 | + $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option('autoptimize_service_availablity'); |
|
116 | + $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option('autoptimize_ccss_deferjquery', false); |
|
117 | + $autoptimize_ccss_options['ao_ccss_domain'] = get_option('autoptimize_ccss_domain'); |
|
118 | + $autoptimize_ccss_options['ao_ccss_unloadccss'] = get_option('autoptimize_ccss_unloadccss', false); |
|
119 | + |
|
120 | + if (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') === false && strpos($autoptimize_ccss_options['ao_ccss_domain'], 'uggc') === 0) { |
|
121 | + $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13($autoptimize_ccss_options['ao_ccss_domain']); |
|
122 | + } elseif (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') !== false) { |
|
123 | 123 | // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain). |
124 | - update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) ); |
|
124 | + update_option('autoptimize_ccss_domain', str_rot13($autoptimize_ccss_options['ao_ccss_domain'])); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | // Setup the rules array. |
128 | - if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) { |
|
128 | + if (empty($autoptimize_ccss_options['ao_ccss_rules_raw'])) { |
|
129 | 129 | $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array(); |
130 | 130 | $autoptimize_ccss_options['ao_ccss_rules']['types'] = array(); |
131 | 131 | } else { |
132 | - $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true ); |
|
132 | + $autoptimize_ccss_options['ao_ccss_rules'] = json_decode($autoptimize_ccss_options['ao_ccss_rules_raw'], true); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | // Setup the queue array. |
136 | - if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) { |
|
136 | + if (empty($autoptimize_ccss_options['ao_ccss_queue_raw'])) { |
|
137 | 137 | $autoptimize_ccss_options['ao_ccss_queue'] = array(); |
138 | 138 | } else { |
139 | - $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true ); |
|
139 | + $autoptimize_ccss_options['ao_ccss_queue'] = json_decode($autoptimize_ccss_options['ao_ccss_queue_raw'], true); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | // Override API key if constant is defined. |
143 | - if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) { |
|
143 | + if (defined('AUTOPTIMIZE_CRITICALCSS_API_KEY')) { |
|
144 | 144 | $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY; |
145 | 145 | } |
146 | 146 | } |
@@ -152,49 +152,49 @@ discard block |
||
152 | 152 | global $ao_ccss_key; |
153 | 153 | |
154 | 154 | // Create the cache directory if it doesn't exist already. |
155 | - if ( ! file_exists( AO_CCSS_DIR ) ) { |
|
156 | - mkdir( AO_CCSS_DIR, 0755, true ); |
|
155 | + if (!file_exists(AO_CCSS_DIR)) { |
|
156 | + mkdir(AO_CCSS_DIR, 0755, true); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | // Create a scheduled event for the queue. |
160 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
161 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
160 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) { |
|
161 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | // Create a scheduled event for log maintenance. |
165 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) { |
|
166 | - wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' ); |
|
165 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_maintenance')) { |
|
166 | + wp_schedule_event(time(), 'twicedaily', 'ao_ccss_maintenance'); |
|
167 | 167 | } |
168 | 168 | } |
169 | 169 | |
170 | 170 | public function check_upgrade() { |
171 | - $db_version = get_option( 'autoptimize_ccss_version', '' ); |
|
172 | - if ( AO_CCSS_VER !== $db_version ) { |
|
171 | + $db_version = get_option('autoptimize_ccss_version', ''); |
|
172 | + if (AO_CCSS_VER !== $db_version) { |
|
173 | 173 | // check schedules & re-schedule if needed. |
174 | 174 | $this->on_upgrade(); |
175 | 175 | // and update db_version. |
176 | - update_option( 'autoptimize_ccss_version', AO_CCSS_VER ); |
|
176 | + update_option('autoptimize_ccss_version', AO_CCSS_VER); |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | |
180 | - public function ao_ccss_interval( $schedules ) { |
|
180 | + public function ao_ccss_interval($schedules) { |
|
181 | 181 | // Let interval be configurable. |
182 | - if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) { |
|
182 | + if (!defined('AO_CCSS_DEBUG_INTERVAL')) { |
|
183 | 183 | $intsec = 600; |
184 | 184 | } else { |
185 | 185 | $intsec = AO_CCSS_DEBUG_INTERVAL; |
186 | - if ( $intsec >= 120 ) { |
|
187 | - $inttxt = $intsec / 60 . ' minutes'; |
|
186 | + if ($intsec >= 120) { |
|
187 | + $inttxt = $intsec/60.' minutes'; |
|
188 | 188 | } else { |
189 | - $inttxt = $intsec . ' second(s)'; |
|
189 | + $inttxt = $intsec.' second(s)'; |
|
190 | 190 | } |
191 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 ); |
|
191 | + autoptimizeCriticalCSSCore::ao_ccss_log('Using custom WP-Cron interval of '.$inttxt, 3); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | // Attach interval to schedule. |
195 | 195 | $schedules['ao_ccss'] = array( |
196 | 196 | 'interval' => $intsec, |
197 | - 'display' => __( 'Autoptimize CriticalCSS' ), |
|
197 | + 'display' => __('Autoptimize CriticalCSS'), |
|
198 | 198 | ); |
199 | 199 | return $schedules; |
200 | 200 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Critical CSS settings AJAX logic. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | { |
13 | 13 | // fetch all options at once and populate them individually explicitely as globals. |
14 | 14 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
15 | - foreach ( $all_options as $_option => $_value ) { |
|
15 | + foreach ($all_options as $_option => $_value) { |
|
16 | 16 | global ${$_option}; |
17 | 17 | ${$_option} = $_value; |
18 | 18 | } |
@@ -21,47 +21,47 @@ discard block |
||
21 | 21 | |
22 | 22 | public function run() { |
23 | 23 | // add filters. |
24 | - add_action( 'wp_ajax_fetch_critcss', array( $this, 'critcss_fetch_callback' ) ); |
|
25 | - add_action( 'wp_ajax_save_critcss', array( $this, 'critcss_save_callback' ) ); |
|
26 | - add_action( 'wp_ajax_rm_critcss', array( $this, 'critcss_rm_callback' ) ); |
|
27 | - add_action( 'wp_ajax_rm_critcss_all', array( $this, 'critcss_rm_all_callback' ) ); |
|
28 | - add_action( 'wp_ajax_ao_ccss_export', array( $this, 'ao_ccss_export_callback' ) ); |
|
29 | - add_action( 'wp_ajax_ao_ccss_import', array( $this, 'ao_ccss_import_callback' ) ); |
|
24 | + add_action('wp_ajax_fetch_critcss', array($this, 'critcss_fetch_callback')); |
|
25 | + add_action('wp_ajax_save_critcss', array($this, 'critcss_save_callback')); |
|
26 | + add_action('wp_ajax_rm_critcss', array($this, 'critcss_rm_callback')); |
|
27 | + add_action('wp_ajax_rm_critcss_all', array($this, 'critcss_rm_all_callback')); |
|
28 | + add_action('wp_ajax_ao_ccss_export', array($this, 'ao_ccss_export_callback')); |
|
29 | + add_action('wp_ajax_ao_ccss_import', array($this, 'ao_ccss_import_callback')); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | public function critcss_fetch_callback() { |
33 | 33 | // Ajax handler to obtain a critical CSS file from the filesystem. |
34 | 34 | // Check referer. |
35 | - check_ajax_referer( 'fetch_critcss_nonce', 'critcss_fetch_nonce' ); |
|
35 | + check_ajax_referer('fetch_critcss_nonce', 'critcss_fetch_nonce'); |
|
36 | 36 | |
37 | 37 | // Initialize error flag. |
38 | 38 | $error = true; |
39 | 39 | |
40 | 40 | // Allow no content for MANUAL rules (as they may not exist just yet). |
41 | - if ( current_user_can( 'manage_options' ) && empty( $_POST['critcssfile'] ) ) { |
|
41 | + if (current_user_can('manage_options') && empty($_POST['critcssfile'])) { |
|
42 | 42 | $content = ''; |
43 | 43 | $error = false; |
44 | - } elseif ( current_user_can( 'manage_options' ) && $this->critcss_check_filename( $_POST['critcssfile'] ) ) { |
|
44 | + } elseif (current_user_can('manage_options') && $this->critcss_check_filename($_POST['critcssfile'])) { |
|
45 | 45 | // Or check user permissios and filename. |
46 | 46 | // Set file path and obtain its content. |
47 | - $critcssfile = AO_CCSS_DIR . strip_tags( $_POST['critcssfile'] ); |
|
48 | - if ( file_exists( $critcssfile ) ) { |
|
49 | - $content = file_get_contents( $critcssfile ); |
|
47 | + $critcssfile = AO_CCSS_DIR.strip_tags($_POST['critcssfile']); |
|
48 | + if (file_exists($critcssfile)) { |
|
49 | + $content = file_get_contents($critcssfile); |
|
50 | 50 | $error = false; |
51 | 51 | } |
52 | 52 | } |
53 | 53 | |
54 | 54 | // Prepare response. |
55 | - if ( $error ) { |
|
55 | + if ($error) { |
|
56 | 56 | $response['code'] = '500'; |
57 | - $response['string'] = 'Error reading file ' . $critcssfile . '.'; |
|
57 | + $response['string'] = 'Error reading file '.$critcssfile.'.'; |
|
58 | 58 | } else { |
59 | 59 | $response['code'] = '200'; |
60 | 60 | $response['string'] = $content; |
61 | 61 | } |
62 | 62 | |
63 | 63 | // Dispatch respose. |
64 | - echo json_encode( $response ); |
|
64 | + echo json_encode($response); |
|
65 | 65 | |
66 | 66 | // Close ajax request. |
67 | 67 | wp_die(); |
@@ -74,22 +74,22 @@ discard block |
||
74 | 74 | |
75 | 75 | // Ajax handler to write a critical CSS to the filesystem |
76 | 76 | // Check referer. |
77 | - check_ajax_referer( 'save_critcss_nonce', 'critcss_save_nonce' ); |
|
77 | + check_ajax_referer('save_critcss_nonce', 'critcss_save_nonce'); |
|
78 | 78 | |
79 | 79 | // Allow empty contents for MANUAL rules (as they are fetched later). |
80 | - if ( current_user_can( 'manage_options' ) && empty( $_POST['critcssfile'] ) ) { |
|
80 | + if (current_user_can('manage_options') && empty($_POST['critcssfile'])) { |
|
81 | 81 | $critcssfile = false; |
82 | 82 | $status = true; |
83 | - } elseif ( current_user_can( 'manage_options' ) && $this->critcss_check_filename( $_POST['critcssfile'] ) ) { |
|
83 | + } elseif (current_user_can('manage_options') && $this->critcss_check_filename($_POST['critcssfile'])) { |
|
84 | 84 | // Or check user permissios and filename |
85 | 85 | // Set critical CSS content. |
86 | - $critcsscontents = stripslashes( $_POST['critcsscontents'] ); |
|
86 | + $critcsscontents = stripslashes($_POST['critcsscontents']); |
|
87 | 87 | |
88 | 88 | // If there is content and it's valid, write the file. |
89 | - if ( $critcsscontents && autoptimizeCriticalCSSCore::ao_ccss_check_contents( $critcsscontents ) ) { |
|
89 | + if ($critcsscontents && autoptimizeCriticalCSSCore::ao_ccss_check_contents($critcsscontents)) { |
|
90 | 90 | // Set file path and status. |
91 | - $critcssfile = AO_CCSS_DIR . strip_tags( $_POST['critcssfile'] ); |
|
92 | - $status = file_put_contents( $critcssfile, $critcsscontents, LOCK_EX ); |
|
91 | + $critcssfile = AO_CCSS_DIR.strip_tags($_POST['critcssfile']); |
|
92 | + $status = file_put_contents($critcssfile, $critcsscontents, LOCK_EX); |
|
93 | 93 | // Or set as error. |
94 | 94 | } else { |
95 | 95 | $error = true; |
@@ -100,20 +100,20 @@ discard block |
||
100 | 100 | } |
101 | 101 | |
102 | 102 | // Prepare response. |
103 | - if ( ! $status || $error ) { |
|
103 | + if (!$status || $error) { |
|
104 | 104 | $response['code'] = '500'; |
105 | - $response['string'] = 'Error saving file ' . $critcssfile . '.'; |
|
105 | + $response['string'] = 'Error saving file '.$critcssfile.'.'; |
|
106 | 106 | } else { |
107 | 107 | $response['code'] = '200'; |
108 | - if ( $critcssfile ) { |
|
109 | - $response['string'] = 'File ' . $critcssfile . ' saved.'; |
|
108 | + if ($critcssfile) { |
|
109 | + $response['string'] = 'File '.$critcssfile.' saved.'; |
|
110 | 110 | } else { |
111 | 111 | $response['string'] = 'Empty content do not need to be saved.'; |
112 | 112 | } |
113 | 113 | } |
114 | 114 | |
115 | 115 | // Dispatch respose. |
116 | - echo json_encode( $response ); |
|
116 | + echo json_encode($response); |
|
117 | 117 | |
118 | 118 | // Close ajax request. |
119 | 119 | wp_die(); |
@@ -123,40 +123,40 @@ discard block |
||
123 | 123 | public function critcss_rm_callback() { |
124 | 124 | // Ajax handler to delete a critical CSS from the filesystem |
125 | 125 | // Check referer. |
126 | - check_ajax_referer( 'rm_critcss_nonce', 'critcss_rm_nonce' ); |
|
126 | + check_ajax_referer('rm_critcss_nonce', 'critcss_rm_nonce'); |
|
127 | 127 | |
128 | 128 | // Initialize error and status flags. |
129 | 129 | $error = true; |
130 | 130 | $status = false; |
131 | 131 | |
132 | 132 | // Allow no file for MANUAL rules (as they may not exist just yet). |
133 | - if ( current_user_can( 'manage_options' ) && empty( $_POST['critcssfile'] ) ) { |
|
133 | + if (current_user_can('manage_options') && empty($_POST['critcssfile'])) { |
|
134 | 134 | $error = false; |
135 | - } elseif ( current_user_can( 'manage_options' ) && $this->critcss_check_filename( $_POST['critcssfile'] ) ) { |
|
135 | + } elseif (current_user_can('manage_options') && $this->critcss_check_filename($_POST['critcssfile'])) { |
|
136 | 136 | // Or check user permissios and filename |
137 | 137 | // Set file path and delete it. |
138 | - $critcssfile = AO_CCSS_DIR . strip_tags( $_POST['critcssfile'] ); |
|
139 | - if ( file_exists( $critcssfile ) ) { |
|
140 | - $status = unlink( $critcssfile ); |
|
138 | + $critcssfile = AO_CCSS_DIR.strip_tags($_POST['critcssfile']); |
|
139 | + if (file_exists($critcssfile)) { |
|
140 | + $status = unlink($critcssfile); |
|
141 | 141 | $error = false; |
142 | 142 | } |
143 | 143 | } |
144 | 144 | |
145 | 145 | // Prepare response. |
146 | - if ( $error ) { |
|
146 | + if ($error) { |
|
147 | 147 | $response['code'] = '500'; |
148 | - $response['string'] = 'Error removing file ' . $critcssfile . '.'; |
|
148 | + $response['string'] = 'Error removing file '.$critcssfile.'.'; |
|
149 | 149 | } else { |
150 | 150 | $response['code'] = '200'; |
151 | - if ( $status ) { |
|
152 | - $response['string'] = 'File ' . $critcssfile . ' removed.'; |
|
151 | + if ($status) { |
|
152 | + $response['string'] = 'File '.$critcssfile.' removed.'; |
|
153 | 153 | } else { |
154 | 154 | $response['string'] = 'No file to be removed.'; |
155 | 155 | } |
156 | 156 | } |
157 | 157 | |
158 | 158 | // Dispatch respose. |
159 | - echo json_encode( $response ); |
|
159 | + echo json_encode($response); |
|
160 | 160 | |
161 | 161 | // Close ajax request. |
162 | 162 | wp_die(); |
@@ -165,28 +165,28 @@ discard block |
||
165 | 165 | public function critcss_rm_all_callback() { |
166 | 166 | // Ajax handler to delete a critical CSS from the filesystem |
167 | 167 | // Check referer. |
168 | - check_ajax_referer( 'rm_critcss_all_nonce', 'critcss_rm_all_nonce' ); |
|
168 | + check_ajax_referer('rm_critcss_all_nonce', 'critcss_rm_all_nonce'); |
|
169 | 169 | |
170 | 170 | // Initialize error and status flags. |
171 | 171 | $error = true; |
172 | 172 | $status = false; |
173 | 173 | |
174 | 174 | // Remove all ccss files on filesystem. |
175 | - if ( current_user_can( 'manage_options' ) ) { |
|
176 | - if ( file_exists( AO_CCSS_DIR ) && is_dir( AO_CCSS_DIR ) ) { |
|
177 | - array_map( 'unlink', glob( AO_CCSS_DIR . 'ccss_*.css', GLOB_BRACE ) ); |
|
175 | + if (current_user_can('manage_options')) { |
|
176 | + if (file_exists(AO_CCSS_DIR) && is_dir(AO_CCSS_DIR)) { |
|
177 | + array_map('unlink', glob(AO_CCSS_DIR.'ccss_*.css', GLOB_BRACE)); |
|
178 | 178 | $error = false; |
179 | 179 | $status = true; |
180 | 180 | } |
181 | 181 | } |
182 | 182 | |
183 | 183 | // Prepare response. |
184 | - if ( $error ) { |
|
184 | + if ($error) { |
|
185 | 185 | $response['code'] = '500'; |
186 | 186 | $response['string'] = 'Error removing all critical CSS files.'; |
187 | 187 | } else { |
188 | 188 | $response['code'] = '200'; |
189 | - if ( $status ) { |
|
189 | + if ($status) { |
|
190 | 190 | $response['string'] = 'Critical CSS Files removed.'; |
191 | 191 | } else { |
192 | 192 | $response['string'] = 'No file removed.'; |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | } |
195 | 195 | |
196 | 196 | // Dispatch respose. |
197 | - echo json_encode( $response ); |
|
197 | + echo json_encode($response); |
|
198 | 198 | |
199 | 199 | // Close ajax request. |
200 | 200 | wp_die(); |
@@ -203,70 +203,70 @@ discard block |
||
203 | 203 | public function ao_ccss_export_callback() { |
204 | 204 | // Ajax handler export settings |
205 | 205 | // Check referer. |
206 | - check_ajax_referer( 'ao_ccss_export_nonce', 'ao_ccss_export_nonce' ); |
|
206 | + check_ajax_referer('ao_ccss_export_nonce', 'ao_ccss_export_nonce'); |
|
207 | 207 | |
208 | - if ( ! class_exists( 'ZipArchive' ) ) { |
|
208 | + if (!class_exists('ZipArchive')) { |
|
209 | 209 | $response['code'] = '500'; |
210 | 210 | $response['msg'] = 'PHP ZipArchive not present, cannot create zipfile'; |
211 | - echo json_encode( $response ); |
|
211 | + echo json_encode($response); |
|
212 | 212 | wp_die(); |
213 | 213 | } |
214 | 214 | |
215 | 215 | // Init array, get options and prepare the raw object. |
216 | 216 | $settings = array(); |
217 | - $settings['rules'] = get_option( 'autoptimize_ccss_rules' ); |
|
218 | - $settings['additional'] = get_option( 'autoptimize_ccss_additional' ); |
|
219 | - $settings['viewport'] = get_option( 'autoptimize_ccss_viewport' ); |
|
220 | - $settings['finclude'] = get_option( 'autoptimize_ccss_finclude' ); |
|
221 | - $settings['rtimelimit'] = get_option( 'autoptimize_ccss_rtimelimit' ); |
|
222 | - $settings['noptimize'] = get_option( 'autoptimize_ccss_noptimize' ); |
|
223 | - $settings['debug'] = get_option( 'autoptimize_ccss_debug' ); |
|
224 | - $settings['key'] = get_option( 'autoptimize_ccss_key' ); |
|
217 | + $settings['rules'] = get_option('autoptimize_ccss_rules'); |
|
218 | + $settings['additional'] = get_option('autoptimize_ccss_additional'); |
|
219 | + $settings['viewport'] = get_option('autoptimize_ccss_viewport'); |
|
220 | + $settings['finclude'] = get_option('autoptimize_ccss_finclude'); |
|
221 | + $settings['rtimelimit'] = get_option('autoptimize_ccss_rtimelimit'); |
|
222 | + $settings['noptimize'] = get_option('autoptimize_ccss_noptimize'); |
|
223 | + $settings['debug'] = get_option('autoptimize_ccss_debug'); |
|
224 | + $settings['key'] = get_option('autoptimize_ccss_key'); |
|
225 | 225 | |
226 | 226 | // Initialize error flag. |
227 | 227 | $error = true; |
228 | 228 | |
229 | 229 | // Check user permissions. |
230 | - if ( current_user_can( 'manage_options' ) ) { |
|
230 | + if (current_user_can('manage_options')) { |
|
231 | 231 | // Prepare settings file path and content. |
232 | - $exportfile = AO_CCSS_DIR . 'settings.json'; |
|
233 | - $contents = json_encode( $settings ); |
|
234 | - $status = file_put_contents( $exportfile, $contents, LOCK_EX ); |
|
232 | + $exportfile = AO_CCSS_DIR.'settings.json'; |
|
233 | + $contents = json_encode($settings); |
|
234 | + $status = file_put_contents($exportfile, $contents, LOCK_EX); |
|
235 | 235 | $error = false; |
236 | 236 | } |
237 | 237 | |
238 | 238 | // Prepare archive. |
239 | - $zipfile = AO_CCSS_DIR . date( 'Ymd-H\hi' ) . '_ao_ccss_settings.zip'; |
|
240 | - $file = pathinfo( $zipfile, PATHINFO_BASENAME ); |
|
239 | + $zipfile = AO_CCSS_DIR.date('Ymd-H\hi').'_ao_ccss_settings.zip'; |
|
240 | + $file = pathinfo($zipfile, PATHINFO_BASENAME); |
|
241 | 241 | $zip = new ZipArchive(); |
242 | - $ret = $zip->open( $zipfile, ZipArchive::CREATE ); |
|
243 | - if ( true !== $ret ) { |
|
242 | + $ret = $zip->open($zipfile, ZipArchive::CREATE); |
|
243 | + if (true !== $ret) { |
|
244 | 244 | $error = true; |
245 | 245 | } else { |
246 | - $zip->addFile( AO_CCSS_DIR . 'settings.json', 'settings.json' ); |
|
247 | - if ( file_exists( AO_CCSS_DIR . 'queue.json' ) ) { |
|
248 | - $zip->addFile( AO_CCSS_DIR . 'queue.json', 'queue.json' ); |
|
246 | + $zip->addFile(AO_CCSS_DIR.'settings.json', 'settings.json'); |
|
247 | + if (file_exists(AO_CCSS_DIR.'queue.json')) { |
|
248 | + $zip->addFile(AO_CCSS_DIR.'queue.json', 'queue.json'); |
|
249 | 249 | } |
250 | 250 | $options = array( |
251 | 251 | 'add_path' => './', |
252 | 252 | 'remove_all_path' => true, |
253 | 253 | ); |
254 | - $zip->addGlob( AO_CCSS_DIR . '*.css', 0, $options ); |
|
254 | + $zip->addGlob(AO_CCSS_DIR.'*.css', 0, $options); |
|
255 | 255 | $zip->close(); |
256 | 256 | } |
257 | 257 | |
258 | 258 | // Prepare response. |
259 | - if ( ! $status || $error ) { |
|
259 | + if (!$status || $error) { |
|
260 | 260 | $response['code'] = '500'; |
261 | - $response['msg'] = 'Error saving file ' . $file . ', code: ' . $ret; |
|
261 | + $response['msg'] = 'Error saving file '.$file.', code: '.$ret; |
|
262 | 262 | } else { |
263 | 263 | $response['code'] = '200'; |
264 | - $response['msg'] = 'File ' . $file . ' saved.'; |
|
264 | + $response['msg'] = 'File '.$file.' saved.'; |
|
265 | 265 | $response['file'] = $file; |
266 | 266 | } |
267 | 267 | |
268 | 268 | // Dispatch respose. |
269 | - echo json_encode( $response ); |
|
269 | + echo json_encode($response); |
|
270 | 270 | |
271 | 271 | // Close ajax request. |
272 | 272 | wp_die(); |
@@ -275,30 +275,30 @@ discard block |
||
275 | 275 | public function ao_ccss_import_callback() { |
276 | 276 | // Ajax handler import settings |
277 | 277 | // Check referer. |
278 | - check_ajax_referer( 'ao_ccss_import_nonce', 'ao_ccss_import_nonce' ); |
|
278 | + check_ajax_referer('ao_ccss_import_nonce', 'ao_ccss_import_nonce'); |
|
279 | 279 | |
280 | 280 | // Initialize error flag. |
281 | 281 | $error = false; |
282 | 282 | |
283 | 283 | // Process an uploaded file with no errors. |
284 | - if ( current_user_can( 'manage_options' ) && ! $_FILES['file']['error'] && $_FILES['file']['size'] < 500001 && strpos( $_FILES['file']['name'], '.zip' ) === strlen( $_FILES['file']['name'] ) - 4 ) { |
|
284 | + if (current_user_can('manage_options') && !$_FILES['file']['error'] && $_FILES['file']['size'] < 500001 && strpos($_FILES['file']['name'], '.zip') === strlen($_FILES['file']['name']) - 4) { |
|
285 | 285 | // create tmp dir with hard guess name in AO_CCSS_DIR. |
286 | - $_secret_dir = wp_hash( uniqid( md5( AUTOPTIMIZE_CACHE_URL ), true ) ); |
|
287 | - $_import_tmp_dir = trailingslashit( AO_CCSS_DIR . $_secret_dir ); |
|
288 | - mkdir( $_import_tmp_dir ); |
|
286 | + $_secret_dir = wp_hash(uniqid(md5(AUTOPTIMIZE_CACHE_URL), true)); |
|
287 | + $_import_tmp_dir = trailingslashit(AO_CCSS_DIR.$_secret_dir); |
|
288 | + mkdir($_import_tmp_dir); |
|
289 | 289 | |
290 | 290 | // Save file to that tmp directory but give it our own name to prevent directory traversal risks when using original name. |
291 | - $zipfile = $_import_tmp_dir . uniqid( 'import_settings-', true ) . '.zip'; |
|
292 | - move_uploaded_file( $_FILES['file']['tmp_name'], $zipfile ); |
|
291 | + $zipfile = $_import_tmp_dir.uniqid('import_settings-', true).'.zip'; |
|
292 | + move_uploaded_file($_FILES['file']['tmp_name'], $zipfile); |
|
293 | 293 | |
294 | 294 | // Extract archive in the tmp directory. |
295 | 295 | $zip = new ZipArchive; |
296 | - if ( $zip->open( $zipfile ) === true ) { |
|
296 | + if ($zip->open($zipfile) === true) { |
|
297 | 297 | // loop through all files in the zipfile. |
298 | 298 | for ($i = 0; $i < $zip->numFiles; $i++) { |
299 | 299 | // but only extract known good files. |
300 | - if ( preg_match('/^settings\.json$|^ccss_[a-z0-9]{32}\.css$/', $zip->getNameIndex( $i ) ) > 0 ) { |
|
301 | - $zip->extractTo( AO_CCSS_DIR, $zip->getNameIndex( $i ) ); |
|
300 | + if (preg_match('/^settings\.json$|^ccss_[a-z0-9]{32}\.css$/', $zip->getNameIndex($i)) > 0) { |
|
301 | + $zip->extractTo(AO_CCSS_DIR, $zip->getNameIndex($i)); |
|
302 | 302 | } |
303 | 303 | } |
304 | 304 | $zip->close(); |
@@ -307,26 +307,26 @@ discard block |
||
307 | 307 | } |
308 | 308 | |
309 | 309 | // and remove temp. dir with all contents (the import-zipfile). |
310 | - $this->rrmdir( $_import_tmp_dir ); |
|
310 | + $this->rrmdir($_import_tmp_dir); |
|
311 | 311 | |
312 | - if ( ! $error ) { |
|
312 | + if (!$error) { |
|
313 | 313 | // Archive extraction ok, continue importing settings from AO_CCSS_DIR. |
314 | 314 | // Settings file. |
315 | - $importfile = AO_CCSS_DIR . 'settings.json'; |
|
315 | + $importfile = AO_CCSS_DIR.'settings.json'; |
|
316 | 316 | |
317 | - if ( file_exists( $importfile ) ) { |
|
317 | + if (file_exists($importfile)) { |
|
318 | 318 | // Get settings and turn them into an object. |
319 | - $settings = json_decode( file_get_contents( $importfile ), true ); |
|
319 | + $settings = json_decode(file_get_contents($importfile), true); |
|
320 | 320 | |
321 | 321 | // Update options. |
322 | - update_option( 'autoptimize_ccss_rules', $settings['rules'] ); |
|
323 | - update_option( 'autoptimize_ccss_additional', $settings['additional'] ); |
|
324 | - update_option( 'autoptimize_ccss_viewport', $settings['viewport'] ); |
|
325 | - update_option( 'autoptimize_ccss_finclude', $settings['finclude'] ); |
|
326 | - update_option( 'autoptimize_ccss_rtimelimit', $settings['rtimelimit'] ); |
|
327 | - update_option( 'autoptimize_ccss_noptimize', $settings['noptimize'] ); |
|
328 | - update_option( 'autoptimize_ccss_debug', $settings['debug'] ); |
|
329 | - update_option( 'autoptimize_ccss_key', $settings['key'] ); |
|
322 | + update_option('autoptimize_ccss_rules', $settings['rules']); |
|
323 | + update_option('autoptimize_ccss_additional', $settings['additional']); |
|
324 | + update_option('autoptimize_ccss_viewport', $settings['viewport']); |
|
325 | + update_option('autoptimize_ccss_finclude', $settings['finclude']); |
|
326 | + update_option('autoptimize_ccss_rtimelimit', $settings['rtimelimit']); |
|
327 | + update_option('autoptimize_ccss_noptimize', $settings['noptimize']); |
|
328 | + update_option('autoptimize_ccss_debug', $settings['debug']); |
|
329 | + update_option('autoptimize_ccss_key', $settings['key']); |
|
330 | 330 | } else { |
331 | 331 | // Settings file doesn't exist, update error flag. |
332 | 332 | $error = 'settings file does not exist'; |
@@ -337,28 +337,28 @@ discard block |
||
337 | 337 | } |
338 | 338 | |
339 | 339 | // Prepare response. |
340 | - if ( $error ) { |
|
340 | + if ($error) { |
|
341 | 341 | $response['code'] = '500'; |
342 | - $response['msg'] = 'Error importing settings: ' . $error; |
|
342 | + $response['msg'] = 'Error importing settings: '.$error; |
|
343 | 343 | } else { |
344 | 344 | $response['code'] = '200'; |
345 | 345 | $response['msg'] = 'Settings imported successfully'; |
346 | 346 | } |
347 | 347 | |
348 | 348 | // Dispatch respose. |
349 | - echo json_encode( $response ); |
|
349 | + echo json_encode($response); |
|
350 | 350 | |
351 | 351 | // Close ajax request. |
352 | 352 | wp_die(); |
353 | 353 | } |
354 | 354 | |
355 | - public function critcss_check_filename( $filename ) { |
|
355 | + public function critcss_check_filename($filename) { |
|
356 | 356 | // Try to avoid directory traversal when reading/writing/deleting critical CSS files. |
357 | - if ( strpos( $filename, 'ccss_' ) !== 0 ) { |
|
357 | + if (strpos($filename, 'ccss_') !== 0) { |
|
358 | 358 | return false; |
359 | - } elseif ( substr( $filename, -4, 4 ) !== '.css' ) { |
|
359 | + } elseif (substr($filename, -4, 4) !== '.css') { |
|
360 | 360 | return false; |
361 | - } elseif ( sanitize_file_name( $filename ) !== $filename ) { |
|
361 | + } elseif (sanitize_file_name($filename) !== $filename) { |
|
362 | 362 | // Use WordPress core's sanitize_file_name to see if anything fishy is going on. |
363 | 363 | return false; |
364 | 364 | } else { |
@@ -366,14 +366,14 @@ discard block |
||
366 | 366 | } |
367 | 367 | } |
368 | 368 | |
369 | - public function rrmdir( $path ) { |
|
369 | + public function rrmdir($path) { |
|
370 | 370 | // recursively remove a directory as found on |
371 | 371 | // https://andy-carter.com/blog/recursively-remove-a-directory-in-php. |
372 | - $files = glob($path . '/*'); |
|
373 | - foreach ( $files as $file ) { |
|
374 | - is_dir( $file ) ? $this->rrmdir( $file ) : unlink( $file ); |
|
372 | + $files = glob($path.'/*'); |
|
373 | + foreach ($files as $file) { |
|
374 | + is_dir($file) ? $this->rrmdir($file) : unlink($file); |
|
375 | 375 | } |
376 | - rmdir( $path ); |
|
376 | + rmdir($path); |
|
377 | 377 | |
378 | 378 | return; |
379 | 379 | } |