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

Fix target and referer reporting when PHP and database differ on timezone #117

Merged
merged 8 commits into from
Apr 19, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
* Fix offset in visitor reporting due to different timezones between PHP and DB
* Introduced separate settinge page and reduced widget backview to widget settings only
* Add options to track logged in users, feeds and search requests
* Add option to show total visits
Expand Down
3 changes: 2 additions & 1 deletion inc/class-statify-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static function cleanup_data() {
// Remove items.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE created <= SUBDATE(CURDATE(), %d)",
"DELETE FROM `$wpdb->statify` WHERE created <= SUBDATE(%s, %d)",
current_time( 'Y-m-d' ),
(int) self::$_options['days']
)
);
Expand Down
49 changes: 38 additions & 11 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private static function _select_data() {
$today = (int) self::$_options['today'];
$show_totals = (int) self::$_options['show_totals'];

$limit_args = ( $today ) ? $limit : array( $days_show, $limit );
$current_date = current_time( 'Y-m-d' );

$data = array(
'visits' => $wpdb->get_results(
Expand All @@ -332,26 +332,53 @@ private static function _select_data() {
),
ARRAY_A
),
'target' => $wpdb->get_results(
);

if ( $today ) {
$data['target'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created " . ( $today ? '= DATE(NOW())' : '>= DATE_SUB(NOW(), INTERVAL %d DAY)' ) . ' GROUP BY `target` ORDER BY `count` DESC LIMIT %d',
$limit_args
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d",
$current_date,
$limit
),
ARRAY_A
),
'referrer' => $wpdb->get_results(
);
$data['referrer'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created " . ( $today ? '= DATE(NOW())' : '>= DATE_SUB(NOW(), INTERVAL %d DAY)' ) . ' GROUP BY `host` ORDER BY `count` DESC LIMIT %d',
$limit_args
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created = %s GROUP BY `host` ORDER BY `count` DESC LIMIT %d",
$current_date,
$limit
),
ARRAY_A
),
);
);
} else {
$data['target'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created >= DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `target` ORDER BY `count` DESC LIMIT %d",
$current_date,
$days_show,
$limit
),
ARRAY_A
);
$data['referrer'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created >= DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `host` ORDER BY `count` DESC LIMIT %d",
$current_date,
$days_show,
$limit
),
ARRAY_A
);
}

if ( $show_totals ) {
$data['visit_totals'] = array(
'today' => $wpdb->get_var(
"SELECT COUNT(`created`) FROM `$wpdb->statify` WHERE created = DATE(NOW())"
$wpdb->prepare(
"SELECT COUNT(`created`) FROM `$wpdb->statify` WHERE created = %s",
$current_date
)
),
'since_beginning' => $wpdb->get_var(
"SELECT COUNT(`created`) FROM `$wpdb->statify`"
Expand Down