Skip to content

Commit

Permalink
Merge pull request #897 from matomo-org/woocommerce-local
Browse files Browse the repository at this point in the history
add woocommerce setup to local dev environment
  • Loading branch information
diosmosis authored Oct 10, 2023
2 parents d9c62be + 2f8763f commit 5db67bc
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 25 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ The first time the container starts it will compile php extensions within the co
and set it up.

After starting the container, visit `http://localhost:3000/` to see the list of available wordpress versions.
Pick one, and it will take you to the Wordpress installer.
Pick one, and it will take you to Wordpress.

After installing Wordpress, go to the plugins page and activate Matomo for Wordpress.
Go to wp-login.php, then enter `root` for the user name and `pass` for the password and login.

Note: docker related files, such as the downloaded wordpress and database files, will be stored in a new folder named `./docker`. As long
as you are using this local dev environment, you should not delete this folder.
Expand All @@ -71,6 +71,10 @@ variables are supported:
- `WP_PLUGINS` - a list of plugin/version pairs like "my-plugin my-other-plugin:1.2.3". For each item, wp-cli will attempt to download and activate the plugin.
This is the same format as the Active Plugins entry in the System Report, so you could copy that value to this environment variable to quickly (or more quickly)
replicate a user's setup.
- `WP_ADMIN_USER` - the admin wordpress username, defaults to 'root'. This variable is only used when installing wordpress. It shouldn't be changed afterwards, unless doing a fresh install.
- `WP_ADMIN_EMAIL` - the admin user's email, defaults to '[email protected]'. This variable is only used when installing wordpress. It shouldn't be changed afterwards, unless doing a fresh install.
- `WOOCOMMERCE` - if set, installs and sets up woocommerce. This includes setting up a payment gateway with stripe and adding some test products. (Note: you will still have to go through woocommerce
onboarding as there is no way to disable it, and that means you will have to enter your stripe test keys manually when setting up the payment gateway.)

**Running wp-cli**

Expand Down
54 changes: 42 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@
# - BACKEND ('mariadb' or 'mysql', defaults to 'mariadb')
# - WORDPRESS_VERSION (defaults to latest (defined below))
# - PORT (the port to expose wordpress on, defaults to 3000)
# - WP_ADMIN_USER (the admin wordpress username, defaults to 'root')
# - WP_ADMIN_EMAIL (the admin user's email, defaults to '[email protected]')
# - WOOCOMMERCE (if set, installs and sets up woocommerce)
# - STRIPE_PUBLISHABLE_KEY (the test publishable key to use with a woocommerce install)
# - STRIPE_SECRET_KEY (the test secret key to use with a woocommerce install)

services:
# basic apache service
wordpress:
image: "php:${PHP_VERSION:-8.1}-apache"
build:
context: ./scripts
dockerfile: ./Dockerfile.local
args:
PHP_VERSION: "$PHP_VERSION"
PHP_TYPE: apache
volumes:
- ./docker/wordpress:/var/www/html
- "./docker/php-${PHP_VERSION:-8.1}/php:/usr/src/php"
- "./docker/php-${PHP_VERSION:-8.1}/extensions:/usr/local/lib/php/extensions"
- "./docker/php-${PHP_VERSION:-8.1}/conf:/usr/local/etc/php/conf.d"
- .:/var/www/html/matomo-for-wordpress
- /var/www/html/matomo-for-wordpress/app/tmp
- ./scripts/local-dev-entrypoint.sh:/usr/src/entrypoint.sh
- ./docker/wp-cli:/.wp-cli
ports:
- "${PORT:-3000}:80"
environment:
WP_DB_HOST: "${BACKEND:-mariadb}"
WOOCOMMERCE: "${WOOCOMMERCE:-}"
STRIPE_PUBLISHABLE_KEY: "$STRIPE_PUBLISHABLE_KEY"
STRIPE_SECRET_KEY: "$STRIPE_SECRET_KEY"
WP_ADMIN_USER: "${WP_ADMIN_USER:-root}"
WP_ADMIN_EMAIL: "${WP_ADMIN_EMAIL:[email protected]}"
entrypoint: /usr/src/entrypoint.sh
depends_on:
- "${BACKEND:-mariadb}"
Expand All @@ -30,17 +43,25 @@ services:

