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-
183function 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.
5972function 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