import { getPluginsShape, pluginShape } from '@auto-launch/fetchers/shape';
} from '@auto-launch/functions/helpers';
} from '@auto-launch/functions/plugins';
import { AI_HOST } from '@constants';
import { reqDataBasics } from '@shared/lib/data';
import { __ } from '@wordpress/i18n';
const { pluginGroupId } = window.extSharedData;
const fallback = { sitePlugins: [] };
const url = `${AI_HOST}/api/site-plugins`;
const headers = { 'Content-Type': 'application/json' };
// Local due to legacy naming inconsistency
const shapeLocal = z.object({
selectedPlugins: z.array(pluginShape),
export const handleSitePlugins = async ({
// translators: this is for a action log UI. Keep it short
setStatus(__('Setting up site functionality', 'extendify-local'));
const body = JSON.stringify({
siteObjective: siteProfile.objective,
const response = await retryTwice(() =>
fetchWithTimeout(url, { method, headers, body }),
if (!response?.ok) return fallback;
const plugins = await failWithFallback(async () => {
const data = await response.json();
const sitePlugins = shapeLocal
.selectedPlugins // We add give to the front. See here why:
// https://github.com/extendify/company-product/issues/713
.toSorted(({ wordpressSlug }) => (wordpressSlug === 'give' ? -1 : 1));
return getPluginsShape.parse({ sitePlugins });
// install partner plugins
const activePlugins = await getActivePlugins();
const pluginsToInstall = plugins.sitePlugins.filter(
({ wordpressSlug: slug }) => !alreadyActive(activePlugins, slug),
if (showStatus && pluginsToInstall.length > 0) {
// translators: this is for a action log UI. Keep it short
__('Setting up functionality for your website', 'extendify-local'),
for (const { wordpressSlug: slug } of pluginsToInstall) {
const p = await installPlugin(slug);
await activatePlugin(p?.plugin ?? slug);