From 91a1fcac746f8c783f775dadf580c16df5cdf7a7 Mon Sep 17 00:00:00 2001
From: Armando Liccardo
Date: Wed, 6 Nov 2024 13:27:21 +0000
Subject: [PATCH 01/42] possible cache fix on option update
---
includes/Performance.php | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/includes/Performance.php b/includes/Performance.php
index ac50c2f..8d2c9c2 100644
--- a/includes/Performance.php
+++ b/includes/Performance.php
@@ -200,7 +200,11 @@ public function onCacheLevelChange( $cacheLevel ) {
*/
$responseHeaderManager = $this->container->get( 'responseHeaderManager' );
$responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', absint( $cacheLevel ) );
-
+
+ Browser::maybeAddRules( $cacheLevel );
+ File::maybeAddRules( $cacheLevel );
+ Skip404::maybeAddRules( $cacheLevel );
+
// Remove the old option from EPC, if it exists
if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'endurance_cache_level', 0 ) ) ) {
update_option( 'endurance_cache_level', 0 );
From b3a6f4992a0ad09a8638af3b03c627d15e7b6d39 Mon Sep 17 00:00:00 2001
From: Armando Liccardo
Date: Wed, 6 Nov 2024 14:10:21 +0000
Subject: [PATCH 02/42] added restApi class to update the option
---
includes/Performance.php | 4 +-
includes/RestApi/CacheSettings.php | 112 +++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 2 deletions(-)
create mode 100644 includes/RestApi/CacheSettings.php
diff --git a/includes/Performance.php b/includes/Performance.php
index 8d2c9c2..4fa5d13 100644
--- a/includes/Performance.php
+++ b/includes/Performance.php
@@ -200,11 +200,11 @@ public function onCacheLevelChange( $cacheLevel ) {
*/
$responseHeaderManager = $this->container->get( 'responseHeaderManager' );
$responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', absint( $cacheLevel ) );
-
+
Browser::maybeAddRules( $cacheLevel );
File::maybeAddRules( $cacheLevel );
Skip404::maybeAddRules( $cacheLevel );
-
+
// Remove the old option from EPC, if it exists
if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'endurance_cache_level', 0 ) ) ) {
update_option( 'endurance_cache_level', 0 );
diff --git a/includes/RestApi/CacheSettings.php b/includes/RestApi/CacheSettings.php
new file mode 100644
index 0000000..894de2a
--- /dev/null
+++ b/includes/RestApi/CacheSettings.php
@@ -0,0 +1,112 @@
+container = $container;
+ }
+
+ /**
+ * Registers rest routes for PluginsController class.
+ *
+ * @return void
+ */
+ public function register_routes() {
+ \register_rest_route(
+ $this->namespace,
+ $this->rest_base . '/settings',
+ array(
+ array(
+ 'methods' => \WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_settings' ),
+ 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
+ ),
+ )
+ );
+ \register_rest_route(
+ $this->namespace,
+ $this->rest_base . '/update',
+ array(
+ array(
+ 'methods' => \WP_REST_Server::CREATABLE,
+ 'callback' => array( $this, 'update_settings' ),
+ 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
+ ),
+ )
+ );
+ }
+
+ /**
+ * Get the settings
+ *
+ * @return \WP_REST_Response
+ */
+ public function get_settings() {
+ return new \WP_REST_Response(
+ array(
+ 'settings' => get_option( 'cache_exlusion', '' ),
+ ),
+ 200
+ );
+ }
+
+ /**
+ * Update the settings
+ *
+ * @param \WP_REST_Request $request the request.
+ * @return \WP_REST_Response
+ */
+ public function update_settings( \WP_REST_Request $request ) {
+ $cache_exlusion = $request->get_param( 'cache_exlusion' );
+ if ( update_option( 'cache_exlusion', $cache_exlusion ) ) {
+ return new \WP_REST_Response(
+ array(
+ 'result' => true,
+ ),
+ 200
+ );
+ }
+
+ return new \WP_REST_Response(
+ array(
+ 'result' => false,
+ ),
+ 400
+ );
+ }
+}
From e3c3fb6b69a8403a7f5f77ddcfaee55cd2614f9d Mon Sep 17 00:00:00 2001
From: Alessio Torrisi
Date: Wed, 6 Nov 2024 16:20:20 +0100
Subject: [PATCH 03/42] New: Cache Exclusion option
---
components/cacheExclusion/index.js | 63 +++++++++++++++++++++++++++
components/performance/defaultText.js | 5 +++
components/performance/index.js | 7 +++
3 files changed, 75 insertions(+)
create mode 100644 components/cacheExclusion/index.js
diff --git a/components/cacheExclusion/index.js b/components/cacheExclusion/index.js
new file mode 100644
index 0000000..71eaf05
--- /dev/null
+++ b/components/cacheExclusion/index.js
@@ -0,0 +1,63 @@
+import { Button, Container, TextareaField } from "@newfold/ui-component-library";
+
+const CacheExclusion = ({ methods, constants }) => {
+
+ const [ currentValue, setCurrentValue ] = methods.useState(constants.store.cacheExclusion);
+ const [ isEdited, setIsEdited ] = methods.useState(false);
+ const [ cacheExclusion, setCacheExclusion ] = methods.useState(constants.store.cacheExclusion);
+
+
+ const handleCacheExclusionChange = (e) => {
+ if( e.target.value !== cacheExclusion ) {
+ setIsEdited(true);
+ }else{
+ setIsEdited(false);
+ }
+ setCurrentValue( e.target.value );
+ }
+
+
+ const handlingSaveButton = () => {
+ methods.newfoldSettingsApiFetch(
+ { cacheExclusion: currentValue },
+ methods.setError, (response) => {
+ setCacheExclusion( currentValue )
+ methods.makeNotice(
+ "disable-old-posts-comments-notice",
+ constants.text.cacheExclusionSaved,
+ null,
+ "success",
+ 5000
+ );
+ setIsEdited(false);
+ }
+ );
+
+ };
+
+ return (
+
+
+
+
+
+
+ );
+;}
+
+export default CacheExclusion;
\ No newline at end of file
diff --git a/components/performance/defaultText.js b/components/performance/defaultText.js
index 2603b96..0f61643 100644
--- a/components/performance/defaultText.js
+++ b/components/performance/defaultText.js
@@ -22,6 +22,11 @@ const defaultText = {
clearCacheDescription: __('We automatically clear your cache as you work (creating content, changing settings, installing plugins and more). But you can manually clear it here to be confident it is fresh.', 'wp-module-performance'),
clearCacheNoticeTitle: __('Cache cleared', 'wp-module-performance'),
clearCacheTitle: __('Clear Cache', 'wp-module-performance'),
+ clearCacheTitle: __('Clear Cache', 'wp-module-performance'),
+ cacheExclusionTitle: __( 'Exclude from cache', 'wp-module-performance' ),
+ cacheExclusionDescription: __( 'This setting controls what pages pass a “no-cache” header so that page caching and browser caching is not used.', 'wp-module-performance' ),
+ cacheExclusionSaved: __( 'Cache Exclusion saved', 'wp-module-performance' ),
+ cacheExclusionSaveButton: __( 'Save', 'wp-module-performance' ),
};
export default defaultText;
\ No newline at end of file
diff --git a/components/performance/index.js b/components/performance/index.js
index e6d7483..ed480e2 100644
--- a/components/performance/index.js
+++ b/components/performance/index.js
@@ -1,6 +1,7 @@
import { Container } from '@newfold/ui-component-library';
import { default as CacheSettings } from '../cacheSettings/';
import { default as ClearCache } from '../clearCache/';
+import { default as CacheExclusion } from '../cacheExclusion/';
import { default as defaultText } from './defaultText';
/**
@@ -45,6 +46,12 @@ const Performance = ({methods, constants, Components, ...props}) => {
Components={Components}
/>
+
+
+
Date: Thu, 7 Nov 2024 11:43:18 +0000
Subject: [PATCH 04/42] register the api and add the value to runtime
---
includes/CacheManager.php | 50 ++++++++++++++++++-
...tings.php => CacheExclusionController.php} | 14 +++---
2 files changed, 56 insertions(+), 8 deletions(-)
rename includes/RestApi/{CacheSettings.php => CacheExclusionController.php} (85%)
diff --git a/includes/CacheManager.php b/includes/CacheManager.php
index 38da5eb..3503f55 100644
--- a/includes/CacheManager.php
+++ b/includes/CacheManager.php
@@ -3,6 +3,7 @@
namespace NewfoldLabs\WP\Module\Performance;
use NewfoldLabs\WP\Module\Performance\CacheTypes\CacheBase;
+use NewfoldLabs\WP\Module\Performance\RestApi\CacheExclusionController;
use NewfoldLabs\WP\ModuleLoader\Container;
use WP_Forge\Collection\Collection;
@@ -15,13 +16,60 @@ class CacheManager {
*/
protected $container;
+ /**
+ * Array map of API controllers.
+ *
+ * @var array
+ */
+ protected $controllers = array(
+ 'NewfoldLabs\\WP\\Module\\Performance\\RestApi\\CacheExclusionController',
+ );
+
/**
* Constructor.
*
- * @param string[] $supportedCacheTypes Cache types supported by the plugin
+ * @param string[] $supportedCacheTypes Cache types supported by the plugin
*/
public function __construct( Container $container ) {
$this->container = $container;
+
+ add_action( 'rest_api_init', array( $this, 'register_routes' ) );
+ add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ) );
+ }
+
+ /**
+ * Register API routes.
+ */
+ public function register_routes() {
+ foreach ( $this->controllers as $Controller ) {
+ /**
+ * Get an instance of the WP_REST_Controller.
+ *
+ * @var $instance \WP_REST_Controller
+ */
+ $instance = new $Controller( $this->container );
+ $instance->register_routes();
+ }
+ }
+
+ /**
+ * Add values to the runtime object.
+ *
+ * @param array $sdk The runtime object.
+ *
+ * @return array
+ */
+ public function add_to_runtime( $sdk ) {
+ return array_merge( $sdk, array( 'cacheExclusion' => get_option( 'cache_exclusion', $this->defaultCacheExclusions() ) ) );
+ }
+
+ /**
+ * Return defaul exclusions.
+ *
+ * @return array
+ */
+ public function defaultCacheExclusions() {
+ return join( ',', [ 'cart', 'checkout', 'wp-admin', rest_get_url_prefix() ] );
}
/**
diff --git a/includes/RestApi/CacheSettings.php b/includes/RestApi/CacheExclusionController.php
similarity index 85%
rename from includes/RestApi/CacheSettings.php
rename to includes/RestApi/CacheExclusionController.php
index 894de2a..a128bca 100644
--- a/includes/RestApi/CacheSettings.php
+++ b/includes/RestApi/CacheExclusionController.php
@@ -6,9 +6,9 @@
use NewfoldLabs\WP\ModuleLoader\Container;
/**
- * Class CacheSettings
+ * Class CacheExclusionController
*/
-class CacheSettings {
+class CacheExclusionController {
/**
@@ -23,7 +23,7 @@ class CacheSettings {
*
* @var string
*/
- protected $rest_base = '/cachesettings';
+ protected $rest_base = '/cacheexclusion';
/**
* Container loaded from the brand plugin.
@@ -79,7 +79,7 @@ public function register_routes() {
public function get_settings() {
return new \WP_REST_Response(
array(
- 'settings' => get_option( 'cache_exlusion', '' ),
+ 'settings' => get_option( 'cache_exclusion', '' ),
),
200
);
@@ -92,8 +92,8 @@ public function get_settings() {
* @return \WP_REST_Response
*/
public function update_settings( \WP_REST_Request $request ) {
- $cache_exlusion = $request->get_param( 'cache_exlusion' );
- if ( update_option( 'cache_exlusion', $cache_exlusion ) ) {
+ $cache_exclusion = $request->get_param( 'cache_exclusion' );
+ if ( update_option( 'cache_exclusion', $cache_exclusion ) ) {
return new \WP_REST_Response(
array(
'result' => true,
@@ -104,7 +104,7 @@ public function update_settings( \WP_REST_Request $request ) {
return new \WP_REST_Response(
array(
- 'result' => false,
+ 'result' => false,
),
400
);
From 449116b1f1d963d43c416567b6546dd7e5d5df7d Mon Sep 17 00:00:00 2001
From: Armando Liccardo
Date: Thu, 7 Nov 2024 11:50:44 +0000
Subject: [PATCH 05/42] fixed string
---
includes/RestApi/CacheExclusionController.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/includes/RestApi/CacheExclusionController.php b/includes/RestApi/CacheExclusionController.php
index a128bca..6527c45 100644
--- a/includes/RestApi/CacheExclusionController.php
+++ b/includes/RestApi/CacheExclusionController.php
@@ -79,7 +79,7 @@ public function register_routes() {
public function get_settings() {
return new \WP_REST_Response(
array(
- 'settings' => get_option( 'cache_exclusion', '' ),
+ 'cacheExclusion' => get_option( 'cache_exclusion', '' ),
),
200
);
@@ -92,7 +92,7 @@ public function get_settings() {
* @return \WP_REST_Response
*/
public function update_settings( \WP_REST_Request $request ) {
- $cache_exclusion = $request->get_param( 'cache_exclusion' );
+ $cache_exclusion = $request->get_param( 'cacheExclusion' );
if ( update_option( 'cache_exclusion', $cache_exclusion ) ) {
return new \WP_REST_Response(
array(
From 9a7cd89fa06b153de1e3d8dfae8a6fb95b7d3013 Mon Sep 17 00:00:00 2001
From: Armando Liccardo
Date: Thu, 7 Nov 2024 11:55:59 +0000
Subject: [PATCH 06/42] moved getDefaultCacheExclusions function to functions
file
---
includes/CacheManager.php | 13 +++----------
includes/RestApi/CacheExclusionController.php | 4 +++-
includes/functions.php | 9 +++++++++
3 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/includes/CacheManager.php b/includes/CacheManager.php
index 3503f55..8ed4ac8 100644
--- a/includes/CacheManager.php
+++ b/includes/CacheManager.php
@@ -7,6 +7,8 @@
use NewfoldLabs\WP\ModuleLoader\Container;
use WP_Forge\Collection\Collection;
+use function NewfoldLabs\WP\Module\Performance\getDefaultCacheExclusions;
+
class CacheManager {
/**
@@ -60,16 +62,7 @@ public function register_routes() {
* @return array
*/
public function add_to_runtime( $sdk ) {
- return array_merge( $sdk, array( 'cacheExclusion' => get_option( 'cache_exclusion', $this->defaultCacheExclusions() ) ) );
- }
-
- /**
- * Return defaul exclusions.
- *
- * @return array
- */
- public function defaultCacheExclusions() {
- return join( ',', [ 'cart', 'checkout', 'wp-admin', rest_get_url_prefix() ] );
+ return array_merge( $sdk, array( 'cacheExclusion' => get_option( 'cache_exclusion', getDefaultCacheExclusions() ) ) );
}
/**
diff --git a/includes/RestApi/CacheExclusionController.php b/includes/RestApi/CacheExclusionController.php
index 6527c45..f352afa 100644
--- a/includes/RestApi/CacheExclusionController.php
+++ b/includes/RestApi/CacheExclusionController.php
@@ -5,6 +5,8 @@
use NewfoldLabs\WP\Module\ECommerce\Permissions;
use NewfoldLabs\WP\ModuleLoader\Container;
+use function NewfoldLabs\WP\Module\Performance\getDefaultCacheExclusions;
+
/**
* Class CacheExclusionController
*/
@@ -79,7 +81,7 @@ public function register_routes() {
public function get_settings() {
return new \WP_REST_Response(
array(
- 'cacheExclusion' => get_option( 'cache_exclusion', '' ),
+ 'cacheExclusion' => get_option( 'cache_exclusion', getDefaultCacheExclusions() ),
),
200
);
diff --git a/includes/functions.php b/includes/functions.php
index 4133e84..49a8310 100644
--- a/includes/functions.php
+++ b/includes/functions.php
@@ -2,6 +2,15 @@
namespace NewfoldLabs\WP\Module\Performance;
+/**
+ * Return defaul exclusions.
+ *
+ * @return array
+ */
+function getDefaultCacheExclusions() {
+ return join( ',', [ 'cart', 'checkout', 'wp-admin', rest_get_url_prefix() ] );
+}
+
/**
* Get the current cache level.
*
From ddb7674910eda6c0f5f57fbe85152544492598be Mon Sep 17 00:00:00 2001
From: Armando Liccardo
Date: Thu, 7 Nov 2024 14:41:50 +0000
Subject: [PATCH 07/42] updated js component
---
components/cacheExclusion/index.js | 70 +++++++++++++++++-------------
includes/CacheManager.php | 1 -
2 files changed, 40 insertions(+), 31 deletions(-)
diff --git a/components/cacheExclusion/index.js b/components/cacheExclusion/index.js
index 71eaf05..ee50b1b 100644
--- a/components/cacheExclusion/index.js
+++ b/components/cacheExclusion/index.js
@@ -1,11 +1,11 @@
import { Button, Container, TextareaField } from "@newfold/ui-component-library";
const CacheExclusion = ({ methods, constants }) => {
-
- const [ currentValue, setCurrentValue ] = methods.useState(constants.store.cacheExclusion);
const [ isEdited, setIsEdited ] = methods.useState(false);
- const [ cacheExclusion, setCacheExclusion ] = methods.useState(constants.store.cacheExclusion);
-
+ const [ isError, setIsError ] = methods.useState(false);
+ const [ isSaved, setIsSaved ] = methods.useState(false);
+ const [ cacheExclusion, setCacheExclusion ] = methods.useState(methods.NewfoldRuntime.sdk.cacheExclusion);
+ const apiUrl = methods.NewfoldRuntime.createApiUrl("/newfold-ecommerce/v1/cacheexclusion/update");
const handleCacheExclusionChange = (e) => {
if( e.target.value !== cacheExclusion ) {
@@ -13,28 +13,36 @@ const CacheExclusion = ({ methods, constants }) => {
}else{
setIsEdited(false);
}
- setCurrentValue( e.target.value );
+ setCacheExclusion( e.target.value );
}
-
const handlingSaveButton = () => {
- methods.newfoldSettingsApiFetch(
- { cacheExclusion: currentValue },
- methods.setError, (response) => {
- setCacheExclusion( currentValue )
- methods.makeNotice(
- "disable-old-posts-comments-notice",
- constants.text.cacheExclusionSaved,
- null,
- "success",
- 5000
- );
- setIsEdited(false);
- }
- );
-
+ methods.apiFetch({
+ url: apiUrl,
+ method: "POST",
+ data: {cacheExclusion: cacheExclusion}
+ }).then((result)=>{
+ setIsSaved(true);
+ }).catch((error) => {
+ setIsError(error.message);
+ });
};
+ methods.useUpdateEffect(() => {
+ methods.setStore({
+ ...constants.store,
+ CacheExclusion: cacheExclusion,
+ });
+
+ methods.makeNotice(
+ "cache-exlusion-notice",
+ constants.text.cacheExclusionTitle,
+ !isError ? constants.text.cacheExclusionSaved : isError,
+ !isError ? "success" : "error",
+ 5000
+ );
+ }, [ isSaved, isError]);
+
return (
{
id="cache-exclusion"
name="cache-xxclusion"
onChange={ handleCacheExclusionChange }
- value={currentValue}
+ value={cacheExclusion}
/>
-
+ {isEdited &&
+
+ }
diff --git a/includes/CacheManager.php b/includes/CacheManager.php
index 8ed4ac8..a32b2af 100644
--- a/includes/CacheManager.php
+++ b/includes/CacheManager.php
@@ -3,7 +3,6 @@
namespace NewfoldLabs\WP\Module\Performance;
use NewfoldLabs\WP\Module\Performance\CacheTypes\CacheBase;
-use NewfoldLabs\WP\Module\Performance\RestApi\CacheExclusionController;
use NewfoldLabs\WP\ModuleLoader\Container;
use WP_Forge\Collection\Collection;
From 6b56ef18703ccc0198d13e4c7cda661fe581039c Mon Sep 17 00:00:00 2001
From: Alessio Torrisi
Date: Fri, 8 Nov 2024 15:11:44 +0100
Subject: [PATCH 08/42] Tweak: show "save" button only if the current value has
been edited
---
components/cacheExclusion/index.js | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/components/cacheExclusion/index.js b/components/cacheExclusion/index.js
index ee50b1b..31bded6 100644
--- a/components/cacheExclusion/index.js
+++ b/components/cacheExclusion/index.js
@@ -4,6 +4,7 @@ const CacheExclusion = ({ methods, constants }) => {
const [ isEdited, setIsEdited ] = methods.useState(false);
const [ isError, setIsError ] = methods.useState(false);
const [ isSaved, setIsSaved ] = methods.useState(false);
+ const [ currentValue, setCurrentValue ] = methods.useState(methods.NewfoldRuntime.sdk.cacheExclusion);
const [ cacheExclusion, setCacheExclusion ] = methods.useState(methods.NewfoldRuntime.sdk.cacheExclusion);
const apiUrl = methods.NewfoldRuntime.createApiUrl("/newfold-ecommerce/v1/cacheexclusion/update");
@@ -13,16 +14,17 @@ const CacheExclusion = ({ methods, constants }) => {
}else{
setIsEdited(false);
}
- setCacheExclusion( e.target.value );
+ setCurrentValue( e.target.value );
}
const handlingSaveButton = () => {
methods.apiFetch({
url: apiUrl,
method: "POST",
- data: {cacheExclusion: cacheExclusion}
+ data: {cacheExclusion: currentValue}
}).then((result)=>{
setIsSaved(true);
+ setCacheExclusion(currentValue);
}).catch((error) => {
setIsError(error.message);
});
@@ -52,7 +54,7 @@ const CacheExclusion = ({ methods, constants }) => {
id="cache-exclusion"
name="cache-xxclusion"
onChange={ handleCacheExclusionChange }
- value={cacheExclusion}
+ value={currentValue}
/>
{isEdited &&