<?php
/**
 * Uninstall script for BW WebP.
 *
 * Runs when the plugin is deleted via the WP Admin plugins screen.
 * Drops the manifest table and (opt-in) deletes generated .webp files.
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

global $wpdb;

$settings = get_option( 'bw_webp_settings', array() );
$delete_files = ! empty( $settings['delete_on_uninstall'] );

$table = $wpdb->prefix . 'bw_webp_jobs';

if ( $delete_files ) {
	$rows = $wpdb->get_col( "SELECT dest_path FROM {$table}" );
	foreach ( (array) $rows as $path ) {
		if ( is_string( $path ) && '' !== $path && is_file( $path ) && substr( $path, -5 ) === '.webp' ) {
			@unlink( $path );
		}
	}
}

$wpdb->query( "DROP TABLE IF EXISTS {$table}" );

delete_option( 'bw_webp_settings' );
delete_option( 'bw_webp_version' );

if ( is_multisite() ) {
	delete_site_option( 'bw_webp_settings' );
}
