get_charset_collate(); $audit_table = $wpdb->prefix . 'aio_audit_logs'; $lockouts_table = $wpdb->prefix . 'aio_lockouts'; $logs_404_table = $wpdb->prefix . 'aio_404_logs'; $email_logs_table = $wpdb->prefix . 'aio_email_logs'; $sql = "CREATE TABLE $audit_table ( id bigint(20) NOT NULL AUTO_INCREMENT, time datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, user_id bigint(20) NOT NULL, user_login varchar(60) NOT NULL, event varchar(100) NOT NULL, details text NOT NULL, ip_address varchar(45) NOT NULL, PRIMARY KEY (id) ) $charset_collate; CREATE TABLE $lockouts_table ( id bigint(20) NOT NULL AUTO_INCREMENT, ip_address varchar(45) NOT NULL, reason varchar(255) NOT NULL, blocked_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, expires_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id), KEY ip_address (ip_address) ) $charset_collate; CREATE TABLE $logs_404_table ( id bigint(20) NOT NULL AUTO_INCREMENT, url varchar(255) NOT NULL, hits int(11) DEFAULT 1 NOT NULL, last_hit datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id), KEY url (url) ) $charset_collate; CREATE TABLE $email_logs_table ( id bigint(20) NOT NULL AUTO_INCREMENT, sent_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, recipient varchar(255) NOT NULL, subject varchar(255) NOT NULL, message longtext NOT NULL, headers text NOT NULL, status varchar(20) NOT NULL, error_msg text NOT NULL, mailer varchar(40) DEFAULT 'smtp' NOT NULL, PRIMARY KEY (id), KEY status (status), KEY sent_at (sent_at) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } /** * Migration & Automatic DB Schema Upgrade Hook (runs once per version bump). */ function aio_maybe_upgrade_database() { if ( get_option( 'aio_db_version' ) === AIO_DB_VERSION ) { return; } $old_options = get_option( 'w2w_options' ); $new_options = get_option( 'aio_options' ); if ( false !== $old_options && false === $new_options ) { update_option( 'aio_options', $old_options ); delete_option( 'w2w_options' ); } aio_tools_create_tables(); update_option( 'aio_db_version', AIO_DB_VERSION ); } add_action( 'admin_init', 'aio_maybe_upgrade_database' ); function aio_tools_activate() { global $wpdb; $lockouts_table = $wpdb->prefix . 'aio_lockouts'; $logs_404_table = $wpdb->prefix . 'aio_404_logs'; aio_tools_create_tables(); update_option( 'aio_db_version', AIO_DB_VERSION ); // Migrate 404 logs from options $old_404_logs = get_option('aio_404_logs'); if (is_array($old_404_logs) && !empty($old_404_logs)) { foreach ($old_404_logs as $url => $data) { $wpdb->insert($logs_404_table, [ 'url' => substr($url, 0, 255), 'hits' => isset($data['count']) ? intval($data['count']) : 1, 'last_hit' => isset($data['last_hit']) ? wp_date('Y-m-d H:i:s', $data['last_hit']) : current_time('mysql') ]); } delete_option('aio_404_logs'); } // Migrate blocked IPs from options $old_blocked_ips = get_option('aio_blocked_ips'); if (is_array($old_blocked_ips) && !empty($old_blocked_ips)) { foreach ($old_blocked_ips as $ip => $expires_timestamp) { $expires_at = ($expires_timestamp > 0) ? wp_date('Y-m-d H:i:s', $expires_timestamp) : '2099-12-31 23:59:59'; $wpdb->insert($lockouts_table, [ 'ip_address' => substr($ip, 0, 45), 'reason' => 'Migrated from v3.0.x options', 'blocked_at' => current_time('mysql'), 'expires_at' => $expires_at ]); } delete_option('aio_blocked_ips'); } // Generate a unique encryption key if not exists if ( ! get_option('aio_encryption_key') ) { update_option('aio_encryption_key', wp_generate_password(64, true, true)); } if ( ! empty( aio_get_option( 'optEnableSitemap' ) ) ) { add_rewrite_rule( '^sitemap\.xml$', 'index.php?aio_sitemap=1', 'top' ); } if ( aio_is_module_active( 'opt-module-performance' ) ) { $aio_opts = get_option( 'aio_options', array() ); $page_cache_on = ! empty( $aio_opts['optEnablePageCache'] ) && empty( $aio_opts['optEnableMaintenance'] ); if ( file_exists( AIO_MODULES_PATH . 'performance/class-aio-dropin-manager.php' ) ) { require_once AIO_MODULES_PATH . 'performance/class-aio-dropin-manager.php'; $adv_on = $page_cache_on && ( ! isset( $aio_opts['optEnableAdvCacheDropin'] ) || ! empty( $aio_opts['optEnableAdvCacheDropin'] ) ); AIO_Dropin_Manager::sync_advanced_cache( $adv_on, false, $aio_opts ); } if ( file_exists( AIO_MODULES_PATH . 'performance/class-aio-htaccess-cache.php' ) ) { require_once AIO_MODULES_PATH . 'performance/class-aio-htaccess-cache.php'; $ht_page_on = $page_cache_on && empty( $aio_opts['optCacheSeparateMobile'] ) && ( ! isset( $aio_opts['optEnableHtaccessPageCache'] ) || ! empty( $aio_opts['optEnableHtaccessPageCache'] ) ); AIO_Htaccess_Cache::sync_page_cache( $ht_page_on, $aio_opts ); AIO_Htaccess_Cache::sync_webp_rewrite( ! empty( $aio_opts['optEnableWebpRewrite'] ) ); AIO_Htaccess_Cache::sync( ! empty( $aio_opts['optEnableBrowserCache'] ) ); } // Note: Do NOT install wp-content/object-cache.php inside register_activation_hook(), // because WordPress calls update_option('active_plugins') AFTER activation hooks finish. // AIO_Object_Cache_Manager::maybe_restore_dropin_after_reactivate() restores it safely on admin_init // with a rotated namespace seed once aio-tools.php is already committed to active_plugins in MySQL. if ( file_exists( AIO_MODULES_PATH . 'performance/class-aio-object-cache.php' ) ) { require_once AIO_MODULES_PATH . 'performance/class-aio-object-cache.php'; AIO_Object_Cache_Manager::purge_plugin_options_cache(); } } flush_rewrite_rules(); } register_activation_hook( __FILE__, 'aio_tools_activate' ); function aio_tools_deactivate() { wp_clear_scheduled_hook( 'aio_weekly_db_cleanup' ); wp_clear_scheduled_hook( 'aio_db_auto_cleanup_event' ); wp_clear_scheduled_hook( 'aio_daily_integrity_scan' ); wp_clear_scheduled_hook( 'aio_process_webp_bulk_queue' ); wp_clear_scheduled_hook( 'aio_convert_webp_cron' ); wp_clear_scheduled_hook( 'aio_cache_preload_batch' ); wp_clear_scheduled_hook( 'aio_page_cache_gc_cron' ); if ( file_exists( AIO_MODULES_PATH . 'performance/class-aio-htaccess-cache.php' ) ) { require_once AIO_MODULES_PATH . 'performance/class-aio-htaccess-cache.php'; AIO_Htaccess_Cache::remove(); } if ( file_exists( AIO_MODULES_PATH . 'performance/class-aio-dropin-manager.php' ) ) { require_once AIO_MODULES_PATH . 'performance/class-aio-dropin-manager.php'; AIO_Dropin_Manager::sync_advanced_cache( false ); } if ( file_exists( AIO_MODULES_PATH . 'performance/class-aio-object-cache.php' ) ) { require_once AIO_MODULES_PATH . 'performance/class-aio-object-cache.php'; AIO_Object_Cache_Manager::remove_dropin(); } flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'aio_tools_deactivate' ); function run_aio_tools() { $plugin = new AIO_Tools(); $plugin->run(); } run_aio_tools();