From c0ce1352169b93ae3b35db726fa8e6c3feebdc0f Mon Sep 17 00:00:00 2001 From: Ashok Patel <37661597+AsFLY-902@users.noreply.github.com> Date: Thu, 7 May 2026 10:54:30 +0530 Subject: [PATCH 1/3] Refactor drop_webp function --- src/utility.cls.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/utility.cls.php b/src/utility.cls.php index ebb9342d7..26c2259a8 100644 --- a/src/utility.cls.php +++ b/src/utility.cls.php @@ -446,10 +446,26 @@ public static function basename( $url ) { * @return string Cleaned filename. */ public static function drop_webp( $filename ) { - if ( in_array( substr( $filename, -5 ), [ '.webp', '.avif' ], true ) ) { - $filename = substr( $filename, 0, -5 ); + $path = (string) wp_parse_url( $filename, PHP_URL_PATH ); + if ( ! $path ) { + $path = $filename; } + $base_ext = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ); + if ( ! in_array( $base_ext, [ 'webp', 'avif' ], true ) ) { + return $filename; + } + + $name_without_ext = pathinfo( $path, PATHINFO_FILENAME ); + $prev_ext = strtolower( pathinfo( $name_without_ext, PATHINFO_EXTENSION ) ); + + // Only drop optimized suffix when a source extension exists, e.g. `.png.webp`. + if ( ! $prev_ext ) { + return $filename; + } + + $filename = substr( $filename, 0, -strlen( $base_ext ) - 1 ); + return $filename; } From 0b7140dc588da8557b6cdbd736a0a4ada77686f2 Mon Sep 17 00:00:00 2001 From: Hai Zheng Date: Thu, 2 Jul 2026 14:26:06 -0400 Subject: [PATCH 2/3] Improve optimized suffix stripping logic Refactor logic to strip optimized suffix based on source image extension. --- src/utility.cls.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/utility.cls.php b/src/utility.cls.php index 26c2259a8..a1f699303 100644 --- a/src/utility.cls.php +++ b/src/utility.cls.php @@ -456,17 +456,14 @@ public static function drop_webp( $filename ) { return $filename; } - $name_without_ext = pathinfo( $path, PATHINFO_FILENAME ); - $prev_ext = strtolower( pathinfo( $name_without_ext, PATHINFO_EXTENSION ) ); - - // Only drop optimized suffix when a source extension exists, e.g. `.png.webp`. - if ( ! $prev_ext ) { + // Only strip the optimized suffix when a real source image extension exists underneath, e.g. `.png.webp`. Native `image.webp` is returned unchanged. + $prev_ext = strtolower( pathinfo( pathinfo( $path, PATHINFO_FILENAME ), PATHINFO_EXTENSION ) ); + if ( ! in_array( $prev_ext, [ 'png', 'jpg', 'jpeg', 'gif', 'bmp' ], true ) ) { return $filename; } - $filename = substr( $filename, 0, -strlen( $base_ext ) - 1 ); - - return $filename; + // Drop the trailing `.webp`/`.avif` suffix now that a source extension underneath is confirmed. + return substr( $filename, 0, -strlen( $base_ext ) - 1 ); } /** From 8e126c69ee7be0f39c0fea012a096be464c5ac5f Mon Sep 17 00:00:00 2001 From: Hai Zheng Date: Thu, 2 Jul 2026 14:28:38 -0400 Subject: [PATCH 3/3] Refactor suffix removal for image filenames Update filename processing to preserve query strings and fragments. --- src/utility.cls.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utility.cls.php b/src/utility.cls.php index a1f699303..56d0b7598 100644 --- a/src/utility.cls.php +++ b/src/utility.cls.php @@ -462,8 +462,8 @@ public static function drop_webp( $filename ) { return $filename; } - // Drop the trailing `.webp`/`.avif` suffix now that a source extension underneath is confirmed. - return substr( $filename, 0, -strlen( $base_ext ) - 1 ); + // Remove the `.webp`/`.avif` suffix only, preserving any query string or fragment (lazyload can pass raw cache-busted `` URLs). + return preg_replace( '~\.' . $base_ext . '(?=$|[?#])~i', '', $filename, 1 ); } /**