<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Template-scope variables and single-post admin meta reads, not globals or slow queries.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

/**
 * TNC FlipBook 3D uninstall handler.
 *
 * Only deletes plugin data if the user has opted in via
 * Settings → Advanced → "Delete Data on Uninstall".
 */

$options = get_option( 'tnc_flipbook_3d_settings', array() );

if ( empty( $options['delete_data_on_uninstall'] ) ) {
    // User has NOT opted in — keep all data intact.
    return;
}

// ── Delete all flipbook posts and their meta ─────────────────────────────────
$post_ids = get_posts( array(
    'post_type'      => 'tnc_flipbook',
    'posts_per_page' => -1,
    'post_status'    => 'any',
    'fields'         => 'ids',
) );

foreach ( $post_ids as $post_id ) {
    wp_delete_post( $post_id, true );
}

// ── Remove global options ─────────────────────────────────────────────────────
delete_option( 'tnc_flipbook_3d_settings' );

// ── Flush rewrite rules ───────────────────────────────────────────────────────
flush_rewrite_rules();
