| 1 |
/** |
| 2 |
* Solid Performance Settings |
| 3 |
* |
| 4 |
*/ |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import { useState, useEffect } from 'react'; |
| 7 |
import { Card, CardBody, CardHeader, Button, TextareaControl, TextControl, ToggleControl, TabPanel, BaseControl } from '@wordpress/components'; |
| 8 |
import { useDispatch } from '@wordpress/data'; |
| 9 |
import apiFetch from '@wordpress/api-fetch'; |
| 10 |
import SafeParseJSON from './parse-json.js'; |
| 11 |
import Notices from './notices.js'; |
| 12 |
import { store as noticesStore } from '@wordpress/notices'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Import Css |
| 16 |
*/ |
| 17 |
import './editor.scss'; |
| 18 |
const SolidPerformanceSettings = () => { |
| 19 |
const {createErrorNotice, createSuccessNotice} = useDispatch(noticesStore); |
| 20 |
const [ performanceSettings, setPerformanceSettings ] = useState( swspParams.settings ); |
| 21 |
const fetchSettings = async () => { |
| 22 |
const { solid_performance_settings } = await apiFetch({ |
| 23 |
path: '/wp/v2/settings', |
| 24 |
}); |
| 25 |
setPerformanceSettings(solid_performance_settings); |
| 26 |
}; |
| 27 |
useEffect( () => { |
| 28 |
fetchSettings().catch( ( error ) => { |
| 29 |
// Add a notice that the settings api isn't working and changes can't be saved. |
| 30 |
createErrorNotice(__('Error: WordPress settings API not working. Please reload and try again.', 'solid-performance')); |
| 31 |
console.error( error ); |
| 32 |
} ); |
| 33 |
}, [] ); |
| 34 |
const handleSubmit = async ( event ) => { |
| 35 |
event.preventDefault(); |
| 36 |
const { solid_performance_settings } = await apiFetch({ |
| 37 |
path: '/wp/v2/settings', |
| 38 |
method: 'POST', |
| 39 |
data: { |
| 40 |
solid_performance_settings: { |
| 41 |
...performanceSettings |
| 42 |
}, |
| 43 |
}, |
| 44 |
} ).catch( ( error ) => { |
| 45 |
createErrorNotice(__('Error Saving Settings', 'solid-performance'), { |
| 46 |
type: 'snackbar', |
| 47 |
}); |
| 48 |
console.error(error); |
| 49 |
}); |
| 50 |
if (solid_performance_settings) { |
| 51 |
createSuccessNotice(__('Settings Saved', 'solid-performance'), { |
| 52 |
type: 'snackbar', |
| 53 |
}); |
| 54 |
setPerformanceSettings(solid_performance_settings); |
| 55 |
} |
| 56 |
}; |
| 57 |
const handleClearCache = async (event) => { |
| 58 |
await apiFetch({ |
| 59 |
path: '/solid-performance/v1/page/clear', |
| 60 |
method: 'POST', |
| 61 |
}).catch((error) => { |
| 62 |
createErrorNotice(__('Error Saving Settings', 'solid-performance'), { |
| 63 |
type: 'snackbar', |
| 64 |
}); |
| 65 |
console.error(error); |
| 66 |
}); |
| 67 |
// Handle clear cache |
| 68 |
createSuccessNotice(__('Cache Cleared', 'solid-performance'), { |
| 69 |
type: 'snackbar', |
| 70 |
}); |
| 71 |
}; |
| 72 |
const handleAdvancedRegenerate = async (event) => { |
| 73 |
const regenerated = await apiFetch({ |
| 74 |
path: '/solid-performance/v1/page/regenerate', |
| 75 |
method: 'POST', |
| 76 |
}).catch((error) => { |
| 77 |
createErrorNotice(__('Error Regenerating', 'solid-performance'), { |
| 78 |
type: 'snackbar', |
| 79 |
}); |
| 80 |
console.error(error); |
| 81 |
}); |
| 82 |
|
| 83 |
if (regenerated.code !== 'solid_performance_advanced_cache_regenerated') { |
| 84 |
createErrorNotice(__('Error Regenerating', 'solid-performance'), { |
| 85 |
type: 'snackbar', |
| 86 |
}); |
| 87 |
console.error(regenerated); |
| 88 |
} else { |
| 89 |
// Handle advanced cache regeneration |
| 90 |
createSuccessNotice(__('advanced-cache.php Regenerated', 'solid-performance'), { |
| 91 |
type: 'snackbar', |
| 92 |
}); |
| 93 |
|
| 94 |
// Remove our WordPress notice, if it's displayed |
| 95 |
const notice = document.getElementById('solidwp-performance-inactive'); |
| 96 |
if (notice) { |
| 97 |
notice.remove(); |
| 98 |
} |
| 99 |
} |
| 100 |
}; |
| 101 |
const setState = ( newState ) => { |
| 102 |
setPerformanceSettings( { |
| 103 |
...performanceSettings, |
| 104 |
...newState, |
| 105 |
page_cache: { |
| 106 |
...performanceSettings?.page_cache, |
| 107 |
...newState?.page_cache |
| 108 |
}, |
| 109 |
} ); |
| 110 |
}; |
| 111 |
const tabs = [ |
| 112 |
{ |
| 113 |
name: 'basic', |
| 114 |
title: __( 'Basic', 'solid-performance' ), |
| 115 |
className: 'basic-tab', |
| 116 |
}, |
| 117 |
{ |
| 118 |
name: 'advanced', |
| 119 |
title: __( 'Advanced', 'solid-performance' ), |
| 120 |
className: 'advanced-tab', |
| 121 |
}, |
| 122 |
]; |
| 123 |
return ( |
| 124 |
<> |
| 125 |
<h1>{ __( 'Performance Settings', 'solid-performance' ) }</h1> |
| 126 |
<Notices/> |
| 127 |
<form className={'swpsp-settings-form'} onSubmit={ handleSubmit }> |
| 128 |
<TabPanel |
| 129 |
className="swpsp-settings-section-tabs" |
| 130 |
orientation="vertical" |
| 131 |
tabs={ tabs }> |
| 132 |
{ |
| 133 |
( tab ) => { |
| 134 |
switch ( tab.name ) { |
| 135 |
case 'basic': |
| 136 |
return ( |
| 137 |
<Card> |
| 138 |
<CardHeader> |
| 139 |
<header> |
| 140 |
<h2> |
| 141 |
{ __( 'Basic Settings', 'solid-performance' ) } |
| 142 |
</h2> |
| 143 |
{ performanceSettings?.page_cache?.enabled && ( |
| 144 |
<Button variant="secondary" onClick={ handleClearCache }> |
| 145 |
{ __( 'Purge Page Cache', 'solid-performance' ) } |
| 146 |
</Button> |
| 147 |
) } |
| 148 |
<Button |
| 149 |
variant='text' |
| 150 |
href='https://go.solidwp.com/performance-page-caching' |
| 151 |
icon={() => ( |
| 152 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"> |
| 153 |
<path 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> |
| 154 |
</svg> |
| 155 |
)} |
| 156 |
target='_blank' |
| 157 |
size='small' |
| 158 |
showTooltip={true} |
| 159 |
label='View external documentation' |
| 160 |
/> |
| 161 |
</header> |
| 162 |
</CardHeader> |
| 163 |
<CardBody> |
| 164 |
<ToggleControl |
| 165 |
label={__('Enable Page Cache', 'solid-performance')} |
| 166 |
checked={performanceSettings?.page_cache?.enabled || false} |
| 167 |
className={'swpsp-large-toggle'} |
| 168 |
onChange={(value) => setState({ page_cache: { enabled: value } })} |
| 169 |
/> |
| 170 |
<p className='submit'> |
| 171 |
<Button variant="primary" type="submit"> |
| 172 |
{ __( 'Save', 'solid-performance' ) } |
| 173 |
</Button> |
| 174 |
</p> |
| 175 |
</CardBody> |
| 176 |
</Card> |
| 177 |
); |
| 178 |
case 'advanced': |
| 179 |
return ( |
| 180 |
<> |
| 181 |
<Card> |
| 182 |
<CardHeader> |
| 183 |
<header> |
| 184 |
<h2> |
| 185 |
{ __( 'Advanced Settings', 'solid-performance' ) } |
| 186 |
</h2> |
| 187 |
<Button |
| 188 |
variant='text' |
| 189 |
href='https://go.solidwp.com/performance-exclusions' |
| 190 |
icon={() => ( |
| 191 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"> |
| 192 |
<path 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> |
| 193 |
</svg> |
| 194 |
)} |
| 195 |
target='_blank' |
| 196 |
size='small' |
| 197 |
showTooltip={true} |
| 198 |
label='View external documentation' |
| 199 |
/> |
| 200 |
</header> |
| 201 |
</CardHeader> |
| 202 |
<CardBody> |
| 203 |
<TextareaControl |
| 204 |
label={__('Cache Exclusions', 'solid-performance')} |
| 205 |
help={__('Enter URLs for pages, or wildcard exclusion patterns to exclude from the cache, one per line.', 'solid-performance')} |
| 206 |
placeholder={__('/example/*', 'solid-performance')} |
| 207 |
value={performanceSettings?.page_cache?.exclusions?.join("\n") || ''} |
| 208 |
onChange={(value) => setState({ page_cache: { exclusions: value.split("\n") }})} |
| 209 |
/> |
| 210 |
<p className='submit'> |
| 211 |
<Button variant="primary" type="submit"> |
| 212 |
{ __( 'Save', 'solid-performance' ) } |
| 213 |
</Button> |
| 214 |
</p> |
| 215 |
</CardBody> |
| 216 |
</Card> |
| 217 |
<Card> |
| 218 |
<CardHeader> |
| 219 |
<header> |
| 220 |
<h2> |
| 221 |
{ __( 'Debug', 'solid-performance' ) } |
| 222 |
</h2> |
| 223 |
<Button |
| 224 |
variant='text' |
| 225 |
href='https://go.solidwp.com/performance-directory' |
| 226 |
icon={() => ( |
| 227 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"> |
| 228 |
<path 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> |
| 229 |
</svg> |
| 230 |
)} |
| 231 |
target='_blank' |
| 232 |
size='small' |
| 233 |
showTooltip={true} |
| 234 |
label='View external documentation' |
| 235 |
/> |
| 236 |
</header> |
| 237 |
</CardHeader> |
| 238 |
<CardBody> |
| 239 |
<ToggleControl |
| 240 |
label={__('Enable Debug Mode', 'solid-performance')} |
| 241 |
help={__('Enable debug mode to log cache status and performance information.', 'solid-performance')} |
| 242 |
checked={performanceSettings?.page_cache?.debug || false} |
| 243 |
onChange={(value) => setState({ page_cache: { debug: value }})} |
| 244 |
/> |
| 245 |
<TextControl |
| 246 |
label={__('Cache File Directory', 'solid-performance')} |
| 247 |
help={__('The directory where cache files are stored on the server.', 'solid-performance')} |
| 248 |
value={swspParams?.cache_path || ''} |
| 249 |
onChange={() => { }} // Read only |
| 250 |
readOnly |
| 251 |
/> |
| 252 |
<BaseControl> |
| 253 |
<Button variant="secondary" onClick={ handleAdvancedRegenerate }> |
| 254 |
{ __( 'Regenerate the advanced-cache.php file', 'solid-performance' ) } |
| 255 |
</Button> |
| 256 |
</BaseControl> |
| 257 |
<p className='submit'> |
| 258 |
<Button variant="primary" type="submit"> |
| 259 |
{ __( 'Save', 'solid-performance' ) } |
| 260 |
</Button> |
| 261 |
</p> |
| 262 |
</CardBody> |
| 263 |
</Card> |
| 264 |
</> |
| 265 |
); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
</TabPanel> |
| 270 |
</form> |
| 271 |
</> |
| 272 |
); |
| 273 |
}; |
| 274 |
export default SolidPerformanceSettings; |
| 275 |
|