Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/studio/src/lib/generate-checkout-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEFAULT_CUSTOM_DOMAIN_SUFFIX } from '@studio/common/constants';
import { stripLocalDomainSuffix } from '@studio/common/lib/domains';

export function generateCheckoutUrl(
selectedSite?: SiteDetails,
Expand All @@ -15,7 +15,7 @@ export function generateCheckoutUrl(
}

const suggestedName = selectedSite.customDomain
? selectedSite.customDomain.replace( DEFAULT_CUSTOM_DOMAIN_SUFFIX, '' )
? stripLocalDomainSuffix( selectedSite.customDomain )
: selectedSite.name;

url.searchParams.set( 'studioSiteId', String( selectedSite.id ) );
Expand Down
6 changes: 5 additions & 1 deletion apps/ui/src/data/core/connectors/publish-checkout-url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stripLocalDomainSuffix } from '@studio/common/lib/domains';
import type { SiteDetails } from '../types';

export function buildPublishCheckoutUrl( site: SiteDetails ): string {
Expand All @@ -6,7 +7,10 @@ export function buildPublishCheckoutUrl( site: SiteDetails ): string {
url.searchParams.set( 'section', 'publish-site' );
url.searchParams.set( 'showDomainStep', 'true' );
url.searchParams.set( 'studioSiteId', site.id );
url.searchParams.set( 'new', site.customDomain ?? site.name );
url.searchParams.set(
'new',
site.customDomain ? stripLocalDomainSuffix( site.customDomain ) : site.name
);
url.searchParams.set( 'autoOpenPush', 'true' );
return url.toString();
}
1 change: 1 addition & 0 deletions packages/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const CERT_UNTRUSTED_ROOT = 'CERT_TRUST_IS_UNTRUSTED_ROOT'; // Windows AP

// Custom domains
export const DEFAULT_CUSTOM_DOMAIN_SUFFIX = '.wp.local';
export const LOCAL_DOMAIN_SUFFIX = '.local';

// WordPress constants
export const MINIMUM_WORDPRESS_VERSION = '6.2.1' as const; // https://wordpress.github.io/wordpress-playground/blueprints/examples/#load-an-older-wordpress-version
Expand Down
14 changes: 12 additions & 2 deletions packages/common/lib/domains.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { __ } from '@wordpress/i18n';
import { DEFAULT_CUSTOM_DOMAIN_SUFFIX } from '@studio/common/constants';
import { DEFAULT_CUSTOM_DOMAIN_SUFFIX, LOCAL_DOMAIN_SUFFIX } from '@studio/common/constants';
import { sanitizeFolderName } from './sanitize-folder-name';

const DOMAIN_PATTERN =
Expand All @@ -14,6 +14,16 @@ export const generateCustomDomainFromSiteName = ( siteName: string ): string =>
return `${ domainBase }${ DEFAULT_CUSTOM_DOMAIN_SUFFIX }`;
};

export const stripLocalDomainSuffix = ( domain: string ): string => {
if ( domain.endsWith( DEFAULT_CUSTOM_DOMAIN_SUFFIX ) ) {
return domain.slice( 0, -DEFAULT_CUSTOM_DOMAIN_SUFFIX.length );
}
if ( domain.endsWith( LOCAL_DOMAIN_SUFFIX ) ) {
return domain.slice( 0, -LOCAL_DOMAIN_SUFFIX.length );
}
return domain;
};

export const getDomainNameValidationError = (
useCustomDomain: boolean,
domainName: string | null,
Expand All @@ -39,7 +49,7 @@ export const getDomainNameValidationError = (
return __( 'The domain name is too long' );
}

if ( ! domainName.endsWith( '.local' ) ) {
if ( ! domainName.endsWith( LOCAL_DOMAIN_SUFFIX ) ) {
return __( 'The domain name must end with .local' );
}

Expand Down
26 changes: 26 additions & 0 deletions packages/common/lib/tests/domains.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @vitest-environment node
*/
import { stripLocalDomainSuffix } from '@studio/common/lib/domains';

describe( 'stripLocalDomainSuffix', () => {
it( 'strips the default .wp.local suffix entirely', () => {
expect( stripLocalDomainSuffix( 'mysite.wp.local' ) ).toBe( 'mysite' );
} );

it( 'strips a single trailing .local label', () => {
expect( stripLocalDomainSuffix( 'mysite.local' ) ).toBe( 'mysite' );
} );

it( 'keeps inner labels when stripping .local', () => {
expect( stripLocalDomainSuffix( 'mysite.com.local' ) ).toBe( 'mysite.com' );
} );

it( 'only strips the suffix, not inner occurrences', () => {
expect( stripLocalDomainSuffix( 'wp.local.mysite.local' ) ).toBe( 'wp.local.mysite' );
} );

it( 'leaves non-local domains unchanged', () => {
expect( stripLocalDomainSuffix( 'mysite.com' ) ).toBe( 'mysite.com' );
} );
} );