| 1 |
import { forceReinstallPlugin, runUpdates } from '@auto-launch/functions/setup'; |
| 2 |
import { digest } from '@shared/api/digest'; |
| 3 |
import { Spinner } from '@wordpress/components'; |
| 4 |
import { useCallback, useEffect } from '@wordpress/element'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
|
| 7 |
// The reload is also the retry; the server bounds how many, so it can't loop. |
| 8 |
export const LaunchUpdate = ({ |
| 9 |
pluginUpdateNeeded, |
| 10 |
themeUpdateNeeded, |
| 11 |
pluginUpdateViaRest, |
| 12 |
pluginSlug, |
| 13 |
attempt, |
| 14 |
}) => { |
| 15 |
const cardClass = 'flex w-full flex-col items-center p-6 text-center lg:p-10'; |
| 16 |
|
| 17 |
const applyUpdates = useCallback(async () => { |
| 18 |
// Run serially, not concurrently: two WP upgraders fight over the shared |
| 19 |
// maintenance flag and filesystem. |
| 20 |
const phpUpdateNeeded = |
| 21 |
themeUpdateNeeded || (pluginUpdateNeeded && !pluginUpdateViaRest); |
| 22 |
try { |
| 23 |
if (phpUpdateNeeded) { |
| 24 |
const result = await runUpdates(); |
| 25 |
if (result?.errors?.length) { |
| 26 |
throw new Error(result.errors.join('; ')); |
| 27 |
} |
| 28 |
} |
| 29 |
if (pluginUpdateNeeded && pluginUpdateViaRest) { |
| 30 |
await forceReinstallPlugin(pluginSlug); |
| 31 |
} |
| 32 |
} catch (error) { |
| 33 |
digest({ |
| 34 |
error, |
| 35 |
details: { source: 'auto-launch', caller: 'launch-update', attempt }, |
| 36 |
}); |
| 37 |
} finally { |
| 38 |
window.location.reload(); |
| 39 |
} |
| 40 |
}, [ |
| 41 |
pluginUpdateNeeded, |
| 42 |
themeUpdateNeeded, |
| 43 |
pluginUpdateViaRest, |
| 44 |
pluginSlug, |
| 45 |
attempt, |
| 46 |
]); |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
applyUpdates(); |
| 50 |
}, [applyUpdates]); |
| 51 |
|
| 52 |
return ( |
| 53 |
<div className={cardClass}> |
| 54 |
<Spinner className="h-10 w-10 text-design-main" /> |
| 55 |
<h2 className="mt-6 mb-0 p-0 text-lg font-semibold text-gray-900"> |
| 56 |
{__('Updating your site', 'extendify-local')} |
| 57 |
</h2> |
| 58 |
<p className="mx-auto mt-3 mb-0 max-w-md p-0 text-base text-gray-900"> |
| 59 |
{__( |
| 60 |
"We're installing the latest updates. Please don't close this window — it will only take a moment.", |
| 61 |
'extendify-local', |
| 62 |
)} |
| 63 |
</p> |
| 64 |
</div> |
| 65 |
); |
| 66 |
}; |
| 67 |
|