Skip to content
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

Add the “Live Preview” button on the Theme Detail page #79775

Merged
merged 12 commits into from
Jul 31, 2023
140 changes: 140 additions & 0 deletions client/my-sites/theme/live-preview-button/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Button } from '@automattic/components';

/**
* Hardcoded list of themes that are not compatible with Block Theme Previews.
* This list should be removed once they are retired.
*
* @see pekYwv-284-p2
*/
const NOT_COMPATIBLE_THEMES = [
'premium/alonso',
'premium/baxter',
'premium/byrne',
'premium/kerr',
'premium/munchies',
'premium/payton',
'premium/skivers',
'premium/thriving-artist',
'pub/ames',
'pub/antonia',
'pub/appleton',
'pub/arbutus',
'pub/attar',
'pub/barnett',
'pub/bennett',
'pub/calvin',
'pub/calyx',
'pub/course',
'pub/dorna',
'pub/farrow',
'pub/geologist-blue',
'pub/geologist-cream',
'pub/geologist-slate',
'pub/geologist-yellow',
'pub/hari',
'pub/heiwa',
'pub/jackson',
'pub/kingsley',
'pub/marl',
'pub/meraki',
'pub/quadrat-black',
'pub/quadrat-green',
'pub/quadrat-red',
'pub/quadrat-white',
'pub/quadrat-yellow',
'pub/quadrat',
'pub/russell',
'pub/seedlet-blocks',
'pub/winkel',
];

const isNotCompatibleThemes = ( stylesheet ) => {
return NOT_COMPATIBLE_THEMES.includes( stylesheet );
};

