155 lines
4.6 KiB
PHP
155 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Uninstall routines for the core SMTP plugin.
|
|
*
|
|
* @package Robotstxt_SMTP
|
|
*/
|
|
|
|
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! function_exists( 'robotstxt_smtp_uninstall_cleanup_site' ) ) {
|
|
/**
|
|
* Removes all plugin data for the current site.
|
|
*
|
|
* @since 1.1.1
|
|
*
|
|
* @return void
|
|
*/
|
|
function robotstxt_smtp_uninstall_cleanup_site(): void {
|
|
delete_option( 'robotstxt_smtp_options' );
|
|
|
|
robotstxt_smtp_uninstall_delete_logs();
|
|
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_tool_', false );
|
|
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_test_result_', false );
|
|
wp_clear_scheduled_hook( 'robotstxt_smtp_cleanup_logs' );
|
|
}
|
|
}
|
|
|
|
if ( ! function_exists( 'robotstxt_smtp_uninstall_cleanup_network' ) ) {
|
|
/**
|
|
* Removes network-wide plugin data.
|
|
*
|
|
* @since 1.1.1
|
|
*
|
|
* @return void
|
|
*/
|
|
function robotstxt_smtp_uninstall_cleanup_network(): void {
|
|
delete_site_option( 'robotstxt_smtp_network_options' );
|
|
delete_site_option( 'robotstxt_smtp_configuration_scope' );
|
|
|
|
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_tool_', true );
|
|
robotstxt_smtp_uninstall_delete_transient_prefix( 'robotstxt_smtp_test_result_', true );
|
|
}
|
|
}
|
|
|
|
if ( ! function_exists( 'robotstxt_smtp_uninstall_delete_transient_prefix' ) ) {
|
|
/**
|
|
* Deletes transients that match a prefix for the current context.
|
|
*
|
|
* @since 1.1.1
|
|
*
|
|
* @param string $prefix Transient prefix without the internal storage prefix.
|
|
* @param bool $is_network Whether to delete network transients.
|
|
*
|
|
* @return void
|
|
*/
|
|
function robotstxt_smtp_uninstall_delete_transient_prefix( string $prefix, bool $is_network ): void {
|
|
global $wpdb;
|
|
|
|
if ( $is_network ) {
|
|
$stored_prefix = '_site_transient_' . $prefix;
|
|
$timeout_prefix = '_site_transient_timeout_' . $prefix;
|
|
$delete_callback = 'delete_site_transient';
|
|
$timeout_prefix_length = strlen( '_site_transient_timeout_' );
|
|
$stored_prefix_length = strlen( '_site_transient_' );
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$matches = $wpdb->get_col(
|
|
$wpdb->prepare(
|
|
"SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s OR meta_key LIKE %s",
|
|
$wpdb->esc_like( $stored_prefix ) . '%',
|
|
$wpdb->esc_like( $timeout_prefix ) . '%'
|
|
)
|
|
);
|
|
} else {
|
|
$stored_prefix = '_transient_' . $prefix;
|
|
$timeout_prefix = '_transient_timeout_' . $prefix;
|
|
$delete_callback = 'delete_transient';
|
|
$timeout_prefix_length = strlen( '_transient_timeout_' );
|
|
$stored_prefix_length = strlen( '_transient_' );
|
|
// 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, $is_network ? '_site_transient_timeout_' : '_transient_timeout_' ) ) {
|
|
$transient_key = substr( $option_name, $timeout_prefix_length );
|
|
} else {
|
|
$transient_key = substr( $option_name, $stored_prefix_length );
|
|
}
|
|
|
|
if ( '' === $transient_key ) {
|
|
continue;
|
|
}
|
|
|
|
call_user_func( $delete_callback, $transient_key );
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( ! function_exists( 'robotstxt_smtp_uninstall_delete_logs' ) ) {
|
|
/**
|
|
* Deletes all stored SMTP logs for the current site.
|
|
*
|
|
* @since 1.1.1
|
|
*
|
|
* @return void
|
|
*/
|
|
function robotstxt_smtp_uninstall_delete_logs(): void {
|
|
do {
|
|
$logs = get_posts(
|
|
array(
|
|
'post_type' => 'robotstxt_smtp_log',
|
|
'post_status' => 'any',
|
|
'fields' => 'ids',
|
|
'posts_per_page' => 100,
|
|
'orderby' => 'ID',
|
|
'order' => 'ASC',
|
|
)
|
|
);
|
|
|
|
foreach ( $logs as $log_id ) {
|
|
wp_delete_post( (int) $log_id, true );
|
|
}
|
|
} while ( ! empty( $logs ) );
|
|
}
|
|
}
|
|
|
|
robotstxt_smtp_uninstall_cleanup_network();
|
|
|
|
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
|
|
$sites = get_sites( array( 'fields' => 'ids' ) );
|
|
|
|
foreach ( $sites as $site_id ) {
|
|
switch_to_blog( (int) $site_id );
|
|
robotstxt_smtp_uninstall_cleanup_site();
|
|
restore_current_blog();
|
|
}
|
|
} else {
|
|
robotstxt_smtp_uninstall_cleanup_site();
|
|
}
|