diff --git a/classes/WpMatomo/WpStatistics/DataConverters/NumberConverter.php b/classes/WpMatomo/WpStatistics/DataConverters/NumberConverter.php index eb3aadcca..adfb5a2dd 100644 --- a/classes/WpMatomo/WpStatistics/DataConverters/NumberConverter.php +++ b/classes/WpMatomo/WpStatistics/DataConverters/NumberConverter.php @@ -29,8 +29,11 @@ public static function aggregate_by_key( $wp_statistics_data, $key ) { 'nb_uniq_visitors' => 0, ]; } - $data[ $row[ $key ] ]['nb_visits'] += intval( $row['number'] ); - $data[ $row[ $key ] ]['nb_uniq_visitors'] += intval( $row['number'] ); + + $nb = isset( $row['number'] ) ? $row['number'] : $row['views']; + + $data[ $row[ $key ] ]['nb_visits'] += intval( $nb ); + $data[ $row[ $key ] ]['nb_uniq_visitors'] += intval( $nb ); } } diff --git a/classes/WpMatomo/WpStatistics/DataConverters/PagesTitleConverter.php b/classes/WpMatomo/WpStatistics/DataConverters/PagesTitleConverter.php index 4cedf3f84..deae418d8 100644 --- a/classes/WpMatomo/WpStatistics/DataConverters/PagesTitleConverter.php +++ b/classes/WpMatomo/WpStatistics/DataConverters/PagesTitleConverter.php @@ -14,6 +14,13 @@ class PagesTitleConverter extends NumberConverter implements DataConverterInterface { public static function convert( array $wp_statistics_data ) { + // the title property is not set in newer versions of wp-statistics + foreach ( $wp_statistics_data as $key => $row ) { + if ( ! isset( $row['title'] ) ) { + $wp_statistics_data[ $key ]['title'] = get_the_title( $row['page_id'] ); + } + } + $rows = self::aggregate_by_key( $wp_statistics_data, 'title' ); $data_tables = [ diff --git a/classes/WpMatomo/WpStatistics/DataConverters/PagesUrlConverter.php b/classes/WpMatomo/WpStatistics/DataConverters/PagesUrlConverter.php index 78956273d..609de4f8f 100644 --- a/classes/WpMatomo/WpStatistics/DataConverters/PagesUrlConverter.php +++ b/classes/WpMatomo/WpStatistics/DataConverters/PagesUrlConverter.php @@ -15,7 +15,10 @@ class PagesUrlConverter extends NumberConverter implements DataConverterInterface { public static function convert( array $wp_statistics_data ) { - $rows = self::aggregate_by_key( $wp_statistics_data, 'str_url' ); + $first_page = reset( $wp_statistics_data ); + $key = isset( $first_page['uri'] ) ? 'uri' : 'str_url'; + + $rows = self::aggregate_by_key( $wp_statistics_data, $key ); $main_url_without_slash = site_url(); $main_url_without_slash = rtrim( $main_url_without_slash, '/' ); $data_tables = [ diff --git a/classes/WpMatomo/WpStatistics/DataConverters/SearchQueryConverter.php b/classes/WpMatomo/WpStatistics/DataConverters/SearchQueryConverter.php index c976841c7..a05286a21 100644 --- a/classes/WpMatomo/WpStatistics/DataConverters/SearchQueryConverter.php +++ b/classes/WpMatomo/WpStatistics/DataConverters/SearchQueryConverter.php @@ -13,10 +13,13 @@ class SearchQueryConverter extends FilterConverter implements DataConverterInterface { public static function convert( array $wp_statistics_data ) { - $data = self::filter( $wp_statistics_data, '?s=', 'str_url' ); + $first_page = reset( $wp_statistics_data ); + $key = isset( $first_page['uri'] ) ? 'uri' : 'str_url'; + + $data = self::filter( $wp_statistics_data, '?s=', $key ); foreach ( $data as $id => $url ) { $matches = []; - if ( preg_match( '/\?s=(.+)$/', $url['str_url'], $matches ) ) { + if ( preg_match( '/\?s=(.+)$/', $url[ $key ], $matches ) ) { $data[ $id ]['keyword'] = $matches[1]; } } diff --git a/classes/WpMatomo/WpStatistics/Importers/Actions/PagesImporter.php b/classes/WpMatomo/WpStatistics/Importers/Actions/PagesImporter.php index 2335bda7f..b104f0097 100644 --- a/classes/WpMatomo/WpStatistics/Importers/Actions/PagesImporter.php +++ b/classes/WpMatomo/WpStatistics/Importers/Actions/PagesImporter.php @@ -9,6 +9,7 @@ use Psr\Log\LoggerInterface; use WP_STATISTICS\MetaBox\pages; use Piwik\Date; +use WP_Statistics\Service\Admin\Metabox\MetaboxDataProvider; use WpMatomo\WpStatistics\Config; use WpMatomo\WpStatistics\DataConverters\PagesUrlConverter; use WpMatomo\WpStatistics\DataConverters\PagesTitleConverter; @@ -36,18 +37,41 @@ public function import_records( Date $date ) { $page = 0; do { $page ++; - $pages_found = pages::get( - [ - 'from' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ), - 'to' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ), - 'per_page' => $limit, - 'paged' => $page, - ] - ); + if ( class_exists( '\WP_STATISTICS\MetaBox\pages' ) ) { + $pages_found = pages::get( + [ + 'from' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ), + 'to' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ), + 'per_page' => $limit, + 'paged' => $page, + ] + ); - $has_no_data_prop = ( array_key_exists( 'no_data', $pages_found ) && ( 1 === $pages_found['no_data'] ) ); - $has_empty_pages_prop = ( array_key_exists( 'pages', $pages_found ) && empty( $pages_found['pages'] ) ); - $no_data = $has_no_data_prop || $has_empty_pages_prop; + $has_no_data_prop = ( array_key_exists( 'no_data', $pages_found ) && ( 1 === $pages_found['no_data'] ) ); + $has_empty_pages_prop = ( array_key_exists( 'pages', $pages_found ) && empty( $pages_found['pages'] ) ); + $no_data = $has_no_data_prop || $has_empty_pages_prop; + } else { + $pages_found = $this->get_metabox_data_provider()->getTopPages( + [ + 'date' => [ + 'from' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ), + 'to' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ), + ], + 'per_page' => $limit, + 'page' => $page, + 'fields' => [ 'id', 'uri', 'type', 'SUM(count) as views', 'page_id' ], + ] + ); + + $pages_found = array_map( + function ( $row_std_class ) { + return (array) $row_std_class; + }, + $pages_found + ); + + $no_data = count( $pages_found ) < 1; + } if ( ! $no_data ) { $pages = array_merge( $pages, array_key_exists( 'pages', $pages_found ) ? $pages_found['pages'] : $pages_found ); @@ -59,9 +83,12 @@ public function import_records( Date $date ) { Common::destroy( $search_keywords ); foreach ( $pages as $id => $page ) { - $pos = strpos( $page['str_url'], '?' ); + $key = isset( $page['uri'] ) ? 'uri' : 'str_url'; + $uri = $page[ $key ]; + + $pos = strpos( $uri, '?' ); if ( false !== $pos ) { - $pages[ $id ]['str_url'] = substr( $page['str_url'], 0, $pos ); + $pages[ $id ][ $key ] = substr( $uri, 0, $pos ); } } $pages_url = PagesUrlConverter::convert( $pages ); @@ -74,4 +101,8 @@ public function import_records( Date $date ) { $this->insert_record( Archiver::PAGE_TITLES_RECORD_NAME, $pages_title, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table, Metrics::INDEX_NB_VISITS ); Common::destroy( $pages_title ); } + + private function get_metabox_data_provider() { + return new MetaboxDataProvider(); + } } diff --git a/classes/WpMatomo/WpStatistics/Importers/Actions/VisitsTimeImporter.php b/classes/WpMatomo/WpStatistics/Importers/Actions/VisitsTimeImporter.php index f35c0c759..a732cea8f 100644 --- a/classes/WpMatomo/WpStatistics/Importers/Actions/VisitsTimeImporter.php +++ b/classes/WpMatomo/WpStatistics/Importers/Actions/VisitsTimeImporter.php @@ -3,10 +3,7 @@ use Piwik\Common; use Piwik\Plugins\VisitTime\Archiver; -use WP_STATISTICS\MetaBox\top_visitors; use Piwik\Date; -use WP_Statistics\Models\VisitorsModel; -use WpMatomo\WpStatistics\Config; use WpMatomo\WpStatistics\DataConverters\VisitsTimeConverter; /** * @package WpMatomo