-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect actual, loaded WP version (#1503)
## Motivation for the change, related issues When loading from browser storage, Playground can incorrectly assume the loaded WP version is the default version when it is actually something else. This can cause issues where remote assets are requested from the wrong version of WP. In addition, this work is needed to support a follow-up PR where we retrieve the `wordpress-remote-asset-paths` listing from the website when it is not already present within browser storage. ## Implementation details This PR adds two functions, one for detecting the version using `wp-includes/version.php`, and another for determining whether the detected version is one of the WP versions supported through the Playground web app. These functions are used when initializing worker threads to detect the version of WP that was booted. If detection fails, the WordPress version is left set to the default version. ## Testing Instructions (or ideally a Blueprint) - CI, including new unit tests
- Loading branch information
1 parent
1ed4c01
commit d6a90af
Showing
5 changed files
with
200 additions
and
13 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
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
97 changes: 97 additions & 0 deletions
97
packages/playground/wordpress/src/test/version-detect.spec.ts
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,97 @@ | ||
import { | ||
SupportedWordPressVersions, | ||
getSqliteDatabaseModule, | ||
getWordPressModule, | ||
} from '@wp-playground/wordpress-builds'; | ||
import { RecommendedPHPVersion } from '@wp-playground/common'; | ||
import { loadNodeRuntime } from '@php-wasm/node'; | ||
import { bootWordPress } from '../boot'; | ||
import { | ||
getLoadedWordPressVersion, | ||
versionStringToLoadedWordPressVersion, | ||
} from '../version-detect'; | ||
|
||
describe('Test WP version detection', async () => { | ||
for (const expectedWordPressVersion of Object.keys( | ||
SupportedWordPressVersions | ||
)) { | ||
it(`detects WP ${expectedWordPressVersion} at runtime`, async () => { | ||
const handler = await bootWordPress({ | ||
createPhpRuntime: async () => | ||
await loadNodeRuntime(RecommendedPHPVersion), | ||
siteUrl: 'http://playground-domain/', | ||
wordPressZip: await getWordPressModule( | ||
expectedWordPressVersion | ||
), | ||
sqliteIntegrationPluginZip: await getSqliteDatabaseModule(), | ||
}); | ||
const loadedWordPressVersion = await getLoadedWordPressVersion( | ||
handler | ||
); | ||
expect(loadedWordPressVersion).to.equal(expectedWordPressVersion); | ||
}); | ||
} | ||
|
||
it('errors when unable to read version at runtime', async () => { | ||
const handler = await bootWordPress({ | ||
createPhpRuntime: async () => | ||
await loadNodeRuntime(RecommendedPHPVersion), | ||
siteUrl: 'http://playground-domain/', | ||
wordPressZip: await getWordPressModule(), | ||
sqliteIntegrationPluginZip: await getSqliteDatabaseModule(), | ||
}); | ||
const php = await handler.getPrimaryPhp(); | ||
|
||
php.unlink(`${handler.documentRoot}/wp-includes/version.php`); | ||
const detectionResult = await getLoadedWordPressVersion(handler).then( | ||
() => 'no-error', | ||
() => 'error' | ||
); | ||
expect(detectionResult).to.equal('error'); | ||
}); | ||
|
||
it('errors on reading empty version at runtime', async () => { | ||
const handler = await bootWordPress({ | ||
createPhpRuntime: async () => | ||
await loadNodeRuntime(RecommendedPHPVersion), | ||
siteUrl: 'http://playground-domain/', | ||
wordPressZip: await getWordPressModule(), | ||
sqliteIntegrationPluginZip: await getSqliteDatabaseModule(), | ||
}); | ||
const php = await handler.getPrimaryPhp(); | ||
|
||
php.writeFile( | ||
`${handler.documentRoot}/wp-includes/version.php`, | ||
'<?php $wp_version = "";' | ||
); | ||
|
||
const detectionResult = await getLoadedWordPressVersion(handler).then( | ||
() => 'no-error', | ||
() => 'error' | ||
); | ||
expect(detectionResult).to.equal('error'); | ||
}); | ||
|
||
const versionMap = { | ||
'6.3': '6.3', | ||
'6.4.2': '6.4', | ||
'6.5': '6.5', | ||
'6.5.4': '6.5', | ||
'6.6-alpha-57783': 'nightly', | ||
'6.6-beta-57783': 'nightly', | ||
'6.6-RC-54321': 'nightly', | ||
'6.6-RC2-12345': 'nightly', | ||
'6.6-beta': 'beta', | ||
'6.6-beta2': 'beta', | ||
'6.6-RC': 'beta', | ||
'6.6-RC2': 'beta', | ||
'custom-version': 'custom-version', | ||
}; | ||
|
||
for (const [input, expected] of Object.entries(versionMap)) { | ||
it(`maps '${input}' to '${expected}'`, () => { | ||
const result = versionStringToLoadedWordPressVersion(input); | ||
expect(result).to.equal(expected); | ||
}); | ||
} | ||
}); |
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,50 @@ | ||
import type { PHPRequestHandler } from '@php-wasm/universal'; | ||
import { SupportedWordPressVersions } from '@wp-playground/wordpress-builds'; | ||
|
||
export async function getLoadedWordPressVersion( | ||
requestHandler: PHPRequestHandler | ||
): Promise<string> { | ||
const php = await requestHandler.getPrimaryPhp(); | ||
const result = await php.run({ | ||
code: `<?php | ||
require '${requestHandler.documentRoot}/wp-includes/version.php'; | ||
echo $wp_version; | ||
`, | ||
}); | ||
|
||
const versionString = result.text; | ||
if (!versionString) { | ||
throw new Error('Unable to read loaded WordPress version.'); | ||
} | ||
|
||
return versionStringToLoadedWordPressVersion(versionString); | ||
} | ||
|
||
export function versionStringToLoadedWordPressVersion( | ||
wpVersionString: string | ||
): string { | ||
const nightlyPattern = /-(alpha|beta|RC)\d*-\d+$/; | ||
if (nightlyPattern.test(wpVersionString)) { | ||
return 'nightly'; | ||
} | ||
|
||
// TODO: Tighten this to detect specific old beta version, like 6.2-beta. | ||
const betaPattern = /-(beta|RC)\d*$/; | ||
if (betaPattern.test(wpVersionString)) { | ||
return 'beta'; | ||
} | ||
|
||
const majorMinorMatch = wpVersionString.match(/^(\d+\.\d+)(?:\.\d+)?$/); | ||
if (majorMinorMatch !== null) { | ||
return majorMinorMatch[1]; | ||
} | ||
|
||
// Return original version string if we could not parse it. | ||
// This is important to allow so folks can bring their own WP builds. | ||
return wpVersionString; | ||
} | ||
|
||
export function isSupportedWordPressVersion(wpVersion: string) { | ||
const supportedVersionKeys = Object.keys(SupportedWordPressVersions); | ||
return supportedVersionKeys.includes(wpVersion); | ||
} |