From 84d1a1c4c63b22b681242c77f40bf660bd2bc071 Mon Sep 17 00:00:00 2001
From: Peter Petrov
Date: Fri, 30 Aug 2024 14:33:07 +0300
Subject: [PATCH] Boost: Update Page Cache notice for WP Cloud clients (#39155)
* Disable page cache on wp cloud clients
* Update title for page cache notice on WoA sites
* Update the Page Cache module for WoA sites to show as "active"
* Add changelog
* Update Page Cache notice colors for WoA and WP Cloud sites
* Use separate components to avoid ternary checks
* Check if advanced-cache.php can be run in order to determine availability
* Check if module is available before showing the notice
* Rename variable for clarity
---
.../plugins/boost/app/admin/class-config.php | 8 ++--
.../activate-license/activate-license.tsx | 4 +-
.../assets/src/js/features/module/module.tsx | 6 ++-
.../src/js/features/page-cache/page-cache.tsx | 44 ++++++++++++-------
.../boost/app/assets/src/js/global.d.ts | 2 +-
.../app/assets/src/js/lib/utils/hosting.ts | 17 +++++++
.../purchase-success/purchase-success.tsx | 5 ++-
.../optimizations/page-cache/Page_Cache.php | 10 +++--
.../page-cache/Page_Cache_Setup.php | 28 +++++++++++-
.../update-page-cache-behavior-on-atomic | 4 ++
10 files changed, 97 insertions(+), 31 deletions(-)
create mode 100644 projects/plugins/boost/app/assets/src/js/lib/utils/hosting.ts
create mode 100644 projects/plugins/boost/changelog/update-page-cache-behavior-on-atomic
diff --git a/projects/plugins/boost/app/admin/class-config.php b/projects/plugins/boost/app/admin/class-config.php
index 9450ca8f6cf6c..d2b86a33d7bab 100644
--- a/projects/plugins/boost/app/admin/class-config.php
+++ b/projects/plugins/boost/app/admin/class-config.php
@@ -27,10 +27,10 @@ public function constants() {
'assetPath' => plugins_url( $internal_path, JETPACK_BOOST_PATH ),
'canResizeImages' => wp_image_editor_supports( array( 'methods' => array( 'resize' ) ) ),
'site' => array(
- 'url' => get_home_url(),
- 'domain' => ( new Status() )->get_site_suffix(),
- 'online' => ! ( new Status() )->is_offline_mode(),
- 'isAtomic' => ( new Host() )->is_woa_site(),
+ 'url' => get_home_url(),
+ 'domain' => ( new Status() )->get_site_suffix(),
+ 'online' => ! ( new Status() )->is_offline_mode(),
+ 'host' => ( new Host() )->get_known_host_guess(),
),
'api' => array(
'namespace' => JETPACK_BOOST_REST_NAMESPACE,
diff --git a/projects/plugins/boost/app/assets/src/js/features/activate-license/activate-license.tsx b/projects/plugins/boost/app/assets/src/js/features/activate-license/activate-license.tsx
index adb576305aded..b886ce25d5f41 100644
--- a/projects/plugins/boost/app/assets/src/js/features/activate-license/activate-license.tsx
+++ b/projects/plugins/boost/app/assets/src/js/features/activate-license/activate-license.tsx
@@ -1,10 +1,10 @@
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import styles from './activate-license.module.scss';
+import { isWoaHosting } from '$lib/utils/hosting';
const ActivateLicense = () => {
- const { site } = Jetpack_Boost;
- if ( site.isAtomic ) {
+ if ( isWoaHosting() ) {
return null;
}
diff --git a/projects/plugins/boost/app/assets/src/js/features/module/module.tsx b/projects/plugins/boost/app/assets/src/js/features/module/module.tsx
index eaf40d209dde2..947bc44d061e4 100644
--- a/projects/plugins/boost/app/assets/src/js/features/module/module.tsx
+++ b/projects/plugins/boost/app/assets/src/js/features/module/module.tsx
@@ -4,6 +4,7 @@ import { useSingleModuleState } from './lib/stores';
import styles from './module.module.scss';
import ErrorBoundary from '$features/error-boundary/error-boundary';
import { __ } from '@wordpress/i18n';
+import { isWoaHosting } from '$lib/utils/hosting';
type ModuleProps = {
title: React.ReactNode;
@@ -37,6 +38,9 @@ const Module = ( {
} );
const isModuleActive = status?.active ?? false;
const isModuleAvailable = status?.available ?? false;
+ // Page Cache is not available for WoA sites, but since WoA sites
+ // have their own caching, we want to show that Page Cache is active.
+ const isFakeActive = ! isModuleAvailable && isWoaHosting() && slug === 'page_cache';
const handleToggle = () => {
if ( onBeforeToggle ) {
@@ -64,7 +68,7 @@ const Module = ( {
diff --git a/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx b/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx
index 2357212068869..774b0a10ed9a4 100644
--- a/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx
+++ b/projects/plugins/boost/app/assets/src/js/features/page-cache/page-cache.tsx
@@ -9,6 +9,7 @@ import { Notice } from '@automattic/jetpack-components';
import { __ } from '@wordpress/i18n';
import { useSingleModuleState } from '$features/module/lib/stores';
import styles from './page-cache.module.scss';
+import { isWpCloudClient, isWoaHosting } from '$lib/utils/hosting';
const DismissableNotice = ( { title, children }: { title: string; children: ReactNode } ) => {
const [ dismissed, setDismissed ] = useState( false );
@@ -35,8 +36,8 @@ const PageCache = () => {
const showCacheEngineErrorNotice = useShowCacheEngineErrorNotice(
pageCacheSetup.isSuccess && !! moduleState?.active
);
-
- const { site } = Jetpack_Boost;
+ const showCacheFromHostingNotice =
+ ! moduleState?.available && ( isWoaHosting() || isWpCloudClient() );
const [ removePageCacheNotice ] = useMutationNotice(
'page-cache-setup',
@@ -97,20 +98,31 @@ const PageCache = () => {
'jetpack-boost'
) }
- { site.isAtomic && (
-
-
- { __(
- 'Your website already has a page cache running on it powered by WordPress.com.',
- 'jetpack-boost'
- ) }
-
-
- ) }
+ { showCacheFromHostingNotice &&
+ ( isWoaHosting() ? (
+
+
+ { __(
+ 'Your website already has a page cache running on it powered by WordPress.com.',
+ 'jetpack-boost'
+ ) }
+
+
+ ) : (
+
+
+ { __( 'Your hosting provider already provides page caching.', 'jetpack-boost' ) }
+
+
+ ) ) }
{
+ return Jetpack_Boost.site.host === 'atomic';
+};
+
+/**
+ * Determine if this site is an WordPress.com on Atomic site.
+ *
+ * @return {boolean} True if the site is an WordPress.com on Atomic site, false otherwise.
+ */
+export const isWoaHosting = (): boolean => {
+ return Jetpack_Boost.site.host === 'woa';
+};
diff --git a/projects/plugins/boost/app/assets/src/js/pages/purchase-success/purchase-success.tsx b/projects/plugins/boost/app/assets/src/js/pages/purchase-success/purchase-success.tsx
index 6f95f8c14dcc5..3b5840778b347 100644
--- a/projects/plugins/boost/app/assets/src/js/pages/purchase-success/purchase-success.tsx
+++ b/projects/plugins/boost/app/assets/src/js/pages/purchase-success/purchase-success.tsx
@@ -7,6 +7,7 @@ import { useSingleModuleState } from '$features/module/lib/stores';
import { useNavigate } from 'react-router-dom';
import CardPage from '$layout/card-page/card-page';
import styles from './purchase-success.module.scss';
+import { isWoaHosting } from '$lib/utils/hosting';
const PurchaseSuccess: React.FC = () => {
const [ , setCloudCssState ] = useSingleModuleState( 'cloud_css' );
@@ -14,7 +15,7 @@ const PurchaseSuccess: React.FC = () => {
const [ isaState ] = useSingleModuleState( 'image_size_analysis' );
const navigate = useNavigate();
const isaRequest = useImageAnalysisRequest();
- const { site, canResizeImages } = Jetpack_Boost;
+ const { canResizeImages } = Jetpack_Boost;
useEffect( () => {
setCloudCssState( true );
@@ -97,7 +98,7 @@ const PurchaseSuccess: React.FC = () => {
- { site.isAtomic
+ { isWoaHosting()
? createInterpolateElement(
__(
`Dedicated email support plus priority Live Chat if your plan includes Premium Support`,
diff --git a/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache.php b/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache.php
index 4c076469c5411..166164e63c870 100644
--- a/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache.php
+++ b/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache.php
@@ -104,9 +104,13 @@ public function is_ready() {
}
public static function is_available() {
- // Disable Page Cache on Atomic.
- // It already has caching enabled.
- if ( ( new Host() )->is_woa_site() ) {
+ // Disable Page Cache on WoA and WP Cloud clients.
+ // They already have caching enabled.
+ if ( ( new Host() )->is_woa_site() || ( new Host() )->is_atomic_platform() ) {
+ if ( Page_Cache_Setup::can_run_cache() ) {
+ return true;
+ }
+
return false;
}
diff --git a/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache_Setup.php b/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache_Setup.php
index d1dc28ed3aefb..c21c21a8782c0 100644
--- a/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache_Setup.php
+++ b/projects/plugins/boost/app/modules/optimizations/page-cache/Page_Cache_Setup.php
@@ -149,6 +149,15 @@ private static function create_settings_file() {
return $result;
}
+ /**
+ * Get the path to the advanced-cache.php file.
+ *
+ * @return string The full path to the advanced-cache.php file.
+ */
+ public static function get_advanced_cache_path() {
+ return WP_CONTENT_DIR . '/advanced-cache.php';
+ }
+
/**
* Creates the advanced-cache.php file.
*
@@ -157,7 +166,7 @@ private static function create_settings_file() {
* @return bool|\WP_Error
*/
private static function create_advanced_cache() {
- $advanced_cache_filename = WP_CONTENT_DIR . '/advanced-cache.php';
+ $advanced_cache_filename = self::get_advanced_cache_path();
if ( file_exists( $advanced_cache_filename ) ) {
$content = file_get_contents( $advanced_cache_filename ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
@@ -264,6 +273,21 @@ private static function add_wp_cache_define() {
return true;
}
+ /**
+ * Checks if page cache can be run or not.
+ *
+ * @return bool True if the advanced-cache.php file doesn't exist or belongs to Boost, false otherwise.
+ */
+ public static function can_run_cache() {
+ $advanced_cache_path = self::get_advanced_cache_path();
+ if ( ! file_exists( $advanced_cache_path ) ) {
+ return true;
+ }
+
+ $content = file_get_contents( $advanced_cache_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
+ return strpos( $content, Page_Cache::ADVANCED_CACHE_SIGNATURE ) !== false;
+ }
+
/**
* Removes the advanced-cache.php file and the WP_CACHE define from wp-config.php
* Fired when the plugin is deactivated.
@@ -300,7 +324,7 @@ public static function uninstall() {
* Deletes the file advanced-cache.php if it exists.
*/
public static function delete_advanced_cache() {
- $advanced_cache_filename = WP_CONTENT_DIR . '/advanced-cache.php';
+ $advanced_cache_filename = self::get_advanced_cache_path();
if ( ! file_exists( $advanced_cache_filename ) ) {
return false;
diff --git a/projects/plugins/boost/changelog/update-page-cache-behavior-on-atomic b/projects/plugins/boost/changelog/update-page-cache-behavior-on-atomic
new file mode 100644
index 0000000000000..d55c07fb808f2
--- /dev/null
+++ b/projects/plugins/boost/changelog/update-page-cache-behavior-on-atomic
@@ -0,0 +1,4 @@
+Significance: patch
+Type: changed
+
+Page Cache: Update notice for WP Cloud clients.