Skip to content

Commit 5e19982

Browse files
add new date preset for all time
1 parent f209c31 commit 5e19982

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- If using cookie-based tracking, the cookie lifetime has been changed to expire at midnight (so a maximum of 24 hours).
77
- Simplified client-side tracking script so it's now smaller than 500 bytes.
88
- Excluded IP addresses now work properly with the optimized endpoint.
9+
- Added new dashboard date preset for "all time".
910

1011

1112
### 1.7.4 - May 14, 2025

src/class-dashboard.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public function show(): void
5353
$posts_limit = isset($_GET['posts']['limit']) ? absint($_GET['posts']['limit']) : $items_per_page;
5454
$referrers_limit = isset($_GET['referrers']['limit']) ? absint($_GET['referrers']['limit']) : $items_per_page;
5555

56+
[$total_start_date, $total_end_date] = $stats->get_total_date_range();
57+
5658
// calculate next and previous dates for datepicker component and comparison
5759
$nextDates = $this->get_next_period($dateStart, $dateEnd, 1);
5860
$prevDates = $this->get_next_period($dateStart, $dateEnd, -1);
@@ -118,6 +120,7 @@ public function get_date_presets(): array
118120
'last_month' => __('Last month', 'koko-analytics'),
119121
'this_year' => __('This year', 'koko-analytics'),
120122
'last_year' => __('Last year', 'koko-analytics'),
123+
'all_time' => __('All time', 'koko-analytics'),
121124
];
122125
}
123126

@@ -198,6 +201,8 @@ public function get_dates_for_range(\DateTimeImmutable $now, string $key, int $w
198201
$now->setDate((int) $now->format('Y') - 1, 1, 1),
199202
$now->setDate((int) $now->format('Y') - 1, 12, 31),
200203
];
204+
case 'all_time':
205+
return (new Stats())->get_total_date_range();
201206
}
202207
}
203208

src/class-stats.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111
class Stats
1212
{
13+
public function get_total_date_range(): array
14+
{
15+
global $wpdb;
16+
$result = $wpdb->get_row("select MIN(date) AS start, MAX(date) AS end FROM {$wpdb->prefix}koko_analytics_site_stats WHERE date IS NOT NULL;");
17+
if (!$result) {
18+
$today = new \DateTimeImmutable('now', wp_timezone());
19+
return [$today, $today];
20+
}
21+
22+
return [new \DateTimeImmutable($result->start, wp_timezone()), new \DateTimeImmutable($result->end, wp_timezone())];
23+
}
24+
1325
/**
1426
* @return object{ visitors: int, pageviews: int }
1527
*/

src/views/dashboard-page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
<div id="ka-datepicker-dropdown" class="ka-datepicker--dropdown" style="display: none;">
3939
<div class="ka-datepicker--quicknav">
4040
<?php // only output pagination for date ranges between reasonable dates... to prevent ever-crawling bots from going wild ?>
41-
<?php if ($dateStart > new \DateTimeImmutable('2000-01-01')) { ?>
41+
<?php if ($dateStart > $total_start_date) { ?>
4242
<a class="ka-datepicker--quicknav-prev" href="<?php echo esc_attr(add_query_arg(['start_date' => $prevDates[0]->format('Y-m-d'), 'end_date' => $prevDates[1]->format('Y-m-d')], $dashboard_url)); ?>"><?php esc_html_e('Previous date range', 'koko-analytics'); ?></a>
4343
<?php } ?>
4444
<span class="ka-datepicker--quicknav-heading"><?php echo wp_date($dateFormat, $dateStart->getTimestamp()); ?><?php echo wp_date($dateFormat, $dateEnd->getTimestamp()); ?></span>
45-
<?php if ($dateEnd < new \DateTimeImmutable('2100-01-01')) { ?>
45+
<?php if ($dateEnd < $total_end_date) { ?>
4646
<a class="ka-datepicker--quicknav-next" href="<?php echo esc_attr(add_query_arg(['start_date' => $nextDates[0]->format('Y-m-d'), 'end_date' => $nextDates[1]->format('Y-m-d')], $dashboard_url)); ?>"><?php esc_html_e('Next date range', 'koko-analytics'); ?></a>
4747
<?php } ?>
4848
</div>

0 commit comments

Comments
 (0)