Skip to content

Commit ef296c9

Browse files
committed
SUpport for multisite, fixes #3
1 parent 5773e96 commit ef296c9

2 files changed

Lines changed: 57 additions & 40 deletions

File tree

docker-compose.yml

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,23 @@ setup:
2525
volumes_from:
2626
- wp
2727
environment:
28+
WP_LOCALE: en_NZ
29+
WP_VERSION: 4.4.2
2830
WP_TITLE: Main
2931
WP_SUBDOMAINS: "yes"
3032
WP_URL: http://main.dev
3133
WP_ADMIN_USER: admin
3234
WP_ADMIN_PASSWORD: admin
33-
WP_ADMIN_EMAIL: webmaster+admin@foobar.net.nz
35+
WP_ADMIN_EMAIL: admin@main.dev
3436
WP_EXTRA_PHP: |
3537
define('DISABLE_WP_CRON', TRUE);
36-
@define('COOKIE_DOMAIN', $$_SERVER[ 'HTTP_HOST' ]);
38+
WP_COMMANDS_0: |
39+
wp --url=http://main.dev theme activate twentyfifteen
3740
WP_COMMANDS_1: |
38-
set -x
39-
ID=$$(wp site list --fields=blog_id --domain=site1.dev | sed 1d)
40-
if [ ! $$ID ]
41-
then
42-
ID=$$(wp site create --slug=site1 --title="Site 1" --porcelain)
43-
wp db query "UPDATE wp_blogs SET domain = 'site1.dev' WHERE blog_id = $$ID"
44-
fi
45-
wp --url=http://site1.dev option update siteurl http://site1.dev
46-
wp --url=http://site1.dev option update home http://site1.dev
47-
wp site list
41+
new_site sitea sitea.dev http://sitea.dev "Site A"
42+
wp --url=http://sitea.dev theme activate twentyfourteen
4843
WP_COMMANDS_2: |
49-
set -x
50-
ID=$$(wp site list --fields=blog_id --domain=site2.dev | sed 1d)
51-
if [ ! $$ID ]
52-
then
53-
ID=$$(wp site create --slug=site2 --title="Site 2" --porcelain)
54-
wp db query "UPDATE wp_blogs SET domain = 'site2.dev' WHERE blog_id = $$ID"
55-
fi
56-
wp --url=http://site2.dev option update siteurl http://site2.dev
57-
wp --url=http://site2.dev option update home http://site2.dev
44+
new_site siteb siteb.dev http://siteb.dev "Site B"
45+
wp --url=http://sitea.dev theme activate twentythirteen
46+
WP_COMMANDS_END: |
5847
wp site list

setup.sh

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
#!/bin/bash
22

