| 1 |
/** |
| 2 |
* Handles WPGraphQL plugin installation at Settings → Headless. |
| 3 |
*/ |
| 4 |
faustwp.installWPGraphQL = (() => { |
| 5 |
const $button = document.getElementById('faustwp-button-install-graphql'); |
| 6 |
const $spinner = document.querySelector('.get-started .spinner'); |
| 7 |
const $status = document.querySelector('.get-started .error-message'); |
| 8 |
|
| 9 |
// Attaches event listeners. |
| 10 |
function init() { |
| 11 |
$button.onclick = (event) => { |
| 12 |
event.preventDefault(); |
| 13 |
installWPGraphQL().then((result) => { |
| 14 |
if (result.status === 'active') { |
| 15 |
update('complete'); |
| 16 |
} else { |
| 17 |
update('failed', faustwp.strings.failed); |
| 18 |
} |
| 19 |
}).catch(result => { |
| 20 |
update('failed', result.message || faustwp.strings.failed); |
| 21 |
}); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
// Installs and activates the WPGraphQL plugin. |
| 26 |
async function installWPGraphQL() { |
| 27 |
update('installing'); |
| 28 |
|
| 29 |
// Default to install and activate. |
| 30 |
const request = { |
| 31 |
path: '/wp/v2/plugins', |
| 32 |
method: 'POST', |
| 33 |
data: {slug: 'wp-graphql', status: 'active'} |
| 34 |
} |
| 35 |
|
| 36 |
// Just activate the plugin if it is already installed. |
| 37 |
if (faustwp.wpgraphqlIsInstalled) { |
| 38 |
request.path = '/wp/v2/plugins/wp-graphql/wp-graphql'; |
| 39 |
request.data = {status: 'active'}; |
| 40 |
} |
| 41 |
|
| 42 |
return await wp.apiFetch(request); |
| 43 |
} |
| 44 |
|
| 45 |
// Updates the button and spinner. |
| 46 |
function update(state = 'installing', error = '') { |
| 47 |
switch (state) { |
| 48 |
case 'installing': |
| 49 |
wp.a11y.speak(faustwp.strings.installing, 'polite'); |
| 50 |
$button.innerHTML = faustwp.strings.installing; |
| 51 |
$button.disabled = true; |
| 52 |
$spinner.style.visibility = 'visible'; |
| 53 |
$status.innerHTML = ''; |
| 54 |
break; |
| 55 |
case 'complete': |
| 56 |
wp.a11y.speak(faustwp.strings.active, 'polite'); |
| 57 |
$button.innerHTML = `☑️ ${faustwp.strings.active}`; |
| 58 |
$spinner.style.visibility = 'hidden'; |
| 59 |
break; |
| 60 |
case 'failed': |
| 61 |
wp.a11y.speak(error || faustwp.strings.failed, 'polite'); |
| 62 |
$button.innerHTML = faustwp.strings.default; |
| 63 |
$button.disabled = false; |
| 64 |
$spinner.style.visibility = 'hidden'; |
| 65 |
$status.innerHTML = error; |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return {init: init} |
| 71 |
})(); |
| 72 |
|
| 73 |
faustwp.installWPGraphQL.init(); |
| 74 |
|