| 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 hasDomains = |
| 17 |
(safeParseJson(window.extSharedData.resourceData)?.domains || [])?.length > 0; |
| 18 |
|
| 19 |
const domainByLanguage = (lang, urlList) => { |
| 20 |
try { |
| 21 |
const urls = JSON.parse(decodeEntities(urlList)); |
| 22 |
return urls?.[lang] ?? urls?.default ?? false; |
| 23 |
} catch (_e) { |
| 24 |
return decodeEntities(urlList) || false; |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
export const domainSearchUrl = |
| 29 |
devbuild && !searchUrl |
| 30 |
? 'https://extendify.com?s={DOMAIN}' |
| 31 |
: domainByLanguage(wpLanguage, searchUrl); |
| 32 |
|
| 33 |
const isStagingDomain = |
| 34 |
stagingSites.filter((l) => hostname.toLowerCase().includes(l))?.length > 0 || |
| 35 |
false; |
| 36 |
|
| 37 |
// Show if it's not a staging domain, has a title, and is enabled |
| 38 |
export const showDomainBanner = (() => { |
| 39 |
if (devbuild) return true; |
| 40 |
if (!showBanner) return false; |
| 41 |
if (!hasDomains) return false; |
| 42 |
if (!siteTitle) return false; |
| 43 |
return isStagingDomain; |
| 44 |
})(); |
| 45 |
|
| 46 |
// Show if it's not a staging domain, has a title, and is enabled |
| 47 |
export const showDomainTask = (() => { |
| 48 |
if (devbuild) return true; |
| 49 |
if (!showTask) return false; |
| 50 |
if (!hasDomains) return false; |
| 51 |
if (!siteTitle) return false; |
| 52 |
return isStagingDomain; |
| 53 |
})(); |
| 54 |
|
| 55 |
// Show if it's a staging domain, has a title, and is enabled |
| 56 |
export const showSecondaryDomainBanner = (() => { |
| 57 |
if (devbuild) return true; |
| 58 |
if (!showSecondaryBanner) return false; |
| 59 |
if (!hasDomains) return false; |
| 60 |
if (!siteTitle) return false; |
| 61 |
return !isStagingDomain; |
| 62 |
})(); |
| 63 |
|
| 64 |
// Show if it's a staging domain, has a title, and is enabled |
| 65 |
export const showSecondaryDomainTask = (() => { |
| 66 |
if (devbuild) return true; |
| 67 |
if (!showSecondaryTask) return false; |
| 68 |
if (!hasDomains) return false; |
| 69 |
if (!siteTitle) return false; |
| 70 |
return !isStagingDomain; |
| 71 |
})(); |
| 72 |
|
| 73 |
/** |
| 74 |
* The domainSearchUrl may contain both {SLD} and {TLD} placeholders |
| 75 |
* e.g., https://example.com?sld={SLD}&tld={TLD} |
| 76 |
* If not present, fallback to {DOMAIN} format |
| 77 |
*/ |
| 78 |
export const createDomainUrlLink = (domainSearchUrl, domain) => { |
| 79 |
if (domainSearchUrl.includes('{SLD}') && domainSearchUrl.includes('{TLD}')) { |
| 80 |
const parts = domain.toLowerCase().split('.'); |
| 81 |
const sld = parts[0]; |
| 82 |
const tld = parts.slice(1).join('.'); |
| 83 |
return domainSearchUrl.replace('{SLD}', sld).replace('{TLD}', `.${tld}`); |
| 84 |
} |
| 85 |
return domainSearchUrl.replace('{DOMAIN}', domain.toLowerCase()); |
| 86 |
}; |
| 87 |
|
| 88 |
export const deleteDomainCache = () => |
| 89 |
apiFetch({ |
| 90 |
path: 'extendify/v1/assist/delete-domains-recommendations', |
| 91 |
method: 'POST', |
| 92 |
}); |
| 93 |
|