Skip to content

Commit effc018

Browse files
committed
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 e40ce70 commit effc018

2 files changed

Lines changed: 64 additions & 37 deletions

File tree

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

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323

2424
import { useSelect } from '@wordpress/data';
2525

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

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

@@ -80,34 +130,11 @@ const Compression = ({
80130
);
81131
};
82132

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

@@ -306,18 +333,18 @@ const Compression = ({
306333
<BaseControl
307334
help={ ! isAutoQualityEnabled && optimoleDashboardApp.strings.options_strings.quality_desc }
308335
>
309-
<ToggleControl
310-
label={ optimoleDashboardApp.strings.options_strings.quality_title }
311-
help={ () => <p dangerouslySetInnerHTML={ { __html: optimoleDashboardApp.strings.options_strings.ml_quality_desc } } /> }
312-
checked={ isAutoQualityEnabled }
313-
disabled={ isLoading }
314-
className={ classnames(
315-
{
316-
'is-disabled': isLoading
317-
}
318-
) }
319-
onChange={ value => updateOption( 'autoquality', value ) }
320-
/>
336+
<ToggleControl
337+
label={ optimoleDashboardApp.strings.options_strings.quality_title }
338+
help={ () => <p dangerouslySetInnerHTML={ { __html: optimoleDashboardApp.strings.options_strings.ml_quality_desc } } /> }
339+
checked={ isAutoQualityEnabled }
340+
disabled={ isLoading }
341+
className={ classnames(
342+
{
343+
'is-disabled': isLoading
344+
}
345+
) }
346+
onChange={ handleAutoQualityToggle }
347+
/>
321348
</BaseControl>
322349

323350
{ ! isAutoQualityEnabled && (

inc/settings.php

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

0 commit comments

Comments
 (0)