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 +50 -9 3.1.2 → 3.2.2 View file →
@@ -1,5 +1,6 @@
1 1 import { getImagesShape } from '@auto-launch/fetchers/shape';
2 +import { collectBuiltPageImageUrls } from '@auto-launch/functions/get-imported-images';
2 3 import {
3 4 failWithFallback,
4 5 fetchWithTimeout,
5 6 retryTwice,
@@ -4,35 +5,75 @@
4 5 fetchWithTimeout,
5 6 retryTwice,
6 7 setStatus,
7 8 } from '@auto-launch/functions/helpers';
9 +import { launchStrings } from '@auto-launch/strings';
8 10 import { IMAGES_HOST } from '@constants';
11 +import { digest } from '@shared/api/digest';
9 12 import { reqDataBasics } from '@shared/lib/data';
10 -import { __ } from '@wordpress/i18n';
11 13
12 -const fallback = { siteImages: [] };
13 -const url = `${IMAGES_HOST}/api/search`;
14 +const fallback = { siteImages: { hero: [], general: [] } };
15 +const url = `${IMAGES_HOST}/api/images`;
16 +const { wpLanguage } = window.extSharedData;
14 17 const method = 'POST';
15 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' }];
21 +const MIN_POOL_SIZE = 10;
16 22
17 -export const handleSiteImages = async ({ siteProfile }) => {
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 +
27 +export const handleSiteImages = async ({ siteProfile, designBuild }) => {
18 28 // translators: this is for a action log UI. Keep it short
19 - setStatus(__('Finding the perfect images', 'extendify-local'));
29 + setStatus(launchStrings().statusImages);
20 30
31 + // Reuse the design preview's own images; only search for what is missing.
32 + const seeded = collectBuiltPageImageUrls(designBuild?.builtPages);
33 + if (seeded.length >= MIN_POOL_SIZE) return { siteImages: asSections(seeded) };
34 +
21 35 const body = JSON.stringify({
22 36 ...reqDataBasics,
23 37 siteProfile,
38 + lang: wpLanguage,
39 + imageTypes,
24 40 source: 'auto-launch',
25 41 });
26 42
27 43 const response = await retryTwice(() =>
28 44 fetchWithTimeout(url, { method, headers, body }),
29 - );
45 + ).catch((error) => {
46 + return { ok: false, statusText: error.message, status: 0 };
47 + });
30 48
31 - if (!response?.ok) return fallback;
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 + }
32 60
33 - return failWithFallback(
34 - async () => getImagesShape.parse(await response.json()),
61 + const { siteImages: found } = await failWithFallback(
62 + async () => {
63 + const { images } = await response.json();
64 + return getImagesShape.parse({ siteImages: images });
65 + },
35 66 fallback,
36 67 { caller: 'handleSiteImages' },
37 68 );
69 +
70 + if (!seeded.length) return { siteImages: found };
71 +
72 + const seededSet = new Set(seeded);
73 + const topup = found.general
74 + .filter((image) => !seededSet.has(image.url))
75 + .slice(0, MIN_POOL_SIZE - seeded.length);
76 + return {
77 + siteImages: { hero: found.hero, general: [...asImages(seeded), ...topup] },
78 + };
38 79 };