<?php
/**
 * ideo Field Porter – Uninstall
 *
 * Fired when the plugin is deleted from the WordPress admin.
 * Removes all options and transients created by the plugin,
 * and cleans up the temporary upload directory.
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

call_user_func( function () {

	// ── Remove plugin settings ──────────────────────────────────────────────
	delete_option( 'ideo_fp_overwrite' );
	delete_option( 'ideo_fp_post_types' );
	delete_option( 'ideo_fp_media_url_format' );
	delete_option( 'ideo_fp_allowed_roles' );
	delete_option( 'ideo_fp_auto_configured' );
	delete_option( 'ideo_fp_logs' );

	// ── Remove the ideo_fp_access capability from every role ──────────────────
	if ( function_exists( 'get_editable_roles' ) || is_readable( ABSPATH . 'wp-admin/includes/user.php' ) ) {
		if ( ! function_exists( 'get_editable_roles' ) ) {
			require_once ABSPATH . 'wp-admin/includes/user.php';
		}
		foreach ( get_editable_roles() as $role_slug => $role_details ) {
			$role = get_role( $role_slug );
			if ( $role && $role->has_cap( 'ideo_fp_access' ) ) {
				$role->remove_cap( 'ideo_fp_access' );
			}
		}
	}

	// ── Remove mapping profiles (one option per post type) ────────────────────
	$post_types = get_post_types( array( 'public' => true ) );
	foreach ( $post_types as $pt ) {
		delete_option( 'ideo_fp_mapping_profiles_' . $pt );
	}

	// ── Remove transients created by the plugin ───────────────────────────────
	// Session/feedback transient keys are 'ideo_fp_{post_type}_session_{user_id}'
	// and 'ideo_fp_{post_type}_feedback_{user_id}' — see Admin_Page::$prefix.
	// Queried directly against wp_options (both the transient value and its
	// companion _transient_timeout_ row) rather than looping over get_users(),
	// so cleanup isn't capped at an arbitrary user count on larger sites.
	global $wpdb;
	$like = $wpdb->esc_like( '_transient_ideo_fp_' ) . '%';
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-time cleanup on uninstall; no safe WP API covers a prefix-wide options purge.
	$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $like ) );
	$like_timeout = $wpdb->esc_like( '_transient_timeout_ideo_fp_' ) . '%';
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Same one-time cleanup, timeout companion rows.
	$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $like_timeout ) );

	// ── Remove the temporary upload directory and its contents ────────────────
	$upload_dir = wp_upload_dir();
	$tmp_dir    = trailingslashit( $upload_dir['basedir'] ) . 'ideo-fp-tmp/';

	if ( is_dir( $tmp_dir ) ) {
		require_once ABSPATH . 'wp-admin/includes/file.php';
		WP_Filesystem();
		global $wp_filesystem;

		if ( $wp_filesystem ) {
			$files = glob( $tmp_dir . '*' );
			if ( $files ) {
				foreach ( $files as $file ) {
					$wp_filesystem->delete( $file );
				}
			}
			$wp_filesystem->rmdir( $tmp_dir );
		}
	}

} );
