From d176c11082fddb7ac399a9d06f039b0b0dfce596 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Wed, 28 Aug 2019 17:13:26 +0300 Subject: [PATCH 01/12] fix: background image replacement url --- inc/manager.php | 2 +- tests/test-background-image.php | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/test-background-image.php diff --git a/inc/manager.php b/inc/manager.php index 0be17b58..9e7886d9 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -481,7 +481,7 @@ public function process_urls_from_content( $html ) { * @return array */ public function extract_image_urls_from_content( $content ) { - $regex = '/(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?/'; + $regex = '/((?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?)|((?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?)/'; preg_match_all( $regex, $content, diff --git a/tests/test-background-image.php b/tests/test-background-image.php new file mode 100644 index 00000000..6aa4658e --- /dev/null +++ b/tests/test-background-image.php @@ -0,0 +1,51 @@ + '; + + + public static $sample_post; + + public function setUp() + { + + parent::setUp(); + $settings = new Optml_Settings(); + $settings->update('service_data', [ + 'cdn_key' => 'test123', + 'cdn_secret' => '12345', + 'whitelist' => ['example.com'], + + ]); + $settings->update('lazyload', 'disabled'); + $settings->update('img_to_video', 'enabled'); + + + Optml_Url_Replacer::instance()->init(); + Optml_Tag_Replacer::instance()->init(); + Optml_Manager::instance()->init(); + + self::$sample_post = self::factory()->post->create([ + 'post_title' => 'Test post', + 'post_content' => self::IMG_TAGS_GIF + ] + ); + } + + public function test_should_replace_tag() + { + + $replaced_content = Optml_Manager::instance()->replace_content(self::IMG_TAGS_GIF); + $this->assertContains('i.optimole.com', $replaced_content); + } +} + From 9c60d5e6b49a691f6e62f2ed3e935aa47a5a9da5 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Thu, 29 Aug 2019 10:09:41 +0300 Subject: [PATCH 02/12] chore: modified background-image test --- tests/test-background-image.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test-background-image.php b/tests/test-background-image.php index 6aa4658e..39b94b35 100644 --- a/tests/test-background-image.php +++ b/tests/test-background-image.php @@ -10,7 +10,8 @@ class Test_Background_Image extends WP_UnitTestCase { - const IMG_TAGS_GIF = '
'; + const BACKGROUND_IMG = '
'; + const BACKGROUND_IMG_SECOND = '
'; public static $sample_post; @@ -36,7 +37,7 @@ public function setUp() self::$sample_post = self::factory()->post->create([ 'post_title' => 'Test post', - 'post_content' => self::IMG_TAGS_GIF + 'post_content' => self::BACKGROUND_IMG ] ); } @@ -44,7 +45,9 @@ public function setUp() public function test_should_replace_tag() { - $replaced_content = Optml_Manager::instance()->replace_content(self::IMG_TAGS_GIF); + $replaced_content = Optml_Manager::instance()->replace_content(self::BACKGROUND_IMG); + $this->assertContains('i.optimole.com', $replaced_content); + $replaced_content = Optml_Manager::instance()->replace_content(self::BACKGROUND_IMG_SECOND); $this->assertContains('i.optimole.com', $replaced_content); } } From 1577fcfd1218259dc1875434ae737d72f37ba223 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Thu, 29 Aug 2019 10:15:48 +0300 Subject: [PATCH 03/12] chore: modified background-image test --- tests/test-background-image.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test-background-image.php b/tests/test-background-image.php index 39b94b35..7f2b708c 100644 --- a/tests/test-background-image.php +++ b/tests/test-background-image.php @@ -11,7 +11,7 @@ class Test_Background_Image extends WP_UnitTestCase { const BACKGROUND_IMG = '
'; - const BACKGROUND_IMG_SECOND = '
'; + const BACKGROUND_IMG_SECOND = '
'; public static $sample_post; @@ -49,6 +49,7 @@ public function test_should_replace_tag() $this->assertContains('i.optimole.com', $replaced_content); $replaced_content = Optml_Manager::instance()->replace_content(self::BACKGROUND_IMG_SECOND); $this->assertContains('i.optimole.com', $replaced_content); + $this->assertNotContains('?test=whatever7_whatever', $replaced_content); } } From ffa3d57256616ef53f1bfd47bbf66aa63e69c137 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Thu, 29 Aug 2019 10:26:10 +0300 Subject: [PATCH 04/12] chore: added some checks to background-image test --- tests/test-background-image.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/test-background-image.php b/tests/test-background-image.php index 7f2b708c..bc665912 100644 --- a/tests/test-background-image.php +++ b/tests/test-background-image.php @@ -11,8 +11,9 @@ class Test_Background_Image extends WP_UnitTestCase { const BACKGROUND_IMG = '
'; - const BACKGROUND_IMG_SECOND = '
'; - + const BACKGROUND_IMG_SECOND = '
'; + const SHOULD_NOT_REPLACE = '
'; + const SHOULD_NOT_REPLACE_SECOND = '
'; public static $sample_post; @@ -42,7 +43,7 @@ public function setUp() ); } - public function test_should_replace_tag() + public function test_should_replace_url() { $replaced_content = Optml_Manager::instance()->replace_content(self::BACKGROUND_IMG); @@ -50,6 +51,13 @@ public function test_should_replace_tag() $replaced_content = Optml_Manager::instance()->replace_content(self::BACKGROUND_IMG_SECOND); $this->assertContains('i.optimole.com', $replaced_content); $this->assertNotContains('?test=whatever7_whatever', $replaced_content); + + } + public function test_should_not_replace_url() { + $replaced_content = Optml_Manager::instance()->replace_content(self::SHOULD_NOT_REPLACE); + $this->assertNotContains('i.optimole.com', $replaced_content); + $replaced_content = Optml_Manager::instance()->replace_content(self::SHOULD_NOT_REPLACE_SECOND); + $this->assertNotContains('i.optimole.com', $replaced_content); } } From 9371e22655aa284c6bec53d100bcd8c96e9313e1 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Thu, 29 Aug 2019 11:19:55 +0300 Subject: [PATCH 05/12] chore: modified background-image test --- tests/test-background-image.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-background-image.php b/tests/test-background-image.php index bc665912..01aa8d46 100644 --- a/tests/test-background-image.php +++ b/tests/test-background-image.php @@ -10,8 +10,8 @@ class Test_Background_Image extends WP_UnitTestCase { - const BACKGROUND_IMG = '
'; - const BACKGROUND_IMG_SECOND = '
'; + const BACKGROUND_IMG = '
'; + const BACKGROUND_IMG_SECOND = '
'; const SHOULD_NOT_REPLACE = '
'; const SHOULD_NOT_REPLACE_SECOND = '
'; From 39609b66e5aed3b43c12599d9c627e928a10958a Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Fri, 30 Aug 2019 16:20:48 +0300 Subject: [PATCH 06/12] fix: it now lazyloads urls that have query --- inc/lazyload_replacer.php | 6 ++++- tests/test-lazyload-query.php | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/test-lazyload-query.php diff --git a/inc/lazyload_replacer.php b/inc/lazyload_replacer.php index 32a8cde6..7249b29f 100644 --- a/inc/lazyload_replacer.php +++ b/inc/lazyload_replacer.php @@ -200,8 +200,12 @@ public function can_lazyload_for( $url, $tag = '' ) { if ( false === Optml_Filters::should_do_image( $url, self::$filters[ Optml_Settings::FILTER_TYPE_LAZYLOAD ][ Optml_Settings::FILTER_FILENAME ] ) ) { return false; } + $url = parse_url( $url ); + unset( $url['query'] ); + array_shift( $url ) . '://' . implode( $url ); + $type = wp_check_filetype( - basename( $url ), + basename( $url['path'] ), Optml_Config::$extensions ); diff --git a/tests/test-lazyload-query.php b/tests/test-lazyload-query.php new file mode 100644 index 00000000..e52ee4b4 --- /dev/null +++ b/tests/test-lazyload-query.php @@ -0,0 +1,50 @@ +'; + + + public static $sample_post; + + public function setUp() + { + + parent::setUp(); + $settings = new Optml_Settings(); + $settings->update('service_data', [ + 'cdn_key' => 'test123', + 'cdn_secret' => '12345', + 'whitelist' => ['example.com'], + + ]); + $settings->update('lazyload', 'enabled'); + $settings->update('img_to_video', 'enabled'); + + + Optml_Url_Replacer::instance()->init(); + Optml_Tag_Replacer::instance()->init(); + Optml_Manager::instance()->init(); + + self::$sample_post = self::factory()->post->create([ + 'post_title' => 'Test post', + 'post_content' => self::IMG + ] + ); + } + public function test_should_replace_url() + { + $replaced_content = Optml_Manager::instance()->replace_content(self::IMG); + var_dump($replaced_content); + $this->assertContains('i.optimole.com', $replaced_content); + $this->assertContains('data-opt-src', $replaced_content); + } +} \ No newline at end of file From c901d5f69b26b909b4000699de6274135e70a3de Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Fri, 30 Aug 2019 16:26:16 +0300 Subject: [PATCH 07/12] chore: code style --- tests/test-lazyload-query.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/test-lazyload-query.php b/tests/test-lazyload-query.php index e52ee4b4..181df06f 100644 --- a/tests/test-lazyload-query.php +++ b/tests/test-lazyload-query.php @@ -9,15 +9,11 @@ */ class Test_Lazyload_Query extends WP_UnitTestCase { - const IMG = ''; - - public static $sample_post; public function setUp() { - parent::setUp(); $settings = new Optml_Settings(); $settings->update('service_data', [ @@ -29,7 +25,6 @@ public function setUp() $settings->update('lazyload', 'enabled'); $settings->update('img_to_video', 'enabled'); - Optml_Url_Replacer::instance()->init(); Optml_Tag_Replacer::instance()->init(); Optml_Manager::instance()->init(); @@ -42,9 +37,11 @@ public function setUp() } public function test_should_replace_url() { + $replaced_content = Optml_Manager::instance()->replace_content(self::IMG); - var_dump($replaced_content); $this->assertContains('i.optimole.com', $replaced_content); $this->assertContains('data-opt-src', $replaced_content); + $this->assertContains('example.org', $replaced_content); + } } \ No newline at end of file From ace17960ecf6b617821f95151aab551937faa881 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Fri, 30 Aug 2019 17:08:02 +0300 Subject: [PATCH 08/12] chore: unnused line of code --- inc/lazyload_replacer.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/inc/lazyload_replacer.php b/inc/lazyload_replacer.php index 7249b29f..47f94733 100644 --- a/inc/lazyload_replacer.php +++ b/inc/lazyload_replacer.php @@ -202,8 +202,6 @@ public function can_lazyload_for( $url, $tag = '' ) { } $url = parse_url( $url ); unset( $url['query'] ); - array_shift( $url ) . '://' . implode( $url ); - $type = wp_check_filetype( basename( $url['path'] ), Optml_Config::$extensions From b3eaef8ad73647a1c9f65fa54efd35ab2f887e26 Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Wed, 11 Sep 2019 12:52:27 +0300 Subject: [PATCH 09/12] chore: now using strtok instead of parse_url --- inc/lazyload_replacer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/lazyload_replacer.php b/inc/lazyload_replacer.php index 47f94733..54058c10 100644 --- a/inc/lazyload_replacer.php +++ b/inc/lazyload_replacer.php @@ -200,10 +200,10 @@ public function can_lazyload_for( $url, $tag = '' ) { if ( false === Optml_Filters::should_do_image( $url, self::$filters[ Optml_Settings::FILTER_TYPE_LAZYLOAD ][ Optml_Settings::FILTER_FILENAME ] ) ) { return false; } - $url = parse_url( $url ); - unset( $url['query'] ); + $url = strtok( $url, '?' ); + $type = wp_check_filetype( - basename( $url['path'] ), + basename( $url ), Optml_Config::$extensions ); From 643cdc9dfb2cba1bb587960bd23935a67511ac5d Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Wed, 11 Sep 2019 13:03:02 +0300 Subject: [PATCH 10/12] chore: merged test for lazyload with query url into lazyload test --- tests/test-lazyload-query.php | 47 ----------------------------------- tests/test-lazyload.php | 9 ++++++- 2 files changed, 8 insertions(+), 48 deletions(-) delete mode 100644 tests/test-lazyload-query.php diff --git a/tests/test-lazyload-query.php b/tests/test-lazyload-query.php deleted file mode 100644 index 181df06f..00000000 --- a/tests/test-lazyload-query.php +++ /dev/null @@ -1,47 +0,0 @@ -'; - public static $sample_post; - - public function setUp() - { - parent::setUp(); - $settings = new Optml_Settings(); - $settings->update('service_data', [ - 'cdn_key' => 'test123', - 'cdn_secret' => '12345', - 'whitelist' => ['example.com'], - - ]); - $settings->update('lazyload', 'enabled'); - $settings->update('img_to_video', 'enabled'); - - Optml_Url_Replacer::instance()->init(); - Optml_Tag_Replacer::instance()->init(); - Optml_Manager::instance()->init(); - - self::$sample_post = self::factory()->post->create([ - 'post_title' => 'Test post', - 'post_content' => self::IMG - ] - ); - } - public function test_should_replace_url() - { - - $replaced_content = Optml_Manager::instance()->replace_content(self::IMG); - $this->assertContains('i.optimole.com', $replaced_content); - $this->assertContains('data-opt-src', $replaced_content); - $this->assertContains('example.org', $replaced_content); - - } -} \ No newline at end of file diff --git a/tests/test-lazyload.php b/tests/test-lazyload.php index 93f913fe..af82ad1e 100644 --- a/tests/test-lazyload.php +++ b/tests/test-lazyload.php @@ -254,5 +254,12 @@ public function test_lazyload_json_data_disabled() { $this->assertEquals( 2, substr_count( $replaced_content2, '/http:\/\/example.org' ) ); } - + public function test_should_replace_query_string_url() + { + $content = ''; + $replaced_content = Optml_Manager::instance()->replace_content($content); + $this->assertContains('i.optimole.com', $replaced_content); + $this->assertContains('data-opt-src', $replaced_content); + $this->assertContains('example.org', $replaced_content); + } } From 9de1bb610d42d58c6faa3529bf4f95ba5110846a Mon Sep 17 00:00:00 2001 From: Grigore Mihai Date: Wed, 16 Oct 2019 14:25:11 +0300 Subject: [PATCH 11/12] fix: regex targeting wrong format schema less url's --- inc/manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/manager.php b/inc/manager.php index 9e7886d9..dcb7a325 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -481,7 +481,7 @@ public function process_urls_from_content( $html ) { * @return array */ public function extract_image_urls_from_content( $content ) { - $regex = '/((?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?)|((?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?)/'; + $regex = '/((?:http(?:s?):)(?:[\/\\\\|.|\w|\s\-|@|%|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?)|(? Date: Wed, 16 Oct 2019 17:25:45 +0300 Subject: [PATCH 12/12] fix: url's that appear with schema and then schema less having optimole domain appended twice --- inc/manager.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inc/manager.php b/inc/manager.php index dcb7a325..4d68f096 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -332,7 +332,11 @@ function ( $url ) { ); foreach ( $urls as $origin => $replace ) { - $html = preg_replace( '/(?