Files
WP-Security-Pack/wp-security-pack/includes/class-wpsp-db.php
T
2026-01-25 20:21:17 +01:00

163 lines
3.8 KiB
PHP

<?php
/**
* Database management for WP Security Pack.
*
* @package WP_Security_Pack
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Database class for table creation and management.
*/
class WPSP_DB {
/**
* Database version.
*
* @var string
*/
const DB_VERSION = '1.0.1';
/**
* Get activity log table name.
*
* @return string
*/
public static function get_log_table() {
global $wpdb;
return $wpdb->prefix . 'wpsp_activity_log';
}
/**
* Get lockouts table name.
*
* @return string
*/
public static function get_lockout_table() {
global $wpdb;
return $wpdb->prefix . 'wpsp_lockouts';
}
/**
* Plugin activation.
*/
public static function activate() {
self::create_tables();
self::set_default_options();
// Store DB version.
update_option( 'wpsp_db_version', self::DB_VERSION );
// Clear rewrite rules for custom login URL.
flush_rewrite_rules();
}
/**
* Check if database needs upgrading.
*/
public static function maybe_upgrade() {
$current_version = get_option( 'wpsp_db_version', '0' );
if ( version_compare( $current_version, self::DB_VERSION, '<' ) ) {
self::create_tables();
update_option( 'wpsp_db_version', self::DB_VERSION );
}
}
/**
* Plugin deactivation.
*/
public static function deactivate() {
// Clear scheduled events.
wp_clear_scheduled_hook( 'wpsp_daily_cleanup' );
// Flush rewrite rules.
flush_rewrite_rules();
}
/**
* Create database tables.
*/
public static function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$log_table = self::get_log_table();
$lockout_table = self::get_lockout_table();
// Activity log table.
$sql_log = "CREATE TABLE {$log_table} (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
event_type varchar(50) NOT NULL,
ip_address varchar(45) NOT NULL,
username varchar(60) DEFAULT NULL,
user_agent varchar(255) DEFAULT NULL,
country_code varchar(2) DEFAULT NULL,
details text DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY event_type (event_type),
KEY ip_address (ip_address),
KEY created_at (created_at)
) {$charset_collate};";
// Lockouts table.
// Note: lockout_until stores Unix timestamp as BIGINT for reliable comparisons.
$sql_lockout = "CREATE TABLE {$lockout_table} (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
ip_address varchar(45) NOT NULL,
failed_attempts int(11) NOT NULL DEFAULT 0,
lockout_until bigint(20) DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY ip_address (ip_address),
KEY lockout_until (lockout_until)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql_log );
dbDelta( $sql_lockout );
}
/**
* Set default options on activation.
*/
private static function set_default_options() {
$existing = get_option( 'wpsp_settings', false );
if ( false === $existing ) {
$defaults = WP_Security_Pack::get_default_settings();
update_option( 'wpsp_settings', $defaults );
}
}
/**
* Uninstall plugin (called from uninstall.php).
*/
public static function uninstall() {
global $wpdb;
// Drop tables.
$log_table = self::get_log_table();
$lockout_table = self::get_lockout_table();
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DROP TABLE IF EXISTS {$log_table}" );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "DROP TABLE IF EXISTS {$lockout_table}" );
// Delete options.
delete_option( 'wpsp_settings' );
delete_option( 'wpsp_db_version' );
// Delete transients.
delete_transient( 'wpsp_geo_cache' );
}
}