Skip to content

Commit 8d4376c

Browse files
ineaguSoare-Robert-Daniel
authored andcommitted
Improve quality setting handling in Compression settings
Refactored the Compression.js component to better handle toggling between auto and manual quality settings, preserving the user's manual quality value when switching modes. Updated the minimum allowed quality value in settings.php from 1 to 50 for better image quality control.
1 parent e94b426 commit 8d4376c

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

assets/src/dashboard/parts/connected/settings/Compression.js

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
import { useSelect } from '@wordpress/data';
2525
import { useEffect } from '@wordpress/element';
2626

27+
import { useEffect, useRef } from '@wordpress/element';
28+
2729
/**
2830
* Internal dependencies.
2931
*/
@@ -39,6 +41,31 @@ const Compression = ({
3941
isSampleLoading,
4042
setIsSampleLoading
4143
}) => {
44+
const getQuality = value => {
45+
if ( 'number' === typeof value ) {
46+
return value;
47+
}
48+
49+
if ( 'auto' === value ) {
50+
return 80;
51+
}
52+
53+
if ( 'high_c' === value ) {
54+
return 90;
55+
}
56+
57+
if ( 'medium_c' === value ) {
58+
return 75;
59+
}
60+
61+
if ( 'low_c' === value ) {
62+
return 55;
63+
}
64+
65+
return 80;
66+
};
67+
68+
const manualQualityRef = useRef( getQuality( settings.quality ) );
4269
const {
4370
sampleImages,
4471
isLoading
@@ -60,13 +87,36 @@ const Compression = ({
6087
const isBestFormatEnabled = 'disabled' !== settings[ 'best_format' ];
6188
const compressionMode = settings[ 'compression_mode' ];
6289
const isRetinaEnabled = 'disabled' !== settings[ 'retina_images' ];
90+
91+
useEffect( () => {
92+
if ( isAutoQualityEnabled ) {
93+
return;
94+
}
95+
96+
manualQualityRef.current = getQuality( settings.quality );
97+
}, [ isAutoQualityEnabled, settings.quality ] );
6398
const updateOption = ( option, value ) => {
6499
setCanSave( true );
65100
const data = { ...settings };
66101
data[ option ] = value ? 'enabled' : 'disabled';
67102
setSettings( data );
68103
};
69104

105+
const handleAutoQualityToggle = value => {
106+
setCanSave( true );
107+
const data = { ...settings };
108+
data.autoquality = value ? 'enabled' : 'disabled';
109+
if ( value ) {
110+
manualQualityRef.current = getQuality( settings.quality );
111+
data.quality = 'mauto';
112+
} else {
113+
const manualQuality = manualQualityRef.current ?? getQuality( settings.quality );
114+
manualQualityRef.current = manualQuality;
115+
data.quality = manualQuality;
116+
}
117+
setSettings( data );
118+
};
119+
70120
const loadSample = () => {
71121
setIsSampleLoading( true );
72122

@@ -79,34 +129,11 @@ const Compression = ({
79129
);
80130
};
81131

82-
const getQuality = value => {
83-
if ( 'number' === typeof value ) {
84-
return value;
85-
}
86-
87-
if ( 'auto' === value ) {
88-
return 90;
89-
}
90-
91-
if ( 'high_c' === value ) {
92-
return 90;
93-
}
94-
95-
if ( 'medium_c' === value ) {
96-
return 75;
97-
}
98-
99-
if ( 'low_c' === value ) {
100-
return 55;
101-
}
102-
103-
return 90;
104-
};
105-
106132
const updateQuality = value => {
107133
setCanSave( true );
108134
const data = { ...settings };
109135
data.quality = value;
136+
manualQualityRef.current = value;
110137
setSettings( data );
111138
};
112139

@@ -296,18 +323,18 @@ const Compression = ({
296323
<BaseControl
297324
help={ ! isAutoQualityEnabled && optimoleDashboardApp.strings.options_strings.quality_desc }
298325
>
299-
<ToggleControl
300-
label={ optimoleDashboardApp.strings.options_strings.quality_title }
301-
help={ () => <p dangerouslySetInnerHTML={ { __html: optimoleDashboardApp.strings.options_strings.ml_quality_desc } } /> }
302-
checked={ isAutoQualityEnabled }
303-
disabled={ isLoading }
304-
className={ classnames(
305-
{
306-
'is-disabled': isLoading
307-
}
308-
) }
309-
onChange={ value => updateOption( 'autoquality', value ) }
310-
/>
326+
<ToggleControl
327+
label={ optimoleDashboardApp.strings.options_strings.quality_title }
328+
help={ () => <p dangerouslySetInnerHTML={ { __html: optimoleDashboardApp.strings.options_strings.ml_quality_desc } } /> }
329+
checked={ isAutoQualityEnabled }
330+
disabled={ isLoading }
331+
className={ classnames(
332+
{
333+
'is-disabled': isLoading
334+
}
335+
) }
336+
onChange={ handleAutoQualityToggle }
337+
/>
311338
</BaseControl>
312339

313340
{ ! isAutoQualityEnabled && (

inc/settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function parse_settings( $new_settings ) {
278278
$sanitized_value = $this->to_bound_integer( $value, 100, 5000 );
279279
break;
280280
case 'quality':
281-
$sanitized_value = $this->to_bound_integer( $value, 1, 100 );
281+
$sanitized_value = $this->to_bound_integer( $value, 50, 100 );
282282
break;
283283
case 'wm_id':
284284
$sanitized_value = intval( $value );

0 commit comments

Comments
 (0)