@wordpress/env 10.4.0 → 10.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/wordpress.js +52 -2
  2. package/package.json +2 -2
package/lib/wordpress.js CHANGED
@@ -27,6 +27,30 @@ const { getCache, setCache } = require( './cache' );
27
27
  * @typedef {'development'|'tests'|'all'} WPEnvironmentSelection
28
28
  */
29
29
 
30
+ /**
31
+ * Utility function to check if a WordPress version is lower than another version.
32
+ *
33
+ * This is a non-comprehensive check only intended for this usage, to avoid pulling in a full semver library.
34
+ * It only considers the major and minor portions of the version and ignores the rest. Additionally, it assumes that
35
+ * the minor version is always a single digit (i.e. 0-9).
36
+ *
37
+ * Do not use this function for general version comparison, as it will not work for all cases.
38
+ *
39
+ * @param {string} version The version to check.
40
+ * @param {string} compareVersion The compare version to check whether the version is lower than.
41
+ * @return {boolean} True if the version is lower than the compare version, false otherwise.
42
+ */
43
+ function isWPMajorMinorVersionLower( version, compareVersion ) {
44
+ const versionNumber = Number.parseFloat(
45
+ version.match( /^[0-9]+(\.[0-9]+)?/ )[ 0 ]
46
+ );
47
+ const compareVersionNumber = Number.parseFloat(
48
+ compareVersion.match( /^[0-9]+(\.[0-9]+)?/ )[ 0 ]
49
+ );
50
+
51
+ return versionNumber < compareVersionNumber;
52
+ }
53
+
30
54
  /**
31
55
  * Checks a WordPress database connection. An error is thrown if the test is
32
56
  * unsuccessful.
@@ -51,11 +75,28 @@ async function checkDatabaseConnection( { dockerComposeConfigPath, debug } ) {
51
75
  * @param {Object} spinner A CLI spinner which indicates progress.
52
76
  */
53
77
  async function configureWordPress( environment, config, spinner ) {
78
+ let wpVersion = '';
79
+ try {
80
+ wpVersion = await readWordPressVersion(
81
+ config.env[ environment ].coreSource,
82
+ spinner,
83
+ config.debug
84
+ );
85
+ } catch ( err ) {
86
+ // Ignore error.
87
+ }
88
+
54
89
  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`;
55
90
 
56
91
  // -eo pipefail exits the command as soon as anything fails in bash.
57
92
  const setupCommands = [ 'set -eo pipefail', installCommand ];
58
93
 
94
+ // WordPress versions below 5.1 didn't use proper spacing in wp-config.
95
+ const configAnchor =
96
+ wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' )
97
+ ? `"define('WP_DEBUG',"`
98
+ : `"define( 'WP_DEBUG',"`;
99
+
59
100
  // Set wp-config.php values.
60
101
  for ( let [ key, value ] of Object.entries(
61
102
  config.env[ environment ].config
@@ -68,7 +109,7 @@ async function configureWordPress( environment, config, spinner ) {
68
109
  // Add quotes around string values to work with multi-word strings better.
69
110
  value = typeof value === 'string' ? `"${ value }"` : value;
70
111
  setupCommands.push(
71
- `wp config set ${ key } ${ value } --anchor="define( 'WP_DEBUG',"${
112
+ `wp config set ${ key } ${ value } --anchor=${ configAnchor }${
72
113
  typeof value !== 'string' ? ' --raw' : ''
73
114
  }`
74
115
  );
@@ -98,6 +139,15 @@ async function configureWordPress( environment, config, spinner ) {
98
139
  }
99
140
  );
100
141
 
142
+ // WordPress versions below 5.1 didn't use proper spacing in wp-config.
143
+ // Additionally, WordPress versions below 5.4 used `dirname( __FILE__ )` instead of `__DIR__`.
144
+ let abspathDef = `define( 'ABSPATH', __DIR__ . '\\/' );`;
145
+ if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' ) ) {
146
+ abspathDef = `define('ABSPATH', dirname(__FILE__) . '\\/');`;
147
+ } else if ( wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.4' ) ) {
148
+ abspathDef = `define( 'ABSPATH', dirname( __FILE__ ) . '\\/' );`;
149
+ }
150
+
101
151
  // WordPress' PHPUnit suite expects a `wp-tests-config.php` in
102
152
  // the directory that the test suite is contained within.
103
153
  // Make sure ABSPATH points to the WordPress install.
@@ -106,7 +156,7 @@ async function configureWordPress( environment, config, spinner ) {
106
156
  [
107
157
  'sh',
108
158
  '-c',
109
- `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`,
159
+ `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`,
110
160
  ],
111
161
  {
112
162
  config: config.dockerComposeConfigPath,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/env",
3
- "version": "10.4.0",
3
+ "version": "10.6.0",
4
4
  "description": "A zero-config, self contained local WordPress environment for development and testing.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -55,5 +55,5 @@
55
55
  "scripts": {
56
56
  "test": "echo \"Error: run tests from root\" && exit 1"
57
57
  },
58
- "gitHead": "363edb39b8dda8727f652e42cbb8497732693ed2"
58
+ "gitHead": "ab9564947967bb3f00343130954b9efacba6cdd7"
59
59
  }