<?php

if (!defined('ABSPATH')) {
    exit;
} // Exit if accessed directly

global $wpdb;

// Run patch only once per site
if (is_admin()) {

    // --- kbucket: publisher_alias ---
    $col = $wpdb->get_results("SHOW COLUMNS FROM {$wpdb->prefix}kbucket LIKE 'publisher_alias'");
    if (empty($col)) {
        $wpdb->query("ALTER TABLE {$wpdb->prefix}kbucket ADD COLUMN publisher_alias varchar(255) DEFAULT NULL AFTER author_alias");
    }

    // --- kb_category: alias_name ---
    $col = $wpdb->get_results("SHOW COLUMNS FROM {$wpdb->prefix}kb_category LIKE 'alias_name'");
    if (empty($col)) {
        $wpdb->query("ALTER TABLE {$wpdb->prefix}kb_category ADD COLUMN alias_name varchar(255) DEFAULT NULL AFTER name");
    }

    // --- kb_tag_details: alias_name ---
    $col = $wpdb->get_results("SHOW COLUMNS FROM {$wpdb->prefix}kb_tag_details LIKE 'alias_name'");
    if (empty($col)) {
        $wpdb->query("ALTER TABLE {$wpdb->prefix}kb_tag_details ADD COLUMN alias_name varchar(255) DEFAULT NULL AFTER name");
    }

    // --- kbucket: KID ---
    $col = $wpdb->get_results("SHOW COLUMNS FROM {$wpdb->prefix}kbucket LIKE 'KID'");
    if (empty($col)) {
        $wpdb->query("ALTER TABLE {$wpdb->prefix}kbucket ADD COLUMN KID varchar(255) DEFAULT NULL AFTER id_kbucket");
    }

    // --- kbucket: last_updated ---
    $col = $wpdb->get_results("SHOW COLUMNS FROM {$wpdb->prefix}kbucket LIKE 'last_updated'");
    if (empty($col)) {
        $wpdb->query("ALTER TABLE {$wpdb->prefix}kbucket ADD COLUMN last_updated varchar(255) DEFAULT NULL AFTER add_date");
    }
}
