Newer
Older
tonitalia-pluginsoci / includes / class-uninstaller.php
<?php
/**
 * Plugin uninstall cleanup.
 *
 * @package TonItaliaRegistration
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class TON_Reg_Uninstaller
 */
class TON_Reg_Uninstaller {

	/**
	 * Run full uninstall.
	 */
	public static function uninstall() {
		global $wpdb;

		$table = $wpdb->prefix . TON_REG_TABLE;
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$registrations = $wpdb->get_results( "SELECT id FROM {$table}" );
		if ( $registrations ) {
			foreach ( $registrations as $reg ) {
				TON_Reg_Documents::delete_all_for_registration( (int) $reg->id, false );
			}
		}
		TON_Reg_Documents::uninstall_folders();

		$delete_users = '1' === get_option( 'ton_reg_delete_users_on_uninstall', '0' );

		if ( $delete_users ) {
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			$user_ids = $wpdb->get_col( "SELECT user_id FROM {$table} WHERE user_id IS NOT NULL" );
			foreach ( $user_ids as $user_id ) {
				if ( $user_id ) {
					wp_delete_user( (int) $user_id );
				}
			}
		}

		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . TON_REG_TABLE );

		$options = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
				'ton_reg_%'
			)
		);

		foreach ( $options as $option ) {
			delete_option( $option );
		}

		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
				'_transient_ton_reg_%',
				'_transient_timeout_ton_reg_%'
			)
		);
	}
}