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/hooks/useInstallRequiredPlugins.js +11 -22 3.0.5 → 3.2.2 View file →
@@ -1,30 +1,19 @@
1 1 import { handleSitePlugins } from '@auto-launch/fetchers/get-plugins';
2 -import { activatePlugin, installPlugin } from '@auto-launch/functions/plugins';
2 +import { ensurePluginsActive } from '@auto-launch/functions/plugins';
3 +import { useEffect, useRef } from '@wordpress/element';
3 4 import useSWR from 'swr/immutable';
4 5
5 -const { installedPluginsSlugs } = window.extSharedData || {};
6 -
7 -export const useInstallRequiredPlugins = () => {
8 - const { data, error } = useSWR('required-plugins', () =>
6 +export const useInstallRequiredPlugins = ({ enabled = true } = {}) => {
7 + const { data } = useSWR(enabled ? 'required-plugins' : null, () =>
9 8 handleSitePlugins({ requiredOnly: true }),
10 9 );
10 + const started = useRef(false);
11 11
12 - if (data?.sitePlugins?.sitePlugins?.length) {
13 - const pluginsToInstall = data.sitePlugins.sitePlugins.filter(
14 - ({ wordpressSlug: slug }) => !installedPluginsSlugs?.includes(slug),
12 + useEffect(() => {
13 + if (started.current || !data?.sitePlugins?.length) return;
14 + started.current = true;
15 + ensurePluginsActive(
16 + data.sitePlugins.map(({ wordpressSlug }) => wordpressSlug),
15 17 );
16 - if (pluginsToInstall.length === 0) return;
17 - (async function install() {
18 - for (const { wordpressSlug: slug } of pluginsToInstall) {
19 - const p = await installPlugin(slug);
20 - await activatePlugin(p?.plugin ?? slug);
21 - }
22 - })();
23 - }
24 -
25 - return {
26 - requiredPlugins: data?.selectedPlugins || [],
27 - isLoading: !error && !data,
28 - isError: error,
29 - };
18 + }, [data]);
30 19 };