PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
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 / hooks / useInstallRequiredPlugins.js

useInstallRequiredPlugins.js in Extendify 3.1.0, at src/AutoLaunch/hooks/useInstallRequiredPlugins.js

40 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { handleSitePlugins } from '@auto-launch/fetchers/get-plugins';
2 import { activatePlugin, installPlugin } from '@auto-launch/functions/plugins';
3 import { useEffect, useRef } from '@wordpress/element';
4 import useSWR from 'swr/immutable';
5
6 const { installedPluginsSlugs } = window.extSharedData || {};
7
8 export const useInstallRequiredPlugins = () => {
9 const { data, error } = useSWR('required-plugins', () =>
10 handleSitePlugins({ requiredOnly: true }),
11 );
12 const started = useRef(false);
13
14 useEffect(() => {
15 if (started.current || !data?.sitePlugins?.length) return;
16 started.current = true;
17 const install = async () => {
18 for (const { wordpressSlug: slug } of data.sitePlugins) {
19 // One plugin failing shouldn't block the rest of the required set.
20 try {
21 let plugin;
22 if (!installedPluginsSlugs?.includes(slug)) {
23 plugin = await installPlugin(slug);
24 }
25 await activatePlugin(plugin?.plugin ?? slug);
26 } catch (error) {
27 console.error(`Failed to set up required plugin ${slug}`, error);
28 }
29 }
30 };
31 install();
32 }, [data]);
33
34 return {
35 requiredPlugins: data?.selectedPlugins || [],
36 isLoading: !error && !data,
37 isError: error,
38 };
39 };
40