Skip to content

Commit

Permalink
fix: domain validation when www is present in the image urls, thanks @…
Browse files Browse the repository at this point in the history
…WPRiders team for reporting
  • Loading branch information
selul committed Feb 1, 2019
1 parent 76c0307 commit 9559ce5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
20 changes: 15 additions & 5 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ public function set_properties() {

if ( defined( 'OPTML_SITE_MIRROR' ) && constant( 'OPTML_SITE_MIRROR' ) ) {
$this->site_mappings = array(
rtrim( get_site_url(), '/' ) => rtrim( ltrim( constant( 'OPTML_SITE_MIRROR' ), 'www.' ), '/' ),
rtrim( get_site_url(), '/' ) => rtrim( constant( 'OPTML_SITE_MIRROR' ), '/' ),
);
}

$this->possible_sources = $this->extract_domain_from_urls(
array_merge(
array( ltrim( get_site_url(), 'www.' ) ),
array( get_site_url() ),
array_values( $this->site_mappings )
)
);
Expand Down Expand Up @@ -240,14 +240,23 @@ protected function extract_domain_from_urls( $urls = array() ) {
function ( $value ) {
$parts = parse_url( $value );

return isset( $parts['host'] ) ? ltrim( $parts['host'], 'www.' ) : '';
return isset( $parts['host'] ) ? $parts['host'] : '';
},
$urls
);
$urls = array_filter( $urls );
$urls = array_unique( $urls );

return array_fill_keys( $urls, true );
$urls = array_fill_keys( $urls, true );
// build www versions of urls, just in case we need them for validation.

foreach ( $urls as $domain => $status ) {
if ( ! ( substr( $domain, 0, 4 ) === 'www.' ) ) {
$urls[ 'www.' . $domain ] = true;
}
}

return $urls;
}

/**
Expand All @@ -262,7 +271,8 @@ public function can_replace_url( $url ) {
return false; // @codeCoverageIgnore
}
$url = parse_url( $url );
return isset( $this->possible_sources[ ltrim( $url['host'], 'www.' ) ] );

return isset( $this->possible_sources[ $url['host'] ] );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
[
$original_url,
'src=',
'srcset=', //Not ideal to disable srcset, we should aim to remove the srcset completely from code.
'srcset=', // Not ideal to disable srcset, we should aim to remove the srcset completely from code.
],
[
$low_url,
$opt_src . ' src=',
'old-srcset='
'old-srcset=',
],
$new_tag
);
Expand Down
2 changes: 1 addition & 1 deletion themeisle-hash.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"optimole-wp.php":"c27858bd3806572aacc60dc98bb8735e"}
{"optimole-wp.php":"ac1b296e575224597f0cba676855333b"}

0 comments on commit 9559ce5

Please sign in to comment.