| 1 |
import { safeParseJson } from '@shared/lib/parsing'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 4 |
|
| 5 |
const { hostname } = window.location; |
| 6 |
const { devbuild, siteTitle, wpLanguage } = window.extSharedData; |
| 7 |
const { |
| 8 |
showBanner, |
| 9 |
showTask, |
| 10 |
searchUrl, |
| 11 |
showSecondaryBanner, |
| 12 |
showSecondaryTask, |
| 13 |
stagingSites, |
| 14 |
} = window.extAssistData?.domainsSuggestionSettings || {}; |
| 15 |
|
| 16 |
const parsedDomains = safeParseJson(window.extSharedData.resourceData)?.domains; |
| 17 |
export const domains = Array.isArray(parsedDomains) ? parsedDomains : []; |
| 18 |
|
| 19 |
const hasDomains = domains.length > 0; |
| 20 |
|
| 21 |
const domainByLanguage = (lang, urlList) => { |
| 22 |
try { |
| 23 |
const urls = JSON.parse(decodeEntities(urlList)); |
| 24 |
return urls?.[lang] ?? urls?.default ?? false; |
| 25 |
} catch (_e) { |
| 26 |
return decodeEntities(urlList) || false; |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
export const domainSearchUrl = |
| 31 |
devbuild && !searchUrl |
| 32 |
? 'https://extendify.com?s={DOMAIN}' |
| 33 |
: domainByLanguage(wpLanguage, searchUrl); |
| 34 |
|
| 35 |
const isStagingDomain = (stagingSites || []).some((l) => |
| 36 |
hostname.toLowerCase().includes(l), |
| 37 |
); |
| 38 |
|
| 39 |
// Show if it's not a staging domain, has a title, and is enabled |
| 40 |
export const showDomainBanner = (() => { |
| 41 |
if (devbuild) return true; |
| 42 |
if (!showBanner) return false; |
| 43 |
if (!hasDomains) return false; |
| 44 |
if (!siteTitle) return false; |
| 45 |
return isStagingDomain; |
| 46 |
})(); |
| 47 |
|
| 48 |
// Show if it's not a staging domain, has a title, and is enabled |
| 49 |
export const showDomainTask = (() => { |
| 50 |
if (devbuild) return true; |
| 51 |
if (!showTask) return false; |
| 52 |
if (!hasDomains) return false; |
| 53 |
if (!siteTitle) return false; |
| 54 |
return isStagingDomain; |
| 55 |
})(); |
| 56 |
|
| 57 |
// Show if it's a staging domain, has a title, and is enabled |
| 58 |
export const showSecondaryDomainBanner = (() => { |
| 59 |
if (devbuild) return true; |
| 60 |
if (!showSecondaryBanner) return false; |
| 61 |
if (!hasDomains) return false; |
| 62 |
if (!siteTitle) return false; |
| 63 |
return !isStagingDomain; |
| 64 |
})(); |
| 65 |
|
| 66 |
// Show if it's a staging domain, has a title, and is enabled |
| 67 |
export const showSecondaryDomainTask = (() => { |
| 68 |
if (devbuild) return true; |
| 69 |
if (!showSecondaryTask) return false; |
| 70 |
if (!hasDomains) return false; |
| 71 |
if (!siteTitle) return false; |
| 72 |
return !isStagingDomain; |
| 73 |
})(); |
| 74 |
|
| 75 |
/** |
| 76 |
* The domainSearchUrl may contain both {SLD} and {TLD} placeholders |
| 77 |
* e.g., https://example.com?sld={SLD}&tld={TLD} |
| 78 |
* If not present, fallback to {DOMAIN} format |
| 79 |
*/ |
| 80 |
export const createDomainUrlLink = (domainSearchUrl, domain) => { |
| 81 |
if (domainSearchUrl.includes('{SLD}') && domainSearchUrl.includes('{TLD}')) { |
| 82 |
const parts = domain.toLowerCase().split('.'); |
| 83 |
const sld = parts[0]; |
| 84 |
const tld = parts.slice(1).join('.'); |
| 85 |
return domainSearchUrl.replace('{SLD}', sld).replace('{TLD}', `.${tld}`); |
| 86 |
} |
| 87 |
return domainSearchUrl.replace('{DOMAIN}', domain.toLowerCase()); |
| 88 |
}; |
| 89 |
|
| 90 |
export const deleteDomainCache = () => |
| 91 |
apiFetch({ |
| 92 |
path: 'extendify/v1/assist/delete-domains-recommendations', |
| 93 |
method: 'POST', |
| 94 |
}); |
| 95 |
|
| 96 |
export const buildDomainViewItems = (domains) => |
| 97 |
domains.map((domain, i) => ({ |
| 98 |
domain, |
| 99 |
type: i === 0 ? 'primary' : 'secondary', |
| 100 |
})); |
| 101 |
|