/** * Kadence Performance Settings */ import { __ } from '@wordpress/i18n'; import { useState, useEffect } from 'react'; import { Card, CardBody, CardHeader, Button, TextareaControl, TextControl, ToggleControl, TabPanel, BaseControl, Flex, FlexItem } from '@wordpress/components'; import { useDispatch, useSelect } from '@wordpress/data'; import apiFetch from '@wordpress/api-fetch'; import Notices from './notices.js'; import { store as noticesStore } from '@wordpress/notices'; import { store as preloadStore } from './preload/store'; import { PreloadingProgress } from './preload/components/progress'; import { PreloadButton } from './preload/components/button'; import { CacheCounter } from './preload/components/cache-counter'; import { PageCacheDelivery } from './components/page-cache-delivery'; /** * Import Css */ import './editor.scss'; const SolidPerformanceSettings = () => { const { createErrorNotice, createSuccessNotice } = useDispatch(noticesStore); const [ performanceSettings, setPerformanceSettings ] = useState(swspParams.settings); /** @type {PreloadState} */ const preloadStatus = useSelect(( select ) => select(preloadStore).getPreloadStatus(), []); const { isPreloading, cacheCount } = preloadStatus; /** @type {PreloadStoreCallables} */ const storeDispatch = useDispatch(preloadStore); const { refreshPreloadStatus } = storeDispatch; const fetchSettings = async () => { try { const { solid_performance_settings } = await apiFetch({ path: '/wp/v2/settings', }); setPerformanceSettings(solid_performance_settings); } catch (error) { createErrorNotice(__('Error: Unable to load settings.', 'solid-performance')); console.error(error); } }; /** * Fetch the current preload status and register any notices. * * @returns {Promise} */ const fetchPreloadStatus = async () => { return refreshPreloadStatus(); }; // Initialize settings and preload status. useEffect(() => { void fetchSettings(); void fetchPreloadStatus(); }, []); // Check preloading status more frequently if we are currently preloading. useEffect(() => { const intervalTime = isPreloading ? 3000 : 8000; const interval = setInterval(fetchPreloadStatus, intervalTime); return () => clearInterval(interval); }, [ preloadStatus ]); const handleSubmit = async ( event ) => { event.preventDefault(); await apiFetch({ path: '/wp/v2/settings', method: 'POST', data: { solid_performance_settings: { ...performanceSettings, }, }, }).then(( result ) => { const { solid_performance_settings } = result; if (solid_performance_settings) { createSuccessNotice(__('Settings Saved', 'solid-performance'), { type: 'snackbar', }); setPerformanceSettings(solid_performance_settings); } }).catch(( error ) => { // Refresh the UI state as we likely didn't successfully save the settings. fetchSettings(); createErrorNotice(error.message, { type: 'snackbar', }); console.error(error); }); }; const handleClearCache = async () => { await apiFetch({ path: '/solid-performance/v1/page/clear', method: 'POST', }).catch(( error ) => { createErrorNotice(error.message, { type: 'snackbar', }); console.error(error); }); // Handle clear cache createSuccessNotice(__('Cache Cleared', 'solid-performance'), { type: 'snackbar', }); await fetchPreloadStatus(); }; const handleAdvancedRegenerate = async () => { const regenerated = await apiFetch({ path: '/solid-performance/v1/page/regenerate', method: 'POST', }).catch(( error ) => { createErrorNotice(__('Error Regenerating', 'solid-performance'), { type: 'snackbar', }); console.error(error); }); if (regenerated.code !== 'solid_performance_advanced_cache_regenerated') { createErrorNotice(__('Error Regenerating', 'solid-performance'), { type: 'snackbar', }); console.error(regenerated); } else { // Handle advanced cache regeneration createSuccessNotice(__('advanced-cache.php Regenerated', 'solid-performance'), { type: 'snackbar', }); // Remove our WordPress notice, if it's displayed const notice = document.getElementById('solidwp-performance-inactive'); if (notice) { notice.remove(); } } }; const setState = ( newState ) => { setPerformanceSettings({ ...performanceSettings, ...newState, page_cache: { ...performanceSettings?.page_cache, ...newState?.page_cache, }, }); }; const tabs = [ { name: 'basic', title: __('Basic', 'solid-performance'), className: 'basic-tab', }, { name: 'advanced', title: __('Advanced', 'solid-performance'), className: 'advanced-tab', }, ]; return ( <>

{__('Performance Settings', 'solid-performance')}

{ ( tab ) => { switch (tab.name) { case 'basic': return (

{__('Basic Settings', 'solid-performance')}

{performanceSettings?.page_cache?.enabled && ( <>
setState( { page_cache: { enabled: value } })} />

); case 'advanced': return ( <>

{__('Advanced Settings', 'solid-performance')}

setState( { page_cache: { lazy_loading: { enabled: value } }, })} /> setState( { page_cache: { image_transformation: { processor: 'cloudflare', // Update this to be dynamic if we add more processors. enabled: value } }, })} /> setState( { page_cache: { mobile_cache: { enabled: value } }, })} /> setState( { page_cache: { exclusions: value.split('\n'), }, })} />

{__('Debug', 'solid-performance')}

setState( { page_cache: { debug: value } })} /> { }} // Read only readOnly />

); } } }
); }; export default SolidPerformanceSettings;