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

View file

@ -299,8 +299,12 @@ class Settings_Page {
add_action( 'admin_post_robotstxt_smtp_test_email', array( $this, 'handle_test_email' ) );
add_action( 'admin_post_robotstxt_smtp_clear_logs', array( $this, 'handle_clear_logs' ) );
add_action( 'admin_post_robotstxt_smtp_save_network_settings', array( $this, 'handle_save_network_settings' ) );
add_action( 'admin_post_robotstxt_smtp_reset_data', array( $this, 'handle_reset_data' ) );
add_action( 'admin_post_robotstxt_smtp_reset_data_network', array( $this, 'handle_reset_data_network' ) );
add_action( 'admin_notices', array( $this, 'display_test_email_notice' ) );
add_action( 'network_admin_notices', array( $this, 'display_test_email_notice' ) );
add_action( 'admin_notices', array( $this, 'display_reset_data_notice' ) );
add_action( 'network_admin_notices', array( $this, 'display_reset_data_notice' ) );
add_action( 'shutdown', array( $this, 'maybe_send_pending_site_test' ) );
add_action( 'updated_option', array( $this, 'handle_settings_option_updated' ), 10, 3 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
@ -1075,6 +1079,7 @@ class Settings_Page {
public function render_site_settings_page(): void {
$this->ensure_capability( self::SCOPE_SITE );
$this->set_scope( self::SCOPE_SITE );
$settings = $this->get_settings();
?>
<div class="wrap">
@ -1085,6 +1090,36 @@ class Settings_Page {
do_settings_sections( self::PAGE_SLUG );
submit_button( esc_html__( 'Save Changes', 'robotstxt-smtp' ) );
?>
<hr style="margin: 30px 0;">
<h2><?php esc_html_e( 'Data Management', 'robotstxt-smtp' ); ?></h2>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row"><?php esc_html_e( 'Delete data on uninstall', 'robotstxt-smtp' ); ?></th>
<td>
<fieldset>
<label>
<input type="checkbox" name="<?php echo esc_attr( self::OPTION_NAME ); ?>[delete_data_on_uninstall]" value="1" <?php checked( ! empty( $settings['delete_data_on_uninstall'] ) ); ?> />
<?php esc_html_e( 'Remove all plugin data when uninstalling', 'robotstxt-smtp' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'When enabled, all settings, logs, statistics, and data from add-ons (Amazon SES, Newsletter) will be permanently deleted when you uninstall this plugin. When disabled (default), all data will be preserved.', 'robotstxt-smtp' ); ?>
</p>
</fieldset>
</td>
</tr>
</tbody>
</table>
</form>
<hr style="margin: 30px 0;">
<h2><?php esc_html_e( 'Reset Plugin Data', 'robotstxt-smtp' ); ?></h2>
<p><?php esc_html_e( 'This will permanently delete all plugin settings, logs, statistics, and reset the plugin to its default state. This action cannot be undone.', 'robotstxt-smtp' ); ?></p>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" onsubmit="return confirm('<?php echo esc_js( __( 'Are you sure you want to reset all plugin data? This action cannot be undone.', 'robotstxt-smtp' ) ); ?>');">
<?php wp_nonce_field( 'robotstxt_smtp_reset_data' ); ?>
<input type="hidden" name="action" value="robotstxt_smtp_reset_data" />
<?php submit_button( esc_html__( 'Reset All Plugin Data', 'robotstxt-smtp' ), 'delete', 'submit', false ); ?>
</form>
</div>
<?php
@ -1187,6 +1222,36 @@ class Settings_Page {
</table>
<?php submit_button( esc_html__( 'Save Changes', 'robotstxt-smtp' ) ); ?>
<hr style="margin: 30px 0;">
<h2><?php esc_html_e( 'Data Management', 'robotstxt-smtp' ); ?></h2>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row"><?php esc_html_e( 'Delete data on uninstall', 'robotstxt-smtp' ); ?></th>
<td>
<fieldset>
<label>
<input type="checkbox" name="robotstxt_smtp_options[delete_data_on_uninstall]" value="1" <?php checked( ! empty( $this->get_settings()['delete_data_on_uninstall'] ) ); ?> />
<?php esc_html_e( 'Remove all plugin data when uninstalling', 'robotstxt-smtp' ); ?>
</label>
<p class="description">
<?php esc_html_e( 'When enabled, all settings, logs, statistics, and data from add-ons (Amazon SES, Newsletter) will be permanently deleted when you uninstall this plugin. When disabled (default), all data will be preserved.', 'robotstxt-smtp' ); ?>
</p>
</fieldset>
</td>
</tr>
</tbody>
</table>
</form>
<hr style="margin: 30px 0;">
<h2><?php esc_html_e( 'Reset Plugin Data', 'robotstxt-smtp' ); ?></h2>
<p><?php esc_html_e( 'This will permanently delete all network-wide and site-specific plugin settings, logs, statistics, and reset the plugin to its default state across all sites. This action cannot be undone.', 'robotstxt-smtp' ); ?></p>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" onsubmit="return confirm('<?php echo esc_js( __( 'Are you sure you want to reset all plugin data across the entire network? This action cannot be undone.', 'robotstxt-smtp' ) ); ?>');">
<?php wp_nonce_field( 'robotstxt_smtp_reset_data_network' ); ?>
<input type="hidden" name="action" value="robotstxt_smtp_reset_data_network" />
<?php submit_button( esc_html__( 'Reset All Plugin Data (Network-wide)', 'robotstxt-smtp' ), 'delete', 'submit', false ); ?>
</form>
</div>
<?php
@ -3422,6 +3487,169 @@ class Settings_Page {
exit;
}
/**
* Handles the reset plugin data request.
*
* @since 2.0.1
*
* @return void
*/
public function handle_reset_data(): void {
$nonce_value = filter_input( INPUT_POST, '_wpnonce', FILTER_UNSAFE_RAW );
// Block reset unless the nonce matches the action button submission.
if ( ! is_string( $nonce_value ) || ! wp_verify_nonce( wp_unslash( $nonce_value ), 'robotstxt_smtp_reset_data' ) ) {
wp_die( esc_html__( 'The link you followed has expired.', 'robotstxt-smtp' ) );
}
$this->ensure_capability( self::SCOPE_SITE );
// Delete all plugin data for current site.
delete_option( self::OPTION_NAME );
delete_option( 'robotstxt_smtp_newsletter_options' );
// Delete logs.
Plugin::get_instance()->clear_all_logs();
// Clear transients.
$this->delete_transient_prefix( 'robotstxt_smtp_tool_' );
$this->delete_transient_prefix( 'robotstxt_smtp_test_result_' );
$this->delete_transient_prefix( 'robotstxt_smtp_amazonses_quota_' );
// Clear 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' );
// Redirect with success message.
$redirect = add_query_arg(
array(
'page' => self::PAGE_SLUG,
'robotstxt_smtp_reset' => 1,
),
admin_url( 'options-general.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Handles the reset plugin data request for network admin.
*
* @since 2.0.1
*
* @return void
*/
public function handle_reset_data_network(): void {
$nonce_value = filter_input( INPUT_POST, '_wpnonce', FILTER_UNSAFE_RAW );
// Block reset unless the nonce matches the action button submission.
if ( ! is_string( $nonce_value ) || ! wp_verify_nonce( wp_unslash( $nonce_value ), 'robotstxt_smtp_reset_data_network' ) ) {
wp_die( esc_html__( 'The link you followed has expired.', 'robotstxt-smtp' ) );
}
if ( ! ROBOTSTXT_SMTP_IS_MULTISITE ) {
wp_die( esc_html__( 'Network reset is only available on multisite installations.', 'robotstxt-smtp' ) );
}
$this->ensure_capability( self::SCOPE_NETWORK );
// Delete network-wide data.
delete_site_option( self::NETWORK_OPTION_NAME );
delete_site_option( 'robotstxt_smtp_configuration_scope' );
// Delete statistics table.
global $wpdb;
$table_name = $wpdb->base_prefix . 'robotstxt_smtp_stats';
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
delete_site_option( 'robotstxt_smtp_stats_db_version' );
// Reset all sites in the network.
$sites = get_sites( array( 'fields' => 'ids' ) );
foreach ( $sites as $site_id ) {
switch_to_blog( (int) $site_id );
// Delete site options.
delete_option( self::OPTION_NAME );
delete_option( 'robotstxt_smtp_newsletter_options' );
// Clear logs.
Plugin::get_instance()->clear_all_logs();
// Clear transients.
$this->delete_transient_prefix( 'robotstxt_smtp_tool_' );
$this->delete_transient_prefix( 'robotstxt_smtp_test_result_' );
$this->delete_transient_prefix( 'robotstxt_smtp_amazonses_quota_' );
// Clear 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' );
restore_current_blog();
}
// Redirect with success message.
$redirect = add_query_arg(
array(
'page' => self::NETWORK_PAGE_SLUG,
'robotstxt_smtp_reset' => 1,
),
network_admin_url( 'settings.php' )
);
wp_safe_redirect( $redirect );
exit;
}
/**
* Deletes transients with a specific prefix.
*
* @since 2.0.1
*
* @param string $prefix Transient prefix without internal storage prefix.
*
* @return void
*/
private function delete_transient_prefix( string $prefix ): void {
global $wpdb;
$stored_prefix = '_transient_' . $prefix;
$timeout_prefix = '_transient_timeout_' . $prefix;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$matches = $wpdb->get_col(
$wpdb->prepare(
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
$wpdb->esc_like( $stored_prefix ) . '%',
$wpdb->esc_like( $timeout_prefix ) . '%'
)
);
if ( empty( $matches ) ) {
return;
}
foreach ( $matches as $option_name ) {
if ( ! is_string( $option_name ) ) {
continue;
}
if ( str_starts_with( $option_name, '_transient_timeout_' ) ) {
$transient_key = substr( $option_name, strlen( '_transient_timeout_' ) );
} else {
$transient_key = substr( $option_name, strlen( '_transient_' ) );
}
if ( '' === $transient_key ) {
continue;
}
delete_transient( $transient_key );
}
}
/**
* Renders the host field.
*
@ -3990,6 +4218,9 @@ class Settings_Page {
$clean['rate_limit_per_day'] = absint( $options['rate_limit_per_day'] );
}
// Sanitize delete on uninstall option.
$clean['delete_data_on_uninstall'] = ! empty( $options['delete_data_on_uninstall'] );
/**
* Filters the sanitized SMTP options before they are persisted.
*
@ -5243,6 +5474,7 @@ class Settings_Page {
'rate_limit_per_second' => 0,
'rate_limit_per_hour' => 0,
'rate_limit_per_day' => 0,
'delete_data_on_uninstall' => false,
);
}
@ -5630,6 +5862,49 @@ class Settings_Page {
<?php
}
/**
* Displays the reset data success notice.
*
* @since 2.0.1
*
* @return void
*/
public function display_reset_data_notice(): void {
if ( ! is_admin() ) {
return;
}
$page = filter_input( INPUT_GET, 'page', FILTER_UNSAFE_RAW );
if ( null === $page ) {
return;
}
$page = sanitize_key( $page );
if ( '' === $page || ! in_array( $page, array( self::PAGE_SLUG, self::NETWORK_PAGE_SLUG ), true ) ) {
return;
}
$reset = filter_input( INPUT_GET, 'robotstxt_smtp_reset', FILTER_UNSAFE_RAW );
if ( null === $reset ) {
return;
}
$reset = absint( $reset );
if ( 1 !== $reset ) {
return;
}
?>
<div class="notice notice-success is-dismissible">
<p><strong><?php esc_html_e( 'All plugin data has been reset successfully.', 'robotstxt-smtp' ); ?></strong></p>
</div>
<?php
}
/**
* Renders the test email form.
*