PluginProbe
Extendify / 3.2.2
Extendify v3.2.2
3.2.2 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 All 128 releases
← All changes | src/AutoLaunch/fetchers/get-images.js +38 -11 3.1.4 → 3.2.2 View file →
@@ -5,40 +5,65 @@
5 5 fetchWithTimeout,
6 6 retryTwice,
7 7 setStatus,
8 8 } from '@auto-launch/functions/helpers';
9 +import { launchStrings } from '@auto-launch/strings';
9 10 import { IMAGES_HOST } from '@constants';
11 +import { digest } from '@shared/api/digest';
10 12 import { reqDataBasics } from '@shared/lib/data';
11 -import { __ } from '@wordpress/i18n';
12 13
13 -const fallback = { siteImages: [] };
14 -const url = `${IMAGES_HOST}/api/search`;
14 +const fallback = { siteImages: { hero: [], general: [] } };
15 +const url = `${IMAGES_HOST}/api/images`;
16 +const { wpLanguage } = window.extSharedData;
15 17 const method = 'POST';
16 18 const headers = { 'Content-Type': 'application/json' };
19 +// A shipped build can never change a count it sends; the service can.
20 +const imageTypes = [{ type: 'hero' }, { type: 'general' }];
17 21 const MIN_POOL_SIZE = 10;
18 22
23 +const asImages = (urls) => urls.map((link) => ({ url: link }));
24 +// The build's reusable images exclude its hero, so they are section photos.
25 +const asSections = (urls) => ({ hero: [], general: asImages(urls) });
26 +
19 27 export const handleSiteImages = async ({ siteProfile, designBuild }) => {
20 28 // translators: this is for a action log UI. Keep it short
21 - setStatus(__('Finding the perfect images', 'extendify-local'));
29 + setStatus(launchStrings().statusImages);
22 30
23 31 // Reuse the design preview's own images; only search for what is missing.
24 32 const seeded = collectBuiltPageImageUrls(designBuild?.builtPages);
25 - if (seeded.length >= MIN_POOL_SIZE) return { siteImages: seeded };
33 + if (seeded.length >= MIN_POOL_SIZE) return { siteImages: asSections(seeded) };
26 34
27 35 const body = JSON.stringify({
28 36 ...reqDataBasics,
29 37 siteProfile,
38 + lang: wpLanguage,
39 + imageTypes,
30 40 source: 'auto-launch',
31 41 });
32 42
33 43 const response = await retryTwice(() =>
34 44 fetchWithTimeout(url, { method, headers, body }),
35 - );
45 + ).catch((error) => {
46 + return { ok: false, statusText: error.message, status: 0 };
47 + });
36 48
37 - if (!response?.ok) return { siteImages: seeded };
49 + if (!response?.ok) {
50 + digest({
51 + error: {
52 + message: response.statusText,
53 + name: 'FetchError',
54 + status: response.status,
55 + },
56 + details: { source: 'auto-launch', caller: 'handleSiteImages' },
57 + });
58 + return { siteImages: asSections(seeded) };
59 + }
38 60
39 61 const { siteImages: found } = await failWithFallback(
40 - async () => getImagesShape.parse(await response.json()),
62 + async () => {
63 + const { images } = await response.json();
64 + return getImagesShape.parse({ siteImages: images });
65 + },
41 66 fallback,
42 67 { caller: 'handleSiteImages' },
43 68 );
44 69
@@ -44,9 +69,11 @@
44 69
45 70 if (!seeded.length) return { siteImages: found };
46 71
47 72 const seededSet = new Set(seeded);
48 - const topup = found
49 - .filter((u) => !seededSet.has(u))
73 + const topup = found.general
74 + .filter((image) => !seededSet.has(image.url))
50 75 .slice(0, MIN_POOL_SIZE - seeded.length);
51 - return { siteImages: [...seeded, ...topup] };
76 + return {
77 + siteImages: { hero: found.hero, general: [...asImages(seeded), ...topup] },
78 + };
52 79 };