Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wp-env compatible with WordPress versions older than 5.4 by fixing wp-config anchors #55864

Merged
merged 12 commits into from
Aug 14, 2024
56 changes: 54 additions & 2 deletions packages/env/lib/wordpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ const { getCache, setCache } = require( './cache' );
* @typedef {'development'|'tests'|'all'} WPEnvironmentSelection
*/

/**
* Utility function to check if a WordPress version is lower than another version.
*
* This is a non-comprehensive check only intended for this usage, to avoid pulling in a full semver library.
* It only considers the major and minor portions of the version and ignores the rest. Additionally, it assumes that
* the minor version is always a single digit (i.e. 0-9).
*
* Do not use this function for general version comparison, as it will not work for all cases.
*
* @param {string} version The version to check.
* @param {string} compareVersion The compare version to check whether the version is lower than.
* @return {boolean} True if the version is lower than the compare version, false otherwise.
*/
function isWPMajorMinorVersionLower( version, compareVersion ) {
console.log( version, typeof version ); // eslint-disable-line no-console

const versionNumber = Number.parseFloat(
version.match( /^[0-9]+(\.[0-9]+)?/ )[ 0 ]
);
const compareVersionNumber = Number.parseFloat(
compareVersion.match( /^[0-9]+(\.[0-9]+)?/ )[ 0 ]
);

return versionNumber < compareVersionNumber;
}

/**
* Checks a WordPress database connection. An error is thrown if the test is
* unsuccessful.
Expand All @@ -51,11 +77,28 @@ async function checkDatabaseConnection( { dockerComposeConfigPath, debug } ) {
* @param {Object} spinner A CLI spinner which indicates progress.
*/
async function configureWordPress( environment, config, spinner ) {
let wpVersion = '';
try {
wpVersion = await readWordPressVersion(
config.env[ environment ].coreSource,
spinner,
config.debug
);
} catch ( err ) {
// Ignore error.
}

const installCommand = `wp core install --url="${ config.env[ environment ].config.WP_SITEURL }" --title="${ config.name }" --admin_user=admin --admin_password=password --admin_email=wordpress@example.com --skip-email`;

// -eo pipefail exits the command as soon as anything fails in bash.
const setupCommands = [ 'set -eo pipefail', installCommand ];

// WordPress versions below 5.1 didn't use proper spacing in wp-config.
const configAnchor =
wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' )
? `"define('WP_DEBUG',"`
: `"define( 'WP_DEBUG',"`;

// Set wp-config.php values.
for ( let [ key, value ] of Object.entries(
config.env[ environment ].config
Expand All @@ -68,7 +111,7 @@ async function configureWordPress( environment, config, spinner ) {
// Add quotes around string values to work with multi-word strings better.
value = typeof value === 'string' ? `"${ value }"` : value;
setupCommands.push(
`wp config set ${ key } ${ value } --anchor="define( 'WP_DEBUG',"${
`wp config set ${ key } ${ value } --anchor=${ configAnchor }${
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
typeof value !== 'string' ? ' --raw' : ''
}`
);
Expand Down Expand Up @@ -98,6 +141,15 @@ async function configureWordPress( environment, config, spinner ) {
}
);

// WordPress versions below 5.1 didn't use proper spacing in wp-config.
// Additionally, WordPress versions below 5.4 used `dirname( __FILE__ )` instead of `__DIR__`.
let abspathDef = `define( 'ABSPATH', __DIR__ . '\\/' );`;
if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' ) ) {
abspathDef = `define('ABSPATH', dirname(__FILE__) . '\\/');`;
} else if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.4' ) ) {
abspathDef = `define( 'ABSPATH', dirname(__FILE__) . '\\/' );`;
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
}

// WordPress' PHPUnit suite expects a `wp-tests-config.php` in
// the directory that the test suite is contained within.
// Make sure ABSPATH points to the WordPress install.
Expand All @@ -106,7 +158,7 @@ async function configureWordPress( environment, config, spinner ) {
[
'sh',
'-c',
`sed -e "/^require.*wp-settings.php/d" -e "s/define( 'ABSPATH', __DIR__ . '\\/' );/define( 'ABSPATH', '\\/var\\/www\\/html\\/' );\\n\\tdefine( 'WP_DEFAULT_THEME', 'default' );/" /var/www/html/wp-config.php > /wordpress-phpunit/wp-tests-config.php`,
`sed -e "/^require.*wp-settings.php/d" -e "s/${ abspathDef }/define( 'ABSPATH', '\\/var\\/www\\/html\\/' );\\n\\tdefine( 'WP_DEFAULT_THEME', 'default' );/" /var/www/html/wp-config.php > /wordpress-phpunit/wp-tests-config.php`,
],
{
config: config.dockerComposeConfigPath,
Expand Down
Loading