robotstxt-2fa/uninstall.php
2026-08-07 14:15:58 +00:00

84 lines
2.3 KiB
PHP

<?php
/**
* Uninstall cleanup for the 2FA (by ROBOTSTXT) plugin.
*
* Data is only deleted when the administrator has explicitly enabled the
* "Delete data on uninstall" option. The default is to preserve all data so
* it survives a reinstall.
*
* @package Robotstxt_2FA
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Determine whether the delete-on-uninstall option is enabled.
*
* On multisite the network option takes precedence; falls back to the
* per-site option on the main site.
*
* @return bool
*/
function robotstxt_2fa_should_delete_data(): bool {
$option_name = 'robotstxt_2fa_settings';
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
$network = get_site_option( $option_name, array() );
if ( is_array( $network ) && ! empty( $network ) ) {
return ! empty( $network['delete_on_uninstall'] );
}
}
$settings = get_option( $option_name, array() );
return is_array( $settings ) && ! empty( $settings['delete_on_uninstall'] );
}
if ( ! robotstxt_2fa_should_delete_data() ) {
return;
}
// Remove plugin-level options.
delete_option( 'robotstxt_2fa_settings' );
delete_option( 'robotstxt_2fa_failed_log' );
delete_option( 'robotstxt_2fa_geoip_status' );
delete_site_option( 'robotstxt_2fa_settings' );
// Clear the GeoIP auto-download cron event.
wp_clear_scheduled_hook( 'robotstxt_2fa_geoip_update' );
// Remove the auto-downloaded GeoIP database from the uploads directory.
$robotstxt_2fa_uploads = wp_upload_dir();
if ( ! empty( $robotstxt_2fa_uploads['basedir'] ) ) {
$robotstxt_2fa_geoip_file = trailingslashit( $robotstxt_2fa_uploads['basedir'] ) . 'robotstxt-2fa/country.mmdb';
if ( is_file( $robotstxt_2fa_geoip_file ) ) {
unlink( $robotstxt_2fa_geoip_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink -- Removing our own artifact on uninstall.
}
}
global $wpdb;
$robotstxt_2fa_meta_keys = array(
'robotstxt_2fa_user_settings',
'robotstxt_2fa_email_challenge',
'robotstxt_2fa_last_verifications',
'robotstxt_2fa_recovery_codes',
'robotstxt_2fa_otp_secret',
'robotstxt_2fa_known_contexts',
'robotstxt_2fa_trusted_devices',
'robotstxt_2fa_grace_period_start',
);
foreach ( $robotstxt_2fa_meta_keys as $robotstxt_2fa_meta_key ) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->usermeta} WHERE meta_key = %s",
$robotstxt_2fa_meta_key
)
);
}