Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Last active July 2, 2018 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshcanhelp/50f66002643ece68f01bf5f94e1abe56 to your computer and use it in GitHub Desktop.
Save joshcanhelp/50f66002643ece68f01bf5f94e1abe56 to your computer and use it in GitHub Desktop.
Generate a WP instance locally - WIP
#!/bin/sh
# #########
# Variables
# #########
WORKING_DIR="/Users/josh-cunningham/Sites/wordpress-test-fixture"
DB_NAME="wordpress_test_fixture"
DB_USER="root"
DB_HOST="localhost"
HOME_URL="http://wp2.localhost.test"
SITE_TITLE="WORDPRESS TEST FIXTURE"
WP_VER="latest"
AUTH0_CHECKOUT="dev"
# DEFAULT prepended values below should be set in your bash profile
WP_AUTH0_DOMAIN=$DEFAULT_WP_AUTH0_DOMAIN
WP_AUTH0_CLIENT_ID=$DEFAULT_WP_AUTH0_CLIENT_ID
WP_AUTH0_CLIENT_SECRET=$DEFAULT_WP_AUTH0_CLIENT_SECRET
ADMIN_AUTH0_ID=$DEFAULT_ADMIN_AUTH0_ID
ADMIN_EMAIL=$DEFAULT_ADMIN_EMAIL
ADMIN_PASS=$DEFAULT_ADMIN_PASS
# #########
# Functions
# #########
# Delete everything prior to starting
reset_all() {
rm -rf $WORKING_DIR
mysql -uroot -e "DROP DATABASE IF EXISTS $DB_NAME;";
}
# Install and configure Auth0
install_auth0() {
# Activate Auth0
wp plugin activate auth0
# Configure Auth0
wp auth0 set_opt domain $WP_AUTH0_DOMAIN
wp auth0 set_opt client_id $WP_AUTH0_CLIENT_ID
wp auth0 set_opt client_secret $WP_AUTH0_CLIENT_SECRET
wp auth0 set_opt requires_verified_email 0
wp auth0 set_opt sso ""
wp auth0 set_opt singlelogout ""
wp auth0 set_opt force_https_callback ""
wp auth0 set_opt auth0_implicit_workflow ""
# Connect admin to Auth0 account
wp user meta add 1 auth0_id $ADMIN_AUTH0_ID
}
# Convert to headless WP
convert_to_headless() {
mkdir wp
mv `\ls -1 ./*.php | grep -v wp-config.php` ./wp/
mv wp-includes wp/wp-includes
mv wp-admin wp/wp-admin
echo "<?php define('WP_USE_THEMES', true);"
echo "require( './wp/wp-blog-header.php' );" >> index.php
cd wp
wp option update siteurl "$HOME_URL/wp"
wp config set --type=constant WP_SITEURL "$HOME_URL/wp"
}
# Convert to mutli-site
convert_to_multi_site() {
wp core multisite-convert
wp site create --slug="sub" --title="$SITE_TITLE SUBSITE" --email=$ADMIN_EMAIL
wp auth0 make_test_pages 2
echo "
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ \$1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) \$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ \$2 [L]
RewriteRule . index.php [L]" > "$WORKING_DIR/.htaccess"
}
# Install and configure Woocommerce
install_woocommerce() {
wp plugin install woocommerce --activate
# Add pages
# Add configuration data
# Add products
}
# ############
# Script start
# ############
DO_HEADLESS=false;
DO_AUTH0_INSTALL=false;
DO_MUTLI_SITE=false;
DO_RESET=false;
DO_WOOCOMMERCE=false;
# -i flag to install as headless (wp in separate directory)
while getopts ':himrwv:' opt; do
case $opt in
# install as headless (wp in separate directory)
h) DO_HEADLESS=true ;;
# install and configure Auth0, connect admin account
i) DO_AUTH0_INSTALL=true ;;
# convert to multi-site
m) DO_MUTLI_SITE=true ;;
# delete files and DB from $WORKING_DIR before installing
r) DO_RESET=true ;;
# install and configure WooCommerce?
w) DO_WOOCOMMERCE=true ;;
# branch or tag of Auth0 to install
v) AUTH0_CHECKOUT=$OPTARG;;
esac
done
# Reset previous install
if [ $DO_RESET = true ] ;
then
reset_all
fi
mkdir $WORKING_DIR
cd $WORKING_DIR
# Download a certain version of WP
wp core download --skip-content --force --version="$WP_VER"
# Clone the master branch of the testing theme
git clone https://github.com/joshcanhelp/auth0-wp-test.git ./wp-content/themes/auth0-wp-test
# Clone the Auth0 and adjust to whatever branch
git clone https://github.com/auth0/wp-auth0.git ./wp-content/plugins/auth0
cd wp-content/plugins/auth0
git checkout $AUTH0_CHECKOUT
cd ../../..
# Set wp-config.php values
wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="" --dbhost="$DB_HOST"
wp config set --type=constant WP_DEBUG true;
wp config set --type=constant SAVEQUERIES true;
wp config set --type=constant WP_CONTENT_DIR "$WORKING_DIR/wp-content"
wp config set --type=constant WP_CONTENT_URL "$HOME_URL/wp-content"
wp config set --type=constant WP_HOME "$HOME_URL"
wp config set --type=constant WP_DEFAULT_THEME "auth0-wp-test"
# Create the database
mysql -uroot -e "CREATE DATABASE $DB_NAME /*\!40100 DEFAULT CHARACTER SET utf8 */;";
mysql -uroot -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'$DB_HOST';";
mysql -uroot -e "FLUSH PRIVILEGES;"
wp core install --skip-email --url="$HOME_URL" \
--title="WORDPRESS TEXT FIXTURE" \
--admin_user="$DEFAULT_ADMIN_EMAIL" \
--admin_email="$DEFAULT_ADMIN_EMAIL" \
--admin_password="$DEFAULT_ADMIN_PASS"
# Allow registrations
wp option update users_can_register 1
# Turn on testing theme
wp auth0 make_test_pages
# Pretty permlinks
wp rewrite structure "/%postname%/"
wp rewrite flush --hard
# Install and configure Auth0
if [ $DO_AUTH0_INSTALL = true ] ;
then
install_auth0
fi
# Convert to headless
if [ $DO_HEADLESS = true ] ;
then
convert_to_headless
fi
# Convert to multi-site
if [ $DO_MUTLI_SITE = true ] ;
then
convert_to_multi_site
wp plugin install debug-bar debug-bar-console query-monitor --activate-network
else
wp plugin install debug-bar debug-bar-console query-monitor --activate
fi
# Add Woocommerce
if [ $DO_WOOCOMMERCE = true ] ;
then
install_woocommerce
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment