PluginProbe
Autoptimize / 3.1.4
Autoptimize v3.1.4
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
← All changes | classes/autoptimizeBase.php +30 -32 2.5.13.1.4 View file →
@@ -22,9 +22,13 @@
22 22 * @var bool
23 23 */
24 24 public $debug_log = false;
25 25
26 - /** @var string */
26 + /**
27 + * Initiated $cdn_url.
28 + *
29 + * @var string
30 + */
27 31 public $cdn_url = '';
28 32
29 33 public function __construct( $content )
30 34 {
@@ -72,8 +76,12 @@
72 76 public function getpath( $url )
73 77 {
74 78 $url = apply_filters( 'autoptimize_filter_cssjs_alter_url', $url );
75 79
80 + if ( is_null( $url ) ) {
81 + return false;
82 + }
83 +
76 84 if ( false !== strpos( $url, '%' ) ) {
77 85 $url = urldecode( $url );
78 86 }
79 87
@@ -90,8 +98,10 @@
90 98 }
91 99 } elseif ( ( false === $double_slash_position ) && ( false === strpos( $url, $site_host ) ) ) {
92 100 if ( AUTOPTIMIZE_WP_SITE_URL === $site_host ) {
93 101 $url = AUTOPTIMIZE_WP_SITE_URL . $url;
102 + } elseif ( 0 === strpos( $url, '/' ) ) {
103 + $url = '//' . $site_host . autoptimizeUtils::path_canonicalize( $url );
94 104 } else {
95 105 $url = AUTOPTIMIZE_WP_SITE_URL . autoptimizeUtils::path_canonicalize( $url );
96 106 }
97 107 }
@@ -140,8 +150,16 @@
140 150 // As we replaced the content-domain with the site-domain, we should match against that.
141 151 $tmp_ao_root = preg_replace( '/https?:/', '', AUTOPTIMIZE_WP_SITE_URL );
142 152 }
143 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 +
144 162 $tmp_url = preg_replace( '/https?:/', '', $url );
145 163 $path = str_replace( $tmp_ao_root, '', $tmp_url );
146 164
147 165 // If path starts with :// or //, this is not a URL in the WP context and
@@ -151,10 +169,14 @@
151 169 return false;
152 170 }
153 171
154 172 // Prepend with WP_ROOT_DIR to have full path to file.
155 - $path = str_replace( '//', '/', WP_ROOT_DIR . $path );
173 + $path = str_replace( '//', '/', trailingslashit( WP_ROOT_DIR ) . $path );
156 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 +
157 179 // Final check: does file exist and is it readable?
158 180 if ( file_exists( $path ) && is_file( $path ) && is_readable( $path ) ) {
159 181 return $path;
160 182 } else {
@@ -296,15 +318,14 @@
296 318 $cdn_url = autoptimizeUtils::tweak_cdn_url_if_needed( $this->cdn_url );
297 319
298 320 // Allows API/filter to further tweak the cdn url...
299 321 $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $cdn_url );
300 - if ( ! empty( $cdn_url ) ) {
301 - $this->debug_log( 'before=' . $url );
322 + if ( ! empty( $cdn_url ) && false === strpos( $url, $cdn_url ) ) {
302 323
303 324 // Simple str_replace-based approach fails when $url is protocol-or-host-relative.
304 325 $is_protocol_relative = autoptimizeUtils::is_protocol_relative( $url );
305 - $is_host_relative = ( ! $is_protocol_relative && ( '/' === $url{0} ) );
306 - $cdn_url = rtrim( $cdn_url, '/' );
326 + $is_host_relative = ( ! $is_protocol_relative && ( '/' === $url[0] ) );
327 + $cdn_url = esc_url( rtrim( $cdn_url, '/' ) );
307 328
308 329 if ( $is_host_relative ) {
309 330 // Prepending host-relative urls with the cdn url.
310 331 $url = $cdn_url . $url;
@@ -316,13 +337,10 @@
316 337 $site_url = str_replace( array( 'http:', 'https:' ), '', AUTOPTIMIZE_WP_SITE_URL );
317 338 } else {
318 339 $site_url = AUTOPTIMIZE_WP_SITE_URL;
319 340 }
320 - $this->debug_log( '`' . $site_url . '` -> `' . $cdn_url . '` in `' . $url . '`' );
321 341 $url = str_replace( $site_url, $cdn_url, $url );
322 342 }
323 -
324 - $this->debug_log( 'after=' . $url );
325 343 }
326 344
327 345 // Allow API filter to take further care of CDN replacement.
328 346 $url = apply_filters( 'autoptimize_filter_base_replace_cdn', $url );
@@ -638,11 +656,11 @@
638 656 */
639 657 protected function prepare_minify_single( $filepath )
640 658 {
641 659 // Decide what we're dealing with, return false if we don't know.
642 - if ( $this->str_ends_in( $filepath, '.js' ) ) {
660 + if ( autoptimizeUtils::str_ends_in( $filepath, '.js' ) ) {
643 661 $type = 'js';
644 - } elseif ( $this->str_ends_in( $filepath, '.css' ) ) {
662 + } elseif ( autoptimizeUtils::str_ends_in( $filepath, '.css' ) ) {
645 663 $type = 'css';
646 664 } else {
647 665 return false;
648 666 }
@@ -654,9 +672,9 @@
654 672 '.min.' . $type,
655 673 'js/jquery/jquery.js',
656 674 );
657 675 foreach ( $minified_variants as $ending ) {
658 - if ( $this->str_ends_in( $filepath, $ending ) ) {
676 + if ( autoptimizeUtils::str_ends_in( $filepath, $ending ) && true === apply_filters( 'autoptimize_filter_base_prepare_exclude_minified', true ) ) {
659 677 return false;
660 678 }
661 679 }
662 680
@@ -681,26 +699,6 @@
681 699 // CDN-replace the resulting URL if needed...
682 700 $url = $this->url_replace_cdn( $url );
683 701
684 702 return $url;
685 - }
686 -
687 - /**
688 - * Returns true if given $str ends with given $test.
689 - *
690 - * @param string $str String to check.
691 - * @param string $test Ending to match.
692 - *
693 - * @return bool
694 - */
695 - protected function str_ends_in( $str, $test )
696 - {
697 - // @codingStandardsIgnoreStart
698 - // substr_compare() is bugged on 5.5.11: https://3v4l.org/qGYBH
699 - // return ( 0 === substr_compare( $str, $test, -strlen( $test ) ) );
700 - // @codingStandardsIgnoreEnd
701 -
702 - $length = strlen( $test );
703 -
704 - return ( substr( $str, -$length, $length ) === $test );
705 703 }
706 704 }