Skip to content

Commit

Permalink
WIP: wp-env: add multisite support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Dec 12, 2024
1 parent 0ef8a35 commit 74f7b98
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Enhancements

- Add phpMyAdmin as an optional service. Enabled via the new `phpmyadminPort` environment config, as well as env vars `WP_ENV_PHPMYADMIN_PORT` and `WP_ENV_TESTS_PHPMYADMIN_PORT`.
- Add support for WordPress multisite installations. Enabled via the new `multisite` environment config.

### Internal

Expand Down
6 changes: 6 additions & 0 deletions packages/env/lib/config/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const mergeConfigs = require( './merge-configs' );
* @property {number} port The port to use.
* @property {number} mysqlPort The port to use for MySQL. Random if empty.
* @property {number} phpmyadminPort The port to use for phpMyAdmin. If empty, disabled phpMyAdmin.
* @property {boolean} multisite Whether to set up a multisite installation.
* @property {Object} config Mapping of wp-config.php constants to their desired values.
* @property {Object.<string, WPSource>} mappings Mapping of WordPress directories to local directories which should be mounted.
* @property {string|null} phpVersion Version of PHP to use in the environments, of the format 0.0.
Expand Down Expand Up @@ -89,6 +90,7 @@ const DEFAULT_ENVIRONMENT_CONFIG = {
testsPort: 8889,
mysqlPort: null,
phpmyadminPort: null,
multisite: false,
mappings: {},
config: {
FS_METHOD: 'direct',
Expand Down Expand Up @@ -466,6 +468,10 @@ async function parseEnvironmentConfig(
parsedConfig.phpmyadminPort = config.phpmyadminPort;
}

if ( config.multisite !== undefined ) {
parsedConfig.multisite = config.multisite;
}

if ( config.phpVersion !== undefined ) {
// Support null as a valid input.
if ( config.phpVersion !== null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports[`Config Integration should load local and override configuration files 1
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": 23306,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -59,6 +60,7 @@ exports[`Config Integration should load local and override configuration files 1
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": 23307,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -106,6 +108,7 @@ exports[`Config Integration should load local configuration file 1`] = `
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": 13306,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -136,6 +139,7 @@ exports[`Config Integration should load local configuration file 1`] = `
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": 23307,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -183,6 +187,7 @@ exports[`Config Integration should use default configuration 1`] = `
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": null,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -213,6 +218,7 @@ exports[`Config Integration should use default configuration 1`] = `
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": null,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -260,6 +266,7 @@ exports[`Config Integration should use environment variables over local and over
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": 23306,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down Expand Up @@ -291,6 +298,7 @@ exports[`Config Integration should use environment variables over local and over
"url": "https://github.com/WordPress/WordPress.git",
},
"mappings": {},
"multisite": false,
"mysqlPort": 23307,
"phpVersion": null,
"phpmyadminPort": null,
Expand Down
1 change: 1 addition & 0 deletions packages/env/lib/config/test/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const DEFAULT_CONFIG = {
testsPort: 8889,
mysqlPort: null,
phpmyadminPort: null,
multisite: false,
phpVersion: null,
coreSource: {
type: 'git',
Expand Down
31 changes: 30 additions & 1 deletion packages/env/lib/wordpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,40 @@ async function configureWordPress( environment, config, spinner ) {
// Ignore error.
}

const installCommand = `wp core install --url="${ config.env[ environment ].config.WP_SITEURL }" --title="${ config.name }" --admin_user=admin --admin_password=password [email protected] --skip-email`;
const isMultisite = config.env[ environment ].multisite;

const installMethod = isMultisite ? 'multisite-install' : 'install';
const installCommand = `wp core ${ installMethod } --url="${ config.env[ environment ].config.WP_SITEURL }" --title="${ config.name }" --admin_user=admin --admin_password=password [email protected] --skip-email`;

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

// Bootstrap .htaccess for multisite
if ( isMultisite ) {
// Using a subshell with `exec` was the best tradeoff I could come up
// with between readability of this source and compatibility with the
// way that all strings in `setupCommands` are later joined with '&&'.
setupCommands.push(
`(
exec > /var/www/html/.htaccess
echo 'RewriteEngine On'
echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]'
echo 'RewriteBase /'
echo 'RewriteRule ^index\.php$ - [L]'
echo ''
echo '# add a trailing slash to /wp-admin'
echo 'RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]'
echo ''
echo 'RewriteCond %{REQUEST_FILENAME} -f [OR]'
echo 'RewriteCond %{REQUEST_FILENAME} -d'
echo 'RewriteRule ^ - [L]'
echo 'RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]'
echo 'RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]'
echo 'RewriteRule . index.php [L]'
)`
);
}

// WordPress versions below 5.1 didn't use proper spacing in wp-config.
const configAnchor =
wpVersion && isWPMajorMinorVersionLower( wpVersion, '5.1' )
Expand Down
7 changes: 6 additions & 1 deletion schemas/json/wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
"phpmyadminPort": {
"description": "The port number to access phpMyAdmin.",
"type": "integer"
},
"multisite": {
"description": "Whether to set up a multisite installation.",
"type": "boolean"
}
}
},
Expand All @@ -78,7 +82,8 @@
"port",
"config",
"mappings",
"phpmyadminPort"
"phpmyadminPort",
"multisite"
]
}
},
Expand Down

0 comments on commit 74f7b98

Please sign in to comment.