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