Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compatibility with the latest wpstatistics release #1240

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Expand Down
57 changes: 44 additions & 13 deletions classes/WpMatomo/WpStatistics/Importers/Actions/PagesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading