Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto purge records #56

Merged
merged 3 commits into from
Dec 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion connectors/installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ public static function callback_deactivate_plugin( $slug, $network_wide ) {
}

public static function callback_switch_theme( $name, $theme ) {
$stylesheet = $theme->get_stylesheet();
self::log(
__( '"%s" theme activated', 'stream' ),
compact( 'name', 'theme' ),
compact( 'name', 'stylesheet' ),
null,
array( 'themes' => 'activated' )
);
Expand Down
25 changes: 25 additions & 0 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public static function load() {

// Reset Streams database
add_action( 'wp_ajax_wp_stream_reset', array( __CLASS__, 'wp_ajax_reset' ) );

// Auto purge setup
add_action( 'init', array( __CLASS__, 'purge_schedule_setup' ) );
add_action( 'stream_auto_purge', array( __CLASS__, 'purge_scheduled_action' ) );
}

/**
Expand Down Expand Up @@ -227,4 +231,25 @@ public static function erase_stream_records() {
}
}

public static function purge_schedule_setup() {
if ( ! wp_next_scheduled( 'stream_auto_purge' ) ) {
wp_schedule_event( time(), 'daily', 'stream_auto_purge' );
}
}

public static function purge_scheduled_action() {
global $wpdb;
$days = WP_Stream_Settings::$options['general_records_ttl'];
$date = new DateTime( 'now', $timezone = new DateTimeZone( 'UTC' ) );
$date->sub( DateInterval::createFromDateString( "$days days" ) );
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->stream WHERE created < %s ", $date->format( 'Y-m-d H:i:s' ) ) );

if ( ! $ids ) {
return;
}
$wpdb->query( sprintf( "DELETE FROM $wpdb->stream WHERE ID IN (%s)", implode( ',', $ids ) ) );
$wpdb->query( sprintf( "DELETE FROM $wpdb->streammeta WHERE record_id IN (%s)", implode( ',', $ids ) ) );
$wpdb->query( sprintf( "DELETE FROM $wpdb->streamcontext WHERE record_id IN (%s)", implode( ',', $ids ) ) );
}

}