<?php

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

global $wpdb;

// Remove settings
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}bundlepress_settings");

// Remove bundle items
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}bundlepress_bundle_items");

// Remove bundle product type meta
$wpdb->query("DELETE FROM {$wpdb->prefix}postmeta WHERE meta_key = '_bundlepress_settings'");

// Get all bundle products
$bundle_ids = get_posts([
    'post_type' => 'product',
    'post_status' => 'any',
    'fields' => 'ids',
    'tax_query' => [
        [
            'taxonomy' => 'product_type',
            'field'    => 'slug',
            'terms'    => 'bundlepress',
        ],
    ],
]);

foreach ($bundle_ids as $id) {
    wp_delete_post($id, true);
}
