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

Closes 2554: Prevent having a webp bigger than the original #751

Merged
merged 17 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions classes/Optimization/Data/AbstractData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class AbstractData implements DataInterface {
*/
protected $default_optimization_data = [
'status' => '',
'message' => '',
'level' => false,
'sizes' => [],
'stats' => [
Expand Down
4 changes: 4 additions & 0 deletions classes/Optimization/Data/CustomFolders.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public function update_size_optimization_data( $size, array $data ) {
$old_data['hash'] = md5_file( $file_path );
}

if ( key_exists( 'message', $data ) ) {
$old_data['message'] = $data['message'];
}

if ( ! $data['success'] ) {
/**
* Error.
Expand Down
5 changes: 5 additions & 0 deletions classes/Optimization/Data/WP.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ public function update_size_optimization_data( $size, array $data ) {
'original_size' => 0,
'optimized_size' => 0,
'percent' => 0,
'message' => '',
], $old_data['stats'] );

if ( key_exists( 'message', $data ) ) {
$old_data['message'] = $data['message'];
}

if ( ! $data['success'] ) {
/**
* Error.
Expand Down
4 changes: 4 additions & 0 deletions classes/Optimization/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ public function optimize( $args = [] ) {
return new \WP_Error( 'temp_file_not_found', $temp_file->get_error_message() );
}

if ( property_exists( $response, 'message' ) ) {
$args['convert'] = '';
}

if ( 'webp' === $args['convert'] ) {
$destination_path = $this->get_path_to_webp();
$this->path = $destination_path;
Expand Down
14 changes: 12 additions & 2 deletions classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ public function optimize_size( $size, $optimization_level = null ) {
'non_webp_file_path' => $sizes[ $thumb_size ]['path'], // Don't use $path nor $file->get_path(), it may return the path to a temporary file.
'optimization_level' => $optimization_level,
] );

if ( property_exists( $response, 'message' ) ) {
$path_is_temp = false;
}
}
}
}
Expand Down Expand Up @@ -731,7 +735,7 @@ protected function compare_webp_file_size( $args ) {
}

// Optimization succeeded.
if ( $args['is_webp'] ) {
if ( ! property_exists( $args['response'], 'message' ) && $args['is_webp'] ) {
/**
* We just created a WebP version:
* Check if it is lighter than the (maybe optimized) non-WebP file.
Expand Down Expand Up @@ -776,7 +780,7 @@ protected function compare_webp_file_size( $args ) {
$webp_size = $args['non_webp_thumb_size'] . static::WEBP_SUFFIX;
$webp_file_size = $this->get_data()->get_size_data( $webp_size, 'optimized_size' );

if ( ! $webp_file_size || $webp_file_size < $args['response']->new_size ) {
if ( property_exists( $args['response'], 'message' ) || ! $webp_file_size || $webp_file_size < $args['response']->new_size ) {
// The WebP file is lighter than this one.
return $args['response'];
}
Expand Down Expand Up @@ -1757,6 +1761,9 @@ public function update_size_optimization_data( $response, $size, $level ) {

// Size data.
$data['success'] = true;
if ( property_exists( $response, 'message' ) ) {
$data['message'] = imagify_translate_api_message( $response->message );
}
$data['original_size'] = $response->original_size;
$data['optimized_size'] = $response->new_size;
}
Expand Down Expand Up @@ -1785,6 +1792,9 @@ public function update_size_optimization_data( $response, $size, $level ) {
*/
$data = (array) apply_filters( "imagify{$_unauthorized}_file_optimization_data", $data, $response, $size, $level, $this->get_data() );

if ( property_exists( $response, 'message' ) ) {
$size = str_replace( '@imagify-webp', '', $size );
}
// Store.
$this->get_data()->update_size_optimization_data( $size, $data );

Expand Down
4 changes: 4 additions & 0 deletions inc/classes/class-imagify-files-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ public function column_optimization( $item ) {
<span class="data"><?php esc_html_e( 'WebP generated:', 'imagify' ); ?></span>
<strong class="data-value"><?php echo esc_html( $has_webp ); ?></strong>
</li>
<li class="imagify-data-item">
<span class="data"><?php esc_html_e( 'WebP generated:', 'imagify' ); ?></span>
<strong class="data-value"><?php echo esc_html( $has_webp ); ?></strong>
</li>
CrochetFeve0251 marked this conversation as resolved.
Show resolved Hide resolved
<?php
}
}
Expand Down
13 changes: 13 additions & 0 deletions inc/classes/class-imagify.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,19 @@ private function curl_http_call( $url, $args = [] ) {
return new WP_Error( 'curl', 'cURL isn\'t installed on the server.' );
}

/**
* Allows to mock Imagify calls to the API.
*
* @param stdClass|null $response Response from the call.
* @param string $url URL from the call.
* @param array $args Arguments from the call.
*/
$response = apply_filters( 'pre_imagify_request', null, $url, $args );

if ( $response ) {
return $response;
}

try {
$url = self::API_ENDPOINT . $url;
$ch = curl_init();
Expand Down
5 changes: 5 additions & 0 deletions inc/functions/admin-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ function get_imagify_attachment_optimization_text( $process ) {
}

$data = $process->get_data();
$optimized_data = $data->get_optimization_data();
$attachment_id = $media->get_id();
$optimization_level = imagify_get_optimization_level_label( $data->get_optimization_level() );

if ( ! $is_media_page ) {
$output .= $output_before . '<span class="data">' . __( 'New Filesize:', 'imagify' ) . '</span> <strong class="big">' . $data->get_optimized_size() . '</strong>' . $output_after;
}

if ( key_exists( 'message', $optimized_data ) ) {
$output .= $output_before . '<span class="data">' . __( 'Convert:', 'imagify' ) . '</span> <strong class="big">' . $optimized_data['message'] . '</strong>' . $output_after;
}

$chart = '';

if ( ! $is_media_page ) {
Expand Down
3 changes: 2 additions & 1 deletion inc/functions/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function get_imagify_max_image_size() {
*/
function imagify_translate_api_message( $message ) {
if ( ! $message ) {
return imagify_translate_api_message( 'Unknown error occurred' );
$message = 'Unknown error occurred';
}

if ( is_wp_error( $message ) ) {
Expand Down Expand Up @@ -199,6 +199,7 @@ function imagify_translate_api_message( $message ) {
'</a>'
),
'Your image is too big to be uploaded on our server' => __( 'Your file is too big to be uploaded on our server.', 'imagify' ),
'Webp is less performant than original' => __( 'Webp is less performant than original', 'imagify' ),
'Our server returned an invalid response' => __( 'Our server returned an invalid response.', 'imagify' ),
'cURL isn\'t installed on the server' => __( 'cURL is not available on the server.', 'imagify' ),
// API messages.
Expand Down
Loading