Last active
February 20, 2025 08:49
-
-
Save mircobabini/da8c7731fe90c00973fc1be3e0c60f55 to your computer and use it in GitHub Desktop.
Extended Login Logs for Wordfence Security: preserve more logins history in a separate table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordFence_Logins_Extended_Logs { | |
protected $VERSION = 1; | |
public function __construct() { | |
add_action( 'admin_init', [ $this, 'init' ] ); | |
add_action( 'wordfence_daily_cron', [ $this, 'update_backup' ], 5 ); | |
add_action( 'wordfence_daily_cron', [ $this, 'cleanup_backup' ], 15 ); | |
} | |
public function init() { | |
// check option to see if we need to upgrade | |
$version = get_option( 'wf_logins_extended_logs_version', 0 ); | |
if ( $version < $this->VERSION ) { | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'wflogins_backup'; | |
$charset_collate = $wpdb->get_charset_collate(); | |
$sql = "CREATE TABLE $table_name ( | |
id int(10) unsigned NOT NULL, | |
hitID int(11) DEFAULT NULL, | |
ctime double(17,6) unsigned NOT NULL, | |
fail tinyint(3) unsigned NOT NULL, | |
action varchar(40) NOT NULL, | |
username varchar(255) NOT NULL, | |
userID int(10) unsigned NOT NULL, | |
IP binary(16) DEFAULT NULL, | |
UA text, | |
PRIMARY KEY (id) | |
) $charset_collate;"; | |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | |
dbDelta( $sql ); | |
} | |
// simulate | |
// $this->update_backup(); | |
// $this->cleanup_backup(); | |
} | |
public function update_backup() { | |
global $wpdb; | |
$source_table = $wpdb->prefix . 'wflogins'; | |
$destination_table = $wpdb->prefix . 'wflogins_backup'; | |
// Copy records from source table to destination table, ignoring duplicates | |
$query = " | |
INSERT IGNORE INTO $destination_table (id, hitID, ctime, fail, action, username, userID, IP, UA) | |
SELECT id, hitID, ctime, fail, action, username, userID, IP, UA | |
FROM $source_table | |
"; | |
$wpdb->query( $query ); | |
} | |
public function cleanup_backup() { | |
global $wpdb; | |
$destination_table = $wpdb->prefix . 'wflogins_backup'; | |
// Delete records older than 2 years days (730gg) | |
$query = " | |
DELETE FROM $destination_table | |
WHERE ctime < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 730 DAY)) | |
"; | |
$wpdb->query( $query ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment