| 1 |
/** |
| 2 |
* Kadence Performance Settings |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { useState, useEffect } from 'react'; |
| 6 |
import { |
| 7 |
Card, |
| 8 |
CardBody, |
| 9 |
CardHeader, |
| 10 |
Button, |
| 11 |
TextareaControl, |
| 12 |
TextControl, |
| 13 |
ToggleControl, |
| 14 |
TabPanel, |
| 15 |
BaseControl, |
| 16 |
Flex, |
| 17 |
FlexItem |
| 18 |
} from '@wordpress/components'; |
| 19 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 20 |
import apiFetch from '@wordpress/api-fetch'; |
| 21 |
import Notices from './notices.js'; |
| 22 |
import { store as noticesStore } from '@wordpress/notices'; |
| 23 |
import { store as preloadStore } from './preload/store'; |
| 24 |
import { PreloadingProgress } from './preload/components/progress'; |
| 25 |
import { PreloadButton } from './preload/components/button'; |
| 26 |
import { CacheCounter } from './preload/components/cache-counter'; |
| 27 |
import { PageCacheDelivery } from './components/page-cache-delivery'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Import Css |
| 31 |
*/ |
| 32 |
import './editor.scss'; |
| 33 |
|
| 34 |
|
| 35 |
const SolidPerformanceSettings = () => { |
| 36 |
const { createErrorNotice, createSuccessNotice } = useDispatch(noticesStore); |
| 37 |
const [ performanceSettings, setPerformanceSettings ] = useState(swspParams.settings); |
| 38 |
|
| 39 |
/** @type {PreloadState} */ |
| 40 |
const preloadStatus = useSelect(( select ) => select(preloadStore).getPreloadStatus(), []); |
| 41 |
const { isPreloading, cacheCount } = preloadStatus; |
| 42 |
|
| 43 |
/** @type {PreloadStoreCallables} */ |
| 44 |
const storeDispatch = useDispatch(preloadStore); |
| 45 |
const { refreshPreloadStatus } = storeDispatch; |
| 46 |
|
| 47 |
const fetchSettings = async () => { |
| 48 |
try { |
| 49 |
const { solid_performance_settings } = await apiFetch({ |
| 50 |
path: '/wp/v2/settings', |
| 51 |
}); |
| 52 |
setPerformanceSettings(solid_performance_settings); |
| 53 |
} catch (error) { |
| 54 |
createErrorNotice(__('Error: Unable to load settings.', 'solid-performance')); |
| 55 |
console.error(error); |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* Fetch the current preload status and register any notices. |
| 61 |
* |
| 62 |
* @returns {Promise<PreloadState>} |
| 63 |
*/ |
| 64 |
const fetchPreloadStatus = async () => { |
| 65 |
return refreshPreloadStatus(); |
| 66 |
}; |
| 67 |
|
| 68 |
// Initialize settings and preload status. |
| 69 |
useEffect(() => { |
| 70 |
void fetchSettings(); |
| 71 |
void fetchPreloadStatus(); |
| 72 |
}, []); |
| 73 |
|
| 74 |
// Check preloading status more frequently if we are currently preloading. |
| 75 |
useEffect(() => { |
| 76 |
const intervalTime = isPreloading ? 3000 : 8000; |
| 77 |
const interval = setInterval(fetchPreloadStatus, intervalTime); |
| 78 |
|
| 79 |
return () => clearInterval(interval); |
| 80 |
}, [ preloadStatus ]); |
| 81 |
|
| 82 |
const handleSubmit = async ( event ) => { |
| 83 |
event.preventDefault(); |
| 84 |
|
| 85 |
await apiFetch({ |
| 86 |
path: '/wp/v2/settings', |
| 87 |
method: 'POST', |
| 88 |
data: { |
| 89 |
solid_performance_settings: { |
| 90 |
...performanceSettings, |
| 91 |
}, |
| 92 |
}, |
| 93 |
}).then(( result ) => { |
| 94 |
const { solid_performance_settings } = result; |
| 95 |
|
| 96 |
if (solid_performance_settings) { |
| 97 |
createSuccessNotice(__('Settings Saved', 'solid-performance'), { |
| 98 |
type: 'snackbar', |
| 99 |
}); |
| 100 |
setPerformanceSettings(solid_performance_settings); |
| 101 |
} |
| 102 |
}).catch(( error ) => { |
| 103 |
// Refresh the UI state as we likely didn't successfully save the settings. |
| 104 |
fetchSettings(); |
| 105 |
|
| 106 |
createErrorNotice(error.message, { |
| 107 |
type: 'snackbar', |
| 108 |
}); |
| 109 |
|
| 110 |
console.error(error); |
| 111 |
}); |
| 112 |
}; |
| 113 |
const handleClearCache = async () => { |
| 114 |
await apiFetch({ |
| 115 |
path: '/solid-performance/v1/page/clear', |
| 116 |
method: 'POST', |
| 117 |
}).catch(( error ) => { |
| 118 |
createErrorNotice(error.message, { |
| 119 |
type: 'snackbar', |
| 120 |
}); |
| 121 |
console.error(error); |
| 122 |
}); |
| 123 |
// Handle clear cache |
| 124 |
createSuccessNotice(__('Cache Cleared', 'solid-performance'), { |
| 125 |
type: 'snackbar', |
| 126 |
}); |
| 127 |
|
| 128 |
await fetchPreloadStatus(); |
| 129 |
}; |
| 130 |
|
| 131 |
const handleAdvancedRegenerate = async () => { |
| 132 |
const regenerated = await apiFetch({ |
| 133 |
path: '/solid-performance/v1/page/regenerate', |
| 134 |
method: 'POST', |
| 135 |
}).catch(( error ) => { |
| 136 |
createErrorNotice(__('Error Regenerating', 'solid-performance'), { |
| 137 |
type: 'snackbar', |
| 138 |
}); |
| 139 |
console.error(error); |
| 140 |
}); |
| 141 |
|
| 142 |
if (regenerated.code !== 'solid_performance_advanced_cache_regenerated') { |
| 143 |
createErrorNotice(__('Error Regenerating', 'solid-performance'), { |
| 144 |
type: 'snackbar', |
| 145 |
}); |
| 146 |
console.error(regenerated); |
| 147 |
} else { |
| 148 |
// Handle advanced cache regeneration |
| 149 |
createSuccessNotice(__('advanced-cache.php Regenerated', 'solid-performance'), { |
| 150 |
type: 'snackbar', |
| 151 |
}); |
| 152 |
|
| 153 |
// Remove our WordPress notice, if it's displayed |
| 154 |
const notice = document.getElementById('solidwp-performance-inactive'); |
| 155 |
if (notice) { |
| 156 |
notice.remove(); |
| 157 |
} |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
const setState = ( newState ) => { |
| 162 |
setPerformanceSettings({ |
| 163 |
...performanceSettings, |
| 164 |
...newState, |
| 165 |
page_cache: { |
| 166 |
...performanceSettings?.page_cache, |
| 167 |
...newState?.page_cache, |
| 168 |
}, |
| 169 |
}); |
| 170 |
}; |
| 171 |
|
| 172 |
const tabs = [ |
| 173 |
{ |
| 174 |
name: 'basic', |
| 175 |
title: __('Basic', 'solid-performance'), |
| 176 |
className: 'basic-tab', |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: 'advanced', |
| 180 |
title: __('Advanced', 'solid-performance'), |
| 181 |
className: 'advanced-tab', |
| 182 |
}, |
| 183 |
]; |
| 184 |
return ( |
| 185 |
<> |
| 186 |
<Flex align="baseline"> |
| 187 |
<FlexItem> |
| 188 |
<h1>{__('Performance Settings', 'solid-performance')}</h1> |
| 189 |
</FlexItem> |
| 190 |
<FlexItem style={{ paddingRight: '20px' }} className="swpsp-page-count"> |
| 191 |
<CacheCounter count={cacheCount} /> |
| 192 |
</FlexItem> |
| 193 |
</Flex> |
| 194 |
<Notices/> |
| 195 |
<form className={'swpsp-settings-form'} onSubmit={handleSubmit}> |
| 196 |
<TabPanel |
| 197 |
className="swpsp-settings-section-tabs" |
| 198 |
orientation="vertical" |
| 199 |
tabs={tabs}> |
| 200 |
{ |
| 201 |
( tab ) => { |
| 202 |
switch (tab.name) { |
| 203 |
case 'basic': |
| 204 |
return ( |
| 205 |
<Card> |
| 206 |
<CardHeader> |
| 207 |
<header> |
| 208 |
<h2> |
| 209 |
{__('Basic Settings', 'solid-performance')} |
| 210 |
</h2> |
| 211 |
{performanceSettings?.page_cache?.enabled && ( |
| 212 |
<> |
| 213 |
<Button variant="secondary" |
| 214 |
onClick={handleClearCache} |
| 215 |
disabled={isPreloading}> |
| 216 |
{__('Purge Page Cache', 'solid-performance')} |
| 217 |
</Button> |
| 218 |
<PreloadButton |
| 219 |
isPreloading={isPreloading} |
| 220 |
text={__('Preload Uncached Pages', 'solid-performance')} |
| 221 |
force={false} |
| 222 |
hidden={isPreloading} |
| 223 |
label={__( |
| 224 |
'Generates cache files only for URLs that are not already cached', |
| 225 |
'solid-performance' |
| 226 |
)} |
| 227 |
/> |
| 228 |
<PreloadButton |
| 229 |
isPreloading={isPreloading} |
| 230 |
text={__('Preload & Refresh All', 'solid-performance')} |
| 231 |
force={true} |
| 232 |
label={__( |
| 233 |
'Cached URLs are served from the existing cache until each is replaced during the site-wide preloading pass', |
| 234 |
'solid-performance' |
| 235 |
)} |
| 236 |
/> |
| 237 |
</> |
| 238 |
)} |
| 239 |
<Button |
| 240 |
variant="text" |
| 241 |
href="https://go.solidwp.com/performance-page-caching" |
| 242 |
icon={() => ( |
| 243 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 244 |
viewBox="0 0 24 24" |
| 245 |
width="24" height="24" aria-hidden="true" |
| 246 |
focusable="false"> |
| 247 |
<path |
| 248 |
d="M12 4.75a7.25 7.25 0 100 14.5 7.25 7.25 0 000-14.5zM3.25 12a8.75 8.75 0 1117.5 0 8.75 8.75 0 01-17.5 0zM12 8.75a1.5 1.5 0 01.167 2.99c-.465.052-.917.44-.917 1.01V14h1.5v-.845A3 3 0 109 10.25h1.5a1.5 1.5 0 011.5-1.5zM11.25 15v1.5h1.5V15h-1.5z"></path> |
| 249 |
</svg> |
| 250 |
)} |
| 251 |
target="_blank" |
| 252 |
size="small" |
| 253 |
showTooltip={true} |
| 254 |
label="View external documentation" |
| 255 |
/> |
| 256 |
</header> |
| 257 |
</CardHeader> |
| 258 |
<CardBody> |
| 259 |
<PreloadingProgress preloadStatus={preloadStatus} /> |
| 260 |
<ToggleControl |
| 261 |
label={__('Enable Page Cache', 'solid-performance')} |
| 262 |
checked={performanceSettings?.page_cache?.enabled || |
| 263 |
false} |
| 264 |
className={'swpsp-large-toggle'} |
| 265 |
onChange={( value ) => setState( |
| 266 |
{ page_cache: { enabled: value } })} |
| 267 |
/> |
| 268 |
<p className="submit"> |
| 269 |
<Button variant="primary" type="submit"> |
| 270 |
{__('Save', 'solid-performance')} |
| 271 |
</Button> |
| 272 |
</p> |
| 273 |
</CardBody> |
| 274 |
</Card> |
| 275 |
); |
| 276 |
case 'advanced': |
| 277 |
return ( |
| 278 |
<> |
| 279 |
<Card> |
| 280 |
<CardHeader> |
| 281 |
<header> |
| 282 |
<h2> |
| 283 |
{__('Advanced Settings', 'solid-performance')} |
| 284 |
</h2> |
| 285 |
<Button |
| 286 |
variant="text" |
| 287 |
href="https://go.solidwp.com/performance-exclusions" |
| 288 |
icon={() => ( |
| 289 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 290 |
viewBox="0 0 24 24" width="24" |
| 291 |
height="24" |
| 292 |
aria-hidden="true" focusable="false"> |
| 293 |
<path |
| 294 |
d="M12 4.75a7.25 7.25 0 100 14.5 7.25 7.25 0 000-14.5zM3.25 12a8.75 8.75 0 1117.5 0 8.75 8.75 0 01-17.5 0zM12 8.75a1.5 1.5 0 01.167 2.99c-.465.052-.917.44-.917 1.01V14h1.5v-.845A3 3 0 109 10.25h1.5a1.5 1.5 0 011.5-1.5zM11.25 15v1.5h1.5V15h-1.5z"></path> |
| 295 |
</svg> |
| 296 |
)} |
| 297 |
target="_blank" |
| 298 |
size="small" |
| 299 |
showTooltip={true} |
| 300 |
label="View external documentation" |
| 301 |
/> |
| 302 |
</header> |
| 303 |
</CardHeader> |
| 304 |
<CardBody> |
| 305 |
<ToggleControl |
| 306 |
label={__('Lazy Load Images', 'solid-performance')} |
| 307 |
checked={performanceSettings?.page_cache?.lazy_loading?.enabled || |
| 308 |
false} |
| 309 |
help={__('Automatically lazy load inline CSS background images.', 'solid-performance')} |
| 310 |
onChange={( value ) => setState( |
| 311 |
{ |
| 312 |
page_cache: { |
| 313 |
lazy_loading: { |
| 314 |
enabled: value |
| 315 |
} |
| 316 |
}, |
| 317 |
})} |
| 318 |
/> |
| 319 |
<ToggleControl |
| 320 |
label={__('Cloudflare Image Transformation', 'solid-performance')} |
| 321 |
checked={performanceSettings?.page_cache?.image_transformation?.enabled || |
| 322 |
false} |
| 323 |
help={__('Rewrite image URLs to use Cloudflare Images to load image sizes directly from the edge. This needs to be enabled in your Cloudflare dashboard.', 'solid-performance')} |
| 324 |
onChange={( value ) => setState( |
| 325 |
{ |
| 326 |
page_cache: { |
| 327 |
image_transformation: { |
| 328 |
processor: 'cloudflare', // Update this to be dynamic if we add more processors. |
| 329 |
enabled: value |
| 330 |
} |
| 331 |
}, |
| 332 |
})} |
| 333 |
/> |
| 334 |
<ToggleControl |
| 335 |
label={__('Mobile Cache', 'solid-performance')} |
| 336 |
checked={performanceSettings?.page_cache?.mobile_cache?.enabled || |
| 337 |
false} |
| 338 |
help={__('Store separate caches for mobiles devices.', 'solid-performance')} |
| 339 |
onChange={( value ) => setState( |
| 340 |
{ |
| 341 |
page_cache: { |
| 342 |
mobile_cache: { |
| 343 |
enabled: value |
| 344 |
} |
| 345 |
}, |
| 346 |
})} |
| 347 |
/> |
| 348 |
<TextareaControl |
| 349 |
label={__('Cache Exclusions', 'solid-performance')} |
| 350 |
help={__( |
| 351 |
'Enter URLs for pages, or wildcard exclusion patterns to exclude from the cache, one per line. Changes purge the entire cache when using htaccess mode.', |
| 352 |
'solid-performance')} |
| 353 |
placeholder={__('/example/*', 'solid-performance')} |
| 354 |
value={performanceSettings?.page_cache?.exclusions?.join( |
| 355 |
'\n') || ''} |
| 356 |
onChange={( value ) => setState( |
| 357 |
{ |
| 358 |
page_cache: { |
| 359 |
exclusions: value.split('\n'), |
| 360 |
}, |
| 361 |
})} |
| 362 |
/> |
| 363 |
<p className="submit"> |
| 364 |
<Button variant="primary" type="submit"> |
| 365 |
{__('Save', 'solid-performance')} |
| 366 |
</Button> |
| 367 |
</p> |
| 368 |
</CardBody> |
| 369 |
</Card> |
| 370 |
<PageCacheDelivery |
| 371 |
performanceSettings={performanceSettings} |
| 372 |
setState={setState} |
| 373 |
/> |
| 374 |
<Card> |
| 375 |
<CardHeader> |
| 376 |
<header> |
| 377 |
<h2> |
| 378 |
{__('Debug', 'solid-performance')} |
| 379 |
</h2> |
| 380 |
<Button |
| 381 |
variant="text" |
| 382 |
href="https://go.solidwp.com/performance-directory" |
| 383 |
icon={() => ( |
| 384 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 385 |
viewBox="0 0 24 24" width="24" |
| 386 |
height="24" |
| 387 |
aria-hidden="true" focusable="false"> |
| 388 |
<path |
| 389 |
d="M12 4.75a7.25 7.25 0 100 14.5 7.25 7.25 0 000-14.5zM3.25 12a8.75 8.75 0 1117.5 0 8.75 8.75 0 01-17.5 0zM12 8.75a1.5 1.5 0 01.167 2.99c-.465.052-.917.44-.917 1.01V14h1.5v-.845A3 3 0 109 10.25h1.5a1.5 1.5 0 011.5-1.5zM11.25 15v1.5h1.5V15h-1.5z"></path> |
| 390 |
</svg> |
| 391 |
)} |
| 392 |
target="_blank" |
| 393 |
size="small" |
| 394 |
showTooltip={true} |
| 395 |
label="View external documentation" |
| 396 |
/> |
| 397 |
</header> |
| 398 |
</CardHeader> |
| 399 |
<CardBody> |
| 400 |
<ToggleControl |
| 401 |
label={__('Enable Debug Mode', 'solid-performance')} |
| 402 |
help={__( |
| 403 |
'Enable debug mode to log cache status and performance information.', |
| 404 |
'solid-performance')} |
| 405 |
checked={performanceSettings?.page_cache?.debug || |
| 406 |
false} |
| 407 |
onChange={( value ) => setState( |
| 408 |
{ page_cache: { debug: value } })} |
| 409 |
/> |
| 410 |
<TextControl |
| 411 |
label={__('Cache File Directory', |
| 412 |
'solid-performance')} |
| 413 |
help={__( |
| 414 |
'The directory where cache files are stored on the server.', |
| 415 |
'solid-performance')} |
| 416 |
value={performanceSettings?.page_cache?.cache_dir || ''} |
| 417 |
onChange={() => { |
| 418 |
}} // Read only |
| 419 |
readOnly |
| 420 |
/> |
| 421 |
<BaseControl> |
| 422 |
<Button variant="secondary" |
| 423 |
onClick={handleAdvancedRegenerate}> |
| 424 |
{__('Regenerate the advanced-cache.php file', |
| 425 |
'solid-performance')} |
| 426 |
</Button> |
| 427 |
</BaseControl> |
| 428 |
<p className="submit"> |
| 429 |
<Button variant="primary" type="submit"> |
| 430 |
{__('Save', 'solid-performance')} |
| 431 |
</Button> |
| 432 |
</p> |
| 433 |
</CardBody> |
| 434 |
</Card> |
| 435 |
</> |
| 436 |
); |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
</TabPanel> |
| 441 |
</form> |
| 442 |
</> |
| 443 |
); |
| 444 |
}; |
| 445 |
export default SolidPerformanceSettings; |
| 446 |
|