From 78bc207967b139f39f4769876f4b60aa5dac89da Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Wed, 11 Dec 2024 13:08:59 +0100 Subject: [PATCH 1/2] Initial commit --- 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 bd3c4a23f8ff5d..d5b210eec2ed09 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -86,7 +86,7 @@ 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 --admin_email=wordpress@example.com --skip-email`; + const installCommand = `wp core multisite-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 ]; From 25e88a5af6e007a8e094787eed107b1e6b744304 Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:54:54 +0100 Subject: [PATCH 2/2] Got it working --- packages/env/lib/init-config.js | 49 ++++++++++++++++++++++++++++++++- packages/env/lib/wordpress.js | 5 +++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js index 318bcae151b296..9831a1b1b263ba 100644 --- a/packages/env/lib/init-config.js +++ b/packages/env/lib/init-config.js @@ -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' ); @@ -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} + */ +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 @@ -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' ); + 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' ] ) { diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index d5b210eec2ed09..ba825c3000a8b8 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -86,7 +86,10 @@ async function configureWordPress( environment, config, spinner ) { // Ignore error. } - const installCommand = `wp core multisite-install --url="${ config.env[ environment ].config.WP_SITEURL }" --title="${ config.name }" --admin_user=admin --admin_password=password --admin_email=wordpress@example.com --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 --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 ];