Skip to content

Commit

Permalink
Merge pull request #55 from matomo-org/wpdbadapter
Browse files Browse the repository at this point in the history
Throw exceptions on DB errors in our DB adapter
  • Loading branch information
tsteur authored Oct 30, 2019
2 parents 880547c + 87fcfee commit b35c4cb
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
53 changes: 47 additions & 6 deletions classes/WpMatomo/Db/Wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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
}
Expand All @@ -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 = '' ) {
Expand All @@ -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;
}
}
25 changes: 23 additions & 2 deletions classes/WpMatomo/Db/WordpressTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}


Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
15 changes: 13 additions & 2 deletions matomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/wpmatomo/test-access.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use WpMatomo\Roles;
use WpMatomo\Settings;

class AccessTest extends MatomoUnit_TestCase {
class AccessTest extends MatomoAnalytics_TestCase {

/**
* @var Access
Expand Down

0 comments on commit b35c4cb

Please sign in to comment.