diff --git a/CHANGELOG.md b/CHANGELOG.md index 1234703aa..30986f735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +0.1.6 +- Handle database errors better and behave same as Matomo DB adapters +- Fix an issue in managing goals +- Improve support for PHP 7.2 +- Show MySQL adapter in system report +- Hide not working link to tracking code in tour widget + 0.1.5 - Fix error in htaccess file preventing tracker file to load - Easy way to embed tag manager containers into site diff --git a/classes/WpMatomo/Db/Wordpress.php b/classes/WpMatomo/Db/Wordpress.php index b1637c89c..52e5fec80 100644 --- a/classes/WpMatomo/Db/Wordpress.php +++ b/classes/WpMatomo/Db/Wordpress.php @@ -156,6 +156,8 @@ public function query( $sql, $bind = array() ) { } else { $prepare = $this->prepareWp( $sql, $bind ); $result = $wpdb->query( $prepare ); + + $this->throwExceptionIfError($wpdb); } return new WordPressDbStatement( $this, $sql, $result ); @@ -175,27 +177,54 @@ public function fetchCol( $sql, $bind = array() ) { global $wpdb; $prepare = $this->prepareWp( $sql, $bind ); - return $wpdb->get_col( $prepare ); + $col = $wpdb->get_col( $prepare ); + + $this->throwExceptionIfError($wpdb); + + return $col; } public function fetchAssoc( $sql, $bind = array() ) { global $wpdb; $prepare = $this->prepareWp( $sql, $bind ); - return $wpdb->get_results( $prepare, ARRAY_A ); + $assoc = $wpdb->get_results( $prepare, ARRAY_A ); + + $this->throwExceptionIfError($wpdb); + + return $assoc; + } + + /** + * @param \wpdb $wpdb + * + * @throws \Zend_Db_Statement_Exception + */ + private function throwExceptionIfError($wpdb) + { + if ($wpdb->last_error) { + throw new \Zend_Db_Statement_Exception('WP DB Error: ' . $wpdb->last_error); + } } public function fetchAll( $sql, $bind = array(), $fetchMode = null ) { global $wpdb; $prepare = $this->prepareWp( $sql, $bind ); - return $wpdb->get_results( $prepare, ARRAY_A ); + $results = $wpdb->get_results( $prepare, ARRAY_A ); + + $this->throwExceptionIfError($wpdb); + + return $results; } public function fetchOne( $sql, $bind = array() ) { global $wpdb; $prepare = $this->prepareWp( $sql, $bind ); $value = $wpdb->get_var( $prepare ); + + $this->throwExceptionIfError($wpdb); + if ( $value === null ) { return false; // make sure to behave same way as matomo } @@ -207,13 +236,21 @@ public function fetchRow( $sql, $bind = array(), $fetchMode = null ) { global $wpdb; $prepare = $this->prepareWp( $sql, $bind ); - return $wpdb->get_row( $prepare, ARRAY_A ); + $row = $wpdb->get_row( $prepare, ARRAY_A ); + + $this->throwExceptionIfError($wpdb); + + return $row; } public function insert( $table, array $bind ) { global $wpdb; - return $wpdb->insert( $table, $bind ); + $insert = $wpdb->insert( $table, $bind ); + + $this->throwExceptionIfError($wpdb); + + return $insert; } public function update( $table, array $bind, $where = '' ) { @@ -228,6 +265,10 @@ public function update( $table, array $bind, $where = '' ) { $sql = "UPDATE `$table` SET $fields " . ( ( $where ) ? " WHERE $where" : '' ); $prepared = $wpdb->prepare( $sql, $bind ); - return $wpdb->query( $prepared ); + $update = $wpdb->query( $prepared ); + + $this->throwExceptionIfError($wpdb); + + return $update; } } diff --git a/classes/WpMatomo/Db/WordpressTracker.php b/classes/WpMatomo/Db/WordpressTracker.php index 9c66f6a3b..a87fa7d11 100644 --- a/classes/WpMatomo/Db/WordpressTracker.php +++ b/classes/WpMatomo/Db/WordpressTracker.php @@ -59,6 +59,18 @@ public function rowCount( $queryResult ) { return $queryResult->rowCount(); } + /** + * @param \wpdb $wpdb + * + * @throws \Zend_Db_Statement_Exception + */ + private function throwExceptionIfError($wpdb) + { + if ($wpdb->last_error) { + throw new \Zend_Db_Statement_Exception($wpdb->last_error); + } + } + private function prepareWp( $sql, $bind = array() ) { global $wpdb; @@ -96,6 +108,7 @@ public function query( $query, $parameters = array() ) { } else { $query = $this->prepareWp( $query, $parameters ); $result = $wpdb->query( $query ); + $this->throwExceptionIfError($wpdb); } return new WordPressDbStatement( $this, $query, $result ); @@ -158,14 +171,22 @@ public function fetch( $query, $parameters = array() ) { global $wpdb; $prepare = $this->prepareWp( $query, $parameters ); - return $wpdb->get_row( $prepare, ARRAY_A ); + $row = $wpdb->get_row( $prepare, ARRAY_A ); + + $this->throwExceptionIfError($wpdb); + + return $row; } public function fetchAll( $query, $parameters = array() ) { global $wpdb; $prepare = $this->prepareWp( $query, $parameters ); - return $wpdb->get_results( $prepare, ARRAY_A ); + $results = $wpdb->get_results( $prepare, ARRAY_A ); + + $this->throwExceptionIfError($wpdb); + + return $results; } diff --git a/config/config.php b/config/config.php index 03d9abdba..42e867adb 100644 --- a/config/config.php +++ b/config/config.php @@ -41,6 +41,8 @@ */ global $wpdb; + \Piwik\Plugins\TagManager\TagManager::$enableAutoContainerCreation = false; + // in case DB credentials change in wordpress, we need to apply these changes here as well on demand $hostParts = explode(':', DB_HOST); $host = $hostParts[0]; diff --git a/matomo.php b/matomo.php index 789914a98..9df56bc6b 100644 --- a/matomo.php +++ b/matomo.php @@ -4,7 +4,7 @@ * Description: Most powerful web analytics for WordPress giving you 100% data ownership and privacy protection * Author: Matomo * Author URI: https://matomo.org - * Version: 0.1.5 + * Version: 0.1.6 * Domain Path: /languages * WC requires at least: 2.4.0 * WC tested up to: 3.2.6 @@ -48,7 +48,18 @@ function has_matomo_tag_manager() if (defined('MATOMO_DISABLE_TAG_MANAGER') && MATOMO_DISABLE_TAG_MANAGER) { return false; } - return !function_exists('is_multisite') || !is_multisite(); + + $is_multisite = function_exists('is_multisite') && is_multisite(); + if ($is_multisite) { + if ( ! function_exists( 'is_plugin_active_for_network' ) ) { + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + } + + $network_enabled = is_plugin_active_for_network( 'matomo/matomo.php' ); + return false; + } + + return true; } function add_matomo_plugin( $plugins_directory, $wp_plugin_file ) { diff --git a/tests/phpunit/wpmatomo/test-access.php b/tests/phpunit/wpmatomo/test-access.php index c34c78978..0cf117358 100644 --- a/tests/phpunit/wpmatomo/test-access.php +++ b/tests/phpunit/wpmatomo/test-access.php @@ -8,7 +8,7 @@ use WpMatomo\Roles; use WpMatomo\Settings; -class AccessTest extends MatomoUnit_TestCase { +class AccessTest extends MatomoAnalytics_TestCase { /** * @var Access