| 1 |
<?php |
| 2 |
namespace Hurrytimer; |
| 3 |
|
| 4 |
class IP_Log_Manager { |
| 5 |
|
| 6 |
const CLEANUP_ACTION = 'hurrytimer_evergreen_daily_cleanup'; |
| 7 |
private $table; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
global $wpdb; |
| 11 |
$this->table = "{$wpdb->prefix}hurrytimer_evergreen"; |
| 12 |
|
| 13 |
if ( $this->has_entries() ) { |
| 14 |
add_action( 'wp', array( $this, 'schedule_daily_cleanup' ) ); |
| 15 |
}else{ |
| 16 |
$this->unschedule_cleanup(); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
public function schedule_daily_cleanup() { |
| 21 |
if ( ! wp_next_scheduled( self::CLEANUP_ACTION ) ) { |
| 22 |
wp_schedule_event( time(), 'daily', self::CLEANUP_ACTION ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public function cleanup_ip_logs() { |
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
$current_time = current_time( 'mysql' ); |
| 30 |
|
| 31 |
$query = $wpdb->prepare( |
| 32 |
"DELETE FROM {$this->table} WHERE destroy_at < %s", |
| 33 |
$current_time |
| 34 |
); |
| 35 |
|
| 36 |
// Run the query |
| 37 |
$wpdb->query( $query ); |
| 38 |
} |
| 39 |
|
| 40 |
public function has_entries() { |
| 41 |
global $wpdb; |
| 42 |
|
| 43 |
$result = $wpdb->get_var( "SELECT COUNT(*) FROM {$this->table}"); |
| 44 |
|
| 45 |
return ( $result > 0 ); |
| 46 |
} |
| 47 |
|
| 48 |
public function unschedule_cleanup() { |
| 49 |
wp_clear_scheduled_hook( self::CLEANUP_ACTION ); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
|