Skip to content

Commit

Permalink
fix: improve compatibility with W3TC minification system
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Dec 16, 2019
1 parent 4d9219f commit bf9f058
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
3 changes: 2 additions & 1 deletion inc/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Optml_Config {
*
* @var string
*/
public static $chars = '@%A-Za-z-ÁÀȦÂÄǞǍĂĀÃÅǺǼǢĆĊĈČĎḌḐḒÉÈĖÊËĚĔĒẼE̊ẸǴĠĜǦĞG̃ĢĤḤáàȧâäǟǎăāãåǻǽǣćċĉčďḍḑḓéèėêëěĕēẽe̊ẹǵġĝǧğg̃ģĥḥÍÌİÎÏǏĬĪĨỊĴĶǨĹĻĽĿḼM̂M̄ʼNŃN̂ṄN̈ŇN̄ÑŅṊÓÒȮȰÔÖȪǑŎŌÕȬŐỌǾƠíìiîïǐĭīĩịĵķǩĺļľŀḽm̂m̄ʼnńn̂ṅn̈ňn̄ñņṋóòôȯȱöȫǒŏōõȭőọǿơP̄ŔŘŖŚŜṠŠȘṢŤȚṬṰÚÙÛÜǓŬŪŨŰŮỤẂẀŴẄÝỲŶŸȲỸŹŻŽẒǮp̄ŕřŗśŝṡšşṣťțṭṱúùûüǔŭūũűůụẃẁŵẅýỳŷÿȳỹźżžẓǯßœŒçÇ';

public static $chars = '\/:,\\\\.\-\d_@%A-Za-z-ÁÀȦÂÄǞǍĂĀÃÅǺǼǢĆĊĈČĎḌḐḒÉÈĖÊËĚĔĒẼE̊ẸǴĠĜǦĞG̃ĢĤḤáàȧâäǟǎăāãåǻǽǣćċĉčďḍḑḓéèėêëěĕēẽe̊ẹǵġĝǧğg̃ģĥḥÍÌİÎÏǏĬĪĨỊĴĶǨĹĻĽĿḼM̂M̄ʼNŃN̂ṄN̈ŇN̄ÑŅṊÓÒȮȰÔÖȪǑŎŌÕȬŐỌǾƠíìiîïǐĭīĩịĵķǩĺļľŀḽm̂m̄ʼnńn̂ṅn̈ňn̄ñņṋóòôȯȱöȫǒŏōõȭőọǿơP̄ŔŘŖŚŜṠŠȘṢŤȚṬṰÚÙÛÜǓŬŪŨŰŮỤẂẀŴẄÝỲŶŸȲỸŹŻŽẒǮp̄ŕřŗśŝṡšşṣťțṭṱúùûüǔŭūũűůụẃẁŵẅýỳŷÿȳỹźżžẓǯßœŒçÇ';

/**
* Service api key.
Expand Down
10 changes: 6 additions & 4 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ public static function parse_images_from_html( $content ) {
$header_start = $matches[0][1];
$header_end = $header_start + strlen( $matches[0][0] );
}
$regex = '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<noscript\s*>\s*)?<img[^>]*?\s?(?:' . implode( '|', array_merge( [ 'src' ], Optml_Tag_Replacer::possible_src_attributes() ) ) . ')=["\'\\\\]*?(?P<img_url>[' . Optml_Config::$chars . ']{10,}).*?>(?:\s*<\/noscript\s*>)?){1}(?:\s*<\/a>)?/ism';

if ( preg_match_all( '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<noscript\s*>\s*)?<img[^>]*?\s?(?:' . implode( '|', array_merge( [ 'src' ], Optml_Tag_Replacer::possible_src_attributes() ) ) . ')=\\\\?["|\'](?P<img_url>[^\s]+?)["|\'].*?>(?:\s*<\/noscript\s*>)?){1}(?:\s*<\/a>)?/ism', $content, $images, PREG_OFFSET_CAPTURE ) ) {
if ( preg_match_all( $regex, $content, $images, PREG_OFFSET_CAPTURE ) ) {

foreach ( $images as $key => $unused ) {
// Simplify the output as much as possible, mostly for confirming test results.
Expand Down Expand Up @@ -410,7 +411,8 @@ public function process_urls_from_content( $html ) {
* @return array
*/
public function extract_image_urls_from_content( $content ) {
$regex = '/(?:[(|\s\';",])((?:http|\/|\\\\){1}(?:[\/:,\\\\.\-\d_' . Optml_Config::$chars . ']{10,}\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')))(?=(?:|\?|"|&|,|\s|\'|\)|\||\\\\|}))/U';

$regex = '/(?:[(|\s\';",=])((?:http|\/|\\\\){1}(?:[' . Optml_Config::$chars . ']{10,}\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')))(?=(?:|\?|"|&|,|\s|\'|\)|\||\\\\|}))/U';
preg_match_all(
$regex,
$content,
Expand Down Expand Up @@ -473,8 +475,8 @@ function ( $url ) use ( $upload_resource ) {
$is_relative = strpos(
$url,
$is_slashed ?
addcslashes( $upload_resource['content_path'], '/' ) :
$upload_resource['content_path']
addcslashes( $upload_resource['content_path'], '/' ) :
$upload_resource['content_path']
) === 0;
if ( $is_relative ) {
$url = $upload_resource['content_host'] . $url;
Expand Down
13 changes: 13 additions & 0 deletions tests/test-lazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ public function test_replacement_lazyload_with_relative_url() {
$this->assertNotContains( '"/wp-content', $replaced_content );
$this->assertContains( 'q:eco', $replaced_content );

}
public function test_replacement_without_quotes() {
$content = '<div class="before-footer">
<div class="codeinwp-container">
<p class="featuredon">Featured On</p>
<img src=http://example.org/wp-content/uploads/2018/11/gradient.png></div>
<img src=http://example.org/wp-content/uploads/2018/11/gradient.png alt="">
</div>';
$replaced_content = Optml_Manager::instance()->replace_content( $content );
$this->assertEquals( 6, substr_count( $replaced_content, 'i.optimole.com' ) );
$this->assertEquals( 2, substr_count( $replaced_content, 'data-opt-src' ) );
$this->assertContains( '</noscript></div>', $replaced_content);

}

public function test_lazy_load_preserve_image_size() {
Expand Down
12 changes: 11 additions & 1 deletion tests/test-replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function test_replacement_remove_query_arg() {
$replaced_content = Optml_Manager::instance()->replace_content( $content );

$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertNotContains( '?param=123', $replaced_content );
$this->assertContains( '?param=123', $replaced_content );
}

public function test_replacement_with_relative_url() {
Expand All @@ -208,6 +208,16 @@ public function test_replacement_with_relative_url() {

$this->assertContains( 'i.optimole.com', $replaced_content );

}
public function test_replacement_without_quotes() {
$content = '<div >
<p custom-attr=http://example.org/wp-content/uploads/2018/05/brands.png>
</div>';

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

$this->assertContains( 'i.optimole.com', $replaced_content );

}

public function test_replacement_strange_chars() {
Expand Down
2 changes: 1 addition & 1 deletion tests/test-video-disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setUp()
]
);
}
public function test_should_replace_tag_disabled()
public function test_should_replace_disabled()
{
$replaced_content = Optml_Manager::instance()->process_images_from_content(self::IMG_TAGS_GIF);
$this->assertNotContains('<video autoplay muted loop playsinline poster', $replaced_content);
Expand Down
1 change: 1 addition & 0 deletions tests/test-video.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function setUp() {
public function test_should_replace_tag () {

$replaced_content = Optml_Manager::instance()->process_images_from_content( self::IMG_TAGS_GIF );

$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertContains( '<video autoplay muted loop playsinline poster', $replaced_content );
$this->assertContains( 'type="video/mp4', $replaced_content );
Expand Down
2 changes: 1 addition & 1 deletion themeisle-hash.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"optimole-wp.php":"405e35cd1cecc377bd06aa40d3bd3593"}
{"optimole-wp.php":"a264f55269244fed4143b86a263fc6c6"}

0 comments on commit bf9f058

Please sign in to comment.