| 1 |
/** |
| 2 |
* Plugin Utility Functions |
| 3 |
* |
| 4 |
* Contains reusable functions for installing and activating plugins. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
import { __, sprintf } from '@wordpress/i18n'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Performs the given plugin or theme operation (install or activate). |
| 12 |
* |
| 13 |
* @param {Object} plugin - The plugin or theme object. |
| 14 |
* @param {string} operation - The operation ('install' or 'activate'). |
| 15 |
* @param {Function} setLoadingState - Function to update loading state (array of plugin slugs). |
| 16 |
* @return {Promise} Resolves when operation is complete. |
| 17 |
*/ |
| 18 |
export const handlePluginOperation = ( plugin, operation, setLoadingState ) => { |
| 19 |
return new Promise( async ( resolve, reject ) => { |
| 20 |
const isInstall = operation === 'install'; |
| 21 |
// Determine the item type. If plugin.type is 'theme', we treat it as a theme; otherwise, as a plugin. |
| 22 |
const itemType = plugin.type === 'theme' ? 'theme' : 'plugin'; |
| 23 |
|
| 24 |
if ( ! wp.updates ) { |
| 25 |
reject( |
| 26 |
new Error( |
| 27 |
__( 'WordPress updates API not available.', 'suremails' ) |
| 28 |
) |
| 29 |
); |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
// Add the plugin slug to the loading state. |
| 34 |
setLoadingState( ( prev ) => [ ...prev, plugin.slug ] ); |
| 35 |
|
| 36 |
// For install, use the appropriate wp.updates function. |
| 37 |
// For activate, we use wp.ajax.send with an action based on the item type. |
| 38 |
const operationFunction = isInstall |
| 39 |
? wp.updates[ |
| 40 |
`install${ |
| 41 |
itemType.charAt( 0 ).toUpperCase() + itemType.slice( 1 ) |
| 42 |
}` |
| 43 |
] |
| 44 |
: wp.ajax.send; |
| 45 |
|
| 46 |
// Data to send: for installation, we simply send the slug; |
| 47 |
// for activation: for plugins we use the plugin file path; for themes, just the slug. |
| 48 |
const data = isInstall |
| 49 |
? { slug: plugin.slug } |
| 50 |
: { |
| 51 |
slug: plugin.init, |
| 52 |
_ajax_nonce: window.suremails?._ajax_nonce, |
| 53 |
}; |
| 54 |
|
| 55 |
const commonOptions = { |
| 56 |
success: () => { |
| 57 |
resolve(); |
| 58 |
}, |
| 59 |
error: ( error ) => { |
| 60 |
reject( |
| 61 |
new Error( |
| 62 |
error.errorMessage || |
| 63 |
__( 'Operation failed.', 'suremails' ) |
| 64 |
) |
| 65 |
); |
| 66 |
}, |
| 67 |
}; |
| 68 |
|
| 69 |
if ( isInstall ) { |
| 70 |
operationFunction( { |
| 71 |
...data, |
| 72 |
...commonOptions, |
| 73 |
} ); |
| 74 |
} else { |
| 75 |
// For activation, use a different ajax action if it's a theme. |
| 76 |
const actionName = |
| 77 |
itemType === 'plugin' |
| 78 |
? 'suremails-activate_plugin' |
| 79 |
: 'suremails-activate_theme'; |
| 80 |
operationFunction( actionName, { |
| 81 |
data, |
| 82 |
...commonOptions, |
| 83 |
} ); |
| 84 |
} |
| 85 |
} ); |
| 86 |
}; |
| 87 |
|
| 88 |
/** |
| 89 |
* Install and then automatically activate a plugin or theme. |
| 90 |
* |
| 91 |
* @param {Object} plugin - The plugin or theme object. |
| 92 |
* @param {Object} pluginsData - Installed/active plugins data. |
| 93 |
* @param {Array} installingPlugins - Array of slugs currently installing. |
| 94 |
* @param {Array} activatingPlugins - Array of slugs currently activating. |
| 95 |
* @param {Function} setInstallingPlugins - Setter for installing plugins state. |
| 96 |
* @param {Function} setActivatingPlugins - Setter for activating plugins state. |
| 97 |
* @param {Object} queryClient - React Query client for cache invalidation. |
| 98 |
* @param {Function} toast - Toast function for notifications. |
| 99 |
* @return {Promise} Resolves when installation and activation complete. |
| 100 |
*/ |
| 101 |
export const installAndActivatePlugin = async ( |
| 102 |
plugin, |
| 103 |
pluginsData, |
| 104 |
installingPlugins, |
| 105 |
activatingPlugins, |
| 106 |
setInstallingPlugins, |
| 107 |
setActivatingPlugins, |
| 108 |
queryClient, |
| 109 |
toast |
| 110 |
) => { |
| 111 |
// Determine the type string for toast messages. |
| 112 |
// translators: %s: Plugin or Theme. |
| 113 |
const typeStr = |
| 114 |
plugin.type === 'theme' |
| 115 |
? __( 'Theme', 'suremails' ) |
| 116 |
: __( 'Plugin', 'suremails' ); |
| 117 |
|
| 118 |
if ( pluginsData.installed.includes( plugin.slug ) ) { |
| 119 |
toast.info( |
| 120 |
/* translators: %s: Plugin or Theme. */ |
| 121 |
sprintf( __( '%s is already installed.', 'suremails' ), typeStr ) |
| 122 |
); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if ( installingPlugins.length > 0 || activatingPlugins.length > 0 ) { |
| 127 |
toast.info( |
| 128 |
sprintf( |
| 129 |
/* translators: %s: Plugin or Theme. */ |
| 130 |
__( |
| 131 |
'Another %s operation is in progress. Please wait.', |
| 132 |
'suremails' |
| 133 |
), |
| 134 |
typeStr |
| 135 |
) |
| 136 |
); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
try { |
| 141 |
await handlePluginOperation( plugin, 'install', setInstallingPlugins ); |
| 142 |
toast.success( |
| 143 |
/* translators: %s: Plugin or Theme. */ |
| 144 |
sprintf( __( '%s installed successfully.', 'suremails' ), typeStr ) |
| 145 |
); |
| 146 |
// Refresh the plugins data. |
| 147 |
queryClient.invalidateQueries( [ 'installed-plugins' ] ); |
| 148 |
// Automatically activate after installation. |
| 149 |
await activatePlugin( |
| 150 |
plugin, |
| 151 |
pluginsData, |
| 152 |
installingPlugins, |
| 153 |
activatingPlugins, |
| 154 |
setActivatingPlugins, |
| 155 |
queryClient, |
| 156 |
toast |
| 157 |
); |
| 158 |
} catch ( error ) { |
| 159 |
toast.error( |
| 160 |
/* translators: %s: Plugin or Theme. */ |
| 161 |
sprintf( __( 'Failed to install %s.', 'suremails' ), typeStr ) |
| 162 |
); |
| 163 |
} finally { |
| 164 |
setInstallingPlugins( ( prev ) => |
| 165 |
prev.filter( ( slug ) => slug !== plugin.slug ) |
| 166 |
); |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
/** |
| 171 |
* Activate a plugin. |
| 172 |
* |
| 173 |
* @param {Object} plugin - The plugin or theme object. |
| 174 |
* @param {Object} pluginsData - Installed/active plugins data. |
| 175 |
* @param {Array} installingPlugins - Array of slugs currently installing. |
| 176 |
* @param {Array} activatingPlugins - Array of slugs currently activating. |
| 177 |
* @param {Function} setActivatingPlugins - Setter for activating plugins state. |
| 178 |
* @param {Object} queryClient - React Query client for cache invalidation. |
| 179 |
* @param {Function} toast - Toast function for notifications. |
| 180 |
* @return {Promise} Resolves when activation is complete. |
| 181 |
*/ |
| 182 |
export const activatePlugin = async ( |
| 183 |
plugin, |
| 184 |
pluginsData, |
| 185 |
installingPlugins, |
| 186 |
activatingPlugins, |
| 187 |
setActivatingPlugins, |
| 188 |
queryClient, |
| 189 |
toast |
| 190 |
) => { |
| 191 |
// Determine the type string (should be "Plugin"). |
| 192 |
// translators: %s: Plugin. |
| 193 |
const typeStr = |
| 194 |
plugin.type === 'theme' |
| 195 |
? __( 'Theme', 'suremails' ) |
| 196 |
: __( 'Plugin', 'suremails' ); |
| 197 |
|
| 198 |
if ( pluginsData.active.includes( plugin.slug ) ) { |
| 199 |
toast.info( |
| 200 |
/* translators: %s: Plugin or Theme. */ |
| 201 |
sprintf( __( '%s is already activated.', 'suremails' ), typeStr ) |
| 202 |
); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
if ( installingPlugins.length > 0 || activatingPlugins.length > 0 ) { |
| 207 |
toast.info( |
| 208 |
sprintf( |
| 209 |
/* translators: %s: Plugin or Theme. */ |
| 210 |
__( |
| 211 |
'Another %s operation is in progress. Please wait.', |
| 212 |
'suremails' |
| 213 |
), |
| 214 |
typeStr |
| 215 |
) |
| 216 |
); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
try { |
| 221 |
await handlePluginOperation( plugin, 'activate', setActivatingPlugins ); |
| 222 |
toast.success( |
| 223 |
/* translators: %s: Plugin or Theme. */ |
| 224 |
sprintf( __( '%s activated successfully.', 'suremails' ), typeStr ) |
| 225 |
); |
| 226 |
// Refresh the plugins data. |
| 227 |
queryClient.invalidateQueries( [ 'installed-plugins' ] ); |
| 228 |
} catch ( error ) { |
| 229 |
toast.error( |
| 230 |
/* translators: %s: Plugin or Theme. */ |
| 231 |
sprintf( __( 'Failed to activate %s.', 'suremails' ), typeStr ) |
| 232 |
); |
| 233 |
} finally { |
| 234 |
setActivatingPlugins( ( prev ) => |
| 235 |
prev.filter( ( slug ) => slug !== plugin.slug ) |
| 236 |
); |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
/** |
| 241 |
* Activate a theme. |
| 242 |
* |
| 243 |
* @param {Object} item - The theme object. |
| 244 |
* @param {Object} pluginsData - Installed/active plugins data. |
| 245 |
* @param {Array} installingPlugins - Array of slugs currently installing. |
| 246 |
* @param {Array} activatingPlugins - Array of slugs currently activating. |
| 247 |
* @param {Function} setActivatingPlugins - Setter for activating plugins state. |
| 248 |
* @param {Object} queryClient - React Query client for cache invalidation. |
| 249 |
* @param {Function} toast - Toast function for notifications. |
| 250 |
* @return {Promise} Resolves when activation is complete. |
| 251 |
*/ |
| 252 |
export const activateTheme = async ( |
| 253 |
item, |
| 254 |
pluginsData, |
| 255 |
installingPlugins, |
| 256 |
activatingPlugins, |
| 257 |
setActivatingPlugins, |
| 258 |
queryClient, |
| 259 |
toast |
| 260 |
) => { |
| 261 |
// translators: %s: Theme. |
| 262 |
const typeStr = __( 'Theme', 'suremails' ); |
| 263 |
|
| 264 |
if ( pluginsData.active.includes( item.slug ) ) { |
| 265 |
toast.info( |
| 266 |
/* translators: %s: Plugin or Theme. */ |
| 267 |
sprintf( __( '%s is already activated.', 'suremails' ), typeStr ) |
| 268 |
); |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
if ( installingPlugins.length > 0 || activatingPlugins.length > 0 ) { |
| 273 |
toast.info( |
| 274 |
sprintf( |
| 275 |
// translators: %s: Theme. |
| 276 |
__( |
| 277 |
'Another %s operation is in progress. Please wait.', |
| 278 |
'suremails' |
| 279 |
), |
| 280 |
typeStr |
| 281 |
) |
| 282 |
); |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
// Add theme slug to activating state. |
| 287 |
setActivatingPlugins( ( prev ) => [ ...prev, item.slug ] ); |
| 288 |
|
| 289 |
const data = { |
| 290 |
slug: item.slug, |
| 291 |
_ajax_nonce: window.suremails?._ajax_nonce, |
| 292 |
}; |
| 293 |
|
| 294 |
wp.ajax.send( 'suremails-activate_theme', { |
| 295 |
data, |
| 296 |
success: () => { |
| 297 |
toast.success( |
| 298 |
sprintf( |
| 299 |
/* translators: %s: Theme. */ |
| 300 |
__( '%s activated successfully.', 'suremails' ), |
| 301 |
typeStr |
| 302 |
) |
| 303 |
); |
| 304 |
queryClient.invalidateQueries( [ 'installed-plugins' ] ); |
| 305 |
}, |
| 306 |
error: () => { |
| 307 |
toast.error( |
| 308 |
/* translators: %s: Theme. */ |
| 309 |
sprintf( __( 'Failed to activate %s.', 'suremails' ), typeStr ) |
| 310 |
); |
| 311 |
}, |
| 312 |
complete: () => { |
| 313 |
setActivatingPlugins( ( prev ) => |
| 314 |
prev.filter( ( slug ) => slug !== item.slug ) |
| 315 |
); |
| 316 |
}, |
| 317 |
} ); |
| 318 |
}; |
| 319 |
|