# nginx service
fpm:
image: "php:${PHP_VERSION:-8.1}-fpm"
build:
context: .
dockerfile: ./scripts/Dockerfile.local
args:
PHP_VERSION: "$PHP_VERSION"
PHP_TYPE: fpm
volumes:
- ./docker/wordpress:/var/www/html
- "./docker/php-${PHP_VERSION:-8.1}/php:/usr/src/php"
- "./docker/php-${PHP_VERSION:-8.1}/extensions:/usr/local/lib/php/extensions"
- "./docker/php-${PHP_VERSION:-8.1}/conf:/usr/local/etc/php/conf.d"
- .:/var/www/html/matomo-for-wordpress
- /var/www/html/matomo-for-wordpress/app/tmp
- ./scripts/local-dev-entrypoint.sh:/usr/src/entrypoint.sh
- ./docker/wp-cli:/.wp-cli
environment:
WP_DB_HOST: "${BACKEND:-mariadb}"
WOOCOMMERCE: "$WOOCOMMERCE"
STRIPE_PUBLISHABLE_KEY: "$STRIPE_PUBLISHABLE_KEY"
STRIPE_SECRET_KEY: "$STRIPE_SECRET_KEY"
WP_ADMIN_USER: "${WP_ADMIN_USER:-root}"
WP_ADMIN_EMAIL: "${WP_ADMIN_EMAIL:[email protected]}"
entrypoint: /usr/src/entrypoint.sh
depends_on:
- "${BACKEND:-mariadb}"
Expand All @@ -64,18 +85,27 @@ services:

# service for running wp-cli
wp:
image: "php:${PHP_VERSION:-8.1}-apache"
build:
context: ./scripts
dockerfile: ./Dockerfile.local
args:
PHP_VERSION: "$PHP_VERSION"
PHP_TYPE: apache
volumes:
- ./docker/wordpress:/var/www/html
- "./docker/php-${PHP_VERSION:-8.1}/php:/usr/src/php"
- "./docker/php-${PHP_VERSION:-8.1}/extensions:/usr/local/lib/php/extensions"
- "./docker/php-${PHP_VERSION:-8.1}/conf:/usr/local/etc/php/conf.d"
- .:/var/www/html/matomo-for-wordpress
- /var/www/html/matomo-for-wordpress/app/tmp
- ./docker/wp-cli:/.wp-cli
ports:
- "${PORT:-3000}:80"
environment:
WP_DB_HOST: "${BACKEND:-mariadb}"
WOOCOMMERCE: "$WOOCOMMERCE"
STRIPE_PUBLISHABLE_KEY: "$STRIPE_PUBLISHABLE_KEY"
STRIPE_SECRET_KEY: "$STRIPE_SECRET_KEY"
WP_ADMIN_USER: "${WP_ADMIN_USER:-root}"
WP_ADMIN_EMAIL: "${WP_ADMIN_EMAIL:[email protected]}"
PAGER: 'more'
user: "${UID:-1000}:${GID:-1000}"
entrypoint: "/var/www/html/wp-cli.phar --path=/var/www/html/${WORDPRESS_VERSION:-6.3.1}"
depends_on:
Expand Down
7 changes: 7 additions & 0 deletions scripts/Dockerfile.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ARG PHP_VERSION=8.1
ARG PHP_TYPE=apache
FROM "php:$PHP_VERSION-$PHP_TYPE" AS base

RUN apt-get update && apt-get install -y --no-install-recommends libfreetype6 zlib1g-dev libjpeg-dev libpng-dev

RUN docker-php-ext-install mysqli pdo pdo_mysql gd
53 changes: 42 additions & 11 deletions scripts/local-dev-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ cd /var/www/html

