diff --git a/CHANGELOG.md b/CHANGELOG.md index a4daec8..58e6a8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/inc/class-statify-cron.php b/inc/class-statify-cron.php index 2b01707..969c770 100644 --- a/inc/class-statify-cron.php +++ b/inc/class-statify-cron.php @@ -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'] ) ); diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index b9b3947..df7cee6 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -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( @@ -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`"