/**
* Live Preview leveraging Gutenberg's Block Theme Previews
*
* The scenarios where the Live Preview does NOT support;
* - On both Simple and Atomic sites;
* - If the theme is users' active theme.
* - If the theme is not FullSiteEditing compatible.
* - If the theme has a static page as a homepage.
* - On Atomic sites;
* - If the theme is not installed on Atomic sites.
* - On Simple sites;
* - If the theme is a 3rd party theme.
* - If the theme is not included in a plan.
*
* @see pbxlJb-3Uv-p2
*/
export const LivePreviewButton = ( {
canUseTheme,
isActive,
isAtomic,
isExternallyManagedTheme,
isFullSiteEditingTheme,
isSimple,
isThemeInstalledOnAtomicSite,
isWporg,
siteSlug,
stylesheet,
themeId,
translate,
} ) => {
// A user doesn't want to preview the active theme.
if ( isActive ) {
return null;
}

// A theme should be FullSiteEditing compatible to use Block Theme Previews.
if ( ! isFullSiteEditingTheme ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not use showTryAndCustomize here as showTryAndCustomize = true does not mean it's not FullSiteEditing compatible. I thought it'd be nice to show the Live Preview button anytime in cases Block Theme Previews is available.

return null;
}

/**
* Block Theme Previews do not support themes with a static page as a homepage
* as the Site Editor cannot control Reading Settings.
*
* @see pekYwv-284-p2#background
*/
if ( isNotCompatibleThemes( stylesheet ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to account for Atomic site here? Or just Simple sites? Asking because the theme slugs on Atomic sites probably won't be prefixed with /pub or /premium CMIIW

Copy link
Member Author

@okmttdhr okmttdhr Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stylesheet includes prefixes (pub/, premium/) even on Atomic sites!

This is the reason I switch themePath depending on if it's Atomic or Simple here. https://github.com/Automattic/wp-calypso/pull/79775/files/ac6c6b80c7ed9a71ab8d736db5254a1e8ce4eceb#diff-6535b936322e5679c10cdf1adeac00cda6c355312582892d7a634c4e2d6fbd64R120-R124

Let me know if I did not answer your comment 🙏

return null;
}

// Block Theme Previews need the theme installed on Atomic sites.
if ( isAtomic && ! isThemeInstalledOnAtomicSite ) {
return null;
}

if ( isSimple ) {
/**
* Disable Live Preview for 3rd party themes, as Block Theme Previews need a theme installed.
* Note that BTP works on Atomic sites if a theme is installed.
*/
if ( isExternallyManagedTheme || isWporg ) {
return null;
}

/**
* Disable Live Preview for themes that are not included in a plan.
* This should be updated as we implement the flow for them.
* Note that BTP works on Atomic sites if a theme is installed.
*
* @see https://github.com/Automattic/wp-calypso/issues/79223
*/
if ( ! canUseTheme ) {
return null;
}
}

const themePath = isAtomic ? themeId : stylesheet;

return (
<Button
href={ `https://${ siteSlug }/wp-admin/site-editor.php?wp_theme_preview=${ themePath }` }
>
{ translate( 'Live Preview' ) }
</Button>
);
};
71 changes: 35 additions & 36 deletions client/my-sites/theme/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import QueryCanonicalTheme from 'calypso/components/data/query-canonical-theme';
import QueryProductsList from 'calypso/components/data/query-products-list';
import QuerySitePlans from 'calypso/components/data/query-site-plans';
import QuerySitePurchases from 'calypso/components/data/query-site-purchases';
import QueryTheme from 'calypso/components/data/query-theme';
import QueryUserPurchases from 'calypso/components/data/query-user-purchases';
import SyncActiveTheme from 'calypso/components/data/sync-active-theme';
import HeaderCake from 'calypso/components/header-cake';
Expand Down Expand Up @@ -66,12 +67,13 @@ import isSiteWPForTeams from 'calypso/state/selectors/is-site-wpforteams';
import isVipSite from 'calypso/state/selectors/is-vip-site';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { useSiteGlobalStylesStatus } from 'calypso/state/sites/hooks/use-site-global-styles-status';
import { getSiteSlug, isJetpackSite } from 'calypso/state/sites/selectors';
import { getSiteSlug, isJetpackSite, isSimpleSite } from 'calypso/state/sites/selectors';
import {
setThemePreviewOptions,
themeStartActivationSync as themeStartActivationSyncAction,
} from 'calypso/state/themes/actions';
import {
canUseTheme as getCanUseTheme,
doesThemeBundleSoftwareSet,
isThemeActive,
isThemePremium,
Expand All @@ -81,55 +83,30 @@ import {
isWporgTheme,
getCanonicalTheme,
getPremiumThemePrice,
getThemeDemoUrl,
getThemeDetailsUrl,
getThemeRequestErrors,
getThemeForumUrl,
getThemeDemoUrl,
getThemeRequestErrors,
shouldShowTryAndCustomize,
isExternallyManagedTheme as getIsExternallyManagedTheme,
isSiteEligibleForManagedExternalThemes as getIsSiteEligibleForManagedExternalThemes,
isMarketplaceThemeSubscribed as getIsMarketplaceThemeSubscribed,
isThemeActivationSyncStarted as getIsThemeActivationSyncStarted,
isFullSiteEditingTheme as getIsFullSiteEditingTheme,
} from 'calypso/state/themes/selectors';
import { getIsLoadingCart } from 'calypso/state/themes/selectors/get-is-loading-cart';
import { getBackPath } from 'calypso/state/themes/themes-ui/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import { getTheme } from '../../state/themes/selectors';
import EligibilityWarningModal from '../themes/atomic-transfer-dialog';
import { LivePreviewButton } from './live-preview-button';
import ThemeDownloadCard from './theme-download-card';
import ThemeFeaturesCard from './theme-features-card';
import ThemeNotFoundError from './theme-not-found-error';
import ThemeStyleVariations from './theme-style-variations';

import './style.scss';

const LivePreviewButton = ( {
isActive,
isAtomic,
isExternallyManagedTheme,
isWporg,
showTryAndCustomize,
siteSlug,
stylesheet,
} ) => {
if ( isActive ) {
return null;
}
if ( showTryAndCustomize ) {
return null;
}
if ( isAtomic ) {
return null;
}
if ( isExternallyManagedTheme || isWporg ) {
return null;
}
return (
<Button href={ `https://${ siteSlug }/wp-admin/site-editor.php?theme_preview=${ stylesheet }` }>
Live Preview (PoC)
</Button>
);
};

const noop = () => {};

class ThemeSheet extends Component {
Expand Down Expand Up @@ -648,6 +625,11 @@ class ThemeSheet extends Component {
isExternallyManagedTheme,
isWporg,
isLoggedIn,
canUseTheme,
isFullSiteEditingTheme,
isSimple,
isThemeInstalledOnAtomicSite,
themeId,
} = this.props;
const placeholder = <span className="theme__sheet-placeholder">loading.....</span>;
const title = name || placeholder;
Expand All @@ -671,21 +653,27 @@ class ThemeSheet extends Component {
<span className="theme__sheet-main-info-tag">{ tag }</span>
</div>
<div className="theme__sheet-main-actions">
{ config.isEnabled( 'themes/block-theme-previews-poc' ) && (
{ shouldRenderButton &&
( this.shouldRenderUnlockStyleButton()
? this.renderUnlockStyleButton()
: this.renderButton() ) }
{ config.isEnabled( 'themes/block-theme-previews' ) && (
<LivePreviewButton
canUseTheme={ canUseTheme }
isActive={ isActive }
isAtomic={ isAtomic }
isExternallyManagedTheme={ isExternallyManagedTheme }
isFullSiteEditingTheme={ isFullSiteEditingTheme }
isSimple={ isSimple }
isThemeInstalledOnAtomicSite={ isThemeInstalledOnAtomicSite }
isWporg={ isWporg }
showTryAndCustomize={ showTryAndCustomize }
siteSlug={ siteSlug }
stylesheet={ stylesheet }
themeId={ themeId }
translate={ translate }
></LivePreviewButton>
) }
{ shouldRenderButton &&
( this.shouldRenderUnlockStyleButton()
? this.renderUnlockStyleButton()
: this.renderButton() ) }
{ this.shouldRenderPreviewButton() && (
<Button
onClick={ ( e ) => {
Expand Down Expand Up @@ -1389,6 +1377,7 @@ class ThemeSheet extends Component {
return (
<Main className={ className }>
<QueryCanonicalTheme themeId={ this.props.themeId } siteId={ siteId } />
<QueryTheme themeId={ this.props.themeId } siteId={ siteId } />
<QueryProductsList />
<QueryUserPurchases />
{
Expand Down Expand Up @@ -1575,6 +1564,12 @@ export default connect(
const isMarketplaceThemeSubscribed =
isExternallyManagedTheme && getIsMarketplaceThemeSubscribed( state, theme?.id, siteId );

const isThemeInstalledOnAtomicSite = isAtomic && !! getTheme( state, siteId, themeId );

const isFullSiteEditingTheme = config.isEnabled( 'themes/block-theme-previews' )
? getIsFullSiteEditingTheme( state, themeId )
: undefined;

return {
...theme,
themeId,
Expand All @@ -1592,15 +1587,19 @@ export default connect(
isActive: isThemeActive( state, themeId, siteId ),
isJetpack,
isAtomic,
isFullSiteEditingTheme,
isStandaloneJetpack,
isVip: isVipSite( state, siteId ),
isPremium: isThemePremium( state, themeId ),
isThemeInstalledOnAtomicSite,
isThemePurchased: isPremiumThemeAvailable( state, themeId, siteId ),
isBundledSoftwareSet: doesThemeBundleSoftwareSet( state, themeId ),
isSimple: isSimpleSite( state, siteId ),
isSiteBundleEligible: isSiteEligibleForBundledSoftware( state, siteId ),
forumUrl: getThemeForumUrl( state, themeId, siteId ),
hasUnlimitedPremiumThemes: siteHasFeature( state, siteId, WPCOM_FEATURES_PREMIUM_THEMES ),
showTryAndCustomize: shouldShowTryAndCustomize( state, themeId, siteId ),
canUseTheme: getCanUseTheme( state, siteId, themeId ),
canUserUploadThemes: siteHasFeature( state, siteId, FEATURE_UPLOAD_THEMES ),
// Remove the trailing slash because the page URL doesn't have one either.
canonicalUrl: localizeUrl( englishUrl, getLocaleSlug(), false ).replace( /\/$/, '' ),
Expand Down
2 changes: 1 addition & 1 deletion client/my-sites/theme/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ $theme-sheet-content-max-width: 1462px;
width: 100%;
}

button {
.button {
border-radius: 4px;

@include breakpoint-deprecated( "<960px" ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function isFullSiteEditingTheme(
if ( ! themeId ) {
return false;
}
const theme = getTheme( state, 'wpcom', themeId );
const theme = getTheme( state, 'wpcom', themeId ) || getTheme( state, 'wporg', themeId );
if ( ! theme ) {
return false;
}
Expand Down
1 change: 0 additions & 1 deletion config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@
"subscription-management/comments-list-controls": true,
"subscription-management-redirect-following": true,
"themes/atomic-homepage-replace": true,
"themes/block-theme-previews-poc": true,
"themes/block-theme-previews": true,
"themes/display-thank-you-page-for-woo": true,
"themes/premium": true,
Expand Down
1 change: 0 additions & 1 deletion config/horizon.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
"subscription-management/comments-list-controls": true,
"subscription-management-redirect-following": true,
"themes/atomic-homepage-replace": true,
"themes/block-theme-previews-poc": true,
"themes/block-theme-previews": true,
"themes/display-thank-you-page-for-woo": true,
"themes/premium": true,
Expand Down
1 change: 0 additions & 1 deletion config/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
"subscription-management/comments-list-controls": true,
"subscription-management-redirect-following": true,
"themes/atomic-homepage-replace": true,
"themes/block-theme-previews-poc": false,
"themes/block-theme-previews": false,
"themes/display-thank-you-page-for-woo": true,
"themes/premium": true,
Expand Down
1 change: 0 additions & 1 deletion config/stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
"subscription-management/comments-list-controls": true,
"subscription-management-redirect-following": true,
"themes/atomic-homepage-replace": true,
"themes/block-theme-previews-poc": false,
"themes/block-theme-previews": false,
"themes/display-thank-you-page-for-woo": true,
"themes/premium": true,
Expand Down
1 change: 0 additions & 1 deletion config/wpcalypso.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@
"subscription-management/comments-list-controls": true,
"subscription-management-redirect-following": true,
"themes/atomic-homepage-replace": true,
"themes/block-theme-previews-poc": true,
"themes/block-theme-previews": true,
"themes/display-thank-you-page-for-woo": true,
"themes/premium": true,
Expand Down