-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow PHP version to be changed from Site Settings (#225)
* Display the PHP version on Site Settings screen * Add a modal for editing the PHP version * First pass at saving selected PHP version * Update site details to allow user to set PHP version (#226) * Add phpVersion to updateSiteDetails * Update tests for changing the PHP version * Abstract available PHP versions to a constant * Use PHP constants from `wp-now` (#231) * Use `DEFAULT_PHP_VERSION` constant from `wp-now` * Use available PHP versions of Playground * Add `web-streams-polyfill` package for unit tests * Polyfill `ReadableStream` in unit tests Web streams are used by `php-wasm`, so we need to polyfull them if we import the library in unit tests. * Add unit tests to cover changing PHP version functionality (#233) * Mock `matchMedia` * Expose label in `SettingsRow` component * Add test to cover the case of changing PHP version * Revert "Expose label in `SettingsRow` component" This reverts commit bc37161. * Update change PHP version test case * Remove `getPhpVersion` hook and IPC handler (#239) * Remove `getPhpVersion` hook and IPC handler * Use `??` operator instead of `||` when setting the php version * Add inline comment in Jest setup The comment clarifies why we need the polyfill. Related to: #231 (comment) * Update `ContentTabSettings` unit tests * Bump default PHP version to `8.1` (#240) * Bump default PHP version to `8.1` This change is driven by the fact that version `8.0` already reached its end-of-life by 2024. https://www.php.net/supported-versions.php * Address failure in `createSite` unit test --------- Co-authored-by: Derek Blank <derekpblank@gmail.com> Co-authored-by: Carlos Garcia <fluiddot@gmail.com>
- Loading branch information
1 parent
dfb43ed
commit ab0cc21
Showing
18 changed files
with
262 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { SupportedPHPVersions } from '@php-wasm/universal'; | ||
import { SelectControl } from '@wordpress/components'; | ||
import { useI18n } from '@wordpress/react-i18n'; | ||
import { FormEvent, useCallback, useEffect, useState } from 'react'; | ||
import { DEFAULT_PHP_VERSION } from '../../vendor/wp-now/src/constants'; | ||
import { useSiteDetails } from '../hooks/use-site-details'; | ||
import Button from './button'; | ||
import Modal from './modal'; | ||
|
||
export default function EditPhpVersion() { | ||
const { __ } = useI18n(); | ||
const { updateSite, selectedSite, stopServer, startServer } = useSiteDetails(); | ||
const [ editPhpVersionError, setEditPhpVersionError ] = useState( '' ); | ||
const [ selectedPhpVersion, setSelectedPhpVersion ] = useState( DEFAULT_PHP_VERSION ); | ||
const [ needsToEditPhpVersion, setNeedsToEditPhpVersion ] = useState( false ); | ||
const [ isEditingSite, setIsEditingSite ] = useState( false ); | ||
|
||
useEffect( () => { | ||
if ( selectedSite ) { | ||
setSelectedPhpVersion( selectedSite.phpVersion ); | ||
} | ||
}, [ selectedSite ] ); | ||
|
||
const resetLocalState = useCallback( () => { | ||
setNeedsToEditPhpVersion( false ); | ||
setSelectedPhpVersion( '' ); | ||
setEditPhpVersionError( '' ); | ||
}, [] ); | ||
|
||
const onSiteEdit = useCallback( | ||
async ( event: FormEvent ) => { | ||
event.preventDefault(); | ||
if ( ! selectedSite ) { | ||
return; | ||
} | ||
setIsEditingSite( true ); | ||
try { | ||
const running = selectedSite.running; | ||
await updateSite( { | ||
...selectedSite, | ||
phpVersion: selectedPhpVersion, | ||
} ); | ||
if ( running ) { | ||
await stopServer( selectedSite.id ); | ||
await startServer( selectedSite.id ); | ||
} | ||
setNeedsToEditPhpVersion( false ); | ||
resetLocalState(); | ||
} catch ( e ) { | ||
setEditPhpVersionError( ( e as Error )?.message ); | ||
} | ||
setIsEditingSite( false ); | ||
}, | ||
[ updateSite, selectedSite, selectedPhpVersion, resetLocalState, startServer, stopServer ] | ||
); | ||
|
||
return ( | ||
<> | ||
{ needsToEditPhpVersion && ( | ||
<Modal | ||
size="medium" | ||
title={ __( 'Edit PHP version' ) } | ||
isDismissible | ||
focusOnMount="firstContentElement" | ||
onRequestClose={ resetLocalState } | ||
> | ||
<form onSubmit={ onSiteEdit }> | ||
<label className="flex flex-col gap-1.5 leading-4"> | ||
<span className="font-semibold">{ __( 'PHP version' ) }</span> | ||
<SelectControl | ||
value={ selectedPhpVersion } | ||
options={ SupportedPHPVersions.map( ( version ) => ( { | ||
label: version, | ||
value: version, | ||
} ) ) } | ||
onChange={ ( version ) => setSelectedPhpVersion( version ) } | ||
/> | ||
</label> | ||
<div className="flex flex-row justify-end gap-x-5 mt-6"> | ||
<Button onClick={ resetLocalState } disabled={ isEditingSite } variant="tertiary"> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
<Button | ||
type="submit" | ||
variant="primary" | ||
isBusy={ isEditingSite } | ||
disabled={ Boolean( | ||
isEditingSite || | ||
! selectedSite || | ||
selectedSite?.phpVersion === selectedPhpVersion || | ||
editPhpVersionError | ||
) } | ||
> | ||
{ isEditingSite ? __( 'Restarting server…' ) : __( 'Save' ) } | ||
</Button> | ||
</div> | ||
</form> | ||
</Modal> | ||
) } | ||
<Button | ||
disabled={ ! selectedSite } | ||
className="!mx-4 shrink-0" | ||
onClick={ () => { | ||
if ( selectedSite ) { | ||
setSelectedPhpVersion( selectedSite.phpVersion ); | ||
} | ||
setNeedsToEditPhpVersion( true ); | ||
} } | ||
label={ __( 'Edit PHP version' ) } | ||
variant="link" | ||
> | ||
{ __( 'Edit' ) } | ||
</Button> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.