<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// BotBanish Update Database on WordPress Update
// Date: 07/19/2026
//
// Update settings in old database versions
///////////////////////////////////////////////////////////////////////////////////////////////////////

// Prevent direct access to the file
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// 1. Define your current plugin version constant
define( 'BOTBANISH_PLUGIN_VERSION', '9.0.02' );

	BotBanishCheckDBVersion();
/**
 * Main function to check and run database updates.
 */
function BotBanishCheckDBVersion() {

    global $wpdb;
	
    $table_name = 'bbc_botbanishclient_settings';
	
	// If table does not exist, this should be a new installation
	
	$sql = 'SHOW TABLES LIKE "' . $table_name . '"';
	$result = $wpdb->get_results($sql);
	
	if (empty($result))
		return;

	$sql = 'SELECT `value` FROM `' . $table_name . '` WHERE `name` = "BOTBANISH_VERSION_CLIENT"';
	$result = $wpdb->get_results($sql);

	// If no data is found, this is a new installation. Nothing to do!
	
	if (empty($result))
		return;
	
	$installed_version = $result[0]->value;
	
	// Get the version currently saved in the database
 //   $installed_version = get_option( 'botbanish_db_version' );

	// If the database version doesn't match the file version, run the update
	if ($installed_version !== BOTBANISH_PLUGIN_VERSION) {

		// Execute your database modifications
		BotBanishUpgradeDatabase($installed_version);

		// Update the version in the database so this only runs once

		$sql = 'UPDATE `' . $table_name . '` SET `value` = "' . BOTBANISH_PLUGIN_VERSION . '" WHERE `name` = "BOTBANISH_VERSION_CLIENT"';
		$wpdb->query($sql);
	}
}

function BotBanishUpgradeDatabase($installed_version) {

    global $wpdb;

    $table_name = 'bbc_botbanishclient_settings';
	$items = array();

	$BotBanishDir = plugin_dir_path( __FILE__ ) . 'bot/';
//	$folders = glob($path . 'botbanish*', GLOB_ONLYDIR);
//	$BotBanishDir = $folders[0] . 'bot/';

	BotBanishSetupItems($items,'BOTBANISH_SUBS_DIR', $BotBanishDir . 'Subs/');
	BotBanishSetupItems($items,'BOTBANISH_INCLUDE_DIR', $BotBanishDir . 'Include/');
	BotBanishSetupItems($items,'BOTBANISH_DATA_DIR', $BotBanishDir . 'Data/');
	BotBanishSetupItems($items,'BOTBANISH_IMAGES_DIR', $BotBanishDir . 'Images/Client/');
	BotBanishSetupItems($items,'BOTBANISH_LANGUAGE_DIR', $BotBanishDir . 'Language/');
	BotBanishSetupItems($items,'BOTBANISH_DOCS_DIR', $BotBanishDir . 'Docs/');
	BotBanishSetupItems($items,'BOTBANISH_CLASS_DIR', $BotBanishDir . 'Class/');
	BotBanishSetupItems($items,'BOTBANISH_TEMPLATES_DIR', $BotBanishDir . 'Templates/');
	BotBanishSetupItems($items,'BOTBANISH_ANALYTICS_DIR', $BotBanishDir . 'Analytics/');
	BotBanishSetupItems($items,'BOTBANISH_CSS_DIR', $BotBanishDir . 'css/');
	BotBanishSetupItems($items,'BOTBANISH_AUTOLOAD_DIR', $BotBanishDir . 'vendor/');
	BotBanishSetupItems($items,'BOTBANISH_DATABASE_TABLE_COLLATION_DEFAULT', 'utf8mb4_0900_ai_ci');
	BotBanishSetupItems($items,'BOTBANISH_DATABASE_TABLE_CHARSET_DEFAULT', 'utf8mb4');
/*
	$items = array(
				array(
					'name' => 'BOTBANISH_DATABASE_TABLE_COLLATION_DEFAULT',
					'value' => 'utf8mb4_0900_ai_ci',
					),
				array(
					'name' => 'BOTBANISH_DATABASE_TABLE_CHARSET_DEFAULT',
					'value' => 'utf8mb4',
					),
			);
*/			
	foreach ($items as $item) {

		$sql = 'SELECT `value` FROM `' . $table_name . '` WHERE `name` = "' . $item['name'] . '"';
		$result = $wpdb->get_results($sql);

		if (empty($result)) {

			$sql = 'INSERT INTO `' . $table_name . '` (`name`, `value`, `type`)
				VALUES ("' . $item['name'] . '", "' . $item['value'] . '", "1")';

			$wpdb->query($sql);		
		}
	}

/*
 // Handles the actual database structural changes.
 // Required to use the dbDelta() function
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';

    // 2. Define your target schema (This example is for version 2.0.0)
    // NOTE: You must follow strict dbDelta formatting rules (see below)
    $sql = "CREATE TABLE $table_name (
        id bigint(20) NOT NULL AUTO_INCREMENT,
        user_id bigint(20) NOT NULL,
        new_column_name varchar(100) DEFAULT '' NOT NULL,
        created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    // dbDelta automatically alters the table if it exists, or creates it if it doesn't
    dbDelta( $sql );

    // 3. Handle data migration for specific older versions if needed
    if ( version_compare( $installed_version, '1.5.0', '<' ) ) {
        // Run specific custom code only if upgrading from a version older than 1.5.0
        // Example: $wpdb->query("UPDATE $table_name SET new_column_name = 'default' WHERE ... ");
    }
*/
}

function BotBanishSetupItems(&$items, $name, $value) {
	
	$items[] = array('name' => $name, 'value' => $value);
}
?>