PluginProbe
Extendify / 3.1.4
Extendify v3.1.4
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / AutoLaunch / fetchers / get-home.js

get-home.js in Extendify 3.1.4, at src/AutoLaunch/fetchers/get-home.js

103 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { applyDesignBuildHero } from '@auto-launch/fetchers/get-design-build';
2 import { getHomeShape, homeTemplateShape } from '@auto-launch/fetchers/shape';
3 import {
4 fetchWithTimeout,
5 retryTwice,
6 setStatus,
7 } from '@auto-launch/functions/helpers';
8 import { getHeadersAndFooters } from '@auto-launch/functions/wp';
9 import { PATTERNS_HOST } from '@constants';
10 import { digest } from '@shared/api/digest';
11 import { reqDataBasics } from '@shared/lib/data';
12 import { __ } from '@wordpress/i18n';
13
14 const url = `${PATTERNS_HOST}/api/home`;
15 const { wpLanguage, showImprint } = window.extSharedData;
16 const method = 'POST';
17 const headers = { 'Content-Type': 'application/json' };
18
19 export const handleHome = async ({
20 siteProfile,
21 sitePlugins,
22 siteImages,
23 aiHeaders,
24 designBuild,
25 }) => {
26 // translators: this is for a action log UI. Keep it short
27 setStatus(__('Preparing your home page', 'extendify-local'));
28
29 // A full-page design build already carries the whole home; skip the
30 // /api/home fetch and build the page from the built patterns directly.
31 const builtHome = designBuild?.builtPages?.find((p) => p.slug === 'home');
32 if (builtHome?.fullPage && builtHome.patterns?.length) {
33 // The full page is final — keep the BE's sections verbatim and flag them
34 // so generatePageContent skips the /api/patterns rewrite.
35 const patterns = builtHome.patterns.map((p) => ({
36 ...p,
37 contentGenerated: true,
38 }));
39 const parts = await resolveTemplateParts({ siteProfile, designBuild });
40 return getHomeShape.parse({
41 home: { id: 'home', slug: 'home', patterns, ...parts },
42 });
43 }
44
45 const body = JSON.stringify({
46 ...reqDataBasics,
47 siteProfile,
48 siteImages,
49 sitePlugins,
50 aiHeaders,
51 // If pages are passed in they may be used
52 pages: designBuild?.pages ?? [],
53 buildId: designBuild?.buildId,
54 });
55
56 const response = await retryTwice(() =>
57 fetchWithTimeout(url, { method, headers, body }),
58 ).catch((error) => {
59 return { ok: false, statusText: error.message, status: 0 };
60 });
61
62 if (!response?.ok) {
63 digest({
64 error: {
65 message: response.statusText,
66 name: 'FetchError',
67 status: response.status,
68 },
69 details: { source: 'auto-launch', caller: 'handleHome' },
70 });
71 throw new Error(response.statusText);
72 }
73
74 const template = homeTemplateShape.parse(await response.json());
75 template.patterns = applyDesignBuildHero(template.patterns, designBuild);
76
77 const parts = await resolveTemplateParts({ siteProfile, designBuild });
78 return getHomeShape.parse({ home: { ...template, ...parts } });
79 };
80
81 const resolveTemplateParts = async ({ siteProfile, designBuild }) => {
82 const headerPart = designBuild?.templateParts?.header;
83 const footerPart = designBuild?.templateParts?.footer;
84 // Both parts came from the design build; skip the template-parts fetch.
85 if (headerPart && footerPart)
86 return { headerCode: headerPart, footerCode: footerPart };
87
88 const hasFooterNav = Array.isArray(showImprint)
89 ? showImprint.includes(wpLanguage ?? '') &&
90 siteProfile.category === 'Business'
91 : false;
92
93 const { headers: head, footers: foot } = await getHeadersAndFooters({
94 useNavFooter: hasFooterNav,
95 });
96 const randomHeader = head[Math.floor(Math.random() * head.length)];
97 const randomFooter = foot[Math.floor(Math.random() * foot.length)];
98 return {
99 headerCode: headerPart ?? randomHeader?.content?.raw?.trim() ?? '',
100 footerCode: footerPart ?? randomFooter?.content?.raw?.trim() ?? '',
101 };
102 };
103