Skip to content

Commit 7ebd339

Browse files
committed
Cleaner updates
1 parent 2f5c911 commit 7ebd339

File tree

4 files changed

+147
-55
lines changed

4 files changed

+147
-55
lines changed

includes/Fonts/FontSettings.php

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class FontSettings {
1919
*/
2020
private $default_settings = array(
2121
'cloudflare' => array(
22-
'fonts' => false,
22+
'fonts' => array(
23+
'value' => false,
24+
'user_set' => false,
25+
),
2326
'last_updated' => 0,
2427
),
2528
);
@@ -31,8 +34,8 @@ class FontSettings {
3134
*/
3235
public function __construct( $container = null ) {
3336
if ( $container && $container->has( 'capabilities' ) ) {
34-
$capabilities = $container->get( 'capabilities' );
35-
$this->default_settings['cloudflare']['fonts'] = (bool) $capabilities->get( 'hasCloudflareFonts' );
37+
$capabilities = $container->get( 'capabilities' );
38+
$this->default_settings['cloudflare']['fonts']['value'] = (bool) $capabilities->get( 'hasCloudflareFonts' );
3639
}
3740

3841
$this->default_settings['cloudflare']['last_updated'] = time();
@@ -62,9 +65,18 @@ private function register_settings() {
6265
'description' => __( 'Cloudflare-related font optimization settings.', 'wp-module-performance' ),
6366
'properties' => array(
6467
'fonts' => array(
65-
'type' => 'boolean',
68+
'type' => 'object',
6669
'description' => __( 'Enable Cloudflare Font Optimization.', 'wp-module-performance' ),
67-
'default' => $this->default_settings['cloudflare']['fonts'],
70+
'properties' => array(
71+
'value' => array(
72+
'type' => 'boolean',
73+
'default' => $this->default_settings['cloudflare']['fonts']['value'],
74+
),
75+
'user_set' => array(
76+
'type' => 'boolean',
77+
'default' => $this->default_settings['cloudflare']['fonts']['user_set'],
78+
),
79+
),
6880
),
6981
'last_updated' => array(
7082
'type' => 'integer',
@@ -99,14 +111,20 @@ private function initialize_settings() {
99111
public function sanitize_settings( $settings ) {
100112
$existing = get_option( self::SETTING_KEY, array() );
101113

102-
$fonts_sanitized = isset( $settings['cloudflare']['fonts'] )
103-
? ! empty( $settings['cloudflare']['fonts'] )
104-
: ( ! empty( $existing['cloudflare']['fonts'] ) );
114+
$fonts_value = isset( $settings['cloudflare']['fonts']['value'] )
115+
? ! empty( $settings['cloudflare']['fonts']['value'] )
116+
: ( ! empty( $existing['cloudflare']['fonts']['value'] ) );
117+
$fonts_user_set = isset( $settings['cloudflare']['fonts']['user_set'] )
118+
? ! empty( $settings['cloudflare']['fonts']['user_set'] )
119+
: ( ! empty( $existing['cloudflare']['fonts']['user_set'] ) );
105120

106121
return array(
107122
'cloudflare' => array(
108-
'fonts' => $fonts_sanitized,
109-
'last_updated' => time(), // ensures value always changes
123+
'fonts' => array(
124+
'value' => $fonts_value,
125+
'user_set' => $fonts_user_set,
126+
),
127+
'last_updated' => time(),
110128
),
111129
);
112130
}
@@ -119,23 +137,27 @@ public function sanitize_settings( $settings ) {
119137
public static function maybe_refresh_with_capabilities( $capabilities ) {
120138
$settings = get_option( self::SETTING_KEY, array() );
121139

122-
// If settings are missing or invalid, fall back to defaults.
123-
if ( empty( $settings ) || ! is_array( $settings ) ) {
124-
$settings = array(
125-
'cloudflare' => array(
126-
'fonts' => false,
127-
'last_updated' => time(),
128-
),
140+
if ( ! isset( $settings['cloudflare']['fonts'] ) ) {
141+
$settings['cloudflare']['fonts'] = array(
142+
'value' => false,
143+
'user_set' => false,
129144
);
130145
}
131146

132-
// Only update if fonts key is not set (i.e. legacy/no-toggle sites).
133-
if ( ! isset( $settings['cloudflare']['fonts'] ) && is_object( $capabilities ) ) {
134-
$settings['cloudflare']['fonts'] = (bool) $capabilities->get( 'hasCloudflareFonts' );
135-
$settings['cloudflare']['last_updated'] = time();
147+
if ( is_object( $capabilities ) ) {
148+
$has_fonts = (bool) $capabilities->get( 'hasCloudflareFonts' );
136149

137-
update_option( self::SETTING_KEY, $settings );
150+
if ( $settings['cloudflare']['fonts']['user_set'] ) {
151+
if ( $settings['cloudflare']['fonts']['value'] && ! $has_fonts ) {
152+
$settings['cloudflare']['fonts']['value'] = false;
153+
}
154+
} elseif ( ! $settings['cloudflare']['fonts']['value'] && $has_fonts ) {
155+
$settings['cloudflare']['fonts']['value'] = true;
156+
}
138157
}
158+
159+
$settings['cloudflare']['last_updated'] = time();
160+
update_option( self::SETTING_KEY, $settings );
139161
}
140162

141163
/**
@@ -144,7 +166,7 @@ public static function maybe_refresh_with_capabilities( $capabilities ) {
144166
* @return bool
145167
*/
146168
public static function is_cloudflare_fonts_enabled() {
147-
$settings = get_option( self::SETTING_KEY, array( 'cloudflare' => array( 'fonts' => false ) ) );
148-
return ! empty( $settings['cloudflare']['fonts'] );
169+
$settings = get_option( self::SETTING_KEY, array( 'cloudflare' => array( 'fonts' => array( 'value' => false ) ) ) );
170+
return ! empty( $settings['cloudflare']['fonts']['value'] );
149171
}
150172
}

includes/Images/ImageSettings.php

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ private static function get_default_settings( $container = null ) {
4141
'maxRequestsPerMonth' => 100000,
4242
),
4343
'cloudflare' => array(
44-
'polish' => false,
45-
'mirage' => false,
44+
'polish' => array(
45+
'value' => false,
46+
'user_set' => false,
47+
),
48+
'mirage' => array(
49+
'value' => false,
50+
'user_set' => false,
51+
),
4652
),
4753
);
4854

@@ -159,18 +165,37 @@ private function register_settings() {
159165
'description' => __( 'Cloudflare-related image optimization options.', 'wp-module-performance' ),
160166
'properties' => array(
161167
'polish' => array(
162-
'type' => 'boolean',
168+
'type' => 'object',
163169
'description' => __( 'Enable Cloudflare Polish optimization.', 'wp-module-performance' ),
164-
'default' => $this->default_settings['cloudflare']['polish'],
170+
'properties' => array(
171+
'value' => array(
172+
'type' => 'boolean',
173+
'default' => $this->default_settings['cloudflare']['polish'],
174+
),
175+
'user_set' => array(
176+
'type' => 'boolean',
177+
'default' => false,
178+
'description' => 'Whether the value was explicitly set by the user.',
179+
),
180+
),
165181
),
166182
'mirage' => array(
167-
'type' => 'boolean',
183+
'type' => 'object',
168184
'description' => __( 'Enable Cloudflare Mirage optimization.', 'wp-module-performance' ),
169-
'default' => $this->default_settings['cloudflare']['mirage'],
185+
'properties' => array(
186+
'value' => array(
187+
'type' => 'boolean',
188+
'default' => $this->default_settings['cloudflare']['mirage'],
189+
),
190+
'user_set' => array(
191+
'type' => 'boolean',
192+
'default' => false,
193+
'description' => 'Whether the value was explicitly set by the user.',
194+
),
195+
),
170196
),
171197
),
172198
),
173-
174199
),
175200
'additionalProperties' => false,
176201
),
@@ -220,13 +245,18 @@ public function sanitize_settings( $settings ) {
220245
'maxRequestsPerMonth' => isset( $settings['monthly_usage']['maxRequestsPerMonth'] ) ? (int) $settings['monthly_usage']['maxRequestsPerMonth'] : ( isset( $existing_settings['monthly_usage']['maxRequestsPerMonth'] ) ? (int) $existing_settings['monthly_usage']['maxRequestsPerMonth'] : 100000 ),
221246
),
222247
'cloudflare' => array(
223-
'polish' => isset( $settings['cloudflare']['polish'] )
224-
? ! empty( $settings['cloudflare']['polish'] )
225-
: ( ! empty( $existing_settings['cloudflare']['polish'] ) ),
226-
227-
'mirage' => isset( $settings['cloudflare']['mirage'] )
228-
? ! empty( $settings['cloudflare']['mirage'] )
229-
: ( ! empty( $existing_settings['cloudflare']['mirage'] ) ),
248+
'polish' => array(
249+
'value' => isset( $settings['cloudflare']['polish'] )
250+
? (bool) $settings['cloudflare']['polish']
251+
: (bool) ( $existing_settings['cloudflare']['polish']['value'] ?? false ),
252+
'user_set' => array_key_exists( 'polish', $settings['cloudflare'] ),
253+
),
254+
'mirage' => array(
255+
'value' => isset( $settings['cloudflare']['mirage'] )
256+
? (bool) $settings['cloudflare']['mirage']
257+
: (bool) ( $existing_settings['cloudflare']['mirage']['value'] ?? false ),
258+
'user_set' => array_key_exists( 'mirage', $settings['cloudflare'] ),
259+
),
230260
),
231261
);
232262
}
@@ -244,22 +274,49 @@ public static function maybe_refresh_with_capabilities( $capabilities ) {
244274
$settings = self::get_default_settings();
245275
}
246276

247-
// Only update if Cloudflare keys are unset (i.e. pre-feature rollout)
248-
if (
249-
! isset( $settings['cloudflare']['polish'] ) &&
250-
! isset( $settings['cloudflare']['mirage'] )
251-
) {
252-
if ( is_object( $capabilities ) ) {
253-
$settings['cloudflare'] = array(
254-
'polish' => (bool) $capabilities->get( 'hasCloudflarePolish' ),
255-
'mirage' => (bool) $capabilities->get( 'hasCloudflareMirage' ),
256-
);
277+
if ( ! isset( $settings['cloudflare']['polish'] ) || ! is_array( $settings['cloudflare']['polish'] ) ) {
278+
$settings['cloudflare']['polish'] = array(
279+
'value' => false,
280+
'user_set' => false,
281+
);
282+
}
283+
if ( ! isset( $settings['cloudflare']['mirage'] ) || ! is_array( $settings['cloudflare']['mirage'] ) ) {
284+
$settings['cloudflare']['mirage'] = array(
285+
'value' => false,
286+
'user_set' => false,
287+
);
288+
}
257289

258-
update_option( self::SETTING_KEY, $settings );
290+
if ( is_object( $capabilities ) ) {
291+
$has_polish = (bool) $capabilities->get( 'hasCloudflarePolish' );
292+
$has_mirage = (bool) $capabilities->get( 'hasCloudflareMirage' );
293+
294+
// Polish
295+
if ( $settings['cloudflare']['polish']['user_set'] ) {
296+
if ( $settings['cloudflare']['polish']['value'] && ! $has_polish ) {
297+
// User enabled but capability is gone — disable it
298+
$settings['cloudflare']['polish']['value'] = false;
299+
}
300+
} elseif ( ! $settings['cloudflare']['polish']['value'] && $has_polish ) {
301+
// Not user set — follow capability
302+
$settings['cloudflare']['polish']['value'] = true;
303+
304+
}
305+
306+
// Mirage
307+
if ( $settings['cloudflare']['mirage']['user_set'] ) {
308+
if ( $settings['cloudflare']['mirage']['value'] && ! $has_mirage ) {
309+
$settings['cloudflare']['mirage']['value'] = false;
310+
}
311+
} elseif ( ! $settings['cloudflare']['mirage']['value'] && $has_mirage ) {
312+
$settings['cloudflare']['mirage']['value'] = true;
259313
}
260314
}
315+
316+
update_option( self::SETTING_KEY, $settings );
261317
}
262318

319+
263320
/**
264321
* Checks if image optimization is enabled.
265322
*

src/sections/FontOptimization/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const FontOptimization = () => {
1818

1919
const { pushNotification } = useDispatch( STORE_NAME );
2020
const apiUrl = NewfoldRuntime.createApiUrl( '/wp/v2/settings' );
21-
const {
21+
const {
2222
fontOptimizationTitle,
2323
fontOptimizationDescription,
2424
fontOptimizationLabel,
@@ -78,7 +78,11 @@ const FontOptimization = () => {
7878
...settings,
7979
cloudflare: {
8080
...( settings.cloudflare || {} ),
81-
fonts: value,
81+
fonts: {
82+
value,
83+
user_set: true,
84+
},
85+
last_updated: Math.floor( Date.now() / 1000 ),
8286
},
8387
};
8488
setSettings( updated );
@@ -95,7 +99,7 @@ const FontOptimization = () => {
9599

96100
if ( ! settings ) return null;
97101

98-
const isEnabled = settings?.cloudflare?.fonts || false;
102+
const isEnabled = settings?.cloudflare?.fonts?.value || false;
99103

100104
const hasCapability = NewfoldRuntime.hasCapability( 'hasCloudflareFonts' );
101105

src/sections/ImageOptimization/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,16 @@ const ImageOptimization = () => {
147147
updated.prefer_optimized_image_when_exists = value;
148148
break;
149149
case 'cloudflarePolish':
150-
updated.cloudflare.polish = value;
150+
updated.cloudflare.polish = {
151+
value,
152+
user_set: true,
153+
};
151154
break;
152155
case 'cloudflareMirage':
153-
updated.cloudflare.mirage = value;
156+
updated.cloudflare.mirage = {
157+
value,
158+
user_set: true,
159+
};
154160
break;
155161
default:
156162
break;
@@ -197,7 +203,10 @@ const ImageOptimization = () => {
197203
cloudflare = {},
198204
} = settings;
199205

200-
const { mirage = false, polish = false } = cloudflare;
206+
const {
207+
mirage: { value: mirage = false } = {},
208+
polish: { value: polish = false } = {},
209+
} = cloudflare;
201210

202211
const { enabled: autoEnabled, auto_delete_original_image: autoDelete } =
203212
auto;

0 commit comments

Comments
 (0)