Skip to content

Commit

Permalink
fix: image replacement was breaking non-image urls with query strings
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Apr 10, 2019
1 parent 4385f68 commit 2ef2212
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 8 additions & 7 deletions inc/url_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ final class Optml_Url_Replacer extends Optml_App_Replacer {
*
* @codeCoverageIgnore
* @static
* @return Optml_Url_Replacer
* @since 1.0.0
* @access public
* @return Optml_Url_Replacer
*/
public static function instance() {
if ( null === self::$instance ) {
Expand Down Expand Up @@ -112,22 +112,23 @@ public function build_image_url(
if ( apply_filters( 'optml_dont_replace_url', false, $url ) ) {
return $url;
}
$original_url = $url;

$is_slashed = strpos( $url, '\/' ) !== false;
$is_slashed = strpos( $url, '\/' ) !== false;

$url = $is_slashed ? stripslashes( $url ) : $url;

if ( strpos( $url, Optml_Config::$service_url ) !== false ) {
return $url;
return $original_url;
}
if ( ! $this->can_replace_url( $url ) ) {
return $url;
return $original_url;
}
// Remove any query strings that might affect conversion.
$url = strtok( $url, '?' );

if ( ! $this->is_valid_mimetype_from_url( $url ) ) {
return $url;
return $original_url;
}
if ( isset( $args['quality'] ) && ! empty( $args['quality'] ) ) {
$args['quality'] = $this->to_accepted_quality( $args['quality'] );
Expand Down Expand Up @@ -178,8 +179,8 @@ public function build_image_url(
*
* @codeCoverageIgnore
* @access public
* @since 1.0.0
* @return void
* @since 1.0.0
*/
public function __clone() {
// Cloning instances of the class is forbidden.
Expand All @@ -191,8 +192,8 @@ public function __clone() {
*
* @codeCoverageIgnore
* @access public
* @since 1.0.0
* @return void
* @since 1.0.0
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
Expand Down
10 changes: 10 additions & 0 deletions tests/test-lazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public function test_replacement_with_jetpack_photon() {
$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertNotContains( 'i0.wp.com', $replaced_content );
}
public function test_replacement_wrong_extension_with_query_string() {
$content = '
<img src="http://example.org/wp-content/plugins/test/generate-qr-code.php?3CRMB6qM1DvLswN6nxKjppX6W5ycjXpeZp">
';

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

$this->assertNotContains( 'i.optimole.com', $replaced_content );
$this->assertNotContains( 'data-opt-src', $replaced_content );
}

public function test_lazy_dont_lazy_load_headers() {
$replaced_content = Optml_Manager::instance()->process_images_from_content( self::HTML_TAGS_HEADER );
Expand Down

0 comments on commit 2ef2212

Please sign in to comment.