Skip to content

Cache refactoring #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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';
18 changes: 10 additions & 8 deletions components/cacheExclusion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand Down Expand Up @@ -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 (
Expand Down
41 changes: 27 additions & 14 deletions components/cacheSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -95,7 +108,7 @@ const CacheSettings = ( { methods, constants, Components } ) => {
<RadioGroup.Radio
defaultChecked={
option.value ===
constants.store.cacheLevel
methods.NewfoldRuntime.sdk.cache.level
}
id={ 'cache-level-' + option.value }
label={ option.label }
Expand Down
30 changes: 23 additions & 7 deletions components/clearCache/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import { Button, Container } from '@newfold/ui-component-library';

const ClearCache = ( { methods, constants } ) => {

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,
null,
'success',
5000
);
}
);
} )
.catch( ( error ) => {
methods.setError( error )
} );
};

const cacheLevel = store.cacheLevel ?? methods.NewfoldRuntime.sdk.cache.level;


return (
<Container.SettingsField
title={ constants.text.clearCacheTitle }
Expand All @@ -25,7 +41,7 @@ const ClearCache = ( { methods, constants } ) => {
<Button
variant="secondary"
className="clear-cache-button"
disabled={ constants.store.cacheLevel > 0 ? false : true }
disabled={ cacheLevel > 0 ? false : true }
onClick={ clearCache }
>
{ constants.text.clearCacheButton }
Expand Down
9 changes: 6 additions & 3 deletions components/linkPrefetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const LinkPrefetch = ( { methods, constants } ) => {
const [ settings, setSettings ] = methods.useState(
methods.NewfoldRuntime.sdk.linkPrefetch.settings
);
const { store, setStore } = methods.useContext( methods.AppStore );

const [ ignoreKeywords, setIgnoreKeywords ] = methods.useState(
settings.ignoreKeywords
);
Expand Down Expand Up @@ -45,10 +47,11 @@ const LinkPrefetch = ( { methods, constants } ) => {
};

methods.useUpdateEffect( () => {
methods.setStore( {
...constants.store,

setStore(prevState => ({
...prevState,
linkPrefetch: settings,
} );
}))

methods.makeNotice(
'link-prefetch-change-notice',
Expand Down
5 changes: 3 additions & 2 deletions components/performance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { default as LinkPrefetch } from '../linkPrefetch/';
* @param {*} props
* @return
*/
const Performance = ( { methods, constants, Components, ...props } ) => {
const Performance = ( { constants, methods, Components, ...props } ) => {
const { store, setStore } = methods.useContext( methods.AppStore );

const [ isError, setError ] = methods.useState( false );

const notify = methods.useNotification();
Expand All @@ -41,7 +42,7 @@ const Performance = ( { methods, constants, Components, ...props } ) => {
autoDismiss: duration,
} );
};
constants.store = store;

methods.makeNotice = makeNotice;
methods.setStore = setStore;
methods.setError = setError;
Expand Down
4 changes: 2 additions & 2 deletions components/skip404/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NewfoldRuntime } from '@newfold/wp-module-runtime';

const Skip404 = ( { methods, constants } ) => {
const [ skip404, setSkip404 ] = methods.useState(
NewfoldRuntime.sdk.performance.skip404
NewfoldRuntime.sdk.skip404.is_active
);

const getSkip404NoticeTitle = () => {
Expand All @@ -17,7 +17,7 @@ const Skip404 = ( { methods, constants } ) => {
const handleSkip404Change = () => {
const value = ! skip404;
apiFetch( {
path: 'newfold-performance/v1/settings',
path: 'newfold-performance/v1/skip404',
method: 'POST',
data: {
field: {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"newfold-labs/wp-module-installer": "^1.4.3"
},
"require-dev": {
"newfold-labs/wp-php-standards": "^1.2.5",
"newfold-labs/wp-php-standards": "^1.2.4",
"wp-cli/wp-cli-bundle": "^2.11",
"wp-cli/i18n-command": "^2.6",
"10up/wp_mock": "^1.1"
Expand Down
29 changes: 12 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions includes/BurstSafetyMode/Browser.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace NewfoldLabs\WP\Module\Performance\BurstSafetyMode;

use NewfoldLabs\WP\Module\Performance\BurstSafetyMode\ResponseHeaderManager;
use WP_Forge\WP_Htaccess_Manager\htaccess;

/**
Expand All @@ -20,7 +19,7 @@ class Browser {
*/
public function __construct() {
$responseHeaderManager = new ResponseHeaderManager();
$responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', BURST_SAFETY_CACHE_LEVEL );
$responseHeaderManager->add_header( 'X-Newfold-Cache-Level', BURST_SAFETY_CACHE_LEVEL );
$this->addRules();
}

Expand Down
Loading
Loading