Skip to content

Commit

Permalink
make sure debug.log is editable in local dev, add wp cli command to g…
Browse files Browse the repository at this point in the history
…et/set global setting and use in local dev to enable tracking
  • Loading branch information
diosmosis committed Oct 18, 2023
1 parent fe2c5ec commit 0a641c0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
32 changes: 32 additions & 0 deletions classes/WpMatomo/Commands/MatomoCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@
}

class MatomoCommands extends WP_CLI_Command {
/**
* Gets or sets a Matomo for Wordpress global setting.
*
* ## OPTIONS
*
* <mode>
* : Either 'get' or 'set'.
*
* <name>
* : The name of the setting.
*
* [<value>]
* : 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.
*
Expand Down
6 changes: 5 additions & 1 deletion scripts/local-dev-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/tracking.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -36,11 +36,11 @@ describe('Tracking', () => {
lastMinutes: '60',
}));

expect(counters).toEqual({
expect(counters).toEqual([{
visits: 1,
actions: 2,
visitors: 1,
visitsConverted: 0,
});
}]);
});
});

0 comments on commit 0a641c0

Please sign in to comment.