Skip to content

Commit

Permalink
fix: duplicate requests when generating the lazyload placeholder, usi…
Browse files Browse the repository at this point in the history
…ng an unified url for every source url, fix #103
  • Loading branch information
selul committed Jun 24, 2019
1 parent e2f4264 commit 1c00c0c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ networks:
optml_service_net:
driver: overlay
external: true
name: optml_service
name: optml_service
18 changes: 13 additions & 5 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ final class Optml_Lazyload_Replacer extends Optml_App_Replacer {
*
* @codeCoverageIgnore
* @static
* @return Optml_Tag_Replacer
* @since 1.0.0
* @access public
* @return Optml_Tag_Replacer
*/
public static function instance() {
if ( null === self::$instance ) {
Expand Down Expand Up @@ -121,9 +121,16 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
if ( ! $this->can_lazyload_for( $original_url, $full_tag ) ) {
return Optml_Tag_Replacer::instance()->regular_tag_replace( $new_tag, $original_url, $new_url, $optml_args, $is_slashed );
}
$optml_args['quality'] = 'eco';

$low_url = apply_filters( 'optml_content_url', $is_slashed ? stripslashes( $original_url ) : $original_url, $optml_args );
$low_url = apply_filters(
'optml_content_url',
$is_slashed ? stripslashes( $original_url ) : $original_url,
[
'width' => 'auto',
'height' => 'auto',
'quality' => 'eco',
]
);
$low_url = $is_slashed ? addcslashes( $low_url, '/' ) : $low_url;
$opt_format = '';

Expand Down Expand Up @@ -159,6 +166,7 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
if ( ! $this->should_add_noscript( $new_tag ) ) {
return $new_tag;
}

return $new_tag . '<noscript>' . $no_script_tag . '</noscript>';
}

Expand Down Expand Up @@ -254,8 +262,8 @@ public static function get_ignore_noscript_flags() {
*
* @codeCoverageIgnore
* @access public
* @since 1.0.0
* @return void
* @since 1.0.0
*/
public function __clone() {
// Cloning instances of the class is forbidden.
Expand All @@ -267,8 +275,8 @@ public function __clone() {
*
* @codeCoverageIgnore
* @access public
* @since 1.0.0
* @return void
* @since 1.0.0
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
Expand Down
12 changes: 8 additions & 4 deletions inc/url_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function build_image_url(
}
$original_url = $url;

$is_slashed = strpos( $url, '\/' ) !== false;
$is_slashed = strpos( $url, '\/' ) !== false;

$url = $is_slashed ? stripslashes( $url ) : $url;

Expand Down Expand Up @@ -142,12 +142,16 @@ public function build_image_url(
if ( substr( $url, 0, 2 ) === '//' ) {
$url = sprintf( '%s:%s', is_ssl() ? 'https' : 'http', $url );
}

$new_url = $this->strip_image_size_from_url( $url );

if ( $new_url !== $url ) {
list( $args['width'], $args['height'], $crop ) = $this->parse_dimensions_from_filename( $url );
if ( $crop ) {
$args['resize'] = $this->to_optml_crop( $crop );
if ( $args['quality'] !== 'eco' ) {
list( $args['width'], $args['height'], $crop ) = $this->parse_dimensions_from_filename( $url );

if ( $crop ) {
$args['resize'] = $this->to_optml_crop( $crop );
}
}
$url = $new_url;
}
Expand Down
3 changes: 2 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<exclude-pattern>*index.php</exclude-pattern>
<exclude-pattern>Gruntfile.js</exclude-pattern>
<exclude-pattern>dist</exclude-pattern>
<exclude-pattern>cypress</exclude-pattern>
<exclude-pattern>dist/*</exclude-pattern>
<exclude-pattern>artifact/*</exclude-pattern>
<exclude-pattern>assets/*</exclude-pattern>
Expand Down Expand Up @@ -50,4 +51,4 @@
<severity>0</severity>
</rule>

</ruleset>
</ruleset>
23 changes: 15 additions & 8 deletions tests/test-lazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function test_lazyload_json_data_valid() {
'html' => '<a href="http://example.org/blog/how-to-monetize-a-blog/"><img class="alignnone wp-image-36442 size-full" src="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png" alt="How to monetize a blog" width="490" height="256"></a> http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png '
];

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

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

Expand All @@ -75,7 +75,7 @@ public function test_lazyload_json_data_valid() {
}


public function test_lazyload_tag_sanity_check(){
public function test_lazyload_tag_sanity_check() {
$text = ' <a href="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png"><img class="alignnone wp-image-36442 size-full" src="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png" srcset="testsrcset" data-srcset="another" data-plugin-src="http://example.org/wp-content/uploads/2018/06/start-a-blog-1-5.png" alt="How to monetize a blog" width="490" height="256"></a>';

$replaced_content = Optml_Manager::instance()->replace_content( $text );
Expand All @@ -85,8 +85,8 @@ public function test_lazyload_tag_sanity_check(){
$this->assertEquals( 1, substr_count( $replaced_content, 'q:eco' ) );
$this->assertEquals( 2, substr_count( $replaced_content, 'old-srcset' ) );


}

public function test_replacement_with_jetpack_photon() {
$content = '<div class="before-footer">
<div class="codeinwp-container">
Expand All @@ -99,6 +99,7 @@ public function test_replacement_with_jetpack_photon() {
$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertNotContains( 'i0.wp.com', $replaced_content );
}

public function test_replacement_wrong_extension_with_query_string() {
$content = '
<img src="http://example.org/wp-content/plugins/test/generate-qr-code.php?3CRMB6qM1DvLswN6nxKjppX6W5ycjXpeZp">
Expand Down Expand Up @@ -147,8 +148,9 @@ public function test_lazy_load_just_first_header() {

$this->assertEquals( 3, substr_count( $replaced_content, 'data-opt-src' ) );
}

public function test_replacement_lazyload_with_relative_url() {
$content = '<div class="before-footer">
$content = '<div class="before-footer">
<div class="codeinwp-container">
<p class="featuredon">Featured On</p>
<img src="/wp-content/uploads/2018/05/brands.png">
Expand All @@ -160,11 +162,15 @@ public function test_replacement_lazyload_with_relative_url() {
$this->assertContains( 'q:eco', $replaced_content );

}

public function test_lazy_load_preserve_image_size() {
$html = wp_get_attachment_image( self::$sample_attachement, 'sample_size_crop' );
$replaced_content = Optml_Manager::instance()->replace_content( $html );

$this->assertNotEquals( $replaced_content, $html );
$this->assertContains( 'rt:fill/g:ce', $replaced_content );
$this->assertNotContains( 'q:eco/rt:fill/g:ce', $replaced_content );
$this->assertContains( '/rt:fill/g:ce', $replaced_content );
$this->assertContains( 'optimole.com/w:auto/h:auto/q:eco/http://example.org/', $replaced_content );

}

Expand All @@ -181,7 +187,7 @@ public function test_check_with_no_script() {


public function test_replacement_with_data_attr() {
$content = '<div class="before-footer">
$content = '<div class="before-footer">
<div class="codeinwp-container">
<p class="featuredon">Featured On</p>
<img src="http://www.example.org/wp-content/uploads/2018/05/brands.png" data-src="http://www.example.org/wp-content/uploads/2018/05/brands.png">
Expand All @@ -192,6 +198,7 @@ public function test_replacement_with_data_attr() {
$this->assertEquals( 5, substr_count( $replaced_content, 'i.optimole.com' ) );
$this->assertEquals( 1, substr_count( $replaced_content, 'data-opt-src' ) );
}

public function test_check_no_script() {
$replaced_content = Optml_Manager::instance()->process_images_from_content( self::HTML_TAGS_HEADER );

Expand Down Expand Up @@ -234,4 +241,4 @@ public function test_lazyload_json_data_disabled() {
$this->assertEquals( 2, substr_count( $replaced_content2, '/http:\/\/example.org' ) );
}

}
}

0 comments on commit 1c00c0c

Please sign in to comment.