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 multi-site alternative. #67852

Closed
wants to merge 2 commits into from
Closed
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
49 changes: 48 additions & 1 deletion packages/env/lib/init-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* External dependencies
*/
const path = require( 'path' );
const { writeFile, mkdir } = require( 'fs' ).promises;
const { writeFile, mkdir, chmod } = require( 'fs' ).promises;
const { existsSync } = require( 'fs' );
const yaml = require( 'js-yaml' );

Expand All @@ -17,6 +17,45 @@ const buildDockerComposeConfig = require( './build-docker-compose-config' );
* @typedef {import('./config').WPConfig} WPConfig
*/

/**
* Default multisite .htaccess content
*/
const multisiteHtaccessContent = `# BEGIN WordPress Multisite
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress Multisite`;

/**
* Writes the .htaccess file for multisite
*
* @param {string} wpPath Path to WordPress installation
* @return {Promise<void>}
*/
async function writeMultisiteHtaccess( wpPath ) {
try {
const htaccessPath = path.join( wpPath, '.htaccess' );
await writeFile( htaccessPath, multisiteHtaccessContent );
// Set 644 permissions
await chmod( htaccessPath, '0644' );
} catch ( error ) {
throw new Error(
`Failed to write multisite .htaccess: ${ error.message }`
);
}
}

/**
* Initializes the local environment so that Docker commands can be run. Reads
* ./.wp-env.json, creates ~/.wp-env, ~/.wp-env/docker-compose.yml, and
Expand Down Expand Up @@ -80,6 +119,14 @@ module.exports = async function initConfig( {
yaml.dump( dockerComposeConfig )
);

// Add multisite .htaccess if multisite is enabled
if ( config.env.multisite ) {
await writeMultisiteHtaccess( '/var/www/html' );
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a drive-by remark (I'm on my phone and I'll have a better look in the morning), but isn't this saving to the host's own filesystem? Isn't there a path.join missing, or a docker-compose mount missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The path join is added in line 48. It is creating the file on the Dockerfile.

if ( config.debug ) {
spinner.info( 'Wrote multisite .htaccess file' );
}
}

// Write four Dockerfiles for each service we provided.
// (WordPress and CLI services, then a development and test environment for each.)
for ( const imageType of [ 'WordPress', 'CLI' ] ) {
Expand Down
5 changes: 4 additions & 1 deletion packages/env/lib/wordpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ 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 installType = config.env[ environment ].config.multisite
? 'multisite-install'
: 'install';
const installCommand = `wp core ${ installType } --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 ];
Expand Down
Loading