Skip to content

Commit 575425b

Browse files
committed
add "statify__skip_aggregation" hook to disable aggregation
If Statify is extended by custom columns the aggregation routine will fail. To support such scenarios, we introduce a boolean hook, so the previous behavior can be preserved without breaking compatibility.
1 parent 6a73330 commit 575425b

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

inc/class-statify-cron.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ class Statify_Cron extends Statify {
2323
*
2424
* @since 0.3.0
2525
* @version 1.4.0
26+
* @wp-hook boolean statify__skip_aggregation
2627
*/
2728
public static function cleanup_data() {
28-
29-
// Global.
3029
global $wpdb;
3130

32-
// Remove items.
31+
// Remove old items.
3332
$wpdb->query(
3433
$wpdb->prepare(
3534
"DELETE FROM `$wpdb->statify` WHERE created <= SUBDATE(%s, %d)",
@@ -38,13 +37,13 @@ public static function cleanup_data() {
3837
)
3938
);
4039

41-
// Aggregate.
42-
self::aggregate_data();
40+
// Aggregate data.
41+
if ( ! apply_filters( 'statify__skip_aggregation', false ) ) {
42+
self::aggregate_data();
43+
}
4344

4445
// Optimize DB.
45-
$wpdb->query(
46-
"OPTIMIZE TABLE `$wpdb->statify`"
47-
);
46+
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
4847
}
4948

5049
/**

tests/test-cron.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function set_up() {
2929
* @preserveGlobalState disabled
3030
*/
3131
public function test_cronjob() {
32+
global $wpdb;
33+
3234
// Initialize normal cycle, configure storage period of 3 days.
3335
$this->init_statify_widget( 3 );
3436
$this->assertNotFalse(
@@ -61,7 +63,8 @@ public function test_cronjob() {
6163
$this->assertEquals( 2, $v['count'], 'Unexpected visit count' );
6264
}
6365

64-
// Run the cron job.
66+
// Run the cron job without aggregation.
67+
add_filter( 'statify__skip_aggregation', '__return_true' );
6568
Statify_Cron::cleanup_data();
6669

6770
// Verify that 2 days have been deleted.
@@ -72,6 +75,20 @@ public function test_cronjob() {
7275
$this->assertContains( $v['date'], $remaining_dates, 'Unexpected remaining date in stats' );
7376
$this->assertEquals( 2, $v['count'], 'Unexpected visit count' );
7477
}
78+
$this->assertEquals(
79+
6,
80+
$wpdb->get_var( "SELECT COUNT(*) FROM `$wpdb->statify`" ),
81+
'Unexpected number of entries after cleanup without aggregation'
82+
);
83+
84+
// Run the cron job with aggregation (default).
85+
remove_filter( 'statify__skip_aggregation', '__return_true' );
86+
Statify_Cron::cleanup_data();
87+
$this->assertEquals(
88+
3,
89+
$wpdb->get_var( "SELECT COUNT(*) FROM `$wpdb->statify`" ),
90+
'Unexpected number of entries after cleanup with aggregation'
91+
);
7592
}
7693

7794
/**

0 commit comments

Comments
 (0)