@@ -41,8 +41,14 @@ private static function get_default_settings( $container = null ) {
41
41
'maxRequestsPerMonth ' => 100000 ,
42
42
),
43
43
'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
+ ),
46
52
),
47
53
);
48
54
@@ -159,18 +165,37 @@ private function register_settings() {
159
165
'description ' => __ ( 'Cloudflare-related image optimization options. ' , 'wp-module-performance ' ),
160
166
'properties ' => array (
161
167
'polish ' => array (
162
- 'type ' => 'boolean ' ,
168
+ 'type ' => 'object ' ,
163
169
'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
+ ),
165
181
),
166
182
'mirage ' => array (
167
- 'type ' => 'boolean ' ,
183
+ 'type ' => 'object ' ,
168
184
'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
+ ),
170
196
),
171
197
),
172
198
),
173
-
174
199
),
175
200
'additionalProperties ' => false ,
176
201
),
@@ -220,13 +245,18 @@ public function sanitize_settings( $settings ) {
220
245
'maxRequestsPerMonth ' => isset ( $ settings ['monthly_usage ' ]['maxRequestsPerMonth ' ] ) ? (int ) $ settings ['monthly_usage ' ]['maxRequestsPerMonth ' ] : ( isset ( $ existing_settings ['monthly_usage ' ]['maxRequestsPerMonth ' ] ) ? (int ) $ existing_settings ['monthly_usage ' ]['maxRequestsPerMonth ' ] : 100000 ),
221
246
),
222
247
'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
+ ),
230
260
),
231
261
);
232
262
}
@@ -244,22 +274,49 @@ public static function maybe_refresh_with_capabilities( $capabilities ) {
244
274
$ settings = self ::get_default_settings ();
245
275
}
246
276
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
+ }
257
289
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 ;
259
313
}
260
314
}
315
+
316
+ update_option ( self ::SETTING_KEY , $ settings );
261
317
}
262
318
319
+
263
320
/**
264
321
* Checks if image optimization is enabled.
265
322
*
0 commit comments