-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Live Preview: Add the upgrade notice for Premium and Woo themes #83630
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9fc2c0d
Add the upgrade notice
okmttdhr 4b9e60f
Make the logic available to both general and upgrade notice
okmttdhr 4d347c8
Avoid showing the redundant notice
okmttdhr 30424aa
Combine the copies
okmttdhr 355b5c0
Convey two pieces of info
okmttdhr 24c73e7
Support the site with a custom domain
okmttdhr fecbe82
Fixed UI that appeared to be off
okmttdhr d442358
Woo Commerce -> WooCommerce
okmttdhr f26b93d
Remove unnecessary rendering
okmttdhr 76fef97
Update apps/wpcom-block-editor/src/wpcom/features/live-preview/upgrad…
okmttdhr d792e75
Remove `requiredPlan`
okmttdhr 2f9b04b
Add a comment about required plan
okmttdhr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 0 additions & 126 deletions
126
apps/wpcom-block-editor/src/wpcom/features/live-preview.tsx
This file was deleted.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
...om-block-editor/src/wpcom/features/live-preview/hooks/use-can-preview-but-need-upgrade.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { | ||
FEATURE_WOOP, | ||
WPCOM_FEATURES_ATOMIC, | ||
WPCOM_FEATURES_PREMIUM_THEMES, | ||
PLAN_PREMIUM, | ||
PLAN_BUSINESS, | ||
} from '@automattic/calypso-products'; | ||
import { getCalypsoUrl } from '@automattic/calypso-url'; | ||
import { useEffect, useState, useCallback } from 'react'; | ||
import wpcom from 'calypso/lib/wp'; | ||
import { PREMIUM_THEME, WOOCOMMERCE_THEME } from '../utils'; | ||
import { usePreviewingTheme } from './use-previewing-theme'; | ||
import type { SiteDetails } from '@automattic/data-stores'; | ||
|
||
const checkNeedUpgrade = ( { | ||
site, | ||
previewingThemeType, | ||
}: { | ||
site?: SiteDetails; | ||
previewingThemeType?: string; | ||
} ) => { | ||
const activeFeatures = site?.plan?.features?.active; | ||
if ( ! activeFeatures ) { | ||
return false; | ||
} | ||
|
||
/** | ||
* This logic is extracted from `client/state/themes/selectors/can-use-theme.js`. | ||
*/ | ||
const canUseWooCommerceTheme = | ||
previewingThemeType === WOOCOMMERCE_THEME && | ||
[ WPCOM_FEATURES_PREMIUM_THEMES, FEATURE_WOOP, WPCOM_FEATURES_ATOMIC ].every( ( feature ) => | ||
activeFeatures.includes( feature ) | ||
); | ||
if ( canUseWooCommerceTheme ) { | ||
return false; | ||
} | ||
const canUsePremiumTheme = | ||
previewingThemeType === PREMIUM_THEME && | ||
activeFeatures.includes( WPCOM_FEATURES_PREMIUM_THEMES ); | ||
if ( canUsePremiumTheme ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
export const useCanPreviewButNeedUpgrade = ( { | ||
previewingTheme, | ||
}: { | ||
previewingTheme: ReturnType< typeof usePreviewingTheme >; | ||
} ) => { | ||
const [ canPreviewButNeedUpgrade, setCanPreviewButNeedUpgrade ] = useState( false ); | ||
const [ siteSlug, setSiteSlug ] = useState< string | undefined >(); | ||
|
||
/** | ||
* Get the theme and site info to decide whether the user needs to upgrade the plan. | ||
*/ | ||
useEffect( () => { | ||
/** | ||
* Currently, Live Preview only supports upgrades for WooCommerce and Premium themes. | ||
*/ | ||
if ( previewingTheme.type !== WOOCOMMERCE_THEME && previewingTheme.type !== PREMIUM_THEME ) { | ||
setCanPreviewButNeedUpgrade( false ); | ||
return; | ||
} | ||
|
||
wpcom.req | ||
.get( `/sites/${ window._currentSiteId }`, { apiVersion: '1.2' } ) | ||
.then( ( site: any ) => { | ||
let parsedUrl; | ||
try { | ||
parsedUrl = new URL( site.URL ); | ||
const { hostname } = parsedUrl; | ||
setSiteSlug( hostname ); | ||
} catch { | ||
// Invalid URL | ||
} | ||
return site; | ||
} ) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
.then( ( site: any ) => { | ||
if ( ! checkNeedUpgrade( { site, previewingThemeType: previewingTheme.type } ) ) { | ||
setCanPreviewButNeedUpgrade( false ); | ||
return; | ||
} | ||
setCanPreviewButNeedUpgrade( true ); | ||
} ) | ||
.catch( () => { | ||
// do nothing | ||
} ); | ||
}, [ previewingTheme.type, setCanPreviewButNeedUpgrade, setSiteSlug ] ); | ||
|
||
const upgradePlan = useCallback( () => { | ||
const generateCheckoutUrl = ( plan: string ) => { | ||
const locationHref = window.location.href; | ||
let url = locationHref; | ||
try { | ||
/** | ||
* If the site has a custom domain, change the hostname to a custom domain. | ||
* This allows the checkout to redirect back to the custom domain. | ||
* @see `client/my-sites/checkout/src/hooks/use-valid-checkout-back-url.ts` | ||
*/ | ||
if ( siteSlug ) { | ||
const parsedUrl = new URL( locationHref ); | ||
parsedUrl.hostname = siteSlug; | ||
url = parsedUrl.toString(); | ||
} | ||
} catch ( error ) { | ||
// Do nothing. | ||
} | ||
return `${ getCalypsoUrl() }/checkout/${ | ||
window._currentSiteId | ||
}/${ plan }?redirect_to=${ encodeURIComponent( url ) }&checkoutBackUrl=${ encodeURIComponent( | ||
url | ||
) }`; | ||
}; | ||
const link = | ||
previewingTheme.type === WOOCOMMERCE_THEME | ||
? /** | ||
* For a WooCommerce theme, the users should have the Business plan or higher, | ||
* AND the WooCommerce plugin has to be installed. | ||
*/ | ||
generateCheckoutUrl( PLAN_BUSINESS ) | ||
: // For a Premium theme, the users should have the Premium plan or higher. | ||
generateCheckoutUrl( PLAN_PREMIUM ); | ||
window.location.href = link; | ||
|
||
// TODO: Add the track event. | ||
}, [ previewingTheme.type, siteSlug ] ); | ||
|
||
return { | ||
canPreviewButNeedUpgrade, | ||
upgradePlan, | ||
}; | ||
}; |
63 changes: 63 additions & 0 deletions
63
apps/wpcom-block-editor/src/wpcom/features/live-preview/hooks/use-previewing-theme.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useState } from 'react'; | ||
import wpcom from 'calypso/lib/wp'; | ||
import { currentlyPreviewingTheme, PREMIUM_THEME, WOOCOMMERCE_THEME } from '../utils'; | ||
import type { Theme } from 'calypso/types'; | ||
|
||
/** | ||
* Get the theme type. | ||
* This only support WooCommerce and Premium themes. | ||
*/ | ||
const getThemeType = ( theme?: Theme ) => { | ||
const theme_software_set = theme?.taxonomies?.theme_software_set; | ||
const isWooCommerceTheme = theme_software_set && theme_software_set.length > 0; | ||
if ( isWooCommerceTheme ) { | ||
return WOOCOMMERCE_THEME; | ||
} | ||
const themeStylesheet = theme?.stylesheet; | ||
const isPremiumTheme = themeStylesheet && themeStylesheet.startsWith( 'premium/' ); | ||
if ( isPremiumTheme ) { | ||
return PREMIUM_THEME; | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const usePreviewingTheme = () => { | ||
const previewingThemeSlug = currentlyPreviewingTheme(); | ||
const previewingThemeId = | ||
( previewingThemeSlug as string )?.split( '/' )?.[ 1 ] || previewingThemeSlug; | ||
const [ previewingThemeName, setPreviewingThemeName ] = useState< string >( | ||
previewingThemeSlug as string | ||
); | ||
const [ previewingThemeType, setPreviewingThemeType ] = useState< string >(); | ||
const previewingThemeTypeDisplay = | ||
previewingThemeType === WOOCOMMERCE_THEME ? 'WooCommerce' : 'Premium'; | ||
|
||
useEffect( () => { | ||
wpcom.req | ||
.get( `/themes/${ previewingThemeId }`, { apiVersion: '1.2' } ) | ||
.then( ( theme: Theme ) => { | ||
const name = theme?.name; | ||
if ( name ) { | ||
setPreviewingThemeName( name ); | ||
} | ||
return theme; | ||
} ) | ||
.then( ( theme: Theme ) => { | ||
const type = getThemeType( theme ); | ||
if ( ! type ) { | ||
return; | ||
} | ||
setPreviewingThemeType( type ); | ||
} ) | ||
.catch( () => { | ||
// do nothing | ||
} ); | ||
return; | ||
}, [ previewingThemeId ] ); | ||
|
||
return { | ||
name: previewingThemeName, | ||
type: previewingThemeType, | ||
typeDisplay: previewingThemeTypeDisplay, | ||
}; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a theme is a WooCommerce theme if it has
woo-on-plans
in the software sets, can you double check?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to be right (I might look into deeper a bit).
The
getThemeType
selectorwp-calypso/client/state/themes/selectors/get-theme-type.js
Lines 27 to 29 in 8ad064c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getThemeType
was first introduced in #75396 (and then extracted as a selector in #77550).I believe we are not purposely using
doesThemeBundleSoftwareSet
for theWOOCOMMERCE_THEME
condition. 🙂 I'll address this in another PR so that it checkswoo-on-plans
(e.g.,wp-calypso/client/landing/stepper/declarative-flow/internals/steps-repository/design-setup/unified-design-picker.tsx
Lines 442 to 444 in 33afc9a
wp-calypso/packages/data-stores/src/starter-designs-queries/use-starter-designs-query.ts
Lines 98 to 100 in 33afc9a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getThemeType
is being refactored in a different way in https://github.com/Automattic/wp-calypso/pull/84483/files#diff-136055987b5718b1aa6eb57c3cda1dad677dd540def25ff13f9985936cb14019 I haven't dug into it yet but it looks like it is using the concept of "bundled theme" rather than specific woo/sensei theme types