Skip to content

Commit

Permalink
fix: compatibility with relative image urls
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Mar 4, 2019
1 parent 56108be commit 8089610
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 4 additions & 3 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,13 @@ public function should_replace() {
* Set the cdn url based on the current connected user.
*/
public function set_properties() {
$upload_data = wp_upload_dir();
$this->upload_resource = array(
$upload_data = wp_upload_dir();
$this->upload_resource = array(
'url' => str_replace( array( 'https://', 'http://' ), '', $upload_data['baseurl'] ),
'directory' => $upload_data['basedir'],
);
$this->upload_resource['url_length'] = strlen( $this->upload_resource['url'] );
$this->upload_resource['url_length'] = strlen( $this->upload_resource['url'] );
$this->upload_resource['content_path'] = str_replace( get_site_url(), '', content_url() );

$service_data = $this->settings->get( 'service_data' );

Expand Down
5 changes: 4 additions & 1 deletion inc/tag_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ public function process_image_tags( $content, $images = array() ) {

$src = $tmp = $is_slashed ? stripslashes( $images['img_url'][ $index ] ) : $images['img_url'][ $index ];

$src = $tmp = strpos( $src, $this->upload_resource['content_path'] ) === 0 ? untrailingslashit( get_site_url() ) . $src : $src;

if ( apply_filters( 'optml_ignore_image_link', false, $src ) ||
false !== strpos( $src, Optml_Config::$service_url ) ||
! $this->can_replace_url( $src )
) {

continue; // @codeCoverageIgnore
}

Expand Down Expand Up @@ -196,7 +199,7 @@ public function filter_srcset_attr( $sources = array(), $size_array = array(), $
return $sources;
}
$original_url = null;
$cropping = null;
$cropping = null;
if ( count( $size_array ) === 2 ) {
$sizes = self::size_to_crop();
$cropping = isset( $sizes[ $size_array[0] . $size_array[1] ] ) ? $this->to_optml_crop( $sizes[ $size_array[0] . $size_array[1] ] ) : null;
Expand Down
14 changes: 14 additions & 0 deletions tests/test-replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public function test_replacement_remove_query_arg() {
$this->assertNotContains( '?param=123', $replaced_content );
}

public function test_replacement_with_relative_url() {
$content = '<div class="before-footer">
<div class="codeinwp-container">
<p class="featuredon">Featured On</p>
<img src="/wp-content/uploads/2018/05/brands.png">
</div>
</div>';

$replaced_content = Optml_Manager::instance()->replace_content( $content );

$this->assertContains( 'i.optimole.com', $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">
Expand Down

0 comments on commit 8089610

Please sign in to comment.