LATEST_WORDPRESS_VERSION=6.3.1 # can't use the github API, too easy to get rate limited

# install PHP extensions needed
for extension in mysqli pdo pdo_mysql; do
if [ ! -f /usr/local/lib/php/extensions/*/$extension.so ]; then
docker-php-ext-install $extension
fi
done
# install wp-cli.phar
if [ ! -f "/var/www/html/wp-cli.phar" ]; then
curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /var/www/html/wp-cli.phar
fi
chmod +x /var/www/html/wp-cli.phar

# TODO: switch download to use wp-cli instead of just curling (also can use wp db create instead of raw php)
# install wordpress if not present
WORDPRESS_VERSION=${WORDPRESS_VERSION:-$LATEST_WORDPRESS_VERSION}
if [ ! -d "/var/www/html/$WORDPRESS_VERSION" ]; then
Expand Down Expand Up @@ -63,6 +63,9 @@ define( 'FS_CHMOD_DIR', ( 0777 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
define( 'FS_METHOD', 'direct' );
define('FORCE_SSL', false);
define('FORCE_SSL_ADMIN', false);
define( 'MATOMO_ANALYTICS_FILE', __DIR__ . '/wp-content/plugins/matomo/matomo.php' );
\$table_prefix = 'wp_';
Expand All @@ -81,11 +84,18 @@ EOF
echo "setup wp-config.php!"
fi

# install wordpress
/var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_VERSION core install --url=localhost:3000 --title="Matomo for Wordpress Test" --admin_user=$WP_ADMIN_USER --admin_password=pass --admin_email=$WP_ADMIN_EMAIL
/var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_VERSION option set siteurl "http://localhost:3000/$WORDPRESS_VERSION"
/var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_VERSION option set home "http://localhost:3000/$WORDPRESS_VERSION"

# link matomo for wordpress volume as wordpress plugin
if [ ! -d "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/matomo" ]; then
ln -s /var/www/html/matomo-for-wordpress "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/matomo"
fi

/var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_VERSION plugin activate matomo

# add index.php file listing available installs to root /var/www/html
if [ ! -f "/var/www/html/index.php" ]; then
cat > "/var/www/html/index.php" <<EOF
Expand Down Expand Up @@ -119,11 +129,6 @@ EOF
fi

# download WP_PLUGINS plugins if not present
if [ ! -f "/var/www/html/wp-cli.phar" ]; then
curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /var/www/html/wp-cli.phar
fi
chmod +x /var/www/html/wp-cli.phar

for PLUGIN_VERSION in $WP_PLUGINS
do
PLUGIN_VERSION_ARRAY=(${PLUGIN_VERSION//:/ })
Expand All @@ -143,6 +148,31 @@ do
/var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_VERSION plugin install --activate $VERSION_ARGUMENT $PLUGIN || true
done

# setup woocommerce if requested
if [[ ! -z "$WOOCOMMERCE" && ! -d "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/woocommerce" ]]; then
# install woocommerce and stripe payment gateway
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root plugin install woocommerce woocommerce-gateway-stripe --activate

# install oceanwp
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root theme install oceanwp --activate

# add 5 test products
IMAGE_ID=$( /var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER media import "/var/www/html/6.3.1/wp-content/plugins/matomo/tests/resources/products/ceiling_fan.jpg" | grep -o 'attachment ID [0-9][0-9]*' | awk '{print $3}' )
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER wc product create --name="Ceiling Fan" --short_description="Pink butterfly ceiling fan" --description="Pink butterfly ceiling fan" --slug="ceiling-fan-pink" --regular_price="309.99" --sku="PROD_1" --images="[{\"id\":$IMAGE_ID}]" || true

IMAGE_ID=$( /var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER media import "/var/www/html/6.3.1/wp-content/plugins/matomo/tests/resources/products/film_projector.jpg" | grep -o 'attachment ID [0-9][0-9]*' | awk '{print $3}' )
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER wc product create --name="Film Projector Lens" --short_description="A film projector lens" --description="A film projector lens" --slug="film-projector-lens" --regular_price="439.89" --sku="PROD_2" --images="[{\"id\":$IMAGE_ID}]" || true

IMAGE_ID=$( /var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER media import "/var/www/html/6.3.1/wp-content/plugins/matomo/tests/resources/products/monitors.jpg" | grep -o 'attachment ID [0-9][0-9]*' | awk '{print $3}' )
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER wc product create --name="Folding monitors" --short_description="Folding monitors, three monitors combined" --description="Folding monitors, three monitors combined" --slug="folding-monitors" --regular_price="286.00" --sku="PROD_3" --images="[{\"id\":$IMAGE_ID}]" || true

IMAGE_ID=$( /var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER media import "/var/www/html/6.3.1/wp-content/plugins/matomo/tests/resources/products/spotlight.jpg" | grep -o 'attachment ID [0-9][0-9]*' | awk '{print $3}' )
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER wc product create --name="Spotlight" --short_description="Single hanging spotlight" --description="Single hanging spotlight, fixed, not portable" --slug="spotlight" --regular_price="279.99" --sku="PROD_4" --images="[{\"id\":$IMAGE_ID}]" || true

IMAGE_ID=$( /var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER media import "/var/www/html/6.3.1/wp-content/plugins/matomo/tests/resources/products/tripod.jpg" | grep -o 'attachment ID [0-9][0-9]*' | awk '{print $3}' )
/var/www/html/wp-cli.phar --path=/var/www/html/$WORDPRESS_VERSION --allow-root --user=$WP_ADMIN_USER wc product create --name="Small camera tripod in red" --short_description="Small camera tripod in red" --description="Small portable tripod for your camera. Available colors: red." --slug="camera-tripod-small" --regular_price="13.99" --sku="PROD_5" --images="[{\"id\":$IMAGE_ID}]" || true
fi

# make sure the files can be edited outside of docker (for easier debugging)
# TODO: file permissions becoming a pain, shouldn't have to deal with this for dev env. this works for now though.
mkdir -p /var/www/html/$WORDPRESS_VERSION/wp-content/uploads
Expand All @@ -151,6 +181,7 @@ find "/var/www/html/$WORDPRESS_VERSION" -path "/var/www/html/$WORDPRESS_VERSION/
chmod -R 0777 "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/matomo/app/tmp" "/var/www/html/index.php" "/usr/local/etc/php/conf.d"

if ! which apache2-foreground &> /dev/null; then
# TODO: is it possible to use wp-cli for this?
# make sure home url points to 'nginx' service
php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass');
\$pdo->exec('UPDATE \`$WP_DB_NAME\`.wp_options SET option_value = REPLACE(option_value, \'localhost\', \'nginx\') WHERE option_name IN (\'home\', \'siteurl\')');" || true
Expand Down
21 changes: 21 additions & 0 deletions tests/resources/products/ATTRIBUTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Product Image Attributions

ceiling_fan.jpg

"ceiling fan for bedroom" by mothaibaa01 is marked with CC0 1.0. To view the terms, visit https://creativecommons.org/publicdomain/zero/1.0/?ref=openverse.

spotlight.jpg

"Isometric Spotlight" by aiiiidan is licensed under CC BY 2.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/2.0/?ref=openverse.

film_projector.jpg

"Film Projector Lens" by DanielSTL is licensed under CC BY 2.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/2.0/?ref=openverse.

monitors.jpg

"Arial" by Mr. Riegel is licensed under CC BY 2.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/2.0/?ref=openverse.

tripod.jpg

"Tripod." by MIKI Yoshihito. (#mikiyoshihito) is licensed under CC BY 2.0. To view a copy of this license, visit https://creativecommons.org/licenses/by/2.0/?ref=openverse.
Binary file added tests/resources/products/ceiling_fan.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/products/film_projector.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/products/monitors.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/products/spotlight.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/products/tripod.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5db67bc

Please sign in to comment.