PluginProbe
Redirection / 2.7
Redirection v2.7
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / flusher.php

flusher.php in Redirection 2.7, at models/flusher.php

70 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $options = red_get_options();
11
12 $total = $this->expire_logs( 'redirection_logs', $options['expire_redirect'] );
13 $total += $this->expire_logs( 'redirection_404', $options['expire_404'] );
14
15 if ( $total >= self::DELETE_MAX ) {
16 $next = time() + ( self::DELETE_KEEP_ON * 60 );
17
18 // There are still more logs to clear - keep on doing until we're clean or until the next normal event
19 if ( $next < wp_next_scheduled( self::DELETE_HOOK ) ) {
20 wp_schedule_single_event( $next, self::DELETE_HOOK );
21 }
22 }
23
24 $this->optimize_logs();
25 }
26
27 private function optimize_logs() {
28 global $wpdb;
29
30 $rand = mt_rand( 1, 5000 );
31
32 if ( $rand === 11 )
33 $wpdb->query( "OPTIMIZE TABLE {$wpdb->prefix}redirection_logs" );
34 elseif ( $rand === 12 )
35 $wpdb->query( "OPTIMIZE TABLE {$wpdb->prefix}redirection_404" );
36 }
37
38 private function expire_logs( $table, $expiry_time ) {
39 global $wpdb;
40
41 if ( $expiry_time > 0 ) {
42 $logs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}{$table} WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY)", $expiry_time ) );
43
44 if ( $logs > 0 ) {
45 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}{$table} WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT %d", $expiry_time, self::DELETE_MAX ) );
46 return min( self::DELETE_MAX, $logs );
47 }
48 }
49
50 return 0;
51 }
52
53 public static function schedule() {
54 $options = red_get_options();
55
56 if ( $options['expire_redirect'] > 0 || $options['expire_404'] > 0 ) {
57 if ( ! wp_next_scheduled( self::DELETE_HOOK ) ) {
58 wp_schedule_event( time(), self::DELETE_FREQ, self::DELETE_HOOK );
59 }
60 }
61 else {
62 Red_Flusher::clear();
63 }
64 }
65
66 public static function clear() {
67 wp_clear_scheduled_hook( self::DELETE_HOOK );
68 }
69 }
70