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

// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

// Check if user has permission to uninstall.
if ( ! current_user_can( 'activate_plugins' ) ) {
    return;
}

// Check if we should delete data (user choice via filter or option).
// Default: ask user (via filter hook) - Envato requirement: must ask before deleting.
$delete_data = apply_filters( 'mt_primelink_uninstall_delete_data', false );

// If no filter is set, check for option (set during uninstall process).
if ( ! has_filter( 'mt_primelink_uninstall_delete_data' ) ) {
    $delete_data = get_option( 'mt_primelink_uninstall_delete_data', false );
}

// If user wants to keep data, only clear scheduled events.
if ( ! $delete_data ) {
    // Clear scheduled cron events only.
    wp_clear_scheduled_hook( 'mt_primelink_import_tick' );
    wp_clear_scheduled_hook( 'mt_primelink_import_start_scheduled' );
    wp_clear_scheduled_hook( 'mt_primelink_export_start_scheduled' );
    wp_clear_scheduled_hook( 'mt_primelink_export_tick' );
    wp_clear_scheduled_hook( 'mt_primelink_cleanup_cron' );
    
    // Clear all scheduled events with args.
    $cron = _get_cron_array();
    if ( $cron ) {
        foreach ( $cron as $timestamp => $cronhooks ) {
            foreach ( $cronhooks as $hook => $keys ) {
                if ( strpos( $hook, 'mt_primelink_' ) === 0 ) {
                    foreach ( $keys as $k => $v ) {
                        wp_unschedule_event( $timestamp, $hook, $v['args'] );
                    }
                }
            }
        }
    }
    
    return;
}

// Delete all plugin options.
$options_to_delete = array(
    'mt_primelink_settings',
    'mt_primelink_import_drafts',
    'mt_primelink_export_drafts',
);

// Delete import/export state options (dynamic, need to query).
global $wpdb;
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( 'mt_primelink_import_state_' ) . '%'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( 'mt_primelink_export_state_' ) . '%'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( 'mt_primelink_import_tracking_' ) . '%'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( 'mt_primelink_webhooks_' ) . '%'
    )
);

// Delete main options.
foreach ( $options_to_delete as $option ) {
    delete_option( $option );
}

// Delete post meta (import tracking meta keys).
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
        '_mt_primelink_import_id'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
        '_mt_primelink_import_time'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
        '_mt_primelink_import_hash'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
        '_mt_primelink_last_import_id'
    )
);
$wpdb->query( 
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
        '_mt_primelink_last_import_time'
    )
);

// Clear all scheduled cron events.
wp_clear_scheduled_hook( 'mt_primelink_import_tick' );
wp_clear_scheduled_hook( 'mt_primelink_import_start_scheduled' );
wp_clear_scheduled_hook( 'mt_primelink_export_start_scheduled' );
wp_clear_scheduled_hook( 'mt_primelink_export_tick' );
wp_clear_scheduled_hook( 'mt_primelink_cleanup_cron' );

// Clear all scheduled events with args.
$cron = _get_cron_array();
if ( $cron ) {
    foreach ( $cron as $timestamp => $cronhooks ) {
        foreach ( $cronhooks as $hook => $keys ) {
            if ( strpos( $hook, 'mt_primelink_' ) === 0 ) {
                foreach ( $keys as $k => $v ) {
                    wp_unschedule_event( $timestamp, $hook, $v['args'] );
                }
            }
        }
    }
}

// Delete uploaded files (optional - user can keep them).
$delete_files = apply_filters( 'mt_primelink_uninstall_delete_files', false );
if ( $delete_files ) {
    $upload_dir = wp_upload_dir();
    $plugin_dir = trailingslashit( $upload_dir['basedir'] ) . 'primelink/';
    if ( is_dir( $plugin_dir ) ) {
        $files = glob( trailingslashit( $plugin_dir ) . '*' );
        foreach ( $files as $file ) {
            if ( is_file( $file ) ) {
                @unlink( $file );
            }
        }
        @rmdir( $plugin_dir );
    }
}

// Fire action for other cleanup (if needed).
do_action( 'mt_primelink_uninstall' );

