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 13 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
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
28 changes: 14 additions & 14 deletions includes/BurstSafetyMode/ResponseHeaderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct() {
*
* @return array
*/
public function parseHeaders() {
public function parse_headers() {

$headers = array();

Expand All @@ -63,10 +63,10 @@ public function parseHeaders() {
* @param string $name Header name
* @param string $value Header value
*/
public function addHeader( string $name, string $value ) {
$this->setHeaders(
public function add_header( string $name, string $value ) {
$this->set_headers(
array_merge(
$this->parseHeaders(),
$this->parse_headers(),
array( $name => $value )
)
);
Expand All @@ -77,38 +77,38 @@ public function addHeader( string $name, string $value ) {
*
* @param string[] $headers Headers to add.
*/
public function addHeaders( array $headers ) {
$headers = array_merge( $this->parseHeaders(), $headers );
$this->setHeaders( $headers );
public function add_headers( array $headers ) {
$headers = array_merge( $this->parse_headers(), $headers );
$this->set_headers( $headers );
}

/**
* Remove a header.
*
* @param string $name Header name
*/
public function removeHeader( $name ) {
$headers = $this->parseHeaders();
public function remove_header( $name ) {
$headers = $this->parse_headers();
unset( $headers[ $name ] );
$this->setHeaders( $headers );
$this->set_headers( $headers );
}

/**
* Remove all headers.
*/
public function removeAllHeaders() {
$this->setHeaders( array() );
public function remove_all_headers() {
$this->set_headers( array() );
}

/**
* Set headers.
*
* @param array $headers Headers to set.
*/
public function setHeaders( array $headers ) {
public function set_headers( array $headers ) {

if ( empty( $headers ) ) {
$this->htaccess->removeContent();
$this->htaccess->remove_content();

return;
}
Expand Down
19 changes: 5 additions & 14 deletions includes/BurstSafetyMode/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,24 @@

use NewfoldLabs\WP\Module\Performance\BurstSafetyMode\Skip404 as BurstSkip404;
use NewfoldLabs\WP\Module\Performance\BurstSafetyMode\Browser as BurstBrowser;
use NewfoldLabs\WP\Module\Performance\CacheTypes\Browser as CacheBrowser;
use NewfoldLabs\WP\Module\Performance\CacheTypes\Skip404 as CacheSkip404;
use NewfoldLabs\WP\Module\Performance\ResponseHeaderManager;

use NewfoldLabs\WP\Module\Performance\Cache\Types\Browser as CacheBrowser;
use NewfoldLabs\WP\Module\Performance\Cache\ResponseHeaderManager;

$newfold_burst_safety_mode = function_exists( 'get_option' ) ? (bool) get_option( 'newfold_burst_safety_mode', false ) : false;
$newfold_cache_level = function_exists( 'newfold_cache_level' ) ? (int) get_option( 'newfold_cache_level', 0 ) : 0;

// Check if Performance feature is enabled and it's necessary reset the cache options
if ( class_exists( 'NewfoldLabs\WP\Module\Performance\PerformanceFeatureHooks' ) ) {
if ( class_exists( 'NewfoldLabs\WP\Module\Performance\Performance' ) ) {
if ( $newfold_burst_safety_mode ) {
$browser = new CacheBrowser();
$browser::maybeAddRules( $newfold_cache_level );

$skip_404_handling = (bool) get_option( 'newfold_skip_404_handling', true );

if ( ! $skip_404_handling ) {
$skip404 = new CacheSkip404();
$skip404::maybeAddRules( false );
}

$response_header_manager = new ResponseHeaderManager();
$response_header_manager->addHeader( 'X-Newfold-Cache-Level', $newfold_cache_level );
$response_header_manager->add_header( 'X-Newfold-Cache-Level', $newfold_cache_level );

delete_option( 'newfold_burst_safety_mode' );
}
} elseif ( ! $newfold_burst_safety_mode ) {
} elseif ( ! $newfold_burst_safety_mode && defined( 'BLUEHOST_PLUGIN_DIR' ) ) {
$files_to_include = array(
'htaccess' => BLUEHOST_PLUGIN_DIR . 'vendor/wp-forge/wp-htaccess-manager/includes/htaccess.php',
'htaccess_functions' => BLUEHOST_PLUGIN_DIR . 'vendor/wp-forge/wp-htaccess-manager/includes/functions.php',
Expand Down
Loading
Loading