This commit is contained in:
Javier Casares 2026-02-09 11:57:42 +00:00
commit a6b592a27d

View file

@ -9,6 +9,30 @@ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// Check if user wants to delete data on uninstall.
$should_delete_data = false;
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
// Check network option first, fallback to first site option.
$network_options = get_site_option( 'robotstxt_smtp_network_options', array() );
if ( is_array( $network_options ) && ! empty( $network_options['delete_data_on_uninstall'] ) ) {
$should_delete_data = true;
} else {
$sites = get_sites( array( 'number' => 1, 'fields' => 'ids' ) );
$first_site_id = ! empty( $sites ) ? $sites[0] : 1;
$site_options = get_blog_option( $first_site_id, 'robotstxt_smtp_options', array() );
$should_delete_data = is_array( $site_options ) && ! empty( $site_options['delete_data_on_uninstall'] );
}
} else {
$options = get_option( 'robotstxt_smtp_options', array() );
$should_delete_data = is_array( $options ) && ! empty( $options['delete_data_on_uninstall'] );
}
// Exit early if user doesn't want to delete data.
if ( ! $should_delete_data ) {
return;
}
if ( ! function_exists( 'robotstxt_smtp_uninstall_cleanup_site' ) ) {
/**
* Removes all plugin data for the current site.
@ -18,12 +42,36 @@ if ( ! function_exists( 'robotstxt_smtp_uninstall_cleanup_site' ) ) {
* @return void
*/
function robotstxt_smtp_uninstall_cleanup_site(): void {
// Delete core SMTP plugin options.
delete_option( 'robotstxt_smtp_options' );
// Delete Newsletter add-on options.
delete_option( 'robotstxt_smtp_newsletter_options' );
// Delete logs (shared by core and add-ons).
robotstxt_smtp_uninstall_delete_logs();
// Delete core SMTP transients.
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_tool_', false );
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_test_result_', false );
// Delete Amazon SES transients.
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_amazonses_quota_', false );
// Delete updater cache transients for all plugins.
$plugin_basenames = array(
'robotstxt-smtp/robotstxt-smtp.php',
'robotstxt-smtp-amazonses/robotstxt-smtp-amazonses.php',
'robotstxt-smtp-newsletter/robotstxt-smtp-newsletter.php',
);
foreach ( $plugin_basenames as $basename ) {
delete_transient( 'robotstxt_updater_' . md5( $basename ) );
}
// Clear scheduled cron hooks.
wp_clear_scheduled_hook( 'robotstxt_smtp_cleanup_logs' );
wp_clear_scheduled_hook( 'robotstxt_smtp_cleanup_stats' );
wp_clear_scheduled_hook( 'robotstxt_smtp_amazonses_update_quota' );
}
}
@ -36,11 +84,29 @@ if ( ! function_exists( 'robotstxt_smtp_uninstall_cleanup_network' ) ) {
* @return void
*/
function robotstxt_smtp_uninstall_cleanup_network(): void {
// Delete core SMTP network options.
delete_site_option( 'robotstxt_smtp_network_options' );
delete_site_option( 'robotstxt_smtp_configuration_scope' );
// Delete core SMTP site transients.
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_tool_', true );
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_test_result_', true );
// Delete Amazon SES site transients.
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_amazonses_quota_', true );
// Delete updater cache site transients for all plugins.
$plugin_basenames = array(
'robotstxt-smtp/robotstxt-smtp.php',
'robotstxt-smtp-amazonses/robotstxt-smtp-amazonses.php',
'robotstxt-smtp-newsletter/robotstxt-smtp-newsletter.php',
);
foreach ( $plugin_basenames as $basename ) {
delete_site_transient( 'robotstxt_updater_' . md5( $basename ) );
}
// Clear WordPress update cache.
delete_site_transient( 'update_plugins' );
}
}
@ -140,8 +206,30 @@ if ( ! function_exists( 'robotstxt_smtp_uninstall_delete_logs' ) ) {
}
}
if ( ! function_exists( 'robotstxt_smtp_uninstall_delete_statistics_table' ) ) {
/**
* Drops the statistics table from the database.
*
* @since 2.0.1
*
* @return void
*/
function robotstxt_smtp_uninstall_delete_statistics_table(): void {
global $wpdb;
$table_name = $wpdb->base_prefix . 'robotstxt_smtp_stats';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
// Delete the database version option.
delete_site_option( 'robotstxt_smtp_stats_db_version' );
}
}
// Clean up network-wide data.
robotstxt_smtp_uninstall_cleanup_network();
// Clean up site-specific data.
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
$robotstxt_smtp_sites = get_sites( array( 'fields' => 'ids' ) );
@ -153,3 +241,6 @@ if ( function_exists( 'is_multisite' ) && is_multisite() ) {
} else {
robotstxt_smtp_uninstall_cleanup_site();
}
// Drop the statistics table (global, only once).
robotstxt_smtp_uninstall_delete_statistics_table();