From 8b49fc59e4ce2915d156a57f1f31908ab4c0f08d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Sat, 4 Nov 2023 21:05:34 -0700 Subject: [PATCH 01/10] Make wp-env compatible with WordPress versions older than 5.4 by fixing wp-config anchors. --- packages/env/lib/wordpress.js | 24 ++++++++++++++++++++++-- packages/env/package.json | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index e8c20aa70f2158..db2835a68075cd 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -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 @@ -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',"`; + // Set wp-config.php values. for ( let [ key, value ] of Object.entries( config.env[ environment ].config @@ -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 }${ typeof value !== 'string' ? ' --raw' : '' }` ); @@ -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__) . '\\/' );`; + } + // 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. @@ -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, diff --git a/packages/env/package.json b/packages/env/package.json index 42ebbdc9a821f6..a06111874d2969 100644 --- a/packages/env/package.json +++ b/packages/env/package.json @@ -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" From 210301815193995d9f117392ebdf9a55db101b77 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Aug 2024 13:16:55 -0700 Subject: [PATCH 02/10] Use simple version compare function instead of semver library. --- packages/env/lib/wordpress.js | 44 +++++++++++++++++++++++++++++++---- packages/env/package.json | 1 - 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index 3e2313d9d891f2..1f99b437608647 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -8,7 +8,6 @@ const fs = require( 'fs' ).promises; const path = require( 'path' ); const got = require( 'got' ); const dns = require( 'dns' ).promises; -const semver = require( 'semver' ); /** * Promisified dependencies @@ -28,6 +27,43 @@ const { getCache, setCache } = require( './cache' ); * @typedef {'development'|'tests'|'all'} WPEnvironmentSelection */ +/** + * Utility function to check if a version is lower than another version, only considering the major and minor portions. + * + * This is a non-comprehensive check only intended for this usage, to avoid pulling in a full semver library. + * + * @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 isMajorMinorVersionLower( version, compareVersion ) { + const versionParts = version.split( '.' ).map( Number ); + const compareVersionParts = compareVersion.split( '.' ).map( Number ); + + // If the major version is lower, the version is lower. + if ( versionParts[ 0 ] < compareVersionParts[ 0 ] ) { + return true; + } + + // If the major version is the same, we need to compare the minor version. + if ( versionParts[ 0 ] === compareVersionParts[ 0 ] ) { + if ( versionParts[ 1 ] !== undefined && compareVersionParts[ 1 ] === undefined ) { + return versionParts[ 1 ] < compareVersionParts[ 1 ]; + } + + // Here the version has no minor version, which is the same as having a minor version of 0. + if ( compareVersionParts[ 1 ] !== undefined ) { + return 0 < compareVersionParts[ 1 ]; + } + + // Here the compare version has no minor version (i.e. 0), and nothing is lower than 0. + return false; + } + + // If the major version is higher, the version is higher. + return false; +} + /** * Checks a WordPress database connection. An error is thrown if the test is * unsuccessful. @@ -65,7 +101,7 @@ async function configureWordPress( environment, config, spinner ) { 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',"`; + const configAnchor = wpVersion && isMajorMinorVersionLower( wpVersion, '5.1' ) ? `"define('WP_DEBUG',"` : `"define( 'WP_DEBUG',"`; // Set wp-config.php values. for ( let [ key, value ] of Object.entries( @@ -112,9 +148,9 @@ 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' ) ) { + if ( wpVersion && isMajorMinorVersionLower( wpVersion, '5.1' ) ) { abspathDef = `define('ABSPATH', dirname(__FILE__) . '\\/');`; - } else if ( wpVersion && semver.lt( wpVersion, '5.4' ) ) { + } else if ( wpVersion && isMajorMinorVersionLower( wpVersion, '5.4' ) ) { abspathDef = `define( 'ABSPATH', dirname(__FILE__) . '\\/' );`; } diff --git a/packages/env/package.json b/packages/env/package.json index 4539464b81465b..2b8efcfbf07af7 100644 --- a/packages/env/package.json +++ b/packages/env/package.json @@ -45,7 +45,6 @@ "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" From 7be491dac0a453f5f4ba0cca3ce06d8c43696e13 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Aug 2024 13:26:39 -0700 Subject: [PATCH 03/10] Fix lint violations. --- packages/env/lib/wordpress.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index 1f99b437608647..36a0547aa81d97 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -47,7 +47,10 @@ function isMajorMinorVersionLower( version, compareVersion ) { // If the major version is the same, we need to compare the minor version. if ( versionParts[ 0 ] === compareVersionParts[ 0 ] ) { - if ( versionParts[ 1 ] !== undefined && compareVersionParts[ 1 ] === undefined ) { + if ( + versionParts[ 1 ] !== undefined && + compareVersionParts[ 1 ] === undefined + ) { return versionParts[ 1 ] < compareVersionParts[ 1 ]; } @@ -90,7 +93,11 @@ async function checkDatabaseConnection( { dockerComposeConfigPath, debug } ) { async function configureWordPress( environment, config, spinner ) { let wpVersion = ''; try { - wpVersion = readWordPressVersion( config.env[ environment ].coreSource, spinner, config.debug ); + wpVersion = readWordPressVersion( + config.env[ environment ].coreSource, + spinner, + config.debug + ); } catch ( err ) { // Ignore error. } @@ -101,7 +108,10 @@ async function configureWordPress( environment, config, spinner ) { const setupCommands = [ 'set -eo pipefail', installCommand ]; // WordPress versions below 5.1 didn't use proper spacing in wp-config. - const configAnchor = wpVersion && isMajorMinorVersionLower( wpVersion, '5.1' ) ? `"define('WP_DEBUG',"` : `"define( 'WP_DEBUG',"`; + const configAnchor = + wpVersion && isMajorMinorVersionLower( wpVersion, '5.1' ) + ? `"define('WP_DEBUG',"` + : `"define( 'WP_DEBUG',"`; // Set wp-config.php values. for ( let [ key, value ] of Object.entries( From 633dc057264d0a2dd719b33cf933cbd88b1f15fa Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Aug 2024 13:59:52 -0700 Subject: [PATCH 04/10] Convert wpVersion to a string if needed. --- packages/env/lib/wordpress.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index 36a0547aa81d97..ea7b57d07051e7 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -98,6 +98,11 @@ async function configureWordPress( environment, config, spinner ) { spinner, config.debug ); + + // Sometimes wpVersion may not be a string, in this case, convert it to a string. + if ( typeof wpVersion !== 'string' ) { + wpVersion = '' + wpVersion; + } } catch ( err ) { // Ignore error. } From 23c3d31cda54e63fa302daabff3b7636fa8cf66b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 14 Aug 2024 13:03:57 -0700 Subject: [PATCH 05/10] Simplify utility function for WP version check. --- packages/env/lib/wordpress.js | 50 ++++++++++++----------------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index ea7b57d07051e7..ed770a3d6bfe10 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -28,43 +28,27 @@ const { getCache, setCache } = require( './cache' ); */ /** - * Utility function to check if a version is lower than another version, only considering the major and minor portions. + * 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 isMajorMinorVersionLower( version, compareVersion ) { - const versionParts = version.split( '.' ).map( Number ); - const compareVersionParts = compareVersion.split( '.' ).map( Number ); - - // If the major version is lower, the version is lower. - if ( versionParts[ 0 ] < compareVersionParts[ 0 ] ) { - return true; - } - - // If the major version is the same, we need to compare the minor version. - if ( versionParts[ 0 ] === compareVersionParts[ 0 ] ) { - if ( - versionParts[ 1 ] !== undefined && - compareVersionParts[ 1 ] === undefined - ) { - return versionParts[ 1 ] < compareVersionParts[ 1 ]; - } - - // Here the version has no minor version, which is the same as having a minor version of 0. - if ( compareVersionParts[ 1 ] !== undefined ) { - return 0 < compareVersionParts[ 1 ]; - } - - // Here the compare version has no minor version (i.e. 0), and nothing is lower than 0. - return false; - } +function isWPMajorMinorVersionLower( version, compareVersion ) { + const versionNumber = Number.parseFloat( + version.match( /^[0-9]+(\.[0-9]+)?/ )[ 0 ] + ); + const compareVersionNumber = Number.parseFloat( + compareVersion.match( /^[0-9]+(\.[0-9]+)?/ )[ 0 ] + ); - // If the major version is higher, the version is higher. - return false; + return versionNumber < compareVersionNumber; } /** @@ -101,7 +85,7 @@ async function configureWordPress( environment, config, spinner ) { // Sometimes wpVersion may not be a string, in this case, convert it to a string. if ( typeof wpVersion !== 'string' ) { - wpVersion = '' + wpVersion; + wpVersion = `${ wpVersion }`; } } catch ( err ) { // Ignore error. @@ -114,7 +98,7 @@ async function configureWordPress( environment, config, spinner ) { // WordPress versions below 5.1 didn't use proper spacing in wp-config. const configAnchor = - wpVersion && isMajorMinorVersionLower( wpVersion, '5.1' ) + wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' ) ? `"define('WP_DEBUG',"` : `"define( 'WP_DEBUG',"`; @@ -163,9 +147,9 @@ 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 && isMajorMinorVersionLower( wpVersion, '5.1' ) ) { + if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' ) ) { abspathDef = `define('ABSPATH', dirname(__FILE__) . '\\/');`; - } else if ( wpVersion && isMajorMinorVersionLower( wpVersion, '5.4' ) ) { + } else if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.4' ) ) { abspathDef = `define( 'ABSPATH', dirname(__FILE__) . '\\/' );`; } From c0e11e443caa48b402780750f0b360eaae68339a Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 14 Aug 2024 13:05:34 -0700 Subject: [PATCH 06/10] Temp test. --- packages/env/lib/wordpress.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index ed770a3d6bfe10..e8974021bb1428 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -41,6 +41,8 @@ const { getCache, setCache } = require( './cache' ); * @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 ] ); From b686ea40ac756692c839b5f4dc1845aa74cd4bde Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 14 Aug 2024 13:06:02 -0700 Subject: [PATCH 07/10] Temp test. --- packages/env/lib/wordpress.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index e8974021bb1428..682cd556f3515c 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -84,11 +84,6 @@ async function configureWordPress( environment, config, spinner ) { spinner, config.debug ); - - // Sometimes wpVersion may not be a string, in this case, convert it to a string. - if ( typeof wpVersion !== 'string' ) { - wpVersion = `${ wpVersion }`; - } } catch ( err ) { // Ignore error. } From 1be7b01181f16f8478b0f987df95768deb40ab7a Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 14 Aug 2024 13:17:23 -0700 Subject: [PATCH 08/10] Temp test. --- packages/env/lib/wordpress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index 682cd556f3515c..b943dfb4300b26 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -79,7 +79,7 @@ async function checkDatabaseConnection( { dockerComposeConfigPath, debug } ) { async function configureWordPress( environment, config, spinner ) { let wpVersion = ''; try { - wpVersion = readWordPressVersion( + wpVersion = await readWordPressVersion( config.env[ environment ].coreSource, spinner, config.debug From edf9c0220e3ef65f7e8110ef090025abc5399627 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 14 Aug 2024 13:47:21 -0700 Subject: [PATCH 09/10] Remove temp code. --- packages/env/lib/wordpress.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index b943dfb4300b26..c102d0c8b1c873 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -41,8 +41,6 @@ const { getCache, setCache } = require( './cache' ); * @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 ] ); From 022c731a0c96328b93695037002135088c96dd12 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 14 Aug 2024 13:56:26 -0700 Subject: [PATCH 10/10] Add missing spaces in WP 5.1-5.3 code. --- packages/env/lib/wordpress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index c102d0c8b1c873..bd3c4a23f8ff5d 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -145,7 +145,7 @@ async function configureWordPress( environment, config, spinner ) { if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' ) ) { abspathDef = `define('ABSPATH', dirname(__FILE__) . '\\/');`; } else if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.4' ) ) { - abspathDef = `define( 'ABSPATH', dirname(__FILE__) . '\\/' );`; + abspathDef = `define( 'ABSPATH', dirname( __FILE__ ) . '\\/' );`; } // WordPress' PHPUnit suite expects a `wp-tests-config.php` in