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
24 changes: 22 additions & 2 deletions packages/env/lib/wordpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require( 'fs' ).promises;
const path = require( 'path' );
const got = require( 'got' );
const dns = require( 'dns' ).promises;
const semver = require( 'semver' );

/**
* Promisified dependencies
Expand Down Expand Up @@ -51,11 +52,21 @@ async function checkDatabaseConnection( { dockerComposeConfigPath, debug } ) {
* @param {Object} spinner A CLI spinner which indicates progress.
*/
async function configureWordPress( environment, config, spinner ) {
let wpVersion = '';
try {
wpVersion = 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 && semver.lt( wpVersion, '5.1' ) ? `"define('WP_DEBUG',"` : `"define( 'WP_DEBUG',"`;
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

// Set wp-config.php values.
for ( let [ key, value ] of Object.entries(
config.env[ environment ].config
Expand All @@ -68,7 +79,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 +109,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 && semver.lt( wpVersion, '5.1' ) ) {
abspathDef = `define('ABSPATH', dirname(__FILE__) . '\\/');`;
} else if ( wpVersion && semver.lt( 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 +126,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
1 change: 1 addition & 0 deletions packages/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"js-yaml": "^3.13.1",
"ora": "^4.0.2",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"simple-git": "^3.5.0",
"terminal-link": "^2.0.0",
"yargs": "^17.3.0"
Expand Down
Loading