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 / functions / plugins.js

plugins.js in Extendify 3.1.0, at src/AutoLaunch/functions/plugins.js

123 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { recordPluginActivity } from '@shared/api/DataApi';
2 import { enableAutoUpdate } from '@shared/api/wp';
3 import apiFetch from '@wordpress/api-fetch';
4 import { addQueryArgs } from '@wordpress/url';
5
6 export const getActivePlugins = () =>
7 apiFetch({ path: 'extendify/v1/auto-launch/active-plugins' });
8
9 export const alreadyActive = (activePlugins, pluginSlug) =>
10 activePlugins?.filter((p) => p.includes(pluginSlug))?.length;
11
12 export const installPlugin = async (slug) => {
13 const fn = async () => {
14 const p = await apiFetch({
15 path: '/wp/v2/plugins',
16 method: 'POST',
17 data: { slug },
18 });
19 await recordPluginActivity({ slug, source: 'auto-launch' });
20 await enableAutoUpdate(p?.plugin);
21 return p;
22 };
23 try {
24 return await fn();
25 } catch (error) {
26 if (error?.code === 'folder_exists') {
27 console.warn(
28 `Plugin ${slug} already installed. Attempting to activate...`,
29 );
30 // Get the plugin info directly here
31 return await getPlugin(slug);
32 }
33 console.error(`Error installing ${slug}. Retrying...`, error);
34 try {
35 return await fn();
36 } catch (error) {
37 console.error(`Failed ${slug} again. Giving up`, error);
38 }
39 }
40 };
41
42 export const getPlugin = async (slug) => {
43 const response = await apiFetch({
44 path: addQueryArgs('/wp/v2/plugins', { search: slug }),
45 });
46 return response?.find((p) => p.plugin?.split('/')[0] === slug);
47 };
48
49 export const activatePlugin = async (slug) => {
50 const fn = (s) =>
51 apiFetch({
52 path: `/wp/v2/plugins/${s}`,
53 method: 'POST',
54 data: { status: 'active' },
55 });
56
57 try {
58 await fn(slug);
59 } catch (_) {
60 console.warn(`Error activating ${slug}. Retrying with fresh data...`);
61 try {
62 // try once more but get the slug first
63 const { plugin } = await getPlugin(slug);
64 await fn(plugin);
65 } catch (error) {
66 console.error(`Failed to activate ${slug} again. Giving up`, error);
67 }
68 }
69 };
70
71 // Currently this only processes patterns with placeholders
72 // by swapping out the placeholders with the actual code
73 // returns the patterns as blocks with the placeholders replaced
74 export const replacePlaceholderPatterns = async (patterns) => {
75 // Directly replace "blog-section" patterns using their replacement code, skipping the API call
76 patterns = patterns.map((pattern) => {
77 if (
78 pattern.patternTypes.includes('blog-section') &&
79 pattern.patternReplacementCode
80 ) {
81 return {
82 ...pattern,
83 code: pattern.patternReplacementCode,
84 };
85 }
86 return pattern;
87 });
88
89 const hasPlaceholders = patterns.filter((p) => p.patternReplacementCode);
90 if (!hasPlaceholders?.length) return patterns;
91
92 const activePlugins =
93 (await getActivePlugins())?.data?.map((path) => path.split('/')[0]) || [];
94
95 const pluginsActivity = patterns
96 .filter((p) => p.pluginDependency)
97 .map((p) => p.pluginDependency)
98 .filter((p) => !activePlugins.includes(p));
99
100 for (const plugin of pluginsActivity) {
101 recordPluginActivity({
102 slug: plugin,
103 source: 'auto-launch',
104 });
105 }
106
107 try {
108 return await processPlaceholders(patterns);
109 } catch (_e) {
110 // Try one more time (plugins installed may not be fully loaded)
111 return await processPlaceholders(patterns)
112 // If this fails, just return the original patterns
113 .catch(() => patterns);
114 }
115 };
116
117 export const processPlaceholders = (patterns) =>
118 apiFetch({
119 path: '/extendify/v1/shared/process-placeholders',
120 method: 'POST',
121 data: { patterns },
122 });
123