<?php
/**
 * Uninstall routine for AccessLens.
 *
 * Called automatically by WordPress when the plugin is deleted via the admin UI.
 * Removes all plugin options and per-user transients.
 *
 * @package AccessLens
 */

// Guard: must be called by WP uninstall, not directly.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

// Remove plugin options.
delete_option( 'accesslens_engine_url' );

// Remove per-user job transients for all users.
$accesslens_user_ids = get_users(
	array(
		'fields' => 'ID',
		'number' => -1,
	)
);

foreach ( $accesslens_user_ids as $accesslens_user_id ) {
	delete_transient( 'accesslens_job_' . absint( $accesslens_user_id ) );
}

// Clean up any orphaned transients created before user deletion.
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
	"DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_accesslens_job_%' OR option_name LIKE '_transient_timeout_accesslens_job_%'"
);
