From 56108be2d3e18b781e3d631ea25822c212482c90 Mon Sep 17 00:00:00 2001 From: selul Date: Mon, 4 Mar 2019 10:07:43 +0200 Subject: [PATCH] fix: compatibility with images which contains query arguments, causing broken image urls --- inc/manager.php | 4 ++-- inc/url_replacer.php | 3 +++ tests/test-replacer.php | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/inc/manager.php b/inc/manager.php index dc860944..c6ccd174 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -198,7 +198,7 @@ public function process_urls_from_json( $json ) { * @return array array of urls. */ public function extract_urls_from_json( $content ) { - $regex = '/(?can_replace_url( $url ) ) { return $url; } + // Remove any query strings that might affect conversion. + $url = strtok( $url, '?' ); + if ( ! $this->is_valid_mimetype_from_url( $url ) ) { return $url; } diff --git a/tests/test-replacer.php b/tests/test-replacer.php index 54440707..8b335908 100644 --- a/tests/test-replacer.php +++ b/tests/test-replacer.php @@ -125,6 +125,19 @@ public function test_replacement_non_whitelisted_urls() { $this->assertContains( 'https://www.codeinwp.org', $replaced_content ); } + public function test_replacement_remove_query_arg() { + $content = ''; + $replaced_content = Optml_Manager::instance()->replace_content( $content ); + + $this->assertContains( 'i.optimole.com', $replaced_content ); + $this->assertNotContains( '?param=123', $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 = '