| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
import { addQueryArgs } from '@wordpress/url'; |
| 3 |
|
| 4 |
export const getPlugin = async (slug) => { |
| 5 |
const response = await apiFetch({ |
| 6 |
path: addQueryArgs('/wp/v2/plugins', { search: slug }), |
| 7 |
}); |
| 8 |
|
| 9 |
let plugin = response?.[0]; |
| 10 |
|
| 11 |
if (!plugin) throw new Error('Plugin not found'); |
| 12 |
|
| 13 |
return plugin; |
| 14 |
}; |
| 15 |
|
| 16 |
export const installPlugin = async (slug) => { |
| 17 |
return await apiFetch({ |
| 18 |
path: '/wp/v2/plugins', |
| 19 |
method: 'POST', |
| 20 |
data: { |
| 21 |
slug, |
| 22 |
}, |
| 23 |
}); |
| 24 |
}; |
| 25 |
|
| 26 |
export const activatePlugin = async (slug) => { |
| 27 |
const plugin = await getPlugin(slug); |
| 28 |
|
| 29 |
return await apiFetch({ |
| 30 |
path: `/wp/v2/plugins/${plugin.plugin}`, |
| 31 |
method: 'POST', |
| 32 |
data: { |
| 33 |
status: 'active', |
| 34 |
}, |
| 35 |
}); |
| 36 |
}; |
| 37 |
|