Skip to content

Commit

Permalink
add "statify__skip_aggregation" hook to disable aggregation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stklcode committed Sep 8, 2022
1 parent b677409 commit ac6cb56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
15 changes: 7 additions & 8 deletions inc/class-statify-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ class Statify_Cron extends Statify {
*
* @since 0.3.0
* @version 1.4.0
* @wp-hook boolean statify__skip_aggregation
*/
public static function cleanup_data() {

// Global.
global $wpdb;

// Remove items.
// Remove old items.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE created <= SUBDATE(%s, %d)",
Expand All @@ -38,13 +37,13 @@ public static function cleanup_data() {
)
);

// Aggregate.
self::aggregate_data();
// Aggregate data.
if ( ! apply_filters( 'statify__skip_aggregation', false ) ) {
self::aggregate_data();
}

// Optimize DB.
$wpdb->query(
"OPTIMIZE TABLE `$wpdb->statify`"
);
$wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/test-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function set_up() {
* @preserveGlobalState disabled
*/
public function test_cronjob() {
global $wpdb;

// Initialize normal cycle, configure storage period of 3 days.
$this->init_statify_widget( 3 );
$this->assertNotFalse(
Expand Down Expand Up @@ -61,7 +63,8 @@ public function test_cronjob() {
$this->assertEquals( 2, $v['count'], 'Unexpected visit count' );
}

// Run the cron job.
// Run the cron job without aggregation.
add_filter( 'statify__skip_aggregation', '__return_true' );
Statify_Cron::cleanup_data();

// Verify that 2 days have been deleted.
Expand All @@ -72,6 +75,20 @@ public function test_cronjob() {
$this->assertContains( $v['date'], $remaining_dates, 'Unexpected remaining date in stats' );
$this->assertEquals( 2, $v['count'], 'Unexpected visit count' );
}
$this->assertEquals(
6,
$wpdb->get_var( "SELECT COUNT(*) FROM `$wpdb->statify`" ),
'Unexpected number of entries after cleanup without aggregation'
);

// Run the cron job with aggregation (default).
remove_filter( 'statify__skip_aggregation', '__return_true' );
Statify_Cron::cleanup_data();
$this->assertEquals(
3,
$wpdb->get_var( "SELECT COUNT(*) FROM `$wpdb->statify`" ),
'Unexpected number of entries after cleanup with aggregation'
);
}

/**
Expand Down

0 comments on commit ac6cb56

Please sign in to comment.