<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals

/**
 * Fired when the plugin is uninstalled.
 *
 * @package Registration_Form_Builder
 */

if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

global $wpdb;

// Only remove plugin data when explicitly allowed.
$delete_data = get_option( 'wefb_delete_data_on_uninstall' );
if ( 'yes' !== $delete_data ) {
	return;
}

// Delete plugin options.
delete_option( 'wpefb_db_version' );
delete_option( 'progrid_db_version' ); // Backward compatibility cleanup.
delete_option( 'wefb_delete_data_on_uninstall' );

// Drop custom plugin tables if they exist.
$tables = array(
	$wpdb->prefix . 'wefb_forms',
	$wpdb->prefix . 'wefb_fields',
	$wpdb->prefix . 'wefb_sections',
	$wpdb->prefix . 'wefb_email_templates',
);

foreach ( $tables as $table ) {
	// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
	$wpdb->query( "DROP TABLE IF EXISTS $table" );
}
