robotstxt-2fa/uninstall.php
2026-06-05 14:06:16 +00:00

65 lines
1.6 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_site_option( 'robotstxt_2fa_settings' );
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',
);
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
)
);
}