Skip to content

Commit

Permalink
Boost: Update Page Cache notice for WP Cloud clients (#39155)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dilirity authored Aug 30, 2024
1 parent 30dab5a commit 84d1a1c
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 31 deletions.
8 changes: 4 additions & 4 deletions projects/plugins/boost/app/admin/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -64,7 +68,7 @@ const Module = ( {
<ToggleControl
className={ `jb-feature-toggle-${ slug }` }
size="small"
checked={ isModuleActive }
checked={ isModuleActive || isFakeActive }
disabled={ ! isModuleAvailable }
onChange={ handleToggle }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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',
Expand Down Expand Up @@ -97,20 +98,31 @@ const PageCache = () => {
'jetpack-boost'
) }
</p>
{ site.isAtomic && (
<Notice
level="warning"
title={ __( 'Page Cache is unavailable', 'jetpack-boost' ) }
hideCloseButton={ true }
>
<p>
{ __(
'Your website already has a page cache running on it powered by WordPress.com.',
'jetpack-boost'
) }
</p>
</Notice>
) }
{ showCacheFromHostingNotice &&
( isWoaHosting() ? (
<Notice
level="success"
title={ __( 'Page Cache is running', 'jetpack-boost' ) }
hideCloseButton={ true }
>
<p>
{ __(
'Your website already has a page cache running on it powered by WordPress.com.',
'jetpack-boost'
) }
</p>
</Notice>
) : (
<Notice
level="info"
title={ __( 'Page Cache is unavailable', 'jetpack-boost' ) }
hideCloseButton={ true }
>
<p>
{ __( 'Your hosting provider already provides page caching.', 'jetpack-boost' ) }
</p>
</Notice>
) ) }
<Health
error={ pageCacheError.data }
setError={ pageCacheErrorMutation.mutate }
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/boost/app/assets/src/js/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare global {
domain: string;
url: string;
online: boolean;
isAtomic: boolean;
host: string;
};
assetPath: string;
pluginDirUrl: string;
Expand Down
17 changes: 17 additions & 0 deletions projects/plugins/boost/app/assets/src/js/lib/utils/hosting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Determine if this site is on a WP Cloud client.
*
* @return {boolean} True if the site is on a WP Cloud client, false otherwise.
*/
export const isWpCloudClient = (): boolean => {
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';
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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' );
const [ imageGuideState ] = useSingleModuleState( 'image_guide' );
const [ isaState ] = useSingleModuleState( 'image_size_analysis' );
const navigate = useNavigate();
const isaRequest = useImageAnalysisRequest();
const { site, canResizeImages } = Jetpack_Boost;
const { canResizeImages } = Jetpack_Boost;

useEffect( () => {
setCloudCssState( true );
Expand Down Expand Up @@ -97,7 +98,7 @@ const PurchaseSuccess: React.FC = () => {
</li>

<li>
{ site.isAtomic
{ isWoaHosting()
? createInterpolateElement(
__(
`Dedicated email support plus priority Live Chat if <link>your plan</link> includes <strong>Premium Support</strong>`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Page Cache: Update notice for WP Cloud clients.

0 comments on commit 84d1a1c

Please sign in to comment.