Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility/smart slider #145

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ 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 = strtok( $url, '?' );

$type = wp_check_filetype(
basename( $url ),
Optml_Config::$extensions
Expand Down
8 changes: 6 additions & 2 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ function ( $url ) {
);

foreach ( $urls as $origin => $replace ) {
$html = preg_replace( '/(?<!\/)' . preg_quote( $origin, '/' ) . '/m', $replace, $html );
if ( substr( $origin, 0, 2 ) === '//' ) {
$html = preg_replace( '/(?<!:)' . preg_quote( $origin, '/' ) . '/m', $replace, $html );
}
else
$html = preg_replace( '/(?<!\/)' . preg_quote( $origin, '/' ) . '/m', $replace, $html );
}

return $html;
Expand Down Expand Up @@ -481,7 +485,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.\-@%\\\\])(?:[\/\\\\|.|\w|\s\-|@|%|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?/';
preg_match_all(
$regex,
$content,
Expand Down
63 changes: 63 additions & 0 deletions tests/test-background-image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* WordPress unit test plugin.
*
* @package Optimole-WP
* @subpackage Tests
* @copyright Copyright (c) 2017, ThemeIsle
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
class Test_Background_Image extends WP_UnitTestCase
{

const BACKGROUND_IMG = '<div data-hash="5c2602bbe76e6f966a846fb661b9d76d" class="n2-ss-slide-background-image" data-blur="0" style="background-image:url("//example.org/wp-content/themes/test/assets/images/header-300x300.png");"> </div>';
const BACKGROUND_IMG_SECOND = '<div data-hash="5c2602bbe76e6f966a846fb661b9d76d" class="n2-ss-slide-background-image" data-blur="0" style="background-image:url("//example.org/wp-content/themes/test/assets/images/header-300x300.png?test=whatever7_whatever");"> </div>';
const SHOULD_NOT_REPLACE = '<div data-hash="5c2602bbe76e6f966a846fb661b9d76d" class="n2-ss-slide-background-image" data-blur="0" style="background-image:url("//example.org/wp-content/themes/test/assets/images/header-300x300");"> </div>';
const SHOULD_NOT_REPLACE_SECOND = '<div data-hash="5c2602bbe76e6f966a846fb661b9d76d" class="n2-ss-slide-background-image" data-blur="0" style="background-image:url("//example.org/wp-content/themes/test/assets/images/header-300x300?test=whatever7_whatever");"> </div>';

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::BACKGROUND_IMG
]
);
}

public function test_should_replace_url()
{

$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);
$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);
}
}

9 changes: 8 additions & 1 deletion tests/test-lazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<img src="https://example.org/photos/814499/pexels-photo-814499.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="">';
$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);
}
}