File size: 4,748 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import { doesStringResembleDomain } from '@automattic/onboarding';
import { translate } from 'i18n-calypso';
import { untrailingslashit } from 'calypso/lib/route';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { getSelectedSite } from 'calypso/state/ui/selectors';
import type { Context } from '@automattic/calypso-router';
/**
* Return the product slug or product alias from a checkout route.
*
* Checkout routes support including a product slug (eg: `value_bundle`) or
* alias (eg: `premium`) in the URL. The most common checkout route uses the
* format `/checkout/:site/:product`, but it also supports the path
* `/checkout/:product/:site`.
*
* Checkout also supports routes that only include one or the other, using the
* formats `/checkout/:product` and `/checkout/:site`.
*
* This function tries to determine which part of the path is the product, if
* any, and return it. It will return an empty string if no product is found.
*
* Some examples of valid paths:
*
* 1. `/checkout/example.com` (path contains a domain)
* 2. `/checkout/no-adverts%25no-adverts.php` (path contains a product)
* 3. `/checkout/domain-mapping:example.com` (path contains a product)
* 4. `/checkout/example.com::blog` (path contains a domain)
* 5. `/checkout/example.com::blog/pro` (path contains a domain followed by a product)
* 6. `/checkout/pro/example.com::blog` (path contains a product followed by a domain)
*/
export function getProductSlugFromContext( context: Context ): string | undefined {
const { params, store, pathname } = context;
const { domainOrProduct, product, productSlug } = params;
const state = store.getState();
const selectedSite = getSelectedSite( state );
const isGiftPurchase = pathname.includes( '/gift/' );
// Jetpack siteless checkout routes always use `:productSlug` in the path.
if ( isContextJetpackSitelessCheckout( context ) ) {
return productSlug;
}
// Gift purchase routes always use `:product` in the path.
if ( isGiftPurchase ) {
return product;
}
if ( ! domainOrProduct && ! product ) {
return '';
}
// If one matches the selected site, return the other.
if ( domainOrProduct && selectedSite?.slug && selectedSite.slug === domainOrProduct ) {
return product || '';
}
if ( product && selectedSite?.slug && selectedSite.slug === product ) {
return domainOrProduct || '';
}
// If there is no selected site, there will never be a `product`, and the
// `domainOrProduct` must be a product slug because a domain would have
// selected a site.
if ( ! selectedSite?.slug ) {
return domainOrProduct || '';
}
// If one string is a valid URL, and there is another, use the other one.
if ( domainOrProduct ) {
const isDomain = doesStringResembleDomain( domainOrProduct );
if ( isDomain && product ) {
return product;
}
if ( ! isDomain ) {
return domainOrProduct;
}
}
if ( product ) {
const isDomain = doesStringResembleDomain( product );
if ( isDomain && domainOrProduct ) {
return domainOrProduct;
}
if ( ! isDomain ) {
return product;
}
}
return '';
}
/**
* Prepends "http(s)" to user-supplied URL if protocol is missing.
* @param {string} inputUrl User-supplied URL
* @param {?boolean} httpsIsDefault Default to 'https' if true vs 'http' if false
* @returns {string} URL string with http(s) included
*/
export function addHttpIfMissing( inputUrl: string, httpsIsDefault = true ): string {
const scheme = httpsIsDefault ? 'https' : 'http';
let url = inputUrl.trim().toLowerCase();
if ( url && url.substr( 0, 4 ) !== 'http' ) {
url = `${ scheme }://` + url;
}
return untrailingslashit( url );
}
export function isContextJetpackSitelessCheckout( context: Context ): boolean {
const hasJetpackPurchaseToken = Boolean( context.query.purchasetoken );
const hasJetpackPurchaseNonce = Boolean( context.query.purchaseNonce );
const isUserComingFromLoginForm = context.query?.flow === 'coming_from_login';
const isUserComingFromPlansPage = [ 'jetpack-plans', 'jetpack-connect-plans' ].includes(
context.query?.source
);
const state = context.store.getState();
const isLoggedOut = ! isUserLoggedIn( state );
if ( ! context.pathname.includes( '/checkout/jetpack' ) ) {
return false;
}
if ( ! isLoggedOut && ! isUserComingFromLoginForm && ! isUserComingFromPlansPage ) {
return false;
}
if ( ! hasJetpackPurchaseToken && ! hasJetpackPurchaseNonce ) {
return false;
}
return true;
}
export function isContextSourceMyJetpack( context: Context ): boolean {
return context.query?.source === 'my-jetpack';
}
export function getAffiliateCouponLabel(): string {
// translators: The label of the coupon line item in checkout
return translate( 'Exclusive Offer Applied' );
}
|