| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Flusher { |
| 4 |
const DELETE_HOOK = 'redirection_log_delete'; |
| 5 |
const DELETE_FREQ = 'daily'; |
| 6 |
const DELETE_MAX = 1000; |
| 7 |
const DELETE_KEEP_ON = 15; // 15 minutes |
| 8 |
|
| 9 |
public function flush() { |
| 10 |
$total = 0; |
| 11 |
$options = red_get_options(); |
| 12 |
|
| 13 |
$total += $this->expire_logs( $options['expire_redirect'] ); |
| 14 |
$total += $this->expire_404( $options['expire_404'] ); |
| 15 |
|
| 16 |
if ( $total >= self::DELETE_MAX ) { |
| 17 |
$next = time() + self::DELETE_KEEP_ON; |
| 18 |
|
| 19 |
// There are still more logs to clear - keep on doing until we've clean or until the next normal event |
| 20 |
if ( $next < wp_next_scheduled( self::DELETE_HOOK ) ) { |
| 21 |
wp_schedule_single_event( time() + ( self::DELETE_KEEP_ON * 60 ), self::DELETE_HOOK ); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
$this->optimize_logs(); |
| 26 |
} |
| 27 |
|
| 28 |
private function optimize_logs() { |
| 29 |
$rand = mt_rand( 1, 5000 ); |
| 30 |
|
| 31 |
if ( $rand === 11 ) |
| 32 |
$wpdb->query( "OPTIMIZE TABLE {$wpdb->prefix}redirection_logs" ); |
| 33 |
elseif ( $rand === 12 ) |
| 34 |
$wpdb->query( "OPTIMIZE TABLE {$wpdb->prefix}redirection_404" ); |
| 35 |
} |
| 36 |
|
| 37 |
private function expire_logs( $expiry_time ) { |
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
if ( $expiry_time > 0 ) { |
| 41 |
$logs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_logs WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY)", $expiry_time ) ); |
| 42 |
|
| 43 |
if ( $logs > 0 ) { |
| 44 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_logs WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT %d", $expiry_time, self::DELETE_MAX ) ); |
| 45 |
return min( self::DELETE_MAX, $logs ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
private function expire_404( $expiry_time ) { |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
if ( $expiry_time > 0 ) { |
| 56 |
$l404 = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_404 WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY)", $expiry_time ) ); |
| 57 |
|
| 58 |
if ( $l404 > 0 ) { |
| 59 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}redirection_404 WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT %d", $expiry_time, self::DELETE_MAX ) ); |
| 60 |
return min( self::DELETE_MAX, $l404 ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
public static function schedule() { |
| 68 |
$options = red_get_options(); |
| 69 |
|
| 70 |
if ( $options['expire_redirect'] > 0 || $options['expire_404'] > 0 ) { |
| 71 |
if ( !wp_next_scheduled( self::DELETE_HOOK ) ) |
| 72 |
wp_schedule_event( time(), self::DELETE_FREQ, self::DELETE_HOOK ); |
| 73 |
} |
| 74 |
else |
| 75 |
Red_Flusher::clear(); |
| 76 |
} |
| 77 |
|
| 78 |
public static function clear() { |
| 79 |
wp_clear_scheduled_hook( self::DELETE_HOOK ); |
| 80 |
} |
| 81 |
} |
| 82 |
|