test
1 month ago
analyticsApi.js
1 month ago
classnames.js
1 month ago
dateUtils.js
1 month ago
formatters.js
1 month ago
helpers.js
1 month ago
icons.js
1 month ago
index.js
1 month ago
pluginUtils.js
1 month ago
transformers.js
1 month ago
viewportBoundary.js
1 month ago
pluginUtils.js
235 lines
| 1 | import wpApiFetch from "@wordpress/api-fetch"; |
| 2 | import { toast } from "@bsf/force-ui"; |
| 3 | |
| 4 | const { __, sprintf } = wp.i18n; |
| 5 | |
| 6 | /** |
| 7 | * Activates a WordPress plugin via REST API |
| 8 | * |
| 9 | * @param {Object} plugin - The plugin object containing init and slug |
| 10 | * @returns {Promise<boolean>} True if activation was successful |
| 11 | * |
| 12 | * @example |
| 13 | * await activatePlugin({ init: "plugin/plugin.php", slug: "plugin", name: "My Plugin" }) |
| 14 | */ |
| 15 | export const activatePlugin = async (plugin) => { |
| 16 | try { |
| 17 | const formData = new FormData(); |
| 18 | formData.append("plugin_init", plugin?.init); |
| 19 | formData.append("plugin_slug", plugin?.slug); |
| 20 | const response = await wpApiFetch({ |
| 21 | path: window.prestoPlayer.api.activatePlugin, |
| 22 | method: "POST", |
| 23 | body: formData, |
| 24 | }); |
| 25 | |
| 26 | if (response.success) { |
| 27 | toast.success( |
| 28 | sprintf( |
| 29 | /* translators: %s: Plugin name */ |
| 30 | __("%s Plugin Activated Successfully..!", "presto-player"), |
| 31 | plugin?.name |
| 32 | ) |
| 33 | ); |
| 34 | return true; |
| 35 | } |
| 36 | } catch (error) { |
| 37 | console.error(`Failed to activate ${plugin?.name}:`, error); |
| 38 | return false; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Installs one or more WordPress plugins using the WordPress updates API |
| 44 | * |
| 45 | * @param {Array<Object>} pluginInstallList - Array of plugin objects to install |
| 46 | * @returns {Promise<void>} Resolves when all plugins are installed and activated |
| 47 | * |
| 48 | * @example |
| 49 | * await installPlugins([ |
| 50 | * { slug: "plugin-1", init: "plugin-1/plugin-1.php", name: "Plugin 1" }, |
| 51 | * { slug: "plugin-2", init: "plugin-2/plugin-2.php", name: "Plugin 2" } |
| 52 | * ]) |
| 53 | */ |
| 54 | export const installPlugins = async (pluginInstallList) => { |
| 55 | return new Promise((resolve) => { |
| 56 | if (!pluginInstallList?.length) { |
| 57 | resolve(); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | const installPromises = pluginInstallList.map((plugin) => { |
| 62 | return new Promise((resolveInstall, rejectInstall) => { |
| 63 | try { |
| 64 | toast.info( |
| 65 | sprintf( |
| 66 | /* translators: %s: Plugin name */ |
| 67 | __("Installing %s plugin. Please wait..", "presto-player"), |
| 68 | plugin?.name |
| 69 | ) |
| 70 | ); |
| 71 | wp.updates.queue.push({ |
| 72 | action: "install-plugin", |
| 73 | data: { |
| 74 | slug: plugin?.slug, |
| 75 | init: plugin?.init, |
| 76 | success: async () => { |
| 77 | toast.success( |
| 78 | sprintf( |
| 79 | /* translators: %s: Plugin name */ |
| 80 | __("%s plugin Installed Successfully..", "presto-player"), |
| 81 | plugin?.name |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | try { |
| 86 | await activatePlugin(plugin); |
| 87 | resolveInstall(); |
| 88 | } catch (activationError) { |
| 89 | console.error( |
| 90 | `Failed to activate ${plugin?.name}:`, |
| 91 | activationError |
| 92 | ); |
| 93 | rejectInstall(activationError); |
| 94 | } |
| 95 | }, |
| 96 | error: (error) => { |
| 97 | console.error(`Failed to install ${plugin?.name}:`, error); |
| 98 | rejectInstall(error); |
| 99 | }, |
| 100 | }, |
| 101 | }); |
| 102 | |
| 103 | wp.updates.queueChecker(); |
| 104 | } catch (error) { |
| 105 | console.error(`Error installing ${plugin?.name}:`, error); |
| 106 | rejectInstall(error); |
| 107 | } |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | // Wait for all plugins to install & activate before resolving |
| 112 | Promise.allSettled(installPromises).then(() => resolve()); |
| 113 | }); |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * Generates plugin suggestions based on current statuses |
| 118 | * Prioritizes non-activated primary plugins, then fills with secondary plugins |
| 119 | * |
| 120 | * @param {Object} statuses - Object mapping plugin slugs to their status |
| 121 | * @param {Array<Object>} primaryPlugins - Array of primary plugin objects |
| 122 | * @param {Array<Object>} secondaryPlugins - Array of secondary plugin objects |
| 123 | * @returns {Array<Object>} Array of plugin suggestions (max 4) |
| 124 | * |
| 125 | * @example |
| 126 | * generateSuggestions( |
| 127 | * { "plugin-1": "active", "plugin-2": "inactive" }, |
| 128 | * primaryPluginsData, |
| 129 | * secondaryPluginsData |
| 130 | * ) |
| 131 | */ |
| 132 | export const generateSuggestions = ( |
| 133 | statuses, |
| 134 | primaryPlugins, |
| 135 | secondaryPlugins |
| 136 | ) => { |
| 137 | const suggestions = []; |
| 138 | |
| 139 | // Add non-activated primary plugins |
| 140 | primaryPlugins.forEach((plugin) => { |
| 141 | if (statuses[plugin.slug] !== "active") { |
| 142 | suggestions.push({ |
| 143 | ...plugin, |
| 144 | status: statuses[plugin.slug], |
| 145 | }); |
| 146 | } |
| 147 | }); |
| 148 | |
| 149 | // Fill remaining slots with secondary plugins |
| 150 | secondaryPlugins.forEach((plugin) => { |
| 151 | if (suggestions.length < 4 && statuses[plugin.slug] !== "active") { |
| 152 | suggestions.push({ |
| 153 | ...plugin, |
| 154 | status: statuses[plugin.slug], |
| 155 | }); |
| 156 | } |
| 157 | }); |
| 158 | |
| 159 | // Fill remaining slots with activated primary plugins if needed |
| 160 | if (suggestions.length < 4) { |
| 161 | primaryPlugins.forEach((plugin) => { |
| 162 | if (suggestions.length < 4 && statuses[plugin.slug] === "active") { |
| 163 | suggestions.push({ |
| 164 | ...plugin, |
| 165 | status: statuses[plugin.slug], |
| 166 | }); |
| 167 | } |
| 168 | }); |
| 169 | } |
| 170 | |
| 171 | return suggestions.slice(0, 4); |
| 172 | }; |
| 173 | |
| 174 | /** |
| 175 | * Check if a plugin is installed and/or active via REST API. |
| 176 | * |
| 177 | * @param {string} slug - The plugin slug to check. |
| 178 | * @returns {Promise<{success: boolean, data?: Object, error?: string}>} |
| 179 | */ |
| 180 | export const checkPluginStatus = async (slug) => { |
| 181 | try { |
| 182 | const response = await wpApiFetch({ |
| 183 | path: `/presto-player/v1/plugin-status/${slug}`, |
| 184 | method: "GET", |
| 185 | }); |
| 186 | return { success: true, data: response }; |
| 187 | } catch (error) { |
| 188 | console.error(`Error checking plugin status for ${slug}:`, error); |
| 189 | return { success: false, error: error.message }; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * Install a plugin from WordPress.org via REST API. |
| 195 | * |
| 196 | * @param {string} slug - The plugin slug to install. |
| 197 | * @returns {Promise<{success: boolean, data?: Object, error?: string}>} |
| 198 | */ |
| 199 | export const installPlugin = async (slug) => { |
| 200 | try { |
| 201 | const response = await wpApiFetch({ |
| 202 | path: "/presto-player/v1/install-plugin", |
| 203 | method: "POST", |
| 204 | data: { plugin: slug }, |
| 205 | }); |
| 206 | return { success: true, data: response }; |
| 207 | } catch (error) { |
| 208 | console.error(`Error installing plugin ${slug}:`, error); |
| 209 | return { success: false, error: error.message }; |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * Activate a plugin by slug via REST API. |
| 215 | * |
| 216 | * @param {string} slug - The plugin slug to activate. |
| 217 | * @returns {Promise<{success: boolean, data?: Object, error?: string}>} |
| 218 | */ |
| 219 | export const activatePluginBySlug = async (slug) => { |
| 220 | try { |
| 221 | const formData = new FormData(); |
| 222 | formData.append("plugin_slug", slug); |
| 223 | formData.append("plugin_init", `${slug}/${slug}.php`); |
| 224 | const response = await wpApiFetch({ |
| 225 | path: window.prestoPlayer.api.activatePlugin, |
| 226 | method: "POST", |
| 227 | body: formData, |
| 228 | }); |
| 229 | return { success: true, data: response }; |
| 230 | } catch (error) { |
| 231 | console.error(`Error activating plugin ${slug}:`, error); |
| 232 | return { success: false, error: error.message }; |
| 233 | } |
| 234 | }; |
| 235 |