Skip to content
Permalink
Browse files

fix: compatibility with relative image urls

  • Loading branch information...
selul committed Mar 4, 2019
1 parent 56108be commit 80896104adc8cfa11df06886b7568d0b7d1b15db
Showing with 22 additions and 4 deletions.
  1. +4 −3 inc/app_replacer.php
  2. +4 −1 inc/tag_replacer.php
  3. +14 −0 tests/test-replacer.php
@@ -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' );
@@ -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
}
@@ -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;
@@ -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">

0 comments on commit 8089610

Please sign in to comment.
You can’t perform that action at this time.