From 0a641c01a4739e1137927ced590cc68615984833 Mon Sep 17 00:00:00 2001 From: diosmosis Date: Wed, 18 Oct 2023 02:54:44 -0700 Subject: [PATCH] make sure debug.log is editable in local dev, add wp cli command to get/set global setting and use in local dev to enable tracking --- classes/WpMatomo/Commands/MatomoCommands.php | 32 ++++++++++++++++++++ scripts/local-dev-entrypoint.sh | 6 +++- tests/e2e/tracking.e2e.ts | 10 +++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/classes/WpMatomo/Commands/MatomoCommands.php b/classes/WpMatomo/Commands/MatomoCommands.php index bbd519c08..0aa0a6826 100644 --- a/classes/WpMatomo/Commands/MatomoCommands.php +++ b/classes/WpMatomo/Commands/MatomoCommands.php @@ -31,6 +31,38 @@ } class MatomoCommands extends WP_CLI_Command { + /** + * Gets or sets a Matomo for Wordpress global setting. + * + * ## OPTIONS + * + * + * : Either 'get' or 'set'. + * + * + * : The name of the setting. + * + * [] + * : Required if 'set' is used. The value to set the setting to. + * + * @when after_wp_load + */ + public function globalSetting( $args, $assoc_args ) { + [ $mode, $key ] = $args; + + if ($mode === 'set') { + $value = $args[2]; + \WpMatomo::$settings->set_global_option( $key, $value ); + \WpMatomo::$settings->save(); + WP_CLI::log( sprintf( 'Modified Matomo setting %s.', $key ) ); + } else if ($mode === 'get') { + $value = \WpMatomo::$settings->get_global_option( $key ); + WP_CLI::log( $value ); + } else { + WP_CLI::error( sprintf( 'Invalid mode "%s".', $mode ) ); + } + } + /** * Uninstalls Matomo. * diff --git a/scripts/local-dev-entrypoint.sh b/scripts/local-dev-entrypoint.sh index a7f1717ac..af020101f 100755 --- a/scripts/local-dev-entrypoint.sh +++ b/scripts/local-dev-entrypoint.sh @@ -47,6 +47,8 @@ if [[ ! -z "$RESET_DATABASE" ]]; then php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass'); \$pdo->exec('DROP DATABASE IF EXISTS \`$WP_DB_NAME\`');" + + rm /var/www/html/$WORDPRESS_FOLDER/wp-content/uploads/matomo/config/config.ini.php || true fi # create database if it does not already exist @@ -116,6 +118,7 @@ if [[ -d "/var/www/html/woocommerce-piwik-analytics" && ! -d "/var/www/html/$WOR fi /var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_FOLDER plugin activate matomo +/var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_FOLDER matomo globalSetting set track_mode default # add index.php file listing available installs to root /var/www/html if [ ! -f "/var/www/html/index.php" ]; then @@ -204,10 +207,11 @@ 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. +touch /var/www/html/$WORDPRESS_FOLDER/debug.log mkdir -p /var/www/html/$WORDPRESS_FOLDER/wp-content/uploads find "/var/www/html/$WORDPRESS_FOLDER" -path "/var/www/html/$WORDPRESS_FOLDER/wp-content/plugins/matomo" -prune -o -exec chown "${UID:-1000}:${GID:-1000}" {} + find "/var/www/html/$WORDPRESS_FOLDER" -path "/var/www/html/$WORDPRESS_FOLDER/wp-content/plugins/matomo" -prune -o -exec chmod 0777 {} + -chmod -R 0777 "/var/www/html/$WORDPRESS_FOLDER/wp-content/plugins/matomo/app/tmp" "/var/www/html/index.php" "/usr/local/etc/php/conf.d" +chmod -R 0777 "/var/www/html/$WORDPRESS_FOLDER/wp-content/plugins/matomo/app/tmp" "/var/www/html/index.php" "/usr/local/etc/php/conf.d" "/var/www/html/$WORDPRESS_FOLDER/debug.log" if ! which apache2-foreground &> /dev/null; then # TODO: is it possible to use wp-cli for this? diff --git a/tests/e2e/tracking.e2e.ts b/tests/e2e/tracking.e2e.ts index 756964792..d6e6e532e 100644 --- a/tests/e2e/tracking.e2e.ts +++ b/tests/e2e/tracking.e2e.ts @@ -18,12 +18,12 @@ describe('Tracking', () => { lastMinutes: '60', })); - expect(countersBefore).toEqual({ - visits: 0, + expect(countersBefore).toEqual([{ + visits: '0', actions: 0, visitors: 0, visitsConverted: 0, - }); + }]); await BlogHomepage.open(); await BlogHomepage.waitForTrackingRequest(); @@ -36,11 +36,11 @@ describe('Tracking', () => { lastMinutes: '60', })); - expect(counters).toEqual({ + expect(counters).toEqual([{ visits: 1, actions: 2, visitors: 1, visitsConverted: 0, - }); + }]); }); });