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

wp-env: Add multisite support #67845

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@cbravobernal cbravobernal Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it in a similar way. #67852

Should we check the file permission on this PR too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, funny coincidence that we should be working on the same thing on the same day! 😀

About the file permissions: did you run into any trouble in the current setup, or are you concerned with anything? Since in this PR we are creating the htaccess file by invoking shell commands inside the container, I expect the resulting file to have "reasonable" permissions and ownership — unless someone really messed up the container's mask.

In contrast, in my experience, those problems arise in scenarios where files are copied from the host to the container or when mounts are set up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be considered defensive programming, I have had file permissions issues since I started setting servers 😆 . It's true that everything by default should work and that the container's mask could be the only issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed 0e2b031, but I'm of two minds between:

  • Yes, it's defensive programming, so it's better to do it — what's the harm?
  • But if this is ever needed then there's something really wrong with the container (and a fresh container at that!), so isn't this a form of cargo cult programming?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can skip it. We don't recommend wp-env for production sites, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The documentation's headline reads: wp-env lets you easily set up a local WordPress environment for building and testing plugins and themes. It’s simple to install and requires no configuration.

I'll revert. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that this .htaccess file was written as part of the multisite install?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not 😓

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
Loading