<?php
/**
 * Plugtuner Radio Player uninstall routine.
 *
 * Removes the options and cached search results created by the plugin when the
 * plugin is deleted from the WordPress admin.
 */

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

// Delete settings and station lists.
delete_option( 'plugtuner_settings' );
delete_option( 'plugtuner_stations' );
delete_option( 'plugtuner_stations_am' );

/*
 * Remove the cached station-directory search results.
 *
 * The transient names contain a hash of the search query, so they cannot be
 * addressed individually with delete_transient(). A direct query is used here
 * intentionally: it runs once, only while the plugin is being deleted, and the
 * result is not cacheable.
 */
global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-off cleanup on uninstall; no caching layer applies.
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
		$wpdb->esc_like( '_transient_plugtuner_search_' ) . '%',
		$wpdb->esc_like( '_transient_timeout_plugtuner_search_' ) . '%'
	)
);
