From 1c00c0cfbc598c91f023ff2730c224d897e53abd Mon Sep 17 00:00:00 2001 From: selul Date: Mon, 24 Jun 2019 18:12:31 +0300 Subject: [PATCH] fix: duplicate requests when generating the lazyload placeholder, using an unified url for every source url, fix #103 --- docker-compose.yml | 2 +- inc/lazyload_replacer.php | 18 +++++++++++++----- inc/url_replacer.php | 12 ++++++++---- phpcs.xml | 3 ++- tests/test-lazyload.php | 23 +++++++++++++++-------- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9ba28459..7673ff34 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,4 +35,4 @@ networks: optml_service_net: driver: overlay external: true - name: optml_service \ No newline at end of file + name: optml_service diff --git a/inc/lazyload_replacer.php b/inc/lazyload_replacer.php index 36c435f6..e9b60419 100644 --- a/inc/lazyload_replacer.php +++ b/inc/lazyload_replacer.php @@ -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 ) { @@ -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 = ''; @@ -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 . ''; } @@ -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. @@ -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. diff --git a/inc/url_replacer.php b/inc/url_replacer.php index cdef7065..f650ac0f 100644 --- a/inc/url_replacer.php +++ b/inc/url_replacer.php @@ -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; @@ -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; } diff --git a/phpcs.xml b/phpcs.xml index cd238d6f..57cef4bb 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -10,6 +10,7 @@ *index.php Gruntfile.js dist + cypress dist/* artifact/* assets/* @@ -50,4 +51,4 @@ 0 - \ No newline at end of file + diff --git a/tests/test-lazyload.php b/tests/test-lazyload.php index 759bfc8e..05f2f9ee 100644 --- a/tests/test-lazyload.php +++ b/tests/test-lazyload.php @@ -61,8 +61,8 @@ public function test_lazyload_json_data_valid() { 'html' => 'How to monetize a blog 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 ); @@ -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 = ' How to monetize a blog'; $replaced_content = Optml_Manager::instance()->replace_content( $text ); @@ -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 = '