| 1 |
// @api/plugins.js |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Fetches the list of installed and active plugins. |
| 7 |
* |
| 8 |
* @return {Promise<Object>} The object containing installed and active plugins. |
| 9 |
*/ |
| 10 |
export const fetchInstalledPluginsData = async () => { |
| 11 |
try { |
| 12 |
const response = await apiFetch( { |
| 13 |
path: '/suremails/v1/installed-plugins', |
| 14 |
method: 'GET', |
| 15 |
headers: { |
| 16 |
'Content-Type': 'application/json', |
| 17 |
'X-WP-Nonce': suremails.nonce, |
| 18 |
}, |
| 19 |
} ); |
| 20 |
|
| 21 |
if ( |
| 22 |
response?.success && |
| 23 |
response?.plugins?.installed && |
| 24 |
response?.plugins?.active |
| 25 |
) { |
| 26 |
return { |
| 27 |
installed: response.plugins.installed, |
| 28 |
active: response.plugins.active, |
| 29 |
}; |
| 30 |
} |
| 31 |
|
| 32 |
throw new Error( |
| 33 |
__( 'Invalid data received from server.', 'suremails' ) |
| 34 |
); |
| 35 |
} catch ( error ) { |
| 36 |
throw new Error( |
| 37 |
error.message || |
| 38 |
__( 'Failed to fetch installed plugins.', 'suremails' ) |
| 39 |
); |
| 40 |
} |
| 41 |
}; |
| 42 |
|