diff --git a/migrations/1.0.20-unlink-custom-endpoint.php b/migrations/1.0.20-unlink-custom-endpoint.php deleted file mode 100644 index dfe7b13..0000000 --- a/migrations/1.0.20-unlink-custom-endpoint.php +++ /dev/null @@ -1,4 +0,0 @@ -get_next_period($dateStart, $dateEnd, 1); + $prevDates = $this->get_next_period($dateStart, $dateEnd, -1); $posts_offset = isset($_GET['posts']['offset']) ? absint($_GET['posts']['offset']) : 0; $referrers_offset = isset($_GET['referrers']['offset']) ? absint($_GET['referrers']['offset']) : 0; @@ -60,6 +62,8 @@ public function show(): void $referrers_limit = isset($_GET['referrers']['limit']) ? absint($_GET['referrers']['limit']) : $items_per_page; $totals = $stats->get_totals($dateStart->format('Y-m-d'), $dateEnd->format('Y-m-d'), $page); + $totals_previous = $stats->get_totals($prevDates[0]->format('Y-m-d'), $prevDates[1]->format('Y-m-d'), $page); + $posts = $stats->get_posts($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), $posts_offset, $posts_limit); $posts_count = $stats->count_posts($dateStart->format('Y-m-d'), $dateEnd->format('Y-m-d')); $referrers = $stats->get_referrers($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), $referrers_offset, $referrers_limit); diff --git a/src/class-stats.php b/src/class-stats.php index b29dfae..7a1c609 100644 --- a/src/class-stats.php +++ b/src/class-stats.php @@ -10,48 +10,38 @@ class Stats { - public function get_totals(string $start_date, string $end_date, int $page = 0): ?object + /** + * @return object { + * @type int visitors + * @type int pageviews + * } + */ + public function get_totals(string $start_date, string $end_date, int $page = 0, $include_previous = true): object { global $wpdb; - $previous_start_date = gmdate('Y-m-d', strtotime($start_date) - (strtotime($end_date . ' 23:59:59') - strtotime($start_date))); - - $table = $wpdb->prefix . 'koko_analytics_site_stats'; - $where_a = 's.date >= %s AND s.date <= %s'; - $args_a = array($start_date, $end_date); - $where_b = 's.date >= %s AND s.date < %s'; - $args_b = array($previous_start_date, $start_date); + $table = $wpdb->prefix . 'koko_analytics_site_stats s'; + $where = 's.date >= %s AND s.date <= %s'; + $args = array($start_date, $end_date); if ($page > 0) { - $table = $wpdb->prefix . 'koko_analytics_post_stats'; - $where_a .= ' AND s.id = %d'; - $where_b .= ' AND s.id = %d'; - $args_a[] = $page; - $args_b[] = $page; + $table = $wpdb->prefix . 'koko_analytics_post_stats s'; + $where .= ' AND s.id = %d'; + $args[] = $page; } - $sql = $wpdb->prepare("SELECT - cur.*, - prev.visitors AS prev_visitors, - cur.visitors - prev.visitors AS visitors_change, - cur.pageviews - prev.pageviews AS pageviews_change, - cur.visitors / prev.visitors - 1 AS visitors_change_rel, - cur.pageviews / prev.pageviews - 1 AS pageviews_change_rel - FROM - (SELECT COALESCE(SUM(visitors), 0) AS visitors, COALESCE(SUM(pageviews), 0) AS pageviews FROM {$table} s WHERE $where_a) AS cur, - (SELECT COALESCE(SUM(visitors), 0) AS visitors, COALESCE(SUM(pageviews), 0) AS pageviews FROM {$table} s WHERE $where_b) AS prev; - ", array_merge($args_a, $args_b)); + $sql = $wpdb->prepare(" + SELECT COALESCE(SUM(visitors), 0) AS visitors, COALESCE(SUM(pageviews), 0) AS pageviews + FROM {$table} + WHERE {$where} + ", $args); $result = $wpdb->get_row($sql); // ensure we always return a valid object containing the keys we need if (!$result) { return (object) [ 'pageviews' => 0, - 'pageviews_change' => 0, - 'pageviews_change_rel' => 0, 'visitors' => 0, - 'visitors_change' => 0, - 'visitors_change_rel' => 0, ]; } @@ -59,9 +49,8 @@ public function get_totals(string $start_date, string $end_date, int $page = 0): // this happens when the cookie was valid over a period of 2 calendar days // we can make this less obviously wrong by always specifying there was at least 1 visitors // whenever we have any pageviews - if ($result && $result->pageviews > 0 && $result->visitors == 0) { + if ($result->visitors == 0 && $result->pageviews > 0) { $result->visitors = 1; - $result->visitors_change += $result->visitors_change > 0 ? -1 : 1; } return $result; diff --git a/src/views/dashboard-page.php b/src/views/dashboard-page.php index 584ead6..7425c54 100644 --- a/src/views/dashboard-page.php +++ b/src/views/dashboard-page.php @@ -95,42 +95,53 @@
visitors); ?> - visitors_change_rel); ?> + | - visitors_change != 0) { - ?>visitors_change)); ?> - visitors_change > 0) { + 0) { ?> - visitors_change < 0) { + | |
pageviews); ?> - pageviews_change_rel); ?> + | - pageviews_change != 0) { - ?>pageviews_change)); ?> - pageviews_change > 0) { + 0) { ?> - pageviews_change < 0) { + |