PluginProbe
Autoptimize / trunk
Autoptimize vtrunk
3.1.16 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 All 108 releases
← All changes | classes/autoptimizeBase.php +32 -11 2.7.6 → trunk View file →
@@ -76,8 +76,12 @@
76 76 public function getpath( $url )
77 77 {
78 78 $url = apply_filters( 'autoptimize_filter_cssjs_alter_url', $url );
79 79
80 + if ( is_null( $url ) ) {
81 + return false;
82 + }
83 +
80 84 if ( false !== strpos( $url, '%' ) ) {
81 85 $url = urldecode( $url );
82 86 }
83 87
@@ -94,8 +98,10 @@
94 98 }
95 99 } elseif ( ( false === $double_slash_position ) && ( false === strpos( $url, $site_host ) ) ) {
96 100 if ( AUTOPTIMIZE_WP_SITE_URL === $site_host ) {
97 101 $url = AUTOPTIMIZE_WP_SITE_URL . $url;
102 + } elseif ( 0 === strpos( $url, '/' ) ) {
103 + $url = '//' . $site_host . autoptimizeUtils::path_canonicalize( $url );
98 104 } else {
99 105 $url = AUTOPTIMIZE_WP_SITE_URL . autoptimizeUtils::path_canonicalize( $url );
100 106 }
101 107 }
@@ -144,8 +150,16 @@
144 150 // As we replaced the content-domain with the site-domain, we should match against that.
145 151 $tmp_ao_root = preg_replace( '/https?:/', '', AUTOPTIMIZE_WP_SITE_URL );
146 152 }
147 153
154 + if ( is_multisite() && ! is_main_site() && ! empty( $this->cdn_url ) && apply_filters( 'autoptimize_filter_base_getpage_multisite_cdn_juggling', true ) ) {
155 + // multisite child sites with CDN need the network_site_url as tmp_ao_root but only if directory-based multisite.
156 + $_network_site_url = network_site_url();
157 + if ( strpos( AUTOPTIMIZE_WP_SITE_URL, $_network_site_url ) !== false ) {
158 + $tmp_ao_root = preg_replace( '/https?:/', '', $_network_site_url );
159 + }
160 + }
161 +
148 162 $tmp_url = preg_replace( '/https?:/', '', $url );
149 163 $path = str_replace( $tmp_ao_root, '', $tmp_url );
150 164
151 165 // If path starts with :// or //, this is not a URL in the WP context and
@@ -155,10 +169,14 @@
155 169 return false;
156 170 }
157 171
158 172 // Prepend with WP_ROOT_DIR to have full path to file.
159 - $path = str_replace( '//', '/', WP_ROOT_DIR . $path );
173 + $path = str_replace( '//', '/', trailingslashit( WP_ROOT_DIR ) . $path );
160 174
175 + // Allow path to be altered, e.g. in the case of bedrock-like setups where
176 + // core, theme & plugins might be in different locations on the filesystem.
177 + $path = apply_filters( 'autoptimize_filter_base_getpath_path', $path, $url );
178 +
161 179 // Final check: does file exist and is it readable?
162 180 if ( file_exists( $path ) && is_file( $path ) && is_readable( $path ) ) {
163 181 return $path;
164 182 } else {
@@ -300,15 +318,14 @@
300 318 $cdn_url = autoptimizeUtils::tweak_cdn_url_if_needed( $this->cdn_url );
301 319
302 320 // Allows API/filter to further tweak the cdn url...
303 321 $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $cdn_url );
304 - if ( ! empty( $cdn_url ) ) {
305 - $this->debug_log( 'before=' . $url );
322 + if ( ! empty( $cdn_url ) && false === strpos( $url, $cdn_url ) && false !== apply_filters( 'autoptimize_filter_base_apply_cdn', true, $url ) ) {
306 323
307 324 // Simple str_replace-based approach fails when $url is protocol-or-host-relative.
308 325 $is_protocol_relative = autoptimizeUtils::is_protocol_relative( $url );
309 326 $is_host_relative = ( ! $is_protocol_relative && ( '/' === $url[0] ) );
310 - $cdn_url = rtrim( $cdn_url, '/' );
327 + $cdn_url = esc_url( rtrim( $cdn_url, '/' ) );
311 328
312 329 if ( $is_host_relative ) {
313 330 // Prepending host-relative urls with the cdn url.
314 331 $url = $cdn_url . $url;
@@ -320,13 +337,10 @@
320 337 $site_url = str_replace( array( 'http:', 'https:' ), '', AUTOPTIMIZE_WP_SITE_URL );
321 338 } else {
322 339 $site_url = AUTOPTIMIZE_WP_SITE_URL;
323 340 }
324 - $this->debug_log( '`' . $site_url . '` -> `' . $cdn_url . '` in `' . $url . '`' );
325 341 $url = str_replace( $site_url, $cdn_url, $url );
326 342 }
327 -
328 - $this->debug_log( 'after=' . $url );
329 343 }
330 344
331 345 // Allow API filter to take further care of CDN replacement.
332 346 $url = apply_filters( 'autoptimize_filter_base_replace_cdn', $url );
@@ -584,8 +598,17 @@
584 598 return autoptimizeBase::build_marker( $marker, $matches[0] );
585 599 },
586 600 $content
587 601 );
602 +
603 + // Check for error (for example, an error can occur if $content is very large).
604 + if ( null === $content ) {
605 + $error_message = 'Autoptimize: preg_replace_callback() failed';
606 + if ( function_exists( 'preg_last_error_msg' ) ) {
607 + $error_message .= ': ' . preg_last_error_msg();
608 + }
609 + error_log( $error_message );
610 + }
588 611 }
589 612
590 613 return $content;
591 614 }
@@ -650,17 +673,15 @@
650 673 } else {
651 674 return false;
652 675 }
653 676
654 - // Bail if it looks like its already minifed (by having -min or .min
655 - // in filename) or if it looks like WP jquery.js (which is minified).
677 + // Bail if it looks like its already minifed (by having -min or .min in filename).
656 678 $minified_variants = array(
657 679 '-min.' . $type,
658 680 '.min.' . $type,
659 - 'js/jquery/jquery.js',
660 681 );
661 682 foreach ( $minified_variants as $ending ) {
662 - if ( autoptimizeUtils::str_ends_in( $filepath, $ending ) ) {
683 + if ( autoptimizeUtils::str_ends_in( $filepath, $ending ) && true === apply_filters( 'autoptimize_filter_base_prepare_exclude_minified', true ) ) {
663 684 return false;
664 685 }
665 686 }
666 687