PluginProbe
Redirection / 4.9.1
Redirection v4.9.1
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 4.9.1, at models/flusher.php

74 lines 2.0 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 = 10000;
7 const DELETE_KEEP_ON = 10; // 10 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 = wp_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
39 private function expire_logs( $table, $expiry_time ) {
40 global $wpdb;
41
42 if ( $expiry_time > 0 ) {
43 // Known values
44 // phpcs:ignore
45 $logs = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}{$table} WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY)", $expiry_time ) );
46
47 if ( $logs > 0 ) {
48 // Known values
49 // phpcs:ignore
50 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}{$table} WHERE created < DATE_SUB(NOW(), INTERVAL %d DAY) LIMIT %d", $expiry_time, self::DELETE_MAX ) );
51 return min( self::DELETE_MAX, $logs );
52 }
53 }
54
55 return 0;
56 }
57
58 public static function schedule() {
59 $options = red_get_options();
60
61 if ( $options['expire_redirect'] > 0 || $options['expire_404'] > 0 ) {
62 if ( ! wp_next_scheduled( self::DELETE_HOOK ) ) {
63 wp_schedule_event( time(), self::DELETE_FREQ, self::DELETE_HOOK );
64 }
65 } else {
66 self::clear();
67 }
68 }
69
70 public static function clear() {
71 wp_clear_scheduled_hook( self::DELETE_HOOK );
72 }
73 }
74