diff --git a/bootstrap.php b/bootstrap.php index f5bfb9c8..5d20b6f0 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -2,6 +2,11 @@ namespace NewfoldLabs\WP\Module\Performance; +use NewfoldLabs\WP\Module\Performance\Cache\CacheFeatureHooks; +use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller; + +use function NewfoldLabs\WP\ModuleLoader\container; + if ( function_exists( 'add_filter' ) ) { add_filter( 'newfold/features/filter/register', @@ -11,6 +16,23 @@ function ( $features ) { ); } -new PerformanceFeatureHooks(); +// Activate Jetpack Boost on fresh installation. +if ( function_exists( 'add_action' ) ) { + add_action( + 'activated_plugin', + function ( $plugin ) { + if ( container()->plugin()->basename === $plugin && + container()->has( 'isFreshInstallation' ) && + container()->get( 'isFreshInstallation' ) && + isset( $_REQUEST['action'] ) && // phpcs:ignore WordPress.Security.NonceVerification + 'activate' === $_REQUEST['action'] // phpcs:ignore WordPress.Security.NonceVerification + ) { + PluginInstaller::install( 'jetpack-boost', true ); + } + } + ); +} + +new CacheFeatureHooks(); require_once __DIR__ . '/includes/BurstSafetyMode/init.php'; diff --git a/components/cacheExclusion/index.js b/components/cacheExclusion/index.js index 74697b07..effed3b2 100644 --- a/components/cacheExclusion/index.js +++ b/components/cacheExclusion/index.js @@ -9,13 +9,16 @@ const CacheExclusion = ( { methods, constants } ) => { const [ isError, setIsError ] = methods.useState( false ); const [ isSaved, setIsSaved ] = methods.useState( false ); const [ currentValue, setCurrentValue ] = methods.useState( - methods.NewfoldRuntime.sdk.cacheExclusion + methods.NewfoldRuntime.sdk.cache.exclusion ); + const [ cacheExclusion, setCacheExclusion ] = methods.useState( - methods.NewfoldRuntime.sdk.cacheExclusion + methods.NewfoldRuntime.sdk.cache.exclusion ); + + const apiUrl = methods.NewfoldRuntime.createApiUrl( - '/newfold-performance/v1/cache-exclusion/update' + '/newfold-performance/v1/cache/settings' ); const handleCacheExclusionChange = ( e ) => { @@ -45,18 +48,17 @@ const CacheExclusion = ( { methods, constants } ) => { }; methods.useUpdateEffect( () => { - methods.setStore( { - ...constants.store, - CacheExclusion: cacheExclusion, - } ); methods.makeNotice( - 'cache-exlusion-notice', + 'cache-exclusion-notice', constants.text.cacheExclusionTitle, ! isError ? constants.text.cacheExclusionSaved : isError, ! isError ? 'success' : 'error', 5000 ); + + setIsSaved( false ); + }, [ isSaved, isError ] ); return ( diff --git a/components/cacheSettings/index.js b/components/cacheSettings/index.js index 383bd46a..9f9b70d4 100644 --- a/components/cacheSettings/index.js +++ b/components/cacheSettings/index.js @@ -3,7 +3,14 @@ import { Container, RadioGroup } from '@newfold/ui-component-library'; const CacheSettings = ( { methods, constants, Components } ) => { const [ cacheLevel, setCacheLevel ] = methods.useState( - constants.store.cacheLevel + methods.NewfoldRuntime.sdk.cache.level + ); + + const { store, setStore } = methods.useContext( methods.AppStore ); + + + const apiUrl = methods.NewfoldRuntime.createApiUrl( + '/newfold-performance/v1/cache/settings' ); const cacheOptions = [ @@ -49,25 +56,31 @@ const CacheSettings = ( { methods, constants, Components } ) => { }; const getCacheLevelNoticeText = () => { - return cacheOptions[ cacheLevel ].notice; + return cacheOptions[ store.cacheLevel ].notice; }; const handleCacheLevelChange = ( e ) => { - methods.newfoldSettingsApiFetch( - { cacheLevel: parseInt( e.target.value ) }, - methods.setError, - () => { + + methods + .apiFetch( { + url: apiUrl, + method: 'POST', + data: { cacheLevel: parseInt( e.target.value ) }, + } ) + .then( () => { setCacheLevel( parseInt( e.target.value ) ); - } - ); + + setStore(prevState => ({ + ...prevState, + cacheLevel: parseInt( e.target.value ), + })) + } ) + .catch( ( error ) => { + methods.setError + } ); }; methods.useUpdateEffect( () => { - methods.setStore( { - ...constants.store, - cacheLevel, - } ); - methods.makeNotice( 'cache-level-change-notice', getCacheLevelNoticeTitle(), @@ -95,7 +108,7 @@ const CacheSettings = ( { methods, constants, Components } ) => { { + + const apiUrl = methods.NewfoldRuntime.createApiUrl( + '/newfold-performance/v1/cache/settings' + ); + + const { store, setStore } = methods.useContext( methods.AppStore ); + const clearCache = () => { - methods.newfoldPurgeCacheApiFetch( - {}, - methods.setError, - ( response ) => { + + + methods + .apiFetch( { + url: apiUrl, + method: 'DELETE', + } ) + .then( () => { methods.makeNotice( 'disable-old-posts-comments-notice', constants.text.clearCacheNoticeTitle, @@ -13,10 +24,15 @@ const ClearCache = ( { methods, constants } ) => { 'success', 5000 ); - } - ); + } ) + .catch( ( error ) => { + methods.setError( error ) + } ); }; + const cacheLevel = store.cacheLevel ?? methods.NewfoldRuntime.sdk.cache.level; + + return ( {