| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { safeParseJson } from '@shared/lib/parsing'; |
| 2 | +import { siteHost } from '@shared/lib/site-host'; | |
| 2 | 3 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | 4 | import { decodeEntities } from '@wordpress/html-entities'; |
| 4 | 5 | |
| 5 | 6 | const { hostname } = window.location; |
| @@ -12,11 +13,13 @@ | ||
| 12 | 13 | showSecondaryTask, |
| 13 | 14 | stagingSites, |
| 14 | 15 | } = window.extAssistData?.domainsSuggestionSettings || {}; |
| 15 | 16 | |
| 16 | -const hasDomains = | |
| 17 | - (safeParseJson(window.extSharedData.resourceData)?.domains || [])?.length > 0; | |
| 17 | +const parsedDomains = safeParseJson(window.extSharedData.resourceData)?.domains; | |
| 18 | +export const domains = Array.isArray(parsedDomains) ? parsedDomains : []; | |
| 18 | 19 | |
| 20 | +const hasDomains = domains.length > 0; | |
| 21 | + | |
| 19 | 22 | const domainByLanguage = (lang, urlList) => { |
| 20 | 23 | try { |
| 21 | 24 | const urls = JSON.parse(decodeEntities(urlList)); |
| 22 | 25 | return urls?.[lang] ?? urls?.default ?? false; |
| @@ -29,11 +32,11 @@ | ||
| 29 | 32 | devbuild && !searchUrl |
| 30 | 33 | ? 'https://extendify.com?s={DOMAIN}' |
| 31 | 34 | : domainByLanguage(wpLanguage, searchUrl); |
| 32 | 35 | |
| 33 | -const isStagingDomain = | |
| 34 | - stagingSites.filter((l) => hostname.toLowerCase().includes(l))?.length > 0 || | |
| 35 | - false; | |
| 36 | +const isStagingDomain = (stagingSites || []).some((l) => | |
| 37 | + hostname.toLowerCase().includes(l), | |
| 38 | +); | |
| 36 | 39 | |
| 37 | 40 | // Show if it's not a staging domain, has a title, and is enabled |
| 38 | 41 | export const showDomainBanner = (() => { |
| 39 | 42 | if (devbuild) return true; |
| @@ -74,16 +77,21 @@ | ||
| 74 | 77 | * The domainSearchUrl may contain both {SLD} and {TLD} placeholders |
| 75 | 78 | * e.g., https://example.com?sld={SLD}&tld={TLD} |
| 76 | 79 | * If not present, fallback to {DOMAIN} format |
| 77 | 80 | */ |
| 78 | -export const createDomainUrlLink = (domainSearchUrl, domain) => { | |
| 79 | - if (domainSearchUrl.includes('{SLD}') && domainSearchUrl.includes('{TLD}')) { | |
| 81 | +export const createDomainUrlLink = ( | |
| 82 | + domainSearchUrl, | |
| 83 | + domain, | |
| 84 | + host = siteHost(), | |
| 85 | +) => { | |
| 86 | + const url = domainSearchUrl.replaceAll('{SITEURL}', host); | |
| 87 | + if (url.includes('{SLD}') && url.includes('{TLD}')) { | |
| 80 | 88 | const parts = domain.toLowerCase().split('.'); |
| 81 | 89 | const sld = parts[0]; |
| 82 | 90 | const tld = parts.slice(1).join('.'); |
| 83 | - return domainSearchUrl.replace('{SLD}', sld).replace('{TLD}', `.${tld}`); | |
| 91 | + return url.replace('{SLD}', sld).replace('{TLD}', `.${tld}`); | |
| 84 | 92 | } |
| 85 | - return domainSearchUrl.replace('{DOMAIN}', domain.toLowerCase()); | |
| 93 | + return url.replace('{DOMAIN}', domain.toLowerCase()); | |
| 86 | 94 | }; |
| 87 | 95 | |
| 88 | 96 | export const deleteDomainCache = () => |
| 89 | 97 | apiFetch({ |
| @@ -89,4 +97,10 @@ | ||
| 89 | 97 | apiFetch({ |
| 90 | 98 | path: 'extendify/v1/assist/delete-domains-recommendations', |
| 91 | 99 | method: 'POST', |
| 92 | 100 | }); |
| 101 | + | |
| 102 | +export const buildDomainViewItems = (domains) => | |
| 103 | + domains.map((domain, i) => ({ | |
| 104 | + domain, | |
| 105 | + type: i === 0 ? 'primary' : 'secondary', | |
| 106 | + })); | |