Skip to content

Conversation

ineagu
Copy link
Contributor

@ineagu ineagu commented Oct 1, 2025

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:

  1. Disable machine learning compression & save
  2. Ensure the default value is used on the frontend.
  3. Make sure going back to mauto or migrating from an older version works as expected.

Other information:

  • [x ] Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • [ x] Have you successfully ran tests with your changes locally?

@pirate-bot
Copy link
Collaborator

pirate-bot commented Oct 1, 2025

Plugin build for 124434d is ready 🛎️!

Copy link
Contributor

@Copilot Copilot AI left a 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.

Comment on lines 43 to 64
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;
};
Copy link

Copilot AI Oct 1, 2025

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.

Comment on lines 106 to 119
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 );
};
Copy link

Copilot AI Oct 1, 2025

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.

Comment on lines 107 to 137
const updateQuality = value => {
setCanSave( true );
const data = { ...settings };
data.quality = value;
manualQualityRef.current = value;
Copy link

Copilot AI Oct 1, 2025

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':
Copy link

Copilot AI Oct 1, 2025

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.

Suggested change
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 );
Copy link
Contributor

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.

Copy link
Contributor

@Soare-Robert-Daniel Soare-Robert-Daniel left a 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.

Comment on lines 106 to 119
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 );
};
Copy link
Contributor

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.

Copy link
Contributor Author

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?

ineagu and others added 4 commits October 8, 2025 17:21
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.
@ineagu
Copy link
Contributor Author

ineagu commented Oct 8, 2025

works as expected for me

@selul selul merged commit d08e039 into development Oct 9, 2025
11 of 12 checks passed
@selul selul deleted the fix/1516 branch October 9, 2025 07:56
@pirate-bot
Copy link
Collaborator

🎉 This PR is included in version 4.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@pirate-bot pirate-bot added the released Indicate that an issue has been resolved and released in a particular version of the product. label Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Indicate that an issue has been resolved and released in a particular version of the product.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants