PluginProbe
Autoptimize / 3.0.3
Autoptimize v3.0.3
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 +35 -34 2.4.13.0.3 View file →
@@ -22,8 +22,15 @@
22 22 * @var bool
23 23 */
24 24 public $debug_log = false;
25 25
26 + /**
27 + * Initiated $cdn_url.
28 + *
29 + * @var string
30 + */
31 + public $cdn_url = '';
32 +
26 33 public function __construct( $content )
27 34 {
28 35 $this->content = $content;
29 36 }
@@ -69,8 +76,12 @@
69 76 public function getpath( $url )
70 77 {
71 78 $url = apply_filters( 'autoptimize_filter_cssjs_alter_url', $url );
72 79
80 + if ( is_null( $url ) ) {
81 + return false;
82 + }
83 +
73 84 if ( false !== strpos( $url, '%' ) ) {
74 85 $url = urldecode( $url );
75 86 }
76 87
@@ -87,8 +98,10 @@
87 98 }
88 99 } elseif ( ( false === $double_slash_position ) && ( false === strpos( $url, $site_host ) ) ) {
89 100 if ( AUTOPTIMIZE_WP_SITE_URL === $site_host ) {
90 101 $url = AUTOPTIMIZE_WP_SITE_URL . $url;
102 + } elseif ( 0 === strpos( $url, '/' ) ) {
103 + $url = '//' . $site_host . autoptimizeUtils::path_canonicalize( $url );
91 104 } else {
92 105 $url = AUTOPTIMIZE_WP_SITE_URL . autoptimizeUtils::path_canonicalize( $url );
93 106 }
94 107 }
@@ -136,8 +149,16 @@
136 149 if ( $site_host !== $content_host ) {
137 150 // As we replaced the content-domain with the site-domain, we should match against that.
138 151 $tmp_ao_root = preg_replace( '/https?:/', '', AUTOPTIMIZE_WP_SITE_URL );
139 152 }
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 + }
140 161
141 162 $tmp_url = preg_replace( '/https?:/', '', $url );
142 163 $path = str_replace( $tmp_ao_root, '', $tmp_url );
143 164
@@ -148,9 +169,13 @@
148 169 return false;
149 170 }
150 171
151 172 // Prepend with WP_ROOT_DIR to have full path to file.
152 - $path = str_replace( '//', '/', WP_ROOT_DIR . $path );
173 + $path = str_replace( '//', '/', trailingslashit( WP_ROOT_DIR ) . $path );
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 );
153 178
154 179 // Final check: does file exist and is it readable?
155 180 if ( file_exists( $path ) && is_file( $path ) && is_readable( $path ) ) {
156 181 return $path;
@@ -293,15 +318,14 @@
293 318 $cdn_url = autoptimizeUtils::tweak_cdn_url_if_needed( $this->cdn_url );
294 319
295 320 // Allows API/filter to further tweak the cdn url...
296 321 $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $cdn_url );
297 - if ( ! empty( $cdn_url ) ) {
298 - $this->debug_log( 'before=' . $url );
322 + if ( ! empty( $cdn_url ) && false === strpos( $url, $cdn_url ) ) {
299 323
300 324 // Simple str_replace-based approach fails when $url is protocol-or-host-relative.
301 325 $is_protocol_relative = autoptimizeUtils::is_protocol_relative( $url );
302 - $is_host_relative = ( ! $is_protocol_relative && ( '/' === $url{0} ) );
303 - $cdn_url = rtrim( $cdn_url, '/' );
326 + $is_host_relative = ( ! $is_protocol_relative && ( '/' === $url[0] ) );
327 + $cdn_url = esc_url( rtrim( $cdn_url, '/' ) );
304 328
305 329 if ( $is_host_relative ) {
306 330 // Prepending host-relative urls with the cdn url.
307 331 $url = $cdn_url . $url;
@@ -313,13 +337,10 @@
313 337 $site_url = str_replace( array( 'http:', 'https:' ), '', AUTOPTIMIZE_WP_SITE_URL );
314 338 } else {
315 339 $site_url = AUTOPTIMIZE_WP_SITE_URL;
316 340 }
317 - $this->debug_log( '`' . $site_url . '` -> `' . $cdn_url . '` in `' . $url . '`' );
318 341 $url = str_replace( $site_url, $cdn_url, $url );
319 342 }
320 -
321 - $this->debug_log( 'after=' . $url );
322 343 }
323 344
324 345 // Allow API filter to take further care of CDN replacement.
325 346 $url = apply_filters( 'autoptimize_filter_base_replace_cdn', $url );
@@ -418,9 +439,9 @@
418 439
419 440 // Grab the parts we need.
420 441 $parts = explode( '|', $matches[1] );
421 442 if ( ! empty( $parts ) ) {
422 - $filepath = isset( $parts[0] ) ? base64_decode($parts[0]) : null;
443 + $filepath = isset( $parts[0] ) ? base64_decode( $parts[0] ) : null;
423 444 $filehash = isset( $parts[1] ) ? $parts[1] : null;
424 445 }
425 446
426 447 // Bail early if something's not right...
@@ -558,9 +579,9 @@
558 579 * @param string $content Content to work on.
559 580 *
560 581 * @return string
561 582 */
562 - protected function replace_contents_with_marker_if_exists( $marker, $search, $re_replace_pattern, $content )
583 + public static function replace_contents_with_marker_if_exists( $marker, $search, $re_replace_pattern, $content )
563 584 {
564 585 $found = false;
565 586
566 587 $is_regex = autoptimizeUtils::str_is_valid_regex( $search );
@@ -590,9 +611,9 @@
590 611 * @param string $content Markup.
591 612 *
592 613 * @return string
593 614 */
594 - protected function restore_marked_content( $marker, $content )
615 + public static function restore_marked_content( $marker, $content )
595 616 {
596 617 if ( false !== strpos( $content, $marker ) ) {
597 618 $content = preg_replace_callback(
598 619 '#%%' . $marker . AUTOPTIMIZE_HASH . '%%(.*?)%%' . $marker . '%%#is',
@@ -635,11 +656,11 @@
635 656 */
636 657 protected function prepare_minify_single( $filepath )
637 658 {
638 659 // Decide what we're dealing with, return false if we don't know.
639 - if ( $this->str_ends_in( $filepath, '.js' ) ) {
660 + if ( autoptimizeUtils::str_ends_in( $filepath, '.js' ) ) {
640 661 $type = 'js';
641 - } elseif ( $this->str_ends_in( $filepath, '.css' ) ) {
662 + } elseif ( autoptimizeUtils::str_ends_in( $filepath, '.css' ) ) {
642 663 $type = 'css';
643 664 } else {
644 665 return false;
645 666 }
@@ -651,9 +672,9 @@
651 672 '.min.' . $type,
652 673 'js/jquery/jquery.js',
653 674 );
654 675 foreach ( $minified_variants as $ending ) {
655 - 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 ) ) {
656 677 return false;
657 678 }
658 679 }
659 680
@@ -678,26 +699,6 @@
678 699 // CDN-replace the resulting URL if needed...
679 700 $url = $this->url_replace_cdn( $url );
680 701
681 702 return $url;
682 - }
683 -
684 - /**
685 - * Returns true if given $str ends with given $test.
686 - *
687 - * @param string $str String to check.
688 - * @param string $test Ending to match.
689 - *
690 - * @return bool
691 - */
692 - protected function str_ends_in( $str, $test )
693 - {
694 - // @codingStandardsIgnoreStart
695 - // substr_compare() is bugged on 5.5.11: https://3v4l.org/qGYBH
696 - // return ( 0 === substr_compare( $str, $test, -strlen( $test ) ) );
697 - // @codingStandardsIgnoreEnd
698 -
699 - $length = strlen( $test );
700 -
701 - return ( substr( $str, -$length, $length ) === $test );
702 703 }
703 704 }