@@ -178,6 +178,27 @@ const ImageOptimization = () => {
178
178
fetchSettings ( ) ;
179
179
} , [ ] ) ;
180
180
181
+ function capabilityKeyExists ( key ) {
182
+ if (
183
+ typeof window . NewfoldRuntime !== 'undefined' &&
184
+ window . NewfoldRuntime . capabilities &&
185
+ Object . prototype . hasOwnProperty . call (
186
+ window . NewfoldRuntime . capabilities ,
187
+ key
188
+ )
189
+ ) {
190
+ return true ;
191
+ }
192
+ return false ;
193
+ }
194
+
195
+ function isCapabilityEnabled ( key ) {
196
+ return (
197
+ capabilityKeyExists ( key ) &&
198
+ window . NewfoldRuntime . capabilities [ key ] === true
199
+ ) ;
200
+ }
201
+
181
202
if ( isLoading ) return < p > { imageOptimizationLoadingMessage } </ p > ;
182
203
183
204
if ( isError )
@@ -216,6 +237,15 @@ const ImageOptimization = () => {
216
237
return `${ window . location . origin } ${ basePath } /wp-admin/upload.php?autoSelectBulk` ;
217
238
} ;
218
239
240
+ const hasPolish = capabilityKeyExists ( 'hasCloudflarePolish' ) ;
241
+ const hasMirage = capabilityKeyExists ( 'hasCloudflareMirage' ) ;
242
+ const polishEnabled = isCapabilityEnabled ( 'hasCloudflarePolish' ) ;
243
+ const mirageEnabled = isCapabilityEnabled ( 'hasCloudflareMirage' ) ;
244
+ const showNothing = ! hasPolish && ! hasMirage ;
245
+ const showUpsell =
246
+ ( hasPolish || hasMirage ) && ! polishEnabled && ! mirageEnabled ;
247
+ const showToggles = polishEnabled || mirageEnabled ;
248
+
219
249
return (
220
250
< Container . SettingsField
221
251
title = { imageOptimizationSettingsTitle }
@@ -347,71 +377,69 @@ const ImageOptimization = () => {
347
377
}
348
378
disabled = { isBanned }
349
379
/>
350
- { ! NewfoldRuntime . hasCapability (
351
- 'hasCloudflarePolish'
352
- ) &&
353
- ! NewfoldRuntime . hasCapability (
354
- 'hasCloudflareMirage'
355
- ) && (
356
- < div >
357
- < FeatureUpsell
358
- cardText = { imageOptimizationUpsellText }
359
- cardLink = { imageOptimizationUpsellLink }
360
- >
361
- < ToggleField
362
- id = "cloudflare-polish"
363
- label = {
364
- imageOptimizationPolishLabel
365
- }
366
- description = {
367
- imageOptimizationPolishDescription
368
- }
369
- checked = { false }
370
- disabled
371
- /> { ' ' }
372
- < ToggleField
373
- id = "cloudflare-mirage"
374
- label = {
375
- imageOptimizationMirageLabel
376
- }
377
- description = {
378
- imageOptimizationMirageDescription
379
- }
380
- checked = { false }
381
- disabled
382
- /> { ' ' }
383
- </ FeatureUpsell >
384
- </ div >
385
- ) }
386
- { NewfoldRuntime . hasCapability (
387
- 'hasCloudflarePolish'
388
- ) && (
389
- < ToggleField
390
- id = "cloudflare-polish"
391
- label = { imageOptimizationPolishLabel }
392
- description = {
393
- imageOptimizationPolishDescription
394
- }
395
- checked = { polish }
396
- onChange = { ( ) =>
397
- handleToggle ( 'cloudflarePolish' , ! polish )
398
- }
399
- />
380
+ { ! showNothing && showUpsell && (
381
+ < div >
382
+ < FeatureUpsell
383
+ cardText = { imageOptimizationUpsellText }
384
+ cardLink = { imageOptimizationUpsellLink }
385
+ >
386
+ < ToggleField
387
+ id = "cloudflare-polish"
388
+ label = { imageOptimizationPolishLabel }
389
+ description = {
390
+ imageOptimizationPolishDescription
391
+ }
392
+ checked = { false }
393
+ disabled
394
+ /> { ' ' }
395
+ < ToggleField
396
+ id = "cloudflare-mirage"
397
+ label = { imageOptimizationMirageLabel }
398
+ description = {
399
+ imageOptimizationMirageDescription
400
+ }
401
+ checked = { false }
402
+ disabled
403
+ /> { ' ' }
404
+ </ FeatureUpsell >
405
+ </ div >
400
406
) }
401
- { NewfoldRuntime . hasCapability (
402
- 'hasCloudflareMirage'
403
- ) && (
404
- < ToggleField
405
- id = "cloudflare-mirage"
406
- label = { imageOptimizationMirageLabel }
407
- description = {
408
- imageOptimizationMirageDescription
409
- }
410
- checked = { mirage }
411
- onChange = { ( ) =>
412
- handleToggle ( 'cloudflareMirage' , ! mirage )
413
- }
414
- />
407
+
408
+ { ! showNothing && showToggles && (
409
+ < >
410
+ { polishEnabled && (
411
+ < ToggleField
412
+ id = "cloudflare-polish"
413
+ label = { imageOptimizationPolishLabel }
414
+ description = {
415
+ imageOptimizationPolishDescription
416
+ }
417
+ checked = { polish }
418
+ onChange = { ( ) =>
419
+ handleToggle (
420
+ 'cloudflarePolish' ,
421
+ ! polish
422
+ )
423
+ }
424
+ />
425
+ ) }
426
+ { mirageEnabled && (
427
+ < ToggleField
428
+ id = "cloudflare-mirage"
429
+ label = { imageOptimizationMirageLabel }
430
+ description = {
431
+ imageOptimizationMirageDescription
432
+ }
433
+ checked = { mirage }
434
+ onChange = { ( ) =>
435
+ handleToggle (
436
+ 'cloudflareMirage' ,
437
+ ! mirage
438
+ )
439
+ }
440
+ />
441
+ ) }
442
+ </ >
415
443
) }
416
444
</ >
417
445
) }
0 commit comments