| Server IP : 66.29.153.156 / Your IP : 216.73.217.22 Web Server : LiteSpeed System : Linux premium322.web-hosting.com 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64 User : lastyfjz ( 1521) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/lastyfjz/./unicitys.com/wp-content/plugins/extendify/src/AutoLaunch/functions/ |
Upload File : |
import { recordPluginActivity } from '@shared/api/DataApi';
import { enableAutoUpdate } from '@shared/api/wp';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
export const getActivePlugins = () =>
apiFetch({ path: 'extendify/v1/auto-launch/active-plugins' });
export const alreadyActive = (activePlugins, pluginSlug) =>
activePlugins?.filter((p) => p.includes(pluginSlug))?.length;
export const installPlugin = async (slug) => {
const fn = async () => {
const p = await apiFetch({
path: '/wp/v2/plugins',
method: 'POST',
data: { slug },
});
await recordPluginActivity({ slug, source: 'auto-launch' });
await enableAutoUpdate(p?.plugin);
return p;
};
try {
return await fn();
} catch (error) {
if (error?.code === 'folder_exists') {
console.warn(
`Plugin ${slug} already installed. Attempting to activate...`,
);
// Get the plugin info directly here
return await getPlugin(slug);
}
console.error(`Error installing ${slug}. Retrying...`, error);
try {
return await fn();
} catch (error) {
console.error(`Failed ${slug} again. Giving up`, error);
}
}
};
export const getPlugin = async (slug) => {
const response = await apiFetch({
path: addQueryArgs('/wp/v2/plugins', { search: slug }),
});
return response?.find((p) => p.plugin?.split('/')[0] === slug);
};
export const activatePlugin = async (slug) => {
const fn = (s) =>
apiFetch({
path: `/wp/v2/plugins/${s}`,
method: 'POST',
data: { status: 'active' },
});
try {
await fn(slug);
} catch (_) {
console.warn(`Error activating ${slug}. Retrying with fresh data...`);
try {
// try once more but get the slug first
const { plugin } = await getPlugin(slug);
await fn(plugin);
} catch (error) {
console.error(`Failed to activate ${slug} again. Giving up`, error);
}
}
};
// Currently this only processes patterns with placeholders
// by swapping out the placeholders with the actual code
// returns the patterns as blocks with the placeholders replaced
export const replacePlaceholderPatterns = async (patterns) => {
// Directly replace "blog-section" patterns using their replacement code, skipping the API call
patterns = patterns.map((pattern) => {
if (
pattern.patternTypes.includes('blog-section') &&
pattern.patternReplacementCode
) {
return {
...pattern,
code: pattern.patternReplacementCode,
};
}
return pattern;
});
const hasPlaceholders = patterns.filter((p) => p.patternReplacementCode);
if (!hasPlaceholders?.length) return patterns;
const activePlugins =
(await getActivePlugins())?.data?.map((path) => path.split('/')[0]) || [];
const pluginsActivity = patterns
.filter((p) => p.pluginDependency)
.map((p) => p.pluginDependency)
.filter((p) => !activePlugins.includes(p));
for (const plugin of pluginsActivity) {
recordPluginActivity({
slug: plugin,
source: 'auto-launch',
});
}
try {
return await processPlaceholders(patterns);
} catch (_e) {
// Try one more time (plugins installed may not be fully loaded)
return await processPlaceholders(patterns)
// If this fails, just return the original patterns
.catch(() => patterns);
}
};
export const processPlaceholders = (patterns) =>
apiFetch({
path: '/extendify/v1/shared/process-placeholders',
method: 'POST',
data: { patterns },
});