Skip to content

Commit

Permalink
fix: allow image urls that does not contain scheme, either http or https
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Feb 8, 2019
1 parent bfb016e commit 2aae664
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions inc/url_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function build_image_url(
$url = str_replace( array_keys( $this->site_mappings ), array_values( $this->site_mappings ), $url );
}

if ( substr( $url, 0, 2 ) === '//' ) {
$url = sprintf( '%s:%s', is_ssl() ? 'https' : 'http', $url );
}
$new_url = $this->strip_image_size_from_url( $url );

if ( $new_url !== $url ) {
Expand Down
13 changes: 13 additions & 0 deletions tests/test-replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ public function test_replacement_non_whitelisted_urls() {
$this->assertContains( 'https://www.codeinwp.org', $replaced_content );
}

// TODO We need to extend this to single url replacement. If we make the url extractor regex with option scheme, the parsing will take huge amount of time. We need to think alternatives.
public function test_replacement_without_scheme() {
$content = '<div class="before-footer">
<div class="codeinwp-container">
<p class="featuredon">Featured On</p>
<img src="//www.example.org/wp-content/uploads/2018/05/brands.png">
</div>
</div>';
$replaced_content = Optml_Manager::instance()->replace_content( $content );
$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertContains( 'http://www.example.org', $replaced_content );;
}

public function test_non_allowed_extensions() {
$replaced_content = Optml_Manager::instance()->replace_content( ( self::CSS_STYLE . self::IMG_TAGS . self::WRONG_EXTENSION ) );
$this->assertContains( 'i.optimole.com', $replaced_content );
Expand Down

0 comments on commit 2aae664

Please sign in to comment.