<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * @package Modelora
 */

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

/**
 * Remove plugin data from the site.
 */
function modelora_uninstall_cleanup()
{
    // Delete all plugin custom post types
    $models = get_posts([
        'post_type'   => 'modelora_model',
        'numberposts' => -1,
        'post_status' => 'any',
        'fields'      => 'ids',
    ]);

    foreach ($models as $model_id) {
        // Delete attachment if present
        $attachment_id = get_post_meta($model_id, '_modelora_attachment_id', true);
        if ($attachment_id) {
            wp_delete_attachment($attachment_id, true);
        }
        
        // Delete the model post
        wp_delete_post($model_id, true);
    }

    // Delete options
    delete_option('modelora_settings');
    delete_option('modelora_version');
}

/**
 * Multisite support for uninstallation.
 */
if (is_multisite()) {
    $modelora_sites = get_sites();
    foreach ($modelora_sites as $modelora_site) {
        switch_to_blog($modelora_site->blog_id);
        modelora_uninstall_cleanup();
        restore_current_blog();
    }
} else {
    modelora_uninstall_cleanup();
}
