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 #875 Add new option to select next-gen format #879

Merged
merged 17 commits into from
Apr 19, 2024
Merged
7 changes: 7 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ p.imagify-section-title.imagify-section-title {
z-index: 2;
-webkit-transition: all .275s;
transition: all .275s;
cursor: pointer;
}

.imagify-inline-options input[type="radio"]:not(:checked) + label:first-of-type,
Expand All @@ -924,6 +925,12 @@ p.imagify-section-title.imagify-section-title {
background: #8BC34A
}

.imagify-inline-options input[type="radio"]:disabled + label {
background: #ccc;
color: #999;
cursor:not-allowed;
}

.imagify-inline-options .imagify-info {
margin-top: 15px;
}
Expand Down
2 changes: 1 addition & 1 deletion assets/css/admin.min.css

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions assets/css/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,22 @@ p.imagify-api-key-invite-title {
align-items: center;
}

.imagify-setting-optim-level {
padding: 8px 0 18px;
}
.imagify-setting-optim-level > p {
margin: 0;
}

.imagify-setting-optim-level .imagify-info {
margin-top: 10px;
}

.imagify-setting-optim-level .imagify-error {
margin: 10px 0 0 0;
}

.imagify-setting-optim-level .imagify-error a {
color: #fff;
}

.imagify-setting-optim-level .imagify-inline-options {
flex-basis: 60%;
flex-grow: 1;
Expand All @@ -141,6 +151,10 @@ p.imagify-api-key-invite-title {
background: #2E3243;
border-radius: 3px;
}
.imagify-setting-optim-level .imagify-inline-options-error {
background: #ccc;
}

.imagify-setting-optim-level .imagify-inline-options label {
display: block !important;
width: 100%;
Expand Down
2 changes: 1 addition & 1 deletion assets/css/options.min.css

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions classes/Bulk/AbstractBulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ function_exists( 'set_time_limit' )
* @return int The number of media.
*/
public function has_optimized_media_without_nextgen() {
$format = 'webp';
$format = get_imagify_option( 'optimization_format' );

if ( get_imagify_option( 'convert_to_avif' ) ) {
$format = 'avif';
if ( 'off' === $format ) {
return 0;
}

return count( $this->get_optimized_media_ids_without_format( $format )['ids'] );
Expand Down
11 changes: 10 additions & 1 deletion classes/Bulk/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,20 @@ public function bulk_get_stats_callback() {
* @return void
*/
public function maybe_generate_missing_nextgen( $old_value, $value ) {
if ( empty( $old_value['convert_to_avif'] ) === empty( $value['convert_to_avif'] ) ) {
if ( ! isset( $old_value['optimization_format'], $value['optimization_format'] ) ) {
return;
}

if ( $old_value['optimization_format'] === $value['optimization_format'] ) {
// Old value = new value so do nothing.
return;
}

if ( 'off' === $value['optimization_format'] ) {
// No need to generate next-gen images.
return;
}

$contexts = $this->get_contexts();
$formats = imagify_nextgen_images_formats();

Expand Down
6 changes: 3 additions & 3 deletions classes/Imagifybeat/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ public function add_options_optimization_status_to_response( $response, $data )

$bulk = Bulk::get_instance();

$format = 'webp';
$format = get_imagify_option( 'optimization_format' );

if ( get_imagify_option( 'convert_to_avif' ) ) {
$format = 'avif';
if ( 'off' === $format ) {
return $response;
}

foreach ( $data[ $imagifybeat_id ] as $context ) {
Expand Down
4 changes: 3 additions & 1 deletion classes/Optimization/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,9 @@ protected function delete_file( string $next_gen_path ) {
* @return string Current format we are targeting.
*/
public function get_current_format() {
return $this->get_option( 'convert_to_avif' ) ? static::AVIF_SUFFIX : static::WEBP_SUFFIX;
$format = get_imagify_option( 'optimization_format' );

return ( 'avif' === $format ) ? static::AVIF_SUFFIX : static::WEBP_SUFFIX;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions classes/Stats/OptimizedMediaWithoutNextGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function maybe_clear_cache_after_optimization( $process, $item ) {
$new_sizes = array_intersect_key( $sizes, $new_sizes );
$size_name = 'full' . $process::WEBP_SUFFIX;

if ( get_imagify_option( 'convert_to_avif' ) ) {
if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
$size_name = 'full' . $process::AVIF_SUFFIX;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public function maybe_clear_cache_after_restoration( $process, $response, $files
$sizes = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
$size_name = 'full' . $process::WEBP_SUFFIX;

if ( get_imagify_option( 'convert_to_avif' ) ) {
if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
$size_name = 'full' . $process::AVIF_SUFFIX;
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public function maybe_clear_cache_on_deletion( $process ) {
$sizes = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
$size_name = 'full' . $process::WEBP_SUFFIX;

if ( get_imagify_option( 'convert_to_avif' ) ) {
if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
$size_name = 'full' . $process::AVIF_SUFFIX;
}

Expand All @@ -198,11 +198,11 @@ public function maybe_clear_cache_on_deletion( $process ) {
* @return void
*/
public function maybe_clear_stat_cache( $old_value, $value ) {
if ( isset( $old_value['convert_to_avif'] ) && isset( $value['convert_to_avif'] ) ) {
if ( ! isset( $old_value['optimization_format'], $value['optimization_format'] ) ) {
return;
}

if ( ! isset( $old_value['convert_to_avif'] ) && ! isset( $value['convert_to_avif'] ) ) {
if ( $old_value['optimization_format'] === $value['optimization_format'] ) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions inc/3rd-party/nextgen-gallery/classes/Bulk/NGG.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function get_optimized_media_ids_without_format( $format ) {
$data_table = DB::get_instance()->get_table_name();
$suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::WEBP_SUFFIX' );

if ( get_imagify_option( 'convert_to_avif' ) ) {
if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
$suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::AVIF_SUFFIX' );
}

Expand Down Expand Up @@ -194,7 +194,7 @@ public function has_optimized_media_without_nextgen() {
$data_table = DB::get_instance()->get_table_name();
$suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::WEBP_SUFFIX' );

if ( get_imagify_option( 'convert_to_avif' ) ) {
if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
$suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::AVIF_SUFFIX' );
}

Expand Down
16 changes: 12 additions & 4 deletions inc/admin/upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function _imagify_new_upgrade( $network_version, $site_version ) {

// 1.8.2
if ( version_compare( $site_version, '1.8.2' ) < 0 ) {
Imagify_Options::get_instance()->set( 'partner_links', 1 );
$options->set( 'partner_links', 1 );
}

// 1.9.6
Expand All @@ -305,12 +305,20 @@ function _imagify_new_upgrade( $network_version, $site_version ) {
}

if ( version_compare( $site_version, '2.0' ) < 0 ) {
Imagify_Options::get_instance()->set( 'optimization_level', 2 );
$options->set( 'optimization_level', 2 );
}

if ( version_compare( $site_version, '2.2' ) < 0 ) {
Imagify_Options::get_instance()->set( 'display_nextgen', Imagify_Options::get_instance()->get( 'display_webp', 0 ) );
Imagify_Options::get_instance()->set( 'display_nextgen_method', Imagify_Options::get_instance()->get( 'display_webp_method' ) );
$options->set( 'display_nextgen', $options->get( 'display_webp', 0 ) );
$options->set( 'display_nextgen_method', $options->get( 'display_webp_method' ) );
}

if ( version_compare( $site_version, '2.2.2', '<' ) ) {
if ( $options->get( 'convert_to_avif' ) ) {
$options->set( 'optimization_format', 'avif' );
} else {
$options->set( 'optimization_format', 'webp' );
}
}
}
add_action( 'imagify_upgrade', '_imagify_new_upgrade', 10, 2 );
Expand Down
13 changes: 8 additions & 5 deletions inc/classes/class-imagify-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Imagify_Options extends Imagify_Abstract_Options {
'partner_links' => 0,
'convert_to_avif' => 0,
'convert_to_webp' => 0,
'optimization_format' => 'webp',
];

/**
Expand Down Expand Up @@ -96,10 +97,6 @@ protected function __construct() {
parent::__construct();
}

/** ----------------------------------------------------------------------------------------- */
/** SANITIZATION, VALIDATION ================================================================ */
/** ----------------------------------------------------------------------------------------- */

/**
* Sanitize and validate an option value. Basic casts have been made.
*
Expand Down Expand Up @@ -127,7 +124,13 @@ public function sanitize_and_validate_value( $key, $value, $default ) {
return $reset_values[ $key ];
}
return $value;

case 'optimization_format':
if ( ! in_array( $value, [ 'off', 'webp', 'avif' ], true ) ) {
// For an invalid value, return the "reset" value.
$reset_values = $this->get_reset_values();
return $reset_values[ $key ];
}
return $value;
case 'auto_optimize':
case 'backup':
case 'lossless':
Expand Down
68 changes: 68 additions & 0 deletions inc/classes/class-imagify-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,74 @@ public function field_radio_list( $args ) {
<?php
}

/**
* Display styled radio list group.
*
* @param array $args Arguments:
* {option_name} string The option name. E.g. 'disallowed-sizes'. Mandatory.
* {values} array List of values to display, in the form of 'value' => 'Label'. Mandatory.
* {attributes} array A list of HTML attributes, as 'attribute' => 'value'.
* {current_value} int|bool USE ONLY WHEN DEALING WITH DATA THAT IS NOT SAVED IN THE PLUGIN OPTIONS. If not provided, the field will automatically get the value from the options.
*
* @return void
*/
public function field_inline_radio_list( $args ) {
$args = array_merge(
[
'option_name' => '',
'values' => [],
'info' => '',
'attributes' => [],
'current_value' => false,
],
$args
);

if ( ! $args['option_name'] || ! $args['values'] ) {
return;
}

if ( is_numeric( $args['current_value'] ) || is_string( $args['current_value'] ) ) {
$current_value = $args['current_value'];
} else {
$current_value = $this->options->get( $args['option_name'] );
}

$option_name_class = sanitize_html_class( $args['option_name'] );
$attributes = array_merge( [
'name' => $this->option_name . '[' . $args['option_name'] . ']',
'id' => 'imagify_' . $option_name_class . '_%s',
'class' => 'imagify-row-radio',
], $args['attributes'] );

$id_attribute = $attributes['id'];
unset( $attributes['id'] );
$args['attributes'] = self::build_attributes( $attributes );
?>
<div class="imagify-setting-optim-level">
<p class="imagify-inline-options imagify-inline-options-<?php echo esc_attr( $args['info_class'] ); ?>">
<?php
foreach ( $args['values'] as $value => $label ) {
$input_id = sprintf( $id_attribute, sanitize_html_class( $value ) );
?>
<input type="radio" value="<?php echo esc_attr( $value ); ?>" id="<?php echo $input_id; ?>"<?php echo $args['attributes']; ?> <?php checked( $current_value, $value ); ?> />
<label for="<?php echo $input_id; ?>" onclick=""><?php echo $label; ?></label>
<?php
}
?>
</p>
<span id="<?php
echo $attributes['aria-describedby'];
?>" class="imagify-<?php echo esc_attr( $args['info_class'] ); ?>">
<span class="dashicons dashicons-info"></span>
<?php
echo $args['info'];
?>
</span>
</div>
<?php
}

/**
* Display a text box.
*
Expand Down
11 changes: 4 additions & 7 deletions inc/common/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,11 @@ function imagify_add_avif_type( $ext2type ) {
* @return array
*/
function imagify_nextgen_images_formats() {
$formats = [
'webp' => 'webp',
];
$value = get_imagify_option( 'optimization_format' );
$formats = [];

if ( get_imagify_option( 'convert_to_avif' ) ) {
$formats['avif'] = 'avif';

unset( $formats['webp'] );
if ( 'off' !== $value ) {
$formats[ $value ] = $value;
}

$default = $formats;
Expand Down
4 changes: 3 additions & 1 deletion inc/functions/admin-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ function get_imagify_attachment_generate_nextgen_versions_link( $process ) {
return '';
}

$format = get_imagify_option( 'optimization_format' );

if (
get_imagify_option( 'convert_to_avif' )
'avif' === $format
&&
'image/avif' === $media->get_mime_type()
) {
Expand Down
Loading
Loading