3-
# Juggle ENV VARS
4-
: ${WP_DB_ROOT_USER:=root}
5-
: ${WP_DB_ROOT_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
6-
: ${WP_DB_NAME:=$MYSQL_ENV_MYSQL_DATABASE}
7-
: ${WP_DB_USER:=$MYSQL_ENV_MYSQL_USER}
8-
: ${WP_DB_PASSWORD:=$MYSQL_ENV_MYSQL_PASSWORD}
9-
: ${WP_DB_HOST:=$MYSQL_PORT_3306_TCP_ADDR}
10-
: ${WP_DB_PORT:=${MYSQL_PORT_3306_TCP_PORT:-3306}}
11-
: ${WP_SUBDOMAINS:-no}
12-
: ${WP_URL:? is required}
13-
: ${WP_TITLE:? is required}
14-
: ${WP_ADMIN_USER:? is required}
15-
: ${WP_ADMIN_PASSWORD:? is required}
16-
: ${WP_ADMIN_EMAIL:? is required}
17-
183
function install_core {
194
# Download the lastest WP, preferebly with the selected locale, but fall back to the default locale.
20-
wp core download ${WP_LOCALE:+--locale="$WP_LOCALE"} || wp core download || true
5+
wp core download ${WP_LOCALE:+--locale="$WP_LOCALE"} ${WP_VERSION:+--version="$WP_VERSION"} || wp core download || true
216

227
local V=$(shopt -qo xtrace && echo "-vvv")
238
# Setup the database if required.
@@ -31,6 +16,21 @@ function install_core {
3116
while ! mysql $V -h$WP_DB_HOST -P$WP_DB_PORT -u$WP_DB_ROOT_USER -p$WP_DB_ROOT_PASSWORD -e "$SQL"; do sleep 5; done
3217
fi
3318

19+
if [ $WP_SUBDOMAINS ]
20+
then
21+
EXTRA_PHP=$(cat <<-EOF
22+
if (isset(\$_SERVER['HTTP_HOST']))
23+
define('COOKIE_DOMAIN', preg_replace('/^www/', '', \$_SERVER['HTTP_HOST']));
24+
define('MULTISITE', true);
25+
define('SUBDOMAIN_INSTALL', true);
26+
define('DOMAIN_CURRENT_SITE', '${WP_URL##*//}');
27+
define('PATH_CURRENT_SITE', '/');
28+
define('SITE_ID_CURRENT_SITE', 1);
29+
define('BLOG_ID_CURRENT_SITE', 1);
30+
EOF
31+
)
32+
fi
33+
3434
# Configure the database
3535
wp core config \
3636
${WP_LOCALE:+--locale="$WP_LOCALE"} \
@@ -39,11 +39,11 @@ function install_core {
3939
--dbpass="${WP_DB_PASSWORD}" \
4040
--dbhost="${WP_DB_HOST}:${WP_DB_PORT}" \
4141
${WP_DB_PREFIX:+--dbprefix="$WP_DB_PREFIX"} \
42-
--extra-php <<< "${WP_EXTRA_PHP}" \
42+
--extra-php <<< "${WP_EXTRA_PHP}${EXTRA_PHP}" \
4343
|| true
4444

4545
# Configure the Blog
46-
wp core is-installed || wp core ${WP_SUBDOMAINS:+multisite-}install \
46+
wp --url="$WP_URL" core is-installed || wp core ${WP_SUBDOMAINS:+multisite-}install \
4747
--url="$WP_URL" \
4848
${WP_SUBDOMAINS:+--subdomains} \
4949
--title="$WP_TITLE" \
@@ -53,13 +53,26 @@ function install_core {
5353
--skip-email
5454
}
5555

56+
# setup a new site
57+
function new_site {
58+
local SLUG=$1 DOMAIN=$2 URL=$3 TITLE=$4 ID
59+
ID=$(wp site list --field=blog_id --domain=$DOMAIN)
60+
if [ ! $ID ]
61+
then
62+
ID=$(wp site create --slug=$SLUG --title="$TITLE" --porcelain)
63+
wp db query "UPDATE wp_blogs SET domain = '$DOMAIN' WHERE blog_id = $ID"
64+
fi
65+
wp --url=$URL option update siteurl $URL
66+
wp --url=$URL option update home $URL
67+
}
68+
5669
# Allows execution of arbitrary WP-CLI commands.
5770
# I suppose this is either quite dangerous and makes most of
5871
# the rest of this script redundant.
5972
function wp_commands {
6073
for V in ${!WP_COMMANDS*}
6174
do
62-
sh <<< "${!V}"
75+
eval "${!V}"
6376
done
6477
}
6578

@@ -68,3 +81,18 @@ function setup {
6881
install_core
6982
wp_commands
7083
}
84+
85+
# Juggle ENV VARS
86+
: ${WP_DB_ROOT_USER:=root}
87+
: ${WP_DB_ROOT_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
88+
: ${WP_DB_NAME:=$MYSQL_ENV_MYSQL_DATABASE}
89+
: ${WP_DB_USER:=$MYSQL_ENV_MYSQL_USER}
90+
: ${WP_DB_PASSWORD:=$MYSQL_ENV_MYSQL_PASSWORD}
91+
: ${WP_DB_HOST:=$MYSQL_PORT_3306_TCP_ADDR}
92+
: ${WP_DB_PORT:=${MYSQL_PORT_3306_TCP_PORT:-3306}}
93+
: ${WP_SUBDOMAINS:-no}
94+
: ${WP_URL:? is required}
95+
: ${WP_TITLE:? is required}
96+
: ${WP_ADMIN_USER:? is required}
97+
: ${WP_ADMIN_PASSWORD:? is required}
98+
: ${WP_ADMIN_EMAIL:? is required}

0 commit comments

Comments
 (0)