-
Notifications
You must be signed in to change notification settings - Fork 12
Improve quality setting handling in Compression settings #978
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Refines compression quality handling: introduces state to preserve manual quality when toggling auto mode, adjusts default/auto mapping to 80 (from 90), and raises the minimum backend quality bound to align with the UI.
- Added ref (manualQualityRef) plus toggle handler to restore prior manual quality after auto mode.
- Changed quality mapping defaults (auto now resolves to 80) and updated backend minimum bound from 1 to 50.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
inc/settings.php | Raised lower quality bound to 50 (was 1) when sanitizing settings. |
assets/src/dashboard/parts/connected/settings/Compression.js | Refactored quality handling: added getQuality(), manual quality preservation via ref, new auto toggle handler, changed default mappings. |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
const getQuality = value => { | ||
if ( 'number' === typeof value ) { | ||
return value; | ||
} | ||
|
||
if ( 'auto' === value ) { | ||
return 80; | ||
} | ||
|
||
if ( 'high_c' === value ) { | ||
return 90; | ||
} | ||
|
||
if ( 'medium_c' === value ) { | ||
return 75; | ||
} | ||
|
||
if ( 'low_c' === value ) { | ||
return 55; | ||
} | ||
|
||
return 80; | ||
}; |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The toggle handler (lines 106-117) writes 'mauto' into data.quality, but getQuality does not recognize 'mauto', only 'auto', so 'mauto' will fall through to the default branch (80) and any logic expecting a distinct auto sentinel may break or become ambiguous. Either change 'mauto' to 'auto' everywhere or add a condition for 'mauto' in getQuality (and any other downstream logic) to keep behavior consistent.
Copilot uses AI. Check for mistakes.
const handleAutoQualityToggle = value => { | ||
setCanSave( true ); | ||
const data = { ...settings }; | ||
data.autoquality = value ? 'enabled' : 'disabled'; | ||
if ( value ) { | ||
manualQualityRef.current = getQuality( settings.quality ); | ||
data.quality = 'mauto'; | ||
} else { | ||
const manualQuality = manualQualityRef.current ?? getQuality( settings.quality ); | ||
manualQualityRef.current = manualQuality; | ||
data.quality = manualQuality; | ||
} | ||
setSettings( data ); | ||
}; |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When returning from auto mode (line 116), data.quality may receive a numeric value (e.g. 90) even if the original pre-toggle setting was an enumerated string like 'high_c'; this changes the type/semantic form of the stored value and could affect UI branches or persistence expecting symbolic presets. Preserve and restore the original raw quality token instead (store original settings.quality before converting) or store both the raw token and numeric resolution separately.
Copilot uses AI. Check for mistakes.
const updateQuality = value => { | ||
setCanSave( true ); | ||
const data = { ...settings }; | ||
data.quality = value; | ||
manualQualityRef.current = value; |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] manualQualityRef.current is set to value without normalization; depending on caller, this could be a preset token or a number, increasing downstream branching complexity. Consider consistently storing the raw selection token plus (optionally) a derived numeric form to avoid mixed-type state and simplify later restoration logic.
Copilot uses AI. Check for mistakes.
case 'limit_width': | ||
$sanitized_value = $this->to_bound_integer( $value, 100, 5000 ); | ||
break; | ||
case 'quality': |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raising the minimum allowed quality from 1 to 50 is a silent tightening of accepted input that will coerce previously valid stored values (<50) up to 50 on next save, potentially surprising users who intentionally chose lower quality for stronger compression. Consider documenting this change inline (comment) and/or providing a migration note or backward-compat handling (e.g., warning, transitional deprecation) to make the behavioral change explicit.
case 'quality': | |
case 'quality': | |
// Minimum allowed quality was raised from 1 to 50. | |
// Any previously stored values <50 will be coerced to 50 on next save. | |
// Consider informing users who intentionally chose lower quality for stronger compression. | |
if (intval($value) < 50) { | |
// Optional: Log a warning or trigger a deprecation notice for legacy values. | |
trigger_error( | |
'The minimum allowed image quality has been raised from 1 to 50. Values below 50 will be set to 50. Please review your settings.', | |
E_USER_DEPRECATED | |
); | |
} |
Copilot uses AI. Check for mistakes.
inc/settings.php
Outdated
break; | ||
case 'quality': | ||
$sanitized_value = $this->to_bound_integer( $value, 1, 100 ); | ||
$sanitized_value = $this->to_bound_integer( $value, 50, 100 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here is the most important thing, the initial issue with value being saved as one is because quality
can be a string, auto
, or number, to which the sanization does not consider the string case, thus the value will be saved as 1
, it is left untouched.
We should handle this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some comments.
const handleAutoQualityToggle = value => { | ||
setCanSave( true ); | ||
const data = { ...settings }; | ||
data.autoquality = value ? 'enabled' : 'disabled'; | ||
if ( value ) { | ||
manualQualityRef.current = getQuality( settings.quality ); | ||
data.quality = 'mauto'; | ||
} else { | ||
const manualQuality = manualQualityRef.current ?? getQuality( settings.quality ); | ||
manualQualityRef.current = manualQuality; | ||
data.quality = manualQuality; | ||
} | ||
setSettings( data ); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this made something a little complicated. If you want to remember the slide value, separated from the settings, you can use another state to remember it and use it when you toggle off. useRef
is used for more tricky things when you manually manage the reference, but I do not think this case to much tricky.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so you suggest handling this exclusively in the backend? when "auto", set the default value?
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.
33ce46b
to
1fe91ed
Compare
works as expected for me |
🎉 This PR is included in version 4.1.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Refactored the Compression.js component to better handle toggling between auto and manual quality settings while fixing https://github.com/Codeinwp/optimole-service/issues/1516#issuecomment-3355204589. Updated the minimum allowed quality value in settings.php from 1 to 50 to stay consistent with the front-end.
Changed default compression setting to 80, which is more likely to be used vs 90, which is not recommended.
All Submissions:
Changes proposed in this Pull Request:
Refactored the Compression.js component to better handle toggling between auto and manual quality settings while fixing https://github.com/Codeinwp/optimole-service/issues/1516#issuecomment-3355204589. Updated the minimum allowed quality value in settings.php from 1 to 50 to stay consistent with the front-end.
Changed default compression setting to 80, which is more likely to be used vs 90, which is not recommended.
Closes # .
How to test the changes in this Pull Request:
Other information: