Skip to content

Commit

Permalink
fix: searching through multiple classes the right way, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoreMihai committed Sep 26, 2019
1 parent 847e90e commit 4751edb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
21 changes: 19 additions & 2 deletions inc/tag_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,27 @@ public function init() {
}

public function contains_banned_lazyload_class( $image_tag ) {
$class_list = strstr( $image_tag, 'class="' );
$tmp_class_list = $class_list;
if ( $class_list === false ) {
return false;
}
foreach ( self::$filters[ Optml_Settings::FILTER_TYPE_LAZYLOAD ][ Optml_Settings::FILTER_CLASS ] as $rule_flag => $status ) {
$regex = '/(class="*' . $rule_flag . '*")/m';
return preg_match( $regex, $image_tag );
$class = strtok( $tmp_class_list, '" ' );
$loop = true;
while ( $class !== false && $loop === true ) {
if ( substr( $class, -1 ) === '"' ) {
$class = substr( $class, 0, strlen( $class ) - 1 );
$loop = false;
}
if ( $class === $rule_flag ) {
return true;
}
$class = strtok( ' ' );
}
$tmp_class_list = $class_list;
}
return false;
}


Expand Down
60 changes: 60 additions & 0 deletions tests/test-lazyload-class-exclusion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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_Generic.
*/
class Test_Lazyload_Class_Exclusion extends WP_UnitTestCase
{

public static $sample_attachement;

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('filters', array(
Optml_Settings::FILTER_TYPE_LAZYLOAD => array (
Optml_Settings::FILTER_CLASS => array (
'test' => true,
'whatever' => true,
'testing' => true,
)
)));
Optml_Url_Replacer::instance()->init();
Optml_Tag_Replacer::instance()->init();
Optml_Lazyload_Replacer::instance()->init();
Optml_Manager::instance()->init();

self::$sample_attachement = self::factory()->attachment->create_upload_object(OPTML_PATH . 'assets/img/logo.png');
}

public function test_class_exclusions()
{
$content = '<div>
<img class="something another test whatever" src="http://example.org/wp-content/uploads/2019/09/Screenshot.png" alt=""/>;
<img class="test" src="http://example.org/wp-content/uploads/2019/09/img.jpg" alt=""/>;
<img class="testing" src="http://example.org/img.png" alt=""/>;
</div>';
$replaced_content = Optml_Manager::instance()->process_images_from_content($content);
var_dump($replaced_content);
$this->assertEquals( 3, substr_count( $replaced_content, 'i.optimole.com' ) );
$this->assertNotContains('data-opt-src', $replaced_content);

}
}

1 comment on commit 4751edb

@GrigoreMihai
Copy link
Contributor Author

@GrigoreMihai GrigoreMihai commented on 4751edb Sep 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@preda-bogdan now it works

Please sign in to comment.