Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not serve WebP images that are larger than the original #6123

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions inc/Addon/WebP/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,20 @@

if ( $this->filesystem->exists( $src_path_webp ) ) {
// File name: image.jpg => image.webp.
return preg_replace( '@\.' . $src_url['extension'] . '$@', '.webp', $src_url['src'] ) . $src_url['query'];
$src_url_webp = preg_replace( '@\.' . $src_url['extension'] . '$@', '.webp', $src_url['src'] ) . $src_url['query'];
} elseif ( $this->filesystem->exists( $src_path_webp = $src_path . '.webp' ) ) { // phpcs:ignore Generic.Sniffs.CodeAnalysis.AssignmentInCondition,Squiz.Sniffs.PHP.DisallowMultipleAssignments

Check notice on line 439 in inc/Addon/WebP/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Addon/WebP/Subscriber.php#L439

Assignments must be the first block of code on a line

Check notice on line 439 in inc/Addon/WebP/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Addon/WebP/Subscriber.php#L439

Variable assignment found within a condition. Did you mean to do a comparison?
Copy link
Author

@Roy-Orbison Roy-Orbison Sep 6, 2023

Choose a reason for hiding this comment

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

Assigning within the condition avoids unnecessarily nested if statements. it's not meant to be a comparison.

// File name: image.jpg => image.jpg.webp.
$src_url_webp = $src_url['src'] . '.webp' . $src_url['query'];
} else {
// No webp exists.
return false;
}

if ( $this->filesystem->exists( $src_path . '.webp' ) ) {
// File name: image.jpg => image.jpg.webp.
return $src_url['src'] . '.webp' . $src_url['query'];
if ( $this->filesystem->size( $src_path_webp ) < $this->filesystem->size( $src_path ) ) {
return $src_url_webp;
}

// The webp isn't smaller than the original, so don't use it.
return false;
}

Expand Down