<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Uninstall routine uses file-local variables only.
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Required cleanup queries during uninstall.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

$option_key = 'nnaxfuf_settings';
$settings = get_option( $option_key, array() );
$purge_all = is_array( $settings ) && ! empty( $settings['delete_all_on_uninstall'] );

delete_option( $option_key );
if ( is_multisite() ) {
    delete_site_option( $option_key );
}

if ( ! $purge_all ) {
    return;
}

global $wpdb;

$is_safe_upload_path = static function ( $path ) {
    $path = (string) $path;
    if ( $path === '' ) {
        return false;
    }

    $resolved_path = realpath( $path );
    if ( $resolved_path === false ) {
        return false;
    }
    $resolved_path = wp_normalize_path( $resolved_path );

    $allowed_roots = array();
    $uploads = wp_upload_dir();
    if ( ! empty( $uploads['basedir'] ) ) {
        $upload_root = realpath( (string) $uploads['basedir'] );
        if ( $upload_root !== false ) {
            $allowed_roots[] = trailingslashit( wp_normalize_path( $upload_root ) );
        }
    }

    $plugin_path = plugin_dir_path( __FILE__ );
    $plugin_storage_paths = array(
        trailingslashit( $plugin_path ) . 'uploads',
        trailingslashit( $plugin_path ) . 'storage',
    );
    foreach ( $plugin_storage_paths as $candidate_path ) {
        $resolved_candidate = realpath( $candidate_path );
        if ( $resolved_candidate === false ) {
            continue;
        }
        $allowed_roots[] = trailingslashit( wp_normalize_path( $resolved_candidate ) );
    }

    foreach ( array_unique( $allowed_roots ) as $root ) {
        if ( strpos( $resolved_path, $root ) === 0 ) {
            return true;
        }
    }

    return false;
};

$delete_physical_file_if_present = static function ( $file_path ) use ( $is_safe_upload_path ) {
    $file_path = (string) $file_path;
    if ( $file_path === '' || ! file_exists( $file_path ) ) {
        return;
    }

    if ( ! $is_safe_upload_path( $file_path ) ) {
        return;
    }

    if ( ! function_exists( 'wp_delete_file' ) ) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }

    wp_delete_file( $file_path );
};

$file_posts = get_posts(
    array(
        'post_type'      => 'nnaxfuf_user_file',
        'post_status'    => 'any',
        'posts_per_page' => -1,
        'fields'         => 'ids',
    )
);

foreach ( $file_posts as $file_post_id ) {
    $file_post_id = (int) $file_post_id;
    if ( $file_post_id <= 0 ) {
        continue;
    }
    $file_path = (string) get_post_meta( $file_post_id, '_nnaxfuf_file_path', true );
    $delete_physical_file_if_present( $file_path );
}

$all_plugin_posts = get_posts(
    array(
        'post_type'      => array( 'nnaxfuf_user_file', 'nnaxfuf_folder' ),
        'post_status'    => 'any',
        'posts_per_page' => -1,
        'fields'         => 'ids',
    )
);

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

delete_option( 'nnaxfuf_migration_config' );
delete_option( 'nnaxfuf_rewrite_rules_version' );
delete_option( 'nnaxfuf_needs_permalink_save' );
delete_option( 'nnaxfuf_rewrite_notice_dismissed_for' );

$table_name = $wpdb->prefix . 'nnaxfuf_activities';
$wpdb->query( "DROP TABLE IF EXISTS `{$table_name}`" );

$wpdb->query(
    "DELETE FROM {$wpdb->usermeta}
     WHERE meta_key = '_nnaxfuf_last_share_search_context'
